forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFireworks.js
209 lines (193 loc) · 4.54 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* https://codepen.io/juliangarnier/pen/gmOwJX
* custom by hexo-theme-yun @YunYouJun
*/
import React from 'react'
import anime from 'animejs'
export const Fireworks = () => {
React.useEffect(() => {
createFireworks({})
}, [])
return <canvas id='fireworks' className='fireworks'></canvas>
}
/**
* 创建烟花
* @param config
*/
function createFireworks(config) {
const defaultColors = ['102, 167, 221', '62, 131, 225', '33, 78, 194']
const defaultConfig = {
colors: defaultColors,
numberOfParticules: 20,
orbitRadius: {
min: 50,
max: 100
},
circleRadius: {
min: 10,
max: 20
},
diffuseRadius: {
min: 50,
max: 100
},
animeDuration: {
min: 900,
max: 1500
}
}
config = Object.assign(defaultConfig, config)
let pointerX = 0
let pointerY = 0
// sky blue
const colors = config.colors || defaultColors
const canvasEl = document.querySelector('.fireworks')
const ctx = canvasEl.getContext('2d')
/**
* 设置画布尺寸
*/
function setCanvasSize(canvasEl) {
canvasEl.width = window.innerWidth
canvasEl.height = window.innerHeight
canvasEl.style.width = `${window.innerWidth}px`
canvasEl.style.height = `${window.innerHeight}px`
}
/**
* update pointer
* @param {TouchEvent} e
*/
function updateCoords(e) {
pointerX =
e.clientX ||
(e.touches[0] ? e.touches[0].clientX : e.changedTouches[0].clientX)
pointerY =
e.clientY ||
(e.touches[0] ? e.touches[0].clientY : e.changedTouches[0].clientY)
}
function setParticuleDirection(p) {
const angle = (anime.random(0, 360) * Math.PI) / 180
const value = anime.random(
config.diffuseRadius.min,
config.diffuseRadius.max
)
const radius = [-1, 1][anime.random(0, 1)] * value
return {
x: p.x + radius * Math.cos(angle),
y: p.y + radius * Math.sin(angle)
}
}
/**
* 在指定位置创建粒子
* @param {number} x
* @param {number} y
* @returns
*/
function createParticule(x, y) {
const p = {
x,
y,
color: `rgba(${
colors[anime.random(0, colors.length - 1)]
},${
anime.random(0.2, 0.8)
})`,
radius: anime.random(config.circleRadius.min, config.circleRadius.max),
endPos: null,
draw() {}
}
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) {
const p = {
x,
y,
color: '#000',
radius: 0.1,
alpha: 0.5,
lineWidth: 6,
draw() {}
}
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 (let i = 0; i < anim.animatables.length; i++) { anim.animatables[i].target.draw() }
}
function animateParticules(x, y) {
const circle = createCircle(x, y)
const particules = []
for (let i = 0; i < config.numberOfParticules; i++) { particules.push(createParticule(x, y)) }
anime
.timeline()
.add({
targets: particules,
x(p) {
return p.endPos.x
},
y(p) {
return p.endPos.y
},
radius: 0.1,
duration: anime.random(
config.animeDuration.min,
config.animeDuration.max
),
easing: 'easeOutExpo',
update: renderParticule
})
.add(
{
targets: circle,
radius: anime.random(config.orbitRadius.min, config.orbitRadius.max),
lineWidth: 0,
alpha: {
value: 0,
easing: 'linear',
duration: anime.random(600, 800)
},
duration: anime.random(1200, 1800),
easing: 'easeOutExpo',
update: renderParticule
},
0
)
}
const render = anime({
duration: Infinity,
update: () => {
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height)
}
})
document.addEventListener(
'mousedown',
(e) => {
render.play()
updateCoords(e)
animateParticules(pointerX, pointerY)
},
false
)
setCanvasSize(canvasEl)
window.addEventListener(
'resize',
() => {
setCanvasSize(canvasEl)
},
false
)
}