-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (88 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
var simplex = new SimplexNoise();
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
const params = new URLSearchParams(window.location.search);
const numberOfParticles = 1;
const showParticles = true;
const particleOpacity = params.get("opacity") || 0.5;
const particleRadius = params.get("size") || 2;
const showMouse = true;
const renderDot = (dot) => {
ctx.beginPath();
ctx.arc(dot.position.x, dot.position.y, dot.radius, 0, Math.PI * 2);
ctx.fillStyle = dot.color || "#ccc";
ctx.fill();
ctx.closePath();
};
let mouseDot = {
radius: 1,
speed: { value: 0, direction: { x: 0, y: 0 } },
position: { x: -10, y: -10 },
color: `rgba(255,255,255,1)`,
};
let particles = []; /*Array(numberOfParticles)
.fill(0)
.map((_, i) => ({
position: {
x: Math.round(canvas.width * Math.random()),
y: Math.round(canvas.height * Math.random()),
},
color: `rgba(255,255,255,${Math.random()})`,
radius: particleRadius,
speed: 1,
angle: 360 * Math.random(),
}));*/
const spawnParticle = (position) => {
particles.push({
position,
color: `rgba(255,255,255,${Math.random() * particleOpacity})`,
radius: particleRadius * Math.random(),
speed: 1,
angle: 360 * Math.random(),
});
};
canvas.addEventListener("mousemove", (e) => {
mouseDot.position = { x: e.offsetX, y: e.offsetY };
// spawnParticle({ x: e.offsetX, y: e.offsetY });
// spawnParticle({ x: e.offsetX, y: e.offsetY });
// spawnParticle({ x: e.offsetX, y: e.offsetY });
// spawnParticle({ x: e.offsetX, y: e.offsetY });
});
function update(timestamp) {
for (let i = 0; i < particles.length; i++) {
particles[i].position.y +=
1 +
simplex.noise(i / 10000, (i + timestamp) / 10000) / 10 +
simplex.noise(i / 10000, (i + timestamp) / 100000);
particles[i].position.x +=
(simplex.noise(i / 10000, (i * i + timestamp) / 2000) /
particles[i].radius) *
(params.get("octave1") || 1) +
simplex.noise(i / 10000, (i + timestamp) / 20000) *
(params.get("octave2") || 0.1);
}
// if (Math.random() < 0.1)
spawnParticle({ x: canvas.width * Math.random(), y: 0 });
// spawnParticle({ ...mouseDot.position });
// spawnParticle({ ...mouseDot.position });
// spawnParticle({ ...mouseDot.position });
}
function render() {
if (params.get("clear") !== "false")
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (showParticles) particles.map(renderDot);
// if (showMouse) renderDot(mouseDot);
}
let lastTimestamp = 0;
function gameLoop(timestamp) {
const deltaTime = timestamp - lastTimestamp;
if (deltaTime >= 16.67) {
lastTimestamp = timestamp;
update(timestamp);
render();
}
requestAnimationFrame(gameLoop);
}
gameLoop();