Move to part the fibres ยท click to re-seed
Source ยท src/sketches/wool-threads.js
// Wool threads โ fibres drifting through a noise field; the cursor parts them.
// p5.js instance mode: (p, el) โ el is the container the canvas fills.
export default function woolThreads(p, el) {
const CREAM = [246, 240, 228];
let W, H, t = 0, fibres = [];
const spawn = (f = {}) => Object.assign(f, { x: p.random(W), y: p.random(H), life: p.random(80, 260) });
const reset = () => { p.background(...CREAM); fibres = Array.from({ length: Math.round(W * H / 700) }, () => spawn()); };
p.setup = () => {
W = el.clientWidth; H = el.clientHeight;
p.createCanvas(W, H);
reset();
};
p.draw = () => {
p.noStroke(); p.fill(...CREAM, 7); p.rect(0, 0, W, H); // slow fade keeps trails soft
p.strokeWeight(1); p.stroke(30, 22, 18, 40);
const inside = p.mouseX > 0 && p.mouseX < W && p.mouseY > 0 && p.mouseY < H;
for (const f of fibres) {
const a = p.noise(f.x * 0.0022, f.y * 0.0022, t) * p.TWO_PI * 2;
let dx = Math.cos(a), dy = Math.sin(a);
if (inside) {
const mx = f.x - p.mouseX, my = f.y - p.mouseY, d = Math.hypot(mx, my);
if (d < 130) { dx += (mx / (d + 1)) * 1.6; dy += (my / (d + 1)) * 1.6; }
}
const nx = f.x + dx * 1.3, ny = f.y + dy * 1.3;
p.line(f.x, f.y, nx, ny);
f.x = nx; f.y = ny;
if (--f.life < 0 || f.x < 0 || f.x > W || f.y < 0 || f.y > H) spawn(f);
}
t += 0.0016;
};
p.mousePressed = () => {
if (p.mouseX >= 0 && p.mouseX <= W && p.mouseY >= 0 && p.mouseY <= H) { p.noiseSeed(p.random(1e6)); reset(); }
};
p.windowResized = () => { W = el.clientWidth; H = el.clientHeight; p.resizeCanvas(W, H); reset(); };
}