Repository navigation
Expand file tree
/
Copy pathtest.html
More file actions
143 lines (127 loc) · 3.66 KB
/
Copy pathtest.html
File metadata and controls
143 lines (127 loc) · 3.66 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS Particle Playground</title>
<style>
body { margin: 0; background: #111; color: #eee; font-family: sans-serif; }
#ui { position: fixed; top: 10px; left: 10px; z-index: 10; }
button { margin-right: 5px; }
</style>
</head>
<body>
<div id="ui">
<button id="spawn">Spawn 50</button>
<button id="clear">Clear</button>
Gravity: <input id="gravity" type="range" min="0" max="1" step="0.01" value="0.2">
</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const spawnBtn = document.getElementById('spawn');
const clearBtn = document.getElementById('clear');
const gravitySlider = document.getElementById('gravity');
let particles = [];
let gravity = parseFloat(gravitySlider.value);
let mouse = { x: 0, y: 0, down: false };
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
class Particle {
constructor(x, y, vx, vy, r, color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.color = color;
}
update() {
this.vy += gravity;
this.x += this.vx;
this.y += this.vy;
// Walls
if (this.x - this.r < 0) {
this.x = this.r;
this.vx *= -0.9;
}
if (this.x + this.r > canvas.width) {
this.x = canvas.width - this.r;
this.vx *= -0.9;
}
if (this.y - this.r < 0) {
this.y = this.r;
this.vy *= -0.9;
}
if (this.y + this.r > canvas.height) {
this.y = canvas.height - this.r;
this.vy *= -0.9;
}
// Mouse attraction
if (mouse.down) {
const dx = mouse.x - this.x;
const dy = mouse.y - this.y;
const dist = Math.sqrt(dx * dx + dy * dy) || 1;
const force = 0.5 / dist;
this.vx += dx * force;
this.vy += dy * force;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
}
function randomColor() {
const h = Math.floor(Math.random() * 360);
return `hsl(${h}, 80%, 60%)`;
}
function spawn(n = 50) {
for (let i = 0; i < n; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height * 0.5;
const vx = (Math.random() - 0.5) * 10;
const vy = (Math.random() - 0.5) * 10;
const r = 5 + Math.random() * 15;
particles.push(new Particle(x, y, vx, vy, r, randomColor()));
}
}
function clear() {
particles = [];
}
spawnBtn.addEventListener('click', () => spawn(50));
clearBtn.addEventListener('click', clear);
gravitySlider.addEventListener('input', () => {
gravity = parseFloat(gravitySlider.value);
});
canvas.addEventListener('mousedown', e => {
mouse.down = true;
mouse.x = e.clientX;
mouse.y = e.clientY;
});
canvas.addEventListener('mouseup', () => {
mouse.down = false;
});
canvas.addEventListener('mousemove', e => {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
function loop() {
ctx.fillStyle = 'rgba(0,0,0,0.3)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (const p of particles) {
p.update();
p.draw();
}
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>