EXP 01PLAYABLEThe studio
The crow that follows the cursor
A spring, not an interpolation: the crow keeps its momentum, slightly overshoots its target and rests when it has nothing left to do. Seeds and the two settings change its temper.
How it works
- On every frame, velocity is pulled towards the target (stiffness), then slowed down (damping).
- Horizontal velocity tilts the sprite and picks its profile, left or right.
- Below a movement threshold the loop stops: no computation at rest.
move the cursor, click to drop a seed
spring.jsMIT
// Un ressort à deux réglages : raideur (k) et amortissement (d).
// Pas de durée : il s'arrête quand il n'a plus d'élan.
const spring = (apply, k = 0.06, d = 0.86) => {
let x = 0, y = 0, vx = 0, vy = 0, tx = 0, ty = 0, raf = 0;
const step = () => {
vx = (vx + (tx - x) * k) * d;
vy = (vy + (ty - y) * k) * d;
x += vx; y += vy;
apply(x, y, vx, vy);
const moving = Math.abs(vx) + Math.abs(vy)
+ Math.abs(tx - x) + Math.abs(ty - y) > 0.01;
raf = moving ? requestAnimationFrame(step) : 0;
};
return {
to(a, b) { tx = a; ty = b; if (!raf) raf = requestAnimationFrame(step); }
};
};
// La vitesse horizontale incline le corbeau et choisit son profil.
const crow = spring((x, y, vx) => {
const tilt = Math.max(-14, Math.min(14, vx * 1.6));
bird.style.transform = `translate(${x}px, ${y}px) rotate(${tilt}deg)`;
bird.classList.toggle('flip', vx < 0);
});
sky.addEventListener('pointermove', e => crow.to(e.offsetX, e.offsetY));