-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton-particles.js
More file actions
140 lines (118 loc) · 3.35 KB
/
button-particles.js
File metadata and controls
140 lines (118 loc) · 3.35 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
// Particle burst effect on button hover
(function() {
'use strict';
const button = document.getElementById('openFlipbook');
const canvas = document.getElementById('buttonParticles');
if (!button || !canvas) return;
// Check for reduced motion preference
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) return;
const ctx = canvas.getContext('2d');
let particles = [];
let animationId = null;
let isHovering = false;
// Particle configuration
const config = {
colors: [
'rgba(139, 92, 246, 0.8)', // Violet
'rgba(196, 181, 253, 0.7)', // Light violet
'rgba(124, 58, 237, 0.75)', // Darker violet
'rgba(221, 214, 254, 0.6)' // Very light violet
],
minSize: 2,
maxSize: 5,
speed: 2,
spawnRate: 3
};
// Position canvas over button (relative to .button-container parent)
function positionCanvas() {
const rect = button.getBoundingClientRect();
// Canvas is inside .button-container, so position relative to it
canvas.style.left = '0';
canvas.style.top = '0';
canvas.width = rect.width;
canvas.height = rect.height;
}
// Create a particle
function createParticle() {
const angle = Math.random() * Math.PI * 2;
const speed = Math.random() * config.speed + 1;
return {
x: canvas.width / 2,
y: canvas.height / 2,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed,
size: Math.random() * (config.maxSize - config.minSize) + config.minSize,
color: config.colors[Math.floor(Math.random() * config.colors.length)],
life: 1,
decay: Math.random() * 0.015 + 0.01
};
}
// Update particle
function updateParticle(p) {
p.x += p.vx;
p.y += p.vy;
p.life -= p.decay;
// Add slight gravity
p.vy += 0.05;
}
// Draw particle
function drawParticle(p) {
ctx.save();
ctx.globalAlpha = p.life;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fillStyle = p.color;
ctx.fill();
ctx.restore();
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Spawn new particles while hovering
if (isHovering) {
for (let i = 0; i < config.spawnRate; i++) {
particles.push(createParticle());
}
}
// Update and draw particles
particles = particles.filter(p => {
updateParticle(p);
drawParticle(p);
return p.life > 0 &&
p.x > -10 && p.x < canvas.width + 10 &&
p.y > -10 && p.y < canvas.height + 10;
});
// Continue animation if particles exist or hovering
if (particles.length > 0 || isHovering) {
animationId = requestAnimationFrame(animate);
} else {
animationId = null;
}
}
// Start animation
function startAnimation() {
isHovering = true;
if (!animationId) {
animate();
}
}
// Stop spawning new particles
function stopAnimation() {
isHovering = false;
}
// Event listeners
button.addEventListener('mouseenter', () => {
positionCanvas();
startAnimation();
});
button.addEventListener('mouseleave', stopAnimation);
// Reposition on resize
window.addEventListener('resize', () => {
if (isHovering) {
positionCanvas();
}
});
// Initial position
positionCanvas();
})();