-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
125 lines (109 loc) · 3.62 KB
/
script.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Polyfill for requestAnimationFrame to ensure compatibility across different browsers
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame
// onload event handler to initialize the application
onload = function (){
setTimeout(init, 0) // Delay the initialization to ensure the DOM is fully loaded
}
// Initialization function
init = function(){
// Get the canvas element and its 2D drawing context
canvas = document.querySelector('canvas')
ctx = canvas.getContext('2d')
// Resize the canvas to match its displayed size
onresize = function(){
canvas.width = canvas.clientWidth
canvas.height = canvas.clientHeight
}
onresize()
// Initialize mouse object to track mouse position and state
mouse = { x: canvas.width / 2, y: canvas.height / 2, out: false }
// Event handler for mouse leaving the canvas
canvas.onmouseout = function(){
mouse.out = true
}
// Event handler for mouse movement over the canvas
canvas.onmousemove = function(e){
var rect = canvas.getBoundingClientRect()
mouse = {
x: e.clientX - rect.left,
y: e.clientY - rect.top,
out: false
}
}
// Initialize particle system variables
gravityStrength = 10
particles = []
spawnTimer = 0
spawnInterval = 10
type = 0
// Start the animation loop
requestAnimationFrame(startLoop)
}
// Function to create a new particle
newParticle = function(){
type = type ? 0 : 1 // Toggle particle type
particles.push({
x: mouse.x,
y: mouse.y,
xv: type ? 18 * Math.random() - 9 : 24 * Math.random() - 12, // Random x velocity
yv: type ? 18 * Math.random() - 9 : 24 * Math.random() - 12, // Random y velocity
c: type ? 'rgb(255,' + ((200 * Math.random()) | 0) + ',' + ((80 * Math.random()) | 0) + ')' : 'rgb(255,255,255)', // Color
s: type ? 5 + 10 * Math.random() : 1, // Size
a: 1 // Alpha (opacity)
})
}
// Function to start the animation loop
startLoop = function(newTime){
time = newTime
requestAnimationFrame(loop)
}
// Main animation loop
loop = function(newTime){
draw() // Draw particles
calculate(newTime) // Update particle positions and states
requestAnimationFrame(loop) // Request the next frame
}
// Function to draw particles on the canvas
draw = function(){
ctx.clearRect(0, 0, canvas.width, canvas.height) // Clear the canvas
for (var i = 0; i < particles.length; i++){
var p = particles[i]
ctx.globalAlpha = p.a // Set particle opacity
ctx.fillStyle = p.c // Set particle color
ctx.beginPath()
ctx.arc(p.x, p.y, p.s, 0, 2 * Math.PI) // Draw particle as a circle
ctx.fill()
}
}
// Function to update particle positions and states
calculate = function(newTime){
var dt = newTime - time // Calculate time delta
time = newTime
// Spawn new particles if the mouse is not outside the canvas
if (!mouse.out){
spawnTimer += (dt < 100) ? dt : 100
for (; spawnTimer > 0; spawnTimer -= spawnInterval){
newParticle()
}
}
// Remove excess particles to limit the total number
particleOverflow = particles.length - 700
if (particleOverflow > 0){
particles.splice(0, particleOverflow)
}
// Update each particle's position and velocity
for (var i = 0; i < particles.length; i++){
var p = particles[i]
if (!mouse.out){
x = mouse.x - p.x
y = mouse.y - p.y
a = x * x + y * y
a = a > 100 ? gravityStrength / a : gravityStrength / 100
p.xv = (p.xv + a * x) * 0.99
p.yv = (p.yv + a * y) * 0.99
}
p.x += p.xv
p.y += p.yv
p.a *= 0.99 // Fade out the particle over time
}
}