|
1 | 1 | /* eslint-disable */
|
2 | 2 | import { useEffect } from 'react'
|
3 |
| -const id = 'canvasFlutteringRibbon' |
| 3 | +import { loadExternalResource } from '@/lib/utils' |
| 4 | + |
4 | 5 | export const FlutteringRibbon = () => {
|
5 |
| - const destroyRibbon = ()=>{ |
6 |
| - const ribbon = document.getElementById(id) |
7 |
| - if(ribbon && ribbon.parentNode){ |
8 |
| - ribbon.parentNode.removeChild(ribbon) |
9 |
| - } |
10 |
| - } |
11 |
| - |
12 | 6 | useEffect(() => {
|
13 |
| - createFlutteringRibbon() |
14 |
| - return () => destroyRibbon() |
| 7 | + loadExternalResource('/js/flutteringRibbon.js', 'js').then(url => { |
| 8 | + window.createFlutteringRibbon && window.createFlutteringRibbon() |
| 9 | + }) |
15 | 10 |
|
| 11 | + return () => |
| 12 | + window.destroyFlutteringRibbon && window.destroyFlutteringRibbon() |
16 | 13 | }, [])
|
17 |
| - return <></> |
| 14 | + return <></> |
18 | 15 | }
|
19 | 16 |
|
20 | 17 | export default FlutteringRibbon
|
21 |
| - |
22 |
| - |
23 |
| -/** |
24 |
| - * 创建连接点 |
25 |
| - * @param config |
26 |
| - */ |
27 |
| -function createFlutteringRibbon() { |
28 |
| - 'object' == typeof window && |
29 |
| - (window.Ribbons = (function () { |
30 |
| - const t = window, |
31 |
| - i = document.body, |
32 |
| - n = document.documentElement |
33 |
| - var o = function () { |
34 |
| - if (1 === arguments.length) { |
35 |
| - if (Array.isArray(arguments[0])) { |
36 |
| - const t = Math.round(o(0, arguments[0].length - 1)) |
37 |
| - return arguments[0][t] |
38 |
| - } |
39 |
| - return o(0, arguments[0]) |
40 |
| - } |
41 |
| - return 2 === arguments.length |
42 |
| - ? Math.random() * (arguments[1] - arguments[0]) + arguments[0] |
43 |
| - : 0 |
44 |
| - } |
45 |
| - const s = function (o) { |
46 |
| - const s = Math.max( |
47 |
| - 0, |
48 |
| - t.innerWidth || n.clientWidth || i.clientWidth || 0 |
49 |
| - ), |
50 |
| - e = Math.max( |
51 |
| - 0, |
52 |
| - t.innerHeight || n.clientHeight || i.clientHeight || 0 |
53 |
| - ) |
54 |
| - return { |
55 |
| - width: s, |
56 |
| - height: e, |
57 |
| - ratio: s / e, |
58 |
| - centerx: s / 2, |
59 |
| - centery: e / 2, |
60 |
| - scrollx: |
61 |
| - Math.max(0, t.pageXOffset || n.scrollLeft || i.scrollLeft || 0) - |
62 |
| - (n.clientLeft || 0), |
63 |
| - scrolly: |
64 |
| - Math.max(0, t.pageYOffset || n.scrollTop || i.scrollTop || 0) - |
65 |
| - (n.clientTop || 0) |
66 |
| - } |
67 |
| - }, |
68 |
| - e = function (t, i) { |
69 |
| - ;(this.x = 0), (this.y = 0), this.set(t, i) |
70 |
| - } |
71 |
| - e.prototype = { |
72 |
| - constructor: e, |
73 |
| - set: function (t, i) { |
74 |
| - ;(this.x = t || 0), (this.y = i || 0) |
75 |
| - }, |
76 |
| - copy: function (t) { |
77 |
| - return (this.x = t.x || 0), (this.y = t.y || 0), this |
78 |
| - }, |
79 |
| - multiply: function (t, i) { |
80 |
| - return (this.x *= t || 1), (this.y *= i || 1), this |
81 |
| - }, |
82 |
| - divide: function (t, i) { |
83 |
| - return (this.x /= t || 1), (this.y /= i || 1), this |
84 |
| - }, |
85 |
| - add: function (t, i) { |
86 |
| - return (this.x += t || 0), (this.y += i || 0), this |
87 |
| - }, |
88 |
| - subtract: function (t, i) { |
89 |
| - return (this.x -= t || 0), (this.y -= i || 0), this |
90 |
| - }, |
91 |
| - clampX: function (t, i) { |
92 |
| - return (this.x = Math.max(t, Math.min(this.x, i))), this |
93 |
| - }, |
94 |
| - clampY: function (t, i) { |
95 |
| - return (this.y = Math.max(t, Math.min(this.y, i))), this |
96 |
| - }, |
97 |
| - flipX: function () { |
98 |
| - return (this.x *= -1), this |
99 |
| - }, |
100 |
| - flipY: function () { |
101 |
| - return (this.y *= -1), this |
102 |
| - } |
103 |
| - } |
104 |
| - const h = function (t) { |
105 |
| - ;(this._canvas = null), |
106 |
| - (this._context = null), |
107 |
| - (this._sto = null), |
108 |
| - (this._width = 0), |
109 |
| - (this._height = 0), |
110 |
| - (this._scroll = 0), |
111 |
| - (this._ribbons = []), |
112 |
| - (this._options = { |
113 |
| - colorSaturation: '80%', |
114 |
| - colorBrightness: '60%', |
115 |
| - colorAlpha: 0.65, |
116 |
| - colorCycleSpeed: 6, |
117 |
| - verticalPosition: 'center', |
118 |
| - horizontalSpeed: 150, |
119 |
| - ribbonCount: 5, |
120 |
| - strokeSize: 5, |
121 |
| - parallaxAmount: -0.5, |
122 |
| - animateSections: !0 |
123 |
| - }), |
124 |
| - (this._onDraw = this._onDraw.bind(this)), |
125 |
| - (this._onResize = this._onResize.bind(this)), |
126 |
| - (this._onScroll = this._onScroll.bind(this)), |
127 |
| - this.setOptions(t), |
128 |
| - this.init() |
129 |
| - } |
130 |
| - return ( |
131 |
| - (h.prototype = { |
132 |
| - constructor: h, |
133 |
| - setOptions: function (t) { |
134 |
| - if ('object' == typeof t) |
135 |
| - for (const i in t) |
136 |
| - t.hasOwnProperty(i) && (this._options[i] = t[i]) |
137 |
| - }, |
138 |
| - init: function () { |
139 |
| - try { |
140 |
| - ;(this._canvas = document.createElement('canvas')), |
141 |
| - (this._canvas.id = id), |
142 |
| - (this._canvas.style.display = 'block'), |
143 |
| - (this._canvas.style.position = 'fixed'), |
144 |
| - (this._canvas.style.margin = '0'), |
145 |
| - (this._canvas.style.padding = '0'), |
146 |
| - (this._canvas.style.border = '0'), |
147 |
| - (this._canvas.style.outline = '0'), |
148 |
| - (this._canvas.style.left = '0'), |
149 |
| - (this._canvas.style.top = '0'), |
150 |
| - (this._canvas.style.width = '100%'), |
151 |
| - (this._canvas.style.height = '100%'), |
152 |
| - (this._canvas.style['z-index'] = '0'), |
153 |
| - (this._canvas.style['pointer-events'] = 'none'), |
154 |
| - this._onResize(), |
155 |
| - (this._context = this._canvas.getContext('2d')), |
156 |
| - this._context.clearRect(0, 0, this._width, this._height), |
157 |
| - (this._context.globalAlpha = this._options.colorAlpha), |
158 |
| - window.addEventListener('resize', this._onResize), |
159 |
| - window.addEventListener('scroll', this._onScroll), |
160 |
| - document.body.appendChild(this._canvas) |
161 |
| - } catch (t) { |
162 |
| - return void console.warn('Canvas Context Error: ' + t.toString()) |
163 |
| - } |
164 |
| - this._onDraw() |
165 |
| - }, |
166 |
| - addRibbon: function () { |
167 |
| - const t = Math.round(o(1, 9)) > 5 ? 'right' : 'left' |
168 |
| - let i = 1e3 |
169 |
| - const n = 200, |
170 |
| - s = 0 - n, |
171 |
| - h = this._width + n |
172 |
| - let a = 0, |
173 |
| - r = 0 |
174 |
| - const l = 'right' === t ? s : h |
175 |
| - let c = Math.round(o(0, this._height)) |
176 |
| - ;/^(top|min)$/i.test(this._options.verticalPosition) |
177 |
| - ? (c = 0 + n) |
178 |
| - : /^(middle|center)$/i.test(this._options.verticalPosition) |
179 |
| - ? (c = this._height / 2) |
180 |
| - : /^(bottom|max)$/i.test(this._options.verticalPosition) && |
181 |
| - (c = this._height - n) |
182 |
| - const p = [], |
183 |
| - _ = new e(l, c), |
184 |
| - d = new e(l, c) |
185 |
| - let u = null, |
186 |
| - b = Math.round(o(0, 360)), |
187 |
| - f = 0 |
188 |
| - for (; !(i <= 0); ) { |
189 |
| - if ( |
190 |
| - (i--, |
191 |
| - (a = Math.round( |
192 |
| - (1 * Math.random() - 0.2) * this._options.horizontalSpeed |
193 |
| - )), |
194 |
| - (r = Math.round( |
195 |
| - (1 * Math.random() - 0.5) * (0.25 * this._height) |
196 |
| - )), |
197 |
| - (u = new e()), |
198 |
| - u.copy(d), |
199 |
| - 'right' === t) |
200 |
| - ) { |
201 |
| - if ((u.add(a, r), d.x >= h)) break |
202 |
| - } else if ('left' === t && (u.subtract(a, r), d.x <= s)) break |
203 |
| - p.push({ |
204 |
| - point1: new e(_.x, _.y), |
205 |
| - point2: new e(d.x, d.y), |
206 |
| - point3: u, |
207 |
| - color: b, |
208 |
| - delay: f, |
209 |
| - dir: t, |
210 |
| - alpha: 0, |
211 |
| - phase: 0 |
212 |
| - }), |
213 |
| - _.copy(d), |
214 |
| - d.copy(u), |
215 |
| - (f += 4), |
216 |
| - (b += this._options.colorCycleSpeed) |
217 |
| - } |
218 |
| - this._ribbons.push(p) |
219 |
| - }, |
220 |
| - _drawRibbonSection: function (t) { |
221 |
| - if (t) { |
222 |
| - if (t.phase >= 1 && t.alpha <= 0) return !0 |
223 |
| - if (t.delay <= 0) { |
224 |
| - if ( |
225 |
| - ((t.phase += 0.02), |
226 |
| - (t.alpha = 1 * Math.sin(t.phase)), |
227 |
| - (t.alpha = t.alpha <= 0 ? 0 : t.alpha), |
228 |
| - (t.alpha = t.alpha >= 1 ? 1 : t.alpha), |
229 |
| - this._options.animateSections) |
230 |
| - ) { |
231 |
| - const i = 0.1 * Math.sin(1 + (t.phase * Math.PI) / 2) |
232 |
| - 'right' === t.dir |
233 |
| - ? (t.point1.add(i, 0), |
234 |
| - t.point2.add(i, 0), |
235 |
| - t.point3.add(i, 0)) |
236 |
| - : (t.point1.subtract(i, 0), |
237 |
| - t.point2.subtract(i, 0), |
238 |
| - t.point3.subtract(i, 0)), |
239 |
| - t.point1.add(0, i), |
240 |
| - t.point2.add(0, i), |
241 |
| - t.point3.add(0, i) |
242 |
| - } |
243 |
| - } else t.delay -= 0.5 |
244 |
| - const i = this._options.colorSaturation, |
245 |
| - n = this._options.colorBrightness, |
246 |
| - o = |
247 |
| - 'hsla(' + |
248 |
| - t.color + |
249 |
| - ', ' + |
250 |
| - i + |
251 |
| - ', ' + |
252 |
| - n + |
253 |
| - ', ' + |
254 |
| - t.alpha + |
255 |
| - ' )' |
256 |
| - this._context.save(), |
257 |
| - 0 !== this._options.parallaxAmount && |
258 |
| - this._context.translate( |
259 |
| - 0, |
260 |
| - this._scroll * this._options.parallaxAmount |
261 |
| - ), |
262 |
| - this._context.beginPath(), |
263 |
| - this._context.moveTo(t.point1.x, t.point1.y), |
264 |
| - this._context.lineTo(t.point2.x, t.point2.y), |
265 |
| - this._context.lineTo(t.point3.x, t.point3.y), |
266 |
| - (this._context.fillStyle = o), |
267 |
| - this._context.fill(), |
268 |
| - this._options.strokeSize > 0 && |
269 |
| - ((this._context.lineWidth = this._options.strokeSize), |
270 |
| - (this._context.strokeStyle = o), |
271 |
| - (this._context.lineCap = 'round'), |
272 |
| - this._context.stroke()), |
273 |
| - this._context.restore() |
274 |
| - } |
275 |
| - return !1 |
276 |
| - }, |
277 |
| - _onDraw: function () { |
278 |
| - for (let t = 0, i = this._ribbons.length; t < i; ++t) |
279 |
| - this._ribbons[t] || this._ribbons.splice(t, 1) |
280 |
| - this._context.clearRect(0, 0, this._width, this._height) |
281 |
| - for (let t = 0; t < this._ribbons.length; ++t) { |
282 |
| - const i = this._ribbons[t], |
283 |
| - n = i.length |
284 |
| - let o = 0 |
285 |
| - for (let t = 0; t < n; ++t) this._drawRibbonSection(i[t]) && o++ |
286 |
| - o >= n && (this._ribbons[t] = null) |
287 |
| - } |
288 |
| - this._ribbons.length < this._options.ribbonCount && |
289 |
| - this.addRibbon(), |
290 |
| - requestAnimationFrame(this._onDraw) |
291 |
| - }, |
292 |
| - _onResize: function (t) { |
293 |
| - const i = s(t) |
294 |
| - ;(this._width = i.width), |
295 |
| - (this._height = i.height), |
296 |
| - this._canvas && |
297 |
| - ((this._canvas.width = this._width), |
298 |
| - (this._canvas.height = this._height), |
299 |
| - this._context && |
300 |
| - (this._context.globalAlpha = this._options.colorAlpha)) |
301 |
| - }, |
302 |
| - _onScroll: function (t) { |
303 |
| - const i = s(t) |
304 |
| - this._scroll = i.scrolly |
305 |
| - } |
306 |
| - }), |
307 |
| - h |
308 |
| - ) |
309 |
| - })()) |
310 |
| - new Ribbons({ |
311 |
| - colorSaturation: '60%', |
312 |
| - colorBrightness: '50%', |
313 |
| - colorAlpha: 0.5, |
314 |
| - colorCycleSpeed: 5, |
315 |
| - verticalPosition: 'random', |
316 |
| - horizontalSpeed: 200, |
317 |
| - ribbonCount: 3, |
318 |
| - strokeSize: 0, |
319 |
| - parallaxAmount: -0.2, |
320 |
| - animateSections: !0 |
321 |
| - }) |
322 |
| -} |
0 commit comments