-
Notifications
You must be signed in to change notification settings - Fork 1
/
fireworks.js
177 lines (162 loc) · 5.48 KB
/
fireworks.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Source https://codepen.io/juliangarnier/pen/gmOwJX
// and http://animejs.com/documentation/assets/js/fireworks.js
// (C) Julian Garnier, 2017
// Adapted by Lilian Besson (Naereen) https://github.com/Naereen
// to add a fireworks effect on a background of a page
var canvasEl = document.querySelector('.fireworks');
var ctx = canvasEl.getContext('2d');
var numberOfParticules = 50;
var pointerX = window.innerWidth / 2;
var pointerY = window.innerHeight / 2;
var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';
var colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'];
function setCanvasSize() {
canvasEl.width = window.innerWidth * 2;
canvasEl.height = window.innerHeight * 2;
canvasEl.style.width = window.innerWidth + 'px';
canvasEl.style.height = window.innerHeight + 'px';
canvasEl.getContext('2d').scale(2, 2);
}
function updateCoords(e) {
pointerX = e.clientX || e.touches[0].clientX || e.pageX;
pointerY = e.clientY || e.touches[0].clientY || e.pageY;
}
// document.addEventListener('mousemove', updateCoords, false);
// document.addEventListener('mouseenter', updateCoords, false);
function setParticuleDirection(p) {
var angle = anime.random(0, 360) * Math.PI / 180;
var value = anime.random(50, 180);
var radius = [-1, 1][anime.random(0, 1)] * value;
return {
x: p.x + radius * Math.cos(angle),
y: p.y + radius * Math.sin(angle)
}
}
function createParticule(x, y) {
var p = {};
p.x = x;
p.y = y;
p.color = colors[anime.random(0, colors.length - 1)];
p.radius = anime.random(16, 42);
p.endPos = setParticuleDirection(p);
p.draw = function() {
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);
ctx.fillStyle = p.color;
ctx.fill();
}
return p;
}
function createCircle(x, y) {
var p = {};
p.x = x;
p.y = y;
p.color = '#FFF';
p.radius = 0.1;
p.alpha = .5;
p.lineWidth = 6;
p.draw = function() {
ctx.globalAlpha = p.alpha;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);
ctx.lineWidth = p.lineWidth;
ctx.strokeStyle = p.color;
ctx.stroke();
ctx.globalAlpha = 1;
}
return p;
}
function renderParticule(anim) {
for (var i = 0; i < anim.animatables.length; i++) {
anim.animatables[i].target.draw();
}
}
function animateParticules(x, y) {
var circle = createCircle(x, y);
var particules = [];
for (var i = 0; i < numberOfParticules; i++) {
particules.push(createParticule(x, y));
}
anime.timeline().add({
targets: particules,
x: function(p) { return p.endPos.x; },
y: function(p) { return p.endPos.y; },
radius: 0.1,
duration: anime.random(1200, 5800),
easing: 'easeOutExpo',
update: renderParticule
})
.add({
targets: circle,
radius: anime.random(120, 1220),
lineWidth: 0,
alpha: {
value: 0,
easing: 'linear',
duration: anime.random(600, 5800),
},
duration: anime.random(1200, 5800),
easing: 'easeOutExpo',
update: renderParticule,
offset: 0
});
}
var render = anime({
duration: Infinity,
update: function() {
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
}
});
function fireTheFireworks(e) {
render.play();
if (e !== undefined) {
updateCoords(e);
}
animateParticules(pointerX, pointerY);
};
// Trick to resize the canvas if needed
var centerX = window.innerWidth / 2;
var centerY = window.innerHeight / 2;
setCanvasSize();
window.addEventListener('resize', setCanvasSize, false);
window.addEventListener(tap, function(e) {
fireTheFireworks(e)
}, false);
// // Change the function, to display fireworks in case of keyboard shortcuts
// $(document).ready(function() {
// Mousetrap.bind(["r", "R"], function(e) {
// console.log("Calling new version of function bound to ['r', 'R']...");
// doublereload();
// fireTheFireworks();
// });
// Mousetrap.bind(["left", "p", "P"], function(e) {
// console.log("Calling new version of function bound to ['left', 'p', 'P']...");
// prev();
// fireTheFireworks();
// });
// Mousetrap.bind(["space", "b", "B"], function(e) {
// console.log("Calling new version of function bound to ['space', 'b', 'B']...");
// playpause();
// fireTheFireworks();
// });
// Mousetrap.bind(["right", "n", "N"], function(e) {
// console.log("Calling new version of function bound to ['right', 'n', 'N']...");
// next();
// fireTheFireworks();
// });
// Mousetrap.bind(["down", "d", "D"], function(e) {
// console.log("Calling new version of function bound to ['down', 'd', 'D']...");
// volumedown();
// fireTheFireworks();
// });
// Mousetrap.bind(["up", "u", "U"], function(e) {
// console.log("Calling new version of function bound to ['up', 'u', 'U']...");
// volumeup();
// fireTheFireworks();
// });
// Mousetrap.bind(["h", "H", "?"], function(e) {
// console.log("Calling new version of[ function bound to ['h', 'H', '?']...");
// alert("Use 'p' for previous song, 'n' for next song, 'space' for pause, 'f' to toggle full screen.");
// fireTheFireworks();
// });
// });