|
43 | 43 |
|
44 | 44 | let width: number, height: number; |
45 | 45 | let dpr = Math.min(window.devicePixelRatio || 1, 2); |
46 | | - let mouse = { x: -1000, y: -1000 }; |
47 | | - let animId: number; |
48 | 46 |
|
49 | 47 | function resize() { |
50 | 48 | const rect = canvas.parentElement!.getBoundingClientRect(); |
|
66 | 64 |
|
67 | 65 | constructor() { |
68 | 66 | this.points = []; |
69 | | - // distribute strokes across the canvas |
70 | 67 | const startX = width * (0.25 + Math.random() * 0.65); |
71 | 68 | const startY = height * (0.05 + Math.random() * 0.9); |
72 | 69 | const spread = 100 + Math.random() * 200; |
73 | 70 |
|
74 | | - // 10-20 control points per stroke for longer curves |
75 | 71 | const numPoints = 10 + Math.floor(Math.random() * 11); |
76 | 72 | for (let i = 0; i < numPoints; i++) { |
77 | 73 | const angle = Math.random() * Math.PI * 2; |
|
84 | 80 | }); |
85 | 81 | } |
86 | 82 |
|
87 | | - // much more visible ink tones |
88 | 83 | const colors = [ |
89 | | - 'rgba(28, 25, 23, 0.18)', |
90 | | - 'rgba(28, 25, 23, 0.14)', |
91 | | - 'rgba(28, 25, 23, 0.12)', |
92 | | - 'rgba(87, 83, 78, 0.13)', |
93 | | - 'rgba(158, 59, 45, 0.10)', |
94 | | - 'rgba(158, 59, 45, 0.08)', |
95 | | - 'rgba(120, 113, 108, 0.11)', |
96 | | - 'rgba(28, 25, 23, 0.20)', |
| 84 | + 'rgba(28, 25, 23, 0.18)', 'rgba(28, 25, 23, 0.14)', 'rgba(28, 25, 23, 0.12)', |
| 85 | + 'rgba(87, 83, 78, 0.13)', 'rgba(158, 59, 45, 0.10)', 'rgba(158, 59, 45, 0.08)', |
| 86 | + 'rgba(120, 113, 108, 0.11)', 'rgba(28, 25, 23, 0.20)' |
97 | 87 | ]; |
98 | 88 | this.color = colors[Math.floor(Math.random() * colors.length)]; |
99 | 89 | this.baseWidth = 1 + Math.random() * 4; |
|
106 | 96 | for (const p of this.points) { |
107 | 97 | const nx = Math.sin(t * this.speed + p.y * 0.004 + this.noiseOffset) * this.wander; |
108 | 98 | const ny = Math.cos(t * this.speed + p.x * 0.004 + this.noiseOffset + 1.5) * this.wander; |
109 | | - |
110 | 99 | p.vx += (nx - p.vx) * 0.015; |
111 | 100 | p.vy += (ny - p.vy) * 0.015; |
112 | 101 | p.x += p.vx; |
113 | 102 | p.y += p.vy; |
114 | 103 |
|
115 | | - // mouse repulsion |
116 | | - const dx = p.x - mouse.x; |
117 | | - const dy = p.y - mouse.y; |
118 | | - const dist = Math.sqrt(dx * dx + dy * dy); |
119 | | - if (dist < 250) { |
120 | | - const force = (250 - dist) / 250 * 1.2; |
121 | | - p.vx += (dx / dist) * force * 0.2; |
122 | | - p.vy += (dy / dist) * force * 0.2; |
123 | | - } |
124 | | - |
125 | | - // soft boundary |
126 | | - if (p.x < -80) p.vx += 0.08; |
127 | | - if (p.x > width + 80) p.vx -= 0.08; |
128 | | - if (p.y < -80) p.vy += 0.08; |
129 | | - if (p.y > height + 80) p.vy -= 0.08; |
| 104 | + // soft spring boundary: gently pull them back if they drift too far out |
| 105 | + if (p.x < -100) p.vx += (-100 - p.x) * 0.0005; |
| 106 | + if (p.x > width + 100) p.vx -= (p.x - (width + 100)) * 0.0005; |
| 107 | + if (p.y < -100) p.vy += (-100 - p.y) * 0.0005; |
| 108 | + if (p.y > height + 100) p.vy -= (p.y - (height + 100)) * 0.0005; |
130 | 109 | } |
131 | 110 |
|
132 | | - // Relaxation pass to prevent sharp turns and kinks |
133 | | - // use much weaker position pulling to prevent rubber-banding/shrinking |
134 | 111 | for (let i = 1; i < this.points.length - 1; i++) { |
135 | 112 | const p = this.points[i]; |
136 | 113 | const prev = this.points[i - 1]; |
137 | 114 | const next = this.points[i + 1]; |
138 | | - |
139 | | - // gently pull towards neighbors (very mild so it doesn't shrink the curve) |
140 | | - const targetX = (prev.x + next.x) / 2; |
141 | | - const targetY = (prev.y + next.y) / 2; |
142 | | - p.x += (targetX - p.x) * 0.005; |
143 | | - p.y += (targetY - p.y) * 0.005; |
144 | | - |
145 | | - // smooth velocities to prevent sharp angular tangles |
146 | 115 | const targetVx = (prev.vx + next.vx) / 2; |
147 | 116 | const targetVy = (prev.vy + next.vy) / 2; |
148 | 117 | p.vx += (targetVx - p.vx) * 0.08; |
149 | 118 | p.vy += (targetVy - p.vy) * 0.08; |
150 | 119 | } |
| 120 | + |
| 121 | + for (let iter = 0; iter < 2; iter++) { |
| 122 | + for (let i = 0; i < this.points.length - 1; i++) { |
| 123 | + const p1 = this.points[i]; |
| 124 | + const p2 = this.points[i + 1]; |
| 125 | + const dx = p2.x - p1.x; |
| 126 | + const dy = p2.y - p1.y; |
| 127 | + const dist = Math.sqrt(dx * dx + dy * dy); |
| 128 | + if (dist > 0) { |
| 129 | + const diff = (dist - 40) / dist * 0.5; |
| 130 | + p1.x += dx * diff; |
| 131 | + p1.y += dy * diff; |
| 132 | + p2.x -= dx * diff; |
| 133 | + p2.y -= dy * diff; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
151 | 137 | } |
152 | 138 |
|
153 | 139 | draw(t: number) { |
|
180 | 166 | y: number; |
181 | 167 | r: number; |
182 | 168 | opacity: number; |
183 | | - drift: number; |
184 | 169 | } |
185 | 170 |
|
186 | 171 | let strokes: InkStroke[] = []; |
187 | 172 | let dots: InkDot[] = []; |
188 | 173 |
|
| 174 | + function drawDots() { |
| 175 | + for (const d of dots) { |
| 176 | + ctx.beginPath(); |
| 177 | + ctx.arc(d.x, d.y, d.r, 0, Math.PI * 2); |
| 178 | + ctx.fillStyle = `rgba(28, 25, 23, ${d.opacity})`; |
| 179 | + ctx.fill(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
189 | 183 | function init() { |
190 | 184 | resize(); |
191 | 185 | strokes = []; |
192 | 186 | dots = []; |
193 | 187 |
|
194 | | - // 20-30 flowing strokes |
195 | 188 | const strokeCount = 20 + Math.floor(Math.random() * 11); |
196 | 189 | for (let i = 0; i < strokeCount; i++) { |
197 | 190 | strokes.push(new InkStroke()); |
198 | 191 | } |
199 | 192 |
|
200 | | - // 60-100 scattered ink dots |
201 | 193 | const dotCount = 60 + Math.floor(Math.random() * 41); |
202 | 194 | for (let i = 0; i < dotCount; i++) { |
203 | 195 | dots.push({ |
204 | 196 | x: width * (0.1 + Math.random() * 0.85), |
205 | 197 | y: Math.random() * height, |
206 | | - r: 0.8 + Math.random() * 3, |
207 | | - opacity: 0.04 + Math.random() * 0.12, |
208 | | - drift: Math.random() * 1000, |
| 198 | + r: 1.5 + Math.random() * 4, |
| 199 | + opacity: 0.4 + Math.random() * 0.5 |
209 | 200 | }); |
210 | 201 | } |
211 | | - } |
212 | | - |
213 | | - function drawDots(t: number) { |
214 | | - for (const d of dots) { |
215 | | - const ox = Math.sin(t * 0.15 + d.drift) * 3; |
216 | | - const oy = Math.cos(t * 0.1 + d.drift + 1) * 3; |
217 | 202 |
|
218 | | - // mouse interaction for dots too |
219 | | - let fx = 0, fy = 0; |
220 | | - const dx = (d.x + ox) - mouse.x; |
221 | | - const dy = (d.y + oy) - mouse.y; |
222 | | - const dist = Math.sqrt(dx * dx + dy * dy); |
223 | | - if (dist < 180) { |
224 | | - const force = (180 - dist) / 180; |
225 | | - fx = (dx / dist) * force * 8; |
226 | | - fy = (dy / dist) * force * 8; |
| 203 | + // Pre-simulate physics to generate stable, smooth, organic curves |
| 204 | + const PRE_SIM_STEPS = 400; |
| 205 | + for (let i = 0; i < PRE_SIM_STEPS; i++) { |
| 206 | + const t = i * 0.016; |
| 207 | + for (const s of strokes) { |
| 208 | + s.update(t); |
227 | 209 | } |
228 | | - |
229 | | - ctx.beginPath(); |
230 | | - ctx.arc(d.x + ox + fx, d.y + oy + fy, d.r, 0, Math.PI * 2); |
231 | | - ctx.fillStyle = `rgba(28, 25, 23, ${d.opacity})`; |
232 | | - ctx.fill(); |
233 | 210 | } |
234 | | - } |
235 | 211 |
|
236 | | - let time = 0; |
237 | | - |
238 | | - function frame() { |
239 | | - time += 0.016; |
| 212 | + // Draw exactly once |
240 | 213 | ctx.clearRect(0, 0, width, height); |
241 | | - |
242 | 214 | for (const s of strokes) { |
243 | | - s.update(time); |
244 | | - s.draw(time); |
| 215 | + s.draw(PRE_SIM_STEPS * 0.016); |
245 | 216 | } |
246 | | - |
247 | | - drawDots(time); |
248 | | - animId = requestAnimationFrame(frame); |
249 | | - } |
250 | | - |
251 | | - // mouse tracking — listen on the parent hero section |
252 | | - const heroEl = canvas.closest('.hero') || canvas.parentElement?.parentElement; |
253 | | - if (heroEl) { |
254 | | - (heroEl as HTMLElement).style.pointerEvents = 'auto'; |
255 | | - heroEl.addEventListener('mousemove', (e: Event) => { |
256 | | - const me = e as MouseEvent; |
257 | | - const rect = canvas.getBoundingClientRect(); |
258 | | - mouse.x = me.clientX - rect.left; |
259 | | - mouse.y = me.clientY - rect.top; |
260 | | - }); |
261 | | - heroEl.addEventListener('mouseleave', () => { |
262 | | - mouse.x = -1000; |
263 | | - mouse.y = -1000; |
264 | | - }); |
| 217 | + drawDots(); |
265 | 218 | } |
266 | 219 |
|
| 220 | + // Use IntersectionObserver or a simple initial call + resize |
267 | 221 | init(); |
268 | | - frame(); |
269 | 222 |
|
270 | 223 | window.addEventListener('resize', () => { init(); }); |
271 | | - |
272 | | - // respect prefers-reduced-motion |
273 | | - const mq = window.matchMedia('(prefers-reduced-motion: reduce)'); |
274 | | - if (mq.matches) { |
275 | | - cancelAnimationFrame(animId); |
276 | | - for (const s of strokes) s.draw(0); |
277 | | - drawDots(0); |
278 | | - } |
279 | 224 | </script> |
0 commit comments