-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (61 loc) · 2.02 KB
/
app.js
File metadata and controls
73 lines (61 loc) · 2.02 KB
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
const canvas = document.getElementById("main");
function updateCanvasSize() {
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
}
window.addEventListener("load", () => {
init();
});
window.addEventListener("resize", (e) => {
updateCanvasSize();
});
updateCanvasSize();
canvas.addEventListener("mousemove", (e) => {
input.targetX = e.clientX;
input.targetY = e.clientY;
});
const checkbox = document.getElementById("particles-mode");
checkbox.addEventListener("change", (e) => {
particles.clear();
particles.trailMode = checkbox.checked;
});
const input = {
targetX: canvas.width / 2,
targetY: canvas.height / 2,
currentX: canvas.width / 2,
currentY: canvas.height / 2,
};
/** @type {WebGL2RenderingContext} */
const gl = canvas.getContext("webgl2");
console.log(gl.getSupportedExtensions());
const particles = new GpuParticlesContext();
async function init() {
await particles.load();
requestAnimationFrame(updateFrame);
}
function updateFrame() {
const startX = input.currentX;
const startY = input.currentY;
const endX = input.currentX += (input.targetX - input.currentX) * 0.2;
const endY = input.currentY += (input.targetY - input.currentY) * 0.2;
const emitCount = particles.trailMode ? 10 : 1000;
for (let i = 0; i < emitCount; i++) {
const lifetime = 60 + Math.random() * 100;
const azimuth = Math.random() * Math.PI * 2.0;
const elevation = Math.random() * Math.PI - Math.PI / 2;
const speed = 0.002 + Math.random() * 0.005;
const vx = speed * Math.sin(azimuth) * Math.cos(elevation);
const vy = speed * Math.sin(elevation);
const vz = speed * Math.cos(azimuth) * Math.cos(elevation);
const mx = startX + (endX - startX) * i / emitCount;
const my = startY + (endY - startY) * i / emitCount;
const x = mx / canvas.width * 2 - 1;
const y = 1 - my / canvas.height * 2;
particles.emit(lifetime, [x, y, 0.0], [vx, vy, vz]);
}
particles.update();
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
particles.render();
requestAnimationFrame(updateFrame);
}