Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
17mie32
GitHub Repository: 17mie32/17mie32.github.io
Path: blob/main/libs/background/ribbon-dynamic.js
1317 views
1
"object" == typeof window && (window.Ribbons = function () {
2
var t = window,
3
i = document.body,
4
n = document.documentElement,
5
o = function () {
6
if (1 === arguments.length) {
7
if (Array.isArray(arguments[0])) {
8
var t = Math.round(o(0, arguments[0].length - 1));
9
return arguments[0][t]
10
}
11
return o(0, arguments[0])
12
}
13
return 2 === arguments.length ? Math.random() * (arguments[1] - arguments[0]) + arguments[0] : 0
14
},
15
s = function (o) {
16
var s = Math.max(0, t.innerWidth || n.clientWidth || i.clientWidth || 0),
17
h = Math.max(0, t.innerHeight || n.clientHeight || i.clientHeight || 0),
18
e = Math.max(0, t.pageXOffset || n.scrollLeft || i.scrollLeft || 0) - (n.clientLeft || 0),
19
a = Math.max(0, t.pageYOffset || n.scrollTop || i.scrollTop || 0) - (n.clientTop || 0);
20
return {
21
width: s,
22
height: h,
23
ratio: s / h,
24
centerx: s / 2,
25
centery: h / 2,
26
scrollx: e,
27
scrolly: a
28
}
29
},
30
h = function (t, i) {
31
this.x = 0, this.y = 0, this.set(t, i)
32
};
33
h.prototype = {
34
constructor: h,
35
set: function (t, i) {
36
this.x = t || 0, this.y = i || 0
37
},
38
copy: function (t) {
39
return this.x = t.x || 0, this.y = t.y || 0, this
40
},
41
multiply: function (t, i) {
42
return this.x *= t || 1, this.y *= i || 1, this
43
},
44
divide: function (t, i) {
45
return this.x /= t || 1, this.y /= i || 1, this
46
},
47
add: function (t, i) {
48
return this.x += t || 0, this.y += i || 0, this
49
},
50
subtract: function (t, i) {
51
return this.x -= t || 0, this.y -= i || 0, this
52
},
53
clampX: function (t, i) {
54
return this.x = Math.max(t, Math.min(this.x, i)), this
55
},
56
clampY: function (t, i) {
57
return this.y = Math.max(t, Math.min(this.y, i)), this
58
},
59
flipX: function () {
60
return this.x *= -1, this
61
},
62
flipY: function () {
63
return this.y *= -1, this
64
}
65
};
66
var e = function (t) {
67
this._canvas = null, this._context = null, this._sto = null, this._width = 0, this._height = 0, this._scroll = 0, this._ribbons = [], this._options = {
68
colorSaturation: "80%",
69
colorBrightness: "60%",
70
colorAlpha: .65,
71
colorCycleSpeed: 6,
72
verticalPosition: "center",
73
horizontalSpeed: 150,
74
ribbonCount: 5,
75
strokeSize: 5,
76
parallaxAmount: -.5,
77
animateSections: !0
78
}, this._onDraw = this._onDraw.bind(this), this._onResize = this._onResize.bind(this), this._onScroll = this._onScroll.bind(this), this.setOptions(t), this.init()
79
};
80
return e.prototype = {
81
constructor: e,
82
setOptions: function (t) {
83
if ("object" == typeof t)
84
for (var i in t) t.hasOwnProperty(i) && (this._options[i] = t[i])
85
},
86
init: function () {
87
try {
88
this._canvas = document.createElement("canvas"), 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"] = "-1", 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)
89
} catch (t) {
90
return void console.warn("Canvas Context Error: " + t.toString())
91
}
92
this._onDraw()
93
},
94
addRibbon: function () {
95
var t = Math.round(o(1, 9)) > 5 ? "right" : "left",
96
i = 1e3,
97
n = this._width + 200,
98
s = 0,
99
e = 0,
100
a = "right" === t ? -200 : n,
101
r = Math.round(o(0, this._height));
102
/^(top|min)$/i.test(this._options.verticalPosition) ? r = 200 : /^(middle|center)$/i.test(this._options.verticalPosition) ? r = this._height / 2 : /^(bottom|max)$/i.test(this._options.verticalPosition) && (r = this._height - 200);
103
for (var l = [], c = new h(a, r), p = new h(a, r), _ = null, d = Math.round(o(0, 360)), u = 0; !(i <= 0);) {
104
if (i--, s = Math.round((1 * Math.random() - .2) * this._options.horizontalSpeed), e = Math.round((1 * Math.random() - .5) * (.25 * this._height)), (_ = new h).copy(p), "right" === t) {
105
if (_.add(s, e), p.x >= n) break
106
} else if ("left" === t && (_.subtract(s, e), p.x <= -200)) break;
107
l.push({
108
point1: new h(c.x, c.y),
109
point2: new h(p.x, p.y),
110
point3: _,
111
color: d,
112
delay: u,
113
dir: t,
114
alpha: 0,
115
phase: 0
116
}), c.copy(p), p.copy(_), u += 4, d += this._options.colorCycleSpeed
117
}
118
this._ribbons.push(l)
119
},
120
_drawRibbonSection: function (t) {
121
if (t) {
122
if (t.phase >= 1 && t.alpha <= 0) return !0;
123
if (t.delay <= 0) {
124
if (t.phase += .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) {
125
var i = .1 * Math.sin(1 + t.phase * Math.PI / 2);
126
"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)
127
}
128
} else t.delay -= .5;
129
var n = this._options.colorSaturation,
130
o = this._options.colorBrightness,
131
s = "hsla(" + t.color + ", " + n + ", " + o + ", " + t.alpha + " )";
132
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 = s, this._context.fill(), this._options.strokeSize > 0 && (this._context.lineWidth = this._options.strokeSize, this._context.strokeStyle = s, this._context.lineCap = "round", this._context.stroke()), this._context.restore()
133
}
134
return !1
135
},
136
_onDraw: function () {
137
for (var t = 0, i = this._ribbons.length; t < i; ++t) this._ribbons[t] || this._ribbons.splice(t, 1);
138
this._context.clearRect(0, 0, this._width, this._height);
139
for (var n = 0; n < this._ribbons.length; ++n) {
140
for (var o = this._ribbons[n], s = o.length, h = 0, e = 0; e < s; ++e) this._drawRibbonSection(o[e]) && h++;
141
h >= s && (this._ribbons[n] = null)
142
}
143
this._ribbons.length < this._options.ribbonCount && this.addRibbon(), requestAnimationFrame(this._onDraw)
144
},
145
_onResize: function (t) {
146
var i = s();
147
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))
148
},
149
_onScroll: function (t) {
150
var i = s();
151
this._scroll = i.scrolly
152
}
153
}, e
154
}()), new Ribbons({
155
colorSaturation: "60%",
156
colorBrightness: "50%",
157
colorAlpha: .5,
158
colorCycleSpeed: 5,
159
verticalPosition: "random",
160
horizontalSpeed: 200,
161
ribbonCount: 3,
162
strokeSize: 0,
163
parallaxAmount: -.2,
164
animateSections: !0
165
});
166