Spaces:
Running
Running
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const canvas = document.getElementById("canvas");
|
| 2 |
+
const ctx = canvas.getContext("2d");
|
| 3 |
+
|
| 4 |
+
const densityInput = document.getElementById("density");
|
| 5 |
+
const curlInput = document.getElementById("curl");
|
| 6 |
+
const speedInput = document.getElementById("speed");
|
| 7 |
+
const regenBtn = document.getElementById("regen");
|
| 8 |
+
|
| 9 |
+
function resize() {
|
| 10 |
+
canvas.width = window.innerWidth;
|
| 11 |
+
canvas.height = window.innerHeight;
|
| 12 |
+
}
|
| 13 |
+
window.addEventListener("resize", resize);
|
| 14 |
+
resize();
|
| 15 |
+
|
| 16 |
+
let seed = Math.random() * 1000;
|
| 17 |
+
let particles = [];
|
| 18 |
+
|
| 19 |
+
function randNoise(x, y) {
|
| 20 |
+
return Math.sin(x * 0.002 + seed) +
|
| 21 |
+
Math.cos(y * 0.002 + seed);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function createParticles() {
|
| 25 |
+
particles = [];
|
| 26 |
+
for (let i = 0; i < densityInput.value; i++) {
|
| 27 |
+
particles.push({
|
| 28 |
+
x: Math.random() * canvas.width,
|
| 29 |
+
y: Math.random() * canvas.height,
|
| 30 |
+
life: Math.random() * 200
|
| 31 |
+
});
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
function flowAngle(x, y) {
|
| 36 |
+
return randNoise(x, y) * curlInput.value;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
function draw() {
|
| 40 |
+
ctx.fillStyle = "rgba(0, 0, 0, 0.08)";
|
| 41 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 42 |
+
|
| 43 |
+
for (const p of particles) {
|
| 44 |
+
const angle = flowAngle(p.x, p.y);
|
| 45 |
+
const vx = Math.cos(angle) * speedInput.value;
|
| 46 |
+
const vy = Math.sin(angle) * speedInput.value;
|
| 47 |
+
|
| 48 |
+
ctx.strokeStyle = `hsla(${(p.life * 3) % 360}, 100%, 60%, 0.8)`;
|
| 49 |
+
ctx.beginPath();
|
| 50 |
+
ctx.moveTo(p.x, p.y);
|
| 51 |
+
|
| 52 |
+
p.x += vx;
|
| 53 |
+
p.y += vy;
|
| 54 |
+
|
| 55 |
+
ctx.lineTo(p.x, p.y);
|
| 56 |
+
ctx.stroke();
|
| 57 |
+
|
| 58 |
+
p.life--;
|
| 59 |
+
|
| 60 |
+
if (
|
| 61 |
+
p.life <= 0 ||
|
| 62 |
+
p.x < 0 || p.x > canvas.width ||
|
| 63 |
+
p.y < 0 || p.y > canvas.height
|
| 64 |
+
) {
|
| 65 |
+
p.x = Math.random() * canvas.width;
|
| 66 |
+
p.y = Math.random() * canvas.height;
|
| 67 |
+
p.life = 200;
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
requestAnimationFrame(draw);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
regenBtn.onclick = () => {
|
| 75 |
+
seed = Math.random() * 1000;
|
| 76 |
+
createParticles();
|
| 77 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
createParticles();
|
| 81 |
+
draw();
|