File size: 11,110 Bytes
1b72d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* eslint-disable */
import { useEffect } from 'react'
const  id = 'canvasFlutteringRibbon'
export const FlutteringRibbon = () => {
     const destroyRibbon = ()=>{
    const ribbon = document.getElementById(id)
      if(ribbon && ribbon.parentNode){
        ribbon.parentNode.removeChild(ribbon)
      }
  }
  
  useEffect(() => {
    createFlutteringRibbon()
        return () => destroyRibbon()

  }, [])
    return <></>
}

export default FlutteringRibbon


/**
 * 创建连接点
 * @param config
 */
function createFlutteringRibbon() {
  'object' == typeof window &&
    (window.Ribbons = (function () {
      const t = window,
        i = document.body,
        n = document.documentElement
      var o = function () {
        if (1 === arguments.length) {
          if (Array.isArray(arguments[0])) {
            const t = Math.round(o(0, arguments[0].length - 1))
            return arguments[0][t]
          }
          return o(0, arguments[0])
        }
        return 2 === arguments.length
          ? Math.random() * (arguments[1] - arguments[0]) + arguments[0]
          : 0
      }
      const s = function (o) {
          const s = Math.max(
              0,
              t.innerWidth || n.clientWidth || i.clientWidth || 0
            ),
            e = Math.max(
              0,
              t.innerHeight || n.clientHeight || i.clientHeight || 0
            )
          return {
            width: s,
            height: e,
            ratio: s / e,
            centerx: s / 2,
            centery: e / 2,
            scrollx:
              Math.max(0, t.pageXOffset || n.scrollLeft || i.scrollLeft || 0) -
              (n.clientLeft || 0),
            scrolly:
              Math.max(0, t.pageYOffset || n.scrollTop || i.scrollTop || 0) -
              (n.clientTop || 0)
          }
        },
        e = function (t, i) {
          ;(this.x = 0), (this.y = 0), this.set(t, i)
        }
      e.prototype = {
        constructor: e,
        set: function (t, i) {
          ;(this.x = t || 0), (this.y = i || 0)
        },
        copy: function (t) {
          return (this.x = t.x || 0), (this.y = t.y || 0), this
        },
        multiply: function (t, i) {
          return (this.x *= t || 1), (this.y *= i || 1), this
        },
        divide: function (t, i) {
          return (this.x /= t || 1), (this.y /= i || 1), this
        },
        add: function (t, i) {
          return (this.x += t || 0), (this.y += i || 0), this
        },
        subtract: function (t, i) {
          return (this.x -= t || 0), (this.y -= i || 0), this
        },
        clampX: function (t, i) {
          return (this.x = Math.max(t, Math.min(this.x, i))), this
        },
        clampY: function (t, i) {
          return (this.y = Math.max(t, Math.min(this.y, i))), this
        },
        flipX: function () {
          return (this.x *= -1), this
        },
        flipY: function () {
          return (this.y *= -1), this
        }
      }
      const h = function (t) {
        ;(this._canvas = null),
          (this._context = null),
          (this._sto = null),
          (this._width = 0),
          (this._height = 0),
          (this._scroll = 0),
          (this._ribbons = []),
          (this._options = {
            colorSaturation: '80%',
            colorBrightness: '60%',
            colorAlpha: 0.65,
            colorCycleSpeed: 6,
            verticalPosition: 'center',
            horizontalSpeed: 150,
            ribbonCount: 5,
            strokeSize: 5,
            parallaxAmount: -0.5,
            animateSections: !0
          }),
          (this._onDraw = this._onDraw.bind(this)),
          (this._onResize = this._onResize.bind(this)),
          (this._onScroll = this._onScroll.bind(this)),
          this.setOptions(t),
          this.init()
      }
      return (
        (h.prototype = {
          constructor: h,
          setOptions: function (t) {
            if ('object' == typeof t)
              for (const i in t)
                t.hasOwnProperty(i) && (this._options[i] = t[i])
          },
          init: function () {
            try {
              ;(this._canvas = document.createElement('canvas')),
                (this._canvas.id = id),
                (this._canvas.style.display = 'block'),
                (this._canvas.style.position = 'fixed'),
                (this._canvas.style.margin = '0'),
                (this._canvas.style.padding = '0'),
                (this._canvas.style.border = '0'),
                (this._canvas.style.outline = '0'),
                (this._canvas.style.left = '0'),
                (this._canvas.style.top = '0'),
                (this._canvas.style.width = '100%'),
                (this._canvas.style.height = '100%'),
                (this._canvas.style['z-index'] = '0'),
                (this._canvas.style['pointer-events'] = 'none'),
                this._onResize(),
                (this._context = this._canvas.getContext('2d')),
                this._context.clearRect(0, 0, this._width, this._height),
                (this._context.globalAlpha = this._options.colorAlpha),
                window.addEventListener('resize', this._onResize),
                window.addEventListener('scroll', this._onScroll),
                document.body.appendChild(this._canvas)
            } catch (t) {
              return void console.warn('Canvas Context Error: ' + t.toString())
            }
            this._onDraw()
          },
          addRibbon: function () {
            const t = Math.round(o(1, 9)) > 5 ? 'right' : 'left'
            let i = 1e3
            const n = 200,
              s = 0 - n,
              h = this._width + n
            let a = 0,
              r = 0
            const l = 'right' === t ? s : h
            let c = Math.round(o(0, this._height))
            ;/^(top|min)$/i.test(this._options.verticalPosition)
              ? (c = 0 + n)
              : /^(middle|center)$/i.test(this._options.verticalPosition)
              ? (c = this._height / 2)
              : /^(bottom|max)$/i.test(this._options.verticalPosition) &&
                (c = this._height - n)
            const p = [],
              _ = new e(l, c),
              d = new e(l, c)
            let u = null,
              b = Math.round(o(0, 360)),
              f = 0
            for (; !(i <= 0); ) {
              if (
                (i--,
                (a = Math.round(
                  (1 * Math.random() - 0.2) * this._options.horizontalSpeed
                )),
                (r = Math.round(
                  (1 * Math.random() - 0.5) * (0.25 * this._height)
                )),
                (u = new e()),
                u.copy(d),
                'right' === t)
              ) {
                if ((u.add(a, r), d.x >= h)) break
              } else if ('left' === t && (u.subtract(a, r), d.x <= s)) break
              p.push({
                point1: new e(_.x, _.y),
                point2: new e(d.x, d.y),
                point3: u,
                color: b,
                delay: f,
                dir: t,
                alpha: 0,
                phase: 0
              }),
                _.copy(d),
                d.copy(u),
                (f += 4),
                (b += this._options.colorCycleSpeed)
            }
            this._ribbons.push(p)
          },
          _drawRibbonSection: function (t) {
            if (t) {
              if (t.phase >= 1 && t.alpha <= 0) return !0
              if (t.delay <= 0) {
                if (
                  ((t.phase += 0.02),
                  (t.alpha = 1 * Math.sin(t.phase)),
                  (t.alpha = t.alpha <= 0 ? 0 : t.alpha),
                  (t.alpha = t.alpha >= 1 ? 1 : t.alpha),
                  this._options.animateSections)
                ) {
                  const i = 0.1 * Math.sin(1 + (t.phase * Math.PI) / 2)
                  'right' === t.dir
                    ? (t.point1.add(i, 0),
                      t.point2.add(i, 0),
                      t.point3.add(i, 0))
                    : (t.point1.subtract(i, 0),
                      t.point2.subtract(i, 0),
                      t.point3.subtract(i, 0)),
                    t.point1.add(0, i),
                    t.point2.add(0, i),
                    t.point3.add(0, i)
                }
              } else t.delay -= 0.5
              const i = this._options.colorSaturation,
                n = this._options.colorBrightness,
                o =
                  'hsla(' +
                  t.color +
                  ', ' +
                  i +
                  ', ' +
                  n +
                  ', ' +
                  t.alpha +
                  ' )'
              this._context.save(),
                0 !== this._options.parallaxAmount &&
                  this._context.translate(
                    0,
                    this._scroll * this._options.parallaxAmount
                  ),
                this._context.beginPath(),
                this._context.moveTo(t.point1.x, t.point1.y),
                this._context.lineTo(t.point2.x, t.point2.y),
                this._context.lineTo(t.point3.x, t.point3.y),
                (this._context.fillStyle = o),
                this._context.fill(),
                this._options.strokeSize > 0 &&
                  ((this._context.lineWidth = this._options.strokeSize),
                  (this._context.strokeStyle = o),
                  (this._context.lineCap = 'round'),
                  this._context.stroke()),
                this._context.restore()
            }
            return !1
          },
          _onDraw: function () {
            for (let t = 0, i = this._ribbons.length; t < i; ++t)
              this._ribbons[t] || this._ribbons.splice(t, 1)
            this._context.clearRect(0, 0, this._width, this._height)
            for (let t = 0; t < this._ribbons.length; ++t) {
              const i = this._ribbons[t],
                n = i.length
              let o = 0
              for (let t = 0; t < n; ++t) this._drawRibbonSection(i[t]) && o++
              o >= n && (this._ribbons[t] = null)
            }
            this._ribbons.length < this._options.ribbonCount &&
              this.addRibbon(),
              requestAnimationFrame(this._onDraw)
          },
          _onResize: function (t) {
            const i = s(t)
            ;(this._width = i.width),
              (this._height = i.height),
              this._canvas &&
                ((this._canvas.width = this._width),
                (this._canvas.height = this._height),
                this._context &&
                  (this._context.globalAlpha = this._options.colorAlpha))
          },
          _onScroll: function (t) {
            const i = s(t)
            this._scroll = i.scrolly
          }
        }),
        h
      )
    })())
  new Ribbons({
    colorSaturation: '60%',
    colorBrightness: '50%',
    colorAlpha: 0.5,
    colorCycleSpeed: 5,
    verticalPosition: 'random',
    horizontalSpeed: 200,
    ribbonCount: 3,
    strokeSize: 0,
    parallaxAmount: -0.2,
    animateSections: !0
  })
}