Skip to content

Commit e8ade9d

Browse files
committed
feat(hero): make ink canvas completely stationary
- Removed the requestAnimationFrame loop and all mouse interaction listeners - Pre-simulate stroke physics for 400 frames on load to generate organic shapes - Draw strokes and splatter dots exactly once to create a static, textured focal point
1 parent 1667090 commit e8ade9d

1 file changed

Lines changed: 46 additions & 101 deletions

File tree

‎src/components/InkCanvas.astro‎

Lines changed: 46 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343

4444
let width: number, height: number;
4545
let dpr = Math.min(window.devicePixelRatio || 1, 2);
46-
let mouse = { x: -1000, y: -1000 };
47-
let animId: number;
4846

4947
function resize() {
5048
const rect = canvas.parentElement!.getBoundingClientRect();
@@ -66,12 +64,10 @@
6664

6765
constructor() {
6866
this.points = [];
69-
// distribute strokes across the canvas
7067
const startX = width * (0.25 + Math.random() * 0.65);
7168
const startY = height * (0.05 + Math.random() * 0.9);
7269
const spread = 100 + Math.random() * 200;
7370

74-
// 10-20 control points per stroke for longer curves
7571
const numPoints = 10 + Math.floor(Math.random() * 11);
7672
for (let i = 0; i < numPoints; i++) {
7773
const angle = Math.random() * Math.PI * 2;
@@ -84,16 +80,10 @@
8480
});
8581
}
8682

87-
// much more visible ink tones
8883
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)'
9787
];
9888
this.color = colors[Math.floor(Math.random() * colors.length)];
9989
this.baseWidth = 1 + Math.random() * 4;
@@ -106,48 +96,44 @@
10696
for (const p of this.points) {
10797
const nx = Math.sin(t * this.speed + p.y * 0.004 + this.noiseOffset) * this.wander;
10898
const ny = Math.cos(t * this.speed + p.x * 0.004 + this.noiseOffset + 1.5) * this.wander;
109-
11099
p.vx += (nx - p.vx) * 0.015;
111100
p.vy += (ny - p.vy) * 0.015;
112101
p.x += p.vx;
113102
p.y += p.vy;
114103

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;
130109
}
131110

132-
// Relaxation pass to prevent sharp turns and kinks
133-
// use much weaker position pulling to prevent rubber-banding/shrinking
134111
for (let i = 1; i < this.points.length - 1; i++) {
135112
const p = this.points[i];
136113
const prev = this.points[i - 1];
137114
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
146115
const targetVx = (prev.vx + next.vx) / 2;
147116
const targetVy = (prev.vy + next.vy) / 2;
148117
p.vx += (targetVx - p.vx) * 0.08;
149118
p.vy += (targetVy - p.vy) * 0.08;
150119
}
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+
}
151137
}
152138

153139
draw(t: number) {
@@ -180,100 +166,59 @@
180166
y: number;
181167
r: number;
182168
opacity: number;
183-
drift: number;
184169
}
185170

186171
let strokes: InkStroke[] = [];
187172
let dots: InkDot[] = [];
188173

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+
189183
function init() {
190184
resize();
191185
strokes = [];
192186
dots = [];
193187

194-
// 20-30 flowing strokes
195188
const strokeCount = 20 + Math.floor(Math.random() * 11);
196189
for (let i = 0; i < strokeCount; i++) {
197190
strokes.push(new InkStroke());
198191
}
199192

200-
// 60-100 scattered ink dots
201193
const dotCount = 60 + Math.floor(Math.random() * 41);
202194
for (let i = 0; i < dotCount; i++) {
203195
dots.push({
204196
x: width * (0.1 + Math.random() * 0.85),
205197
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
209200
});
210201
}
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;
217202

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);
227209
}
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();
233210
}
234-
}
235211

236-
let time = 0;
237-
238-
function frame() {
239-
time += 0.016;
212+
// Draw exactly once
240213
ctx.clearRect(0, 0, width, height);
241-
242214
for (const s of strokes) {
243-
s.update(time);
244-
s.draw(time);
215+
s.draw(PRE_SIM_STEPS * 0.016);
245216
}
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();
265218
}
266219

220+
// Use IntersectionObserver or a simple initial call + resize
267221
init();
268-
frame();
269222

270223
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-
}
279224
</script>

0 commit comments

Comments
 (0)