Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (114 loc) · 4.27 KB
/
Copy pathscript.js
File metadata and controls
137 lines (114 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(function () {
const DROP_SIZE_MIN = 45; // px
const DROP_SIZE_MAX = 50;
const FALL_SPEED_MIN = 0.05; // seconds per 100px of travel
const FALL_SPEED_MAX = 0.15;
const MAX_DROPS = 300; // cap for performance
const CLOUD_HEIGHT = ()=> {
const img = document.getElementById('cloudImg');
// estimate cloud base: roughly 60% down the cloud image height
return img ? img.getBoundingClientRect().bottom * 0.6 : 100;
};
let activeDrops = 0;
const hint = document.getElementById('hint');
let hintGone = false;
function dismissHint() {
if (hintGone) return;
hintGone = true;
hint.style.transition = 'opacity 0.8s';
hint.style.opacity = '0';
setTimeout(() => hint.remove(), 200);
}
function spawnDrop(key) {
if (activeDrops >= MAX_DROPS) return;
activeDrops++;
dismissHint();
const vw = window.innerWidth;
const cloudBase = CLOUD_HEIGHT();
// Random x within viewport
const x = Math.random() * (vw - DROP_SIZE_MAX);
const size = DROP_SIZE_MIN + Math.random() * (DROP_SIZE_MAX - DROP_SIZE_MIN);
// Fall distance = from cloudBase to just below viewport
const fallDist = window.innerHeight - cloudBase + size;
const speed = FALL_SPEED_MIN + Math.random() * (FALL_SPEED_MAX - FALL_SPEED_MIN);
const duration = (fallDist / 100) * speed;
// Minimal drift — heavy rain falls nearly straight
const drift = (Math.random() - 0.5) * 10;
/* Raindrop image */
const drop = document.createElement('img');
drop.src = 'raindrop.png';
drop.className = 'raindrop';
drop.style.cssText = `
left: ${x}px;
top: ${cloudBase}px;
width: ${size}px;
height: auto;
animation-duration: ${duration}s;
animation-delay: 0s;
filter: hue-rotate(${Math.random()*40-20}deg) brightness(${0.85 + Math.random()*0.3});
`;
/* Key label */
const label = document.createElement('div');
label.className = 'key-label';
label.textContent = key.length === 1 ? key : formatKey(key);
label.style.cssText = `
left: ${x + size / 2 - 8}px;
top: ${cloudBase - 2}px;
animation-duration: ${Math.min(duration, 1.4)}s;
`;
document.body.appendChild(drop);
document.body.appendChild(label);
// Splash at landing
const splashDelay = duration * 1000 - 60;
const splashTimer = setTimeout(() => {
createSplash(x + size / 2, window.innerHeight - 8);
}, splashDelay);
// Cleanup
const cleanupTimer = setTimeout(() => {
drop.remove();
label.remove();
activeDrops--;
}, (duration + 0.15) * 1000);
// Override transform to include drift
drop.style.setProperty('--drift', drift + 'px');
drop.style.animation = 'none';
drop.getBoundingClientRect(); // reflow
drop.style.transition = `transform ${duration}s linear, opacity ${duration}s linear`;
drop.style.transform = `translateY(${fallDist}px) translateX(${drift}px) rotate(${drift * 0.4}deg)`;
drop.style.opacity = '0';
// Also animate label
label.style.animation = 'none';
label.getBoundingClientRect();
label.style.transition = `opacity ${Math.min(duration, 1.2)}s linear, transform ${Math.min(duration, 1.2)}s linear`;
label.style.opacity = '0';
label.style.transform = 'translateY(20px)';
}
function createSplash(x, y) {
const splash = document.createElement('div');
splash.className = 'splash';
splash.style.left = (x - 20) + 'px';
splash.style.top = (y - 4) + 'px';
document.body.appendChild(splash);
setTimeout(() => splash.remove(), 420);
}
function formatKey(key) {
const map = {
' ': 'SPC', 'Enter': '↵', 'Backspace': '⌫', 'Tab': '⇥',
'Shift': '⇧', 'Control': 'Ctrl', 'Alt': 'Alt', 'Meta': '⌘',
'ArrowUp': '↑', 'ArrowDown': '↓', 'ArrowLeft': '←', 'ArrowRight': '→',
'Escape': 'Esc', 'CapsLock': '⇪', 'Delete': 'Del',
};
return map[key] || key.slice(0, 3);
}
document.addEventListener('keydown', e => {
// Allow browser shortcuts
if (e.ctrlKey || e.metaKey) return;
// Throttle rapid repeats slightly
spawnDrop(e.key);
});
document.addEventListener('pointerdown', e => {
if (e.pointerType === 'touch' || e.pointerType === 'pen') {
spawnDrop('☔');
}
});
})();