-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle generator.html
152 lines (131 loc) · 4.4 KB
/
particle generator.html
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
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Particle Donut</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #ffffff;
touch-action: none; /* Prevent default touch behaviors */
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="whiteboard"></canvas>
<script>
const canvas = document.getElementById('whiteboard');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particles = [];
let hue = 0;
let isMouseDown = false;
class Particle {
constructor(x, y, angle, speed, radius) {
this.x = x;
this.y = y;
this.angle = angle;
this.speed = speed;
this.radius = radius;
this.hue = hue;
this.life = 240; // Life in frames (4 seconds at 60fps)
}
update() {
if (this.life > 0) {
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
this.speed *= 0.95; // Slow down for ease effect
this.hue += 5;
// Avoid entering the donut hole and aggregate on the outer edge
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const distanceToCenter = Math.hypot(this.x - centerX, this.y - centerY);
const donutInnerRadius = 80;
const donutOuterRadius = 120;
const pullStrength = 0.02;
if (distanceToCenter < donutInnerRadius || distanceToCenter > donutOuterRadius) {
const pullAngle = Math.atan2(centerY - this.y, centerX - this.x);
const targetRadius = distanceToCenter < donutInnerRadius ? donutInnerRadius : donutOuterRadius;
this.x += Math.cos(pullAngle) * pullStrength * (targetRadius - distanceToCenter);
this.y += Math.sin(pullAngle) * pullStrength * (targetRadius - distanceToCenter);
}
this.life -= 1;
} else {
// Move particle away from the donut after life ends
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const moveAngle = Math.atan2(this.y - centerY, this.x - centerX);
this.x += Math.cos(moveAngle) * 2;
this.y += Math.sin(moveAngle) * 2;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = `hsl(${this.hue}, 70%, 70%)`; // Pastel colors with reduced saturation and higher lightness
ctx.fill();
}
}
function createParticles(x, y) {
for (let i = 0; i < 10; i++) {
const angle = Math.random() * Math.PI * 2;
const speed = Math.random() * 5 + 2;
const radius = Math.random() * 5 + 2;
particles.push(new Particle(x, y, angle, speed, radius));
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach((particle, index) => {
particle.update();
particle.draw();
// Remove particles far outside the canvas
if (particle.x < -100 || particle.x > canvas.width + 100 || particle.y < -100 || particle.y > canvas.height + 100) {
particles.splice(index, 1);
}
});
hue += 1;
requestAnimationFrame(animate);
}
canvas.addEventListener('mousedown', (e) => {
isMouseDown = true;
createParticles(e.clientX, e.clientY);
});
canvas.addEventListener('mouseup', () => {
isMouseDown = false;
});
canvas.addEventListener('mousemove', (e) => {
if (isMouseDown) {
createParticles(e.clientX, e.clientY);
}
});
canvas.addEventListener('touchstart', (e) => {
isMouseDown = true;
const touch = e.touches[0];
createParticles(touch.clientX, touch.clientY);
e.preventDefault();
});
canvas.addEventListener('touchend', () => {
isMouseDown = false;
});
canvas.addEventListener('touchmove', (e) => {
if (isMouseDown) {
const touch = e.touches[0];
createParticles(touch.clientX, touch.clientY);
e.preventDefault();
}
});
animate();
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>