From e01f9779030955d828d0c7c8668651fd4686ec58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Mon, 4 May 2020 18:46:26 +0200 Subject: [PATCH] Add boids by clicking --- index.mjs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/index.mjs b/index.mjs index 882b24e..80e2ddc 100644 --- a/index.mjs +++ b/index.mjs @@ -37,13 +37,23 @@ window.onresize = updateSize; const activeBoids = []; -for (let i = 0; i < 300; ++i) +const addBoids = (pos, count) => { - activeBoids.push({ - pos: new Vector(200 + (i % 2) * (-400), 0), - vel: new Vector(Math.random() * 100, Math.random() * 100), - }); -} + for (let i = 0; i < count; ++i) + { + activeBoids.push({ + pos: pos.clone(), + vel: new Vector(Math.random(), Math.random()), + }); + } +}; + +boidsCanvas.onclick = ev => +{ + const pos = new Vector(ev.offsetX, ev.offsetY); + pos.sub(center); + addBoids(pos, 50); +}; let paused = false; let lastTime = null;