-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathribbons.js
More file actions
550 lines (467 loc) · 18.4 KB
/
ribbons.js
File metadata and controls
550 lines (467 loc) · 18.4 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*global document, window, console, Ribbons*/
/**
* Ribbons Class File.
* Creates low-poly ribbons background effect inside a target container.
*/
(function (name, factory) {
"use strict";
if (typeof window === "object") {
window[name] = factory();
}
})("Ribbons", function () {
"use strict";
var _w = window,
_b = document.body,
_d = document.documentElement;
// random helper
var random = function () {
if (arguments.length === 1) { // only 1 argument
if (Array.isArray(arguments[0])) { // extract index from array
var index = Math.round(random(0, arguments[0].length - 1));
return arguments[0][index];
}
return random(0, arguments[0]); // assume numeric
} else if (arguments.length === 2) { // two arguments range
return Math.random() * (arguments[1] - arguments[0]) + arguments[0];
}
return 0; // default
};
// screen helper
var screenInfo = function (e) {
var width = Math.max(0, _w.innerWidth || _d.clientWidth || _b.clientWidth || 0),
height = Math.max(0, _w.innerHeight || _d.clientHeight || _b.clientHeight || 0),
scrollx = Math.max(0, _w.pageXOffset || _d.scrollLeft || _b.scrollLeft || 0) - (_d.clientLeft || 0),
scrolly = Math.max(0, _w.pageYOffset || _d.scrollTop || _b.scrollTop || 0) - (_d.clientTop || 0);
return {
width: width,
height: height,
ratio: width / height,
centerx: width / 2,
centery: height / 2,
scrollx: scrollx,
scrolly: scrolly
};
};
// mouse/input helper
var mouseInfo = function (e) {
var screen = screenInfo(e),
mousex = e ? Math.max(0, e.pageX || e.clientX || 0) : 0,
mousey = e ? Math.max(0, e.pageY || e.clientY || 0) : 0;
return {
mousex: mousex,
mousey: mousey,
centerx: mousex - (screen.width / 2),
centery: mousey - (screen.height / 2)
};
};
// point object
var Point = function (x, y) {
this.x = 0;
this.y = 0;
this.set(x, y);
};
Point.prototype = {
constructor: Point,
set: function (x, y) {
this.x = (x || 0);
this.y = (y || 0);
},
copy: function (point) {
this.x = (point.x || 0);
this.y = (point.y || 0);
return this;
},
multiply: function (x, y) {
this.x *= (x || 1);
this.y *= (y || 1);
return this;
},
divide: function (x, y) {
this.x /= (x || 1);
this.y /= (y || 1);
return this;
},
add: function (x, y) {
this.x += (x || 0);
this.y += (y || 0);
return this;
},
subtract: function (x, y) {
this.x -= (x || 0);
this.y -= (y || 0);
return this;
},
clampX: function (min, max) {
this.x = Math.max(min, Math.min(this.x, max));
return this;
},
clampY: function (min, max) {
this.y = Math.max(min, Math.min(this.y, max));
return this;
},
flipX: function () {
this.x *= -1;
return this;
},
flipY: function () {
this.y *= -1;
return this;
}
};
// class constructor
var Factory = function (options) {
this._canvas = null;
this._context = null;
this._sto = null;
this._width = 0;
this._height = 0;
this._scroll = 0;
this._ribbons = [];
this._saved_color_index = 0;
this._options = {
// ribbon color HSL saturation amount
colorSaturation: "80%",
// ribbon color HSL brightness amount
colorBrightness: "60%",
// ribbon color opacity amount
colorAlpha: 0.65,
// how fast to cycle through colors in the HSL color space
colorCycleSpeed: 6,
// where to start from on the Y axis on each side (top|min, middle|center, bottom|max, random)
verticalPosition: "center",
// how fast to get to the other side of the screen
horizontalSpeed: 150,
delay: 4, //set delay
phase: 0.014, //set phase
// how many ribbons to keep on screen at any given time
ribbonCount: 3,
// add stroke along with ribbon fill colors
strokeSize: 0,
// move ribbons vertically by a factor on page scroll
parallaxAmount: -0.5,
// add animation effect to each ribbon section over time
animateSections: true,
// add blur shadow effect to each ribbon with radius
ribbonBlur: 10,
// single color default is off (random ribbons colors)
singleColor: false
};
this._onDraw = this._onDraw.bind(this);
this._onResize = this._onResize.bind(this);
this._onScroll = this._onScroll.bind(this);
this.setOptions(options);
this.init();
};
// class prototype
Factory.prototype = {
constructor: Factory,
// Set and merge local options
setOptions: function (options) {
var key;
if (typeof options === "object") {
for (key in options) {
if (options.hasOwnProperty(key)) {
this._options[key] = options[key];
}
}
}
},
// Initialize the ribbons effect
init: function () {
try {
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);
} catch (e) {
console.warn("Canvas Context Error: " + e.toString());
return;
}
this._onDraw();
},
// Create a new random ribbon and to the list
addRibbon: function() {
// tricolor
if (this._options.singleColor === 667 ) {
this._options.ribbonCount = 3;
}
// movement data
var dir = (Math.round(random(1, 9)) > 5) ? "right" : "left",
stop = 1000,
hide = 400,
min = 0 - hide,
max = this._width + hide,
movex = 0,
movey = 0,
startx = (dir === "right") ? min : max,
starty = Math.round(random(0, this._height));
// asjust starty based on options
if (/^(top|min)$/i.test(this._options.verticalPosition)) {
starty = 0 + hide;
} else if (/^(middle|center)$/i.test(this._options.verticalPosition)) {
starty = (this._height / 2);
} else if (/^(bottom|max)$/i.test(this._options.verticalPosition)) {
starty = this._height - hide;
}
//console.log(this._options.singleColor)
// ribbon sections data
// tricolor
if (this._options.singleColor === 667 ) {
var hues = [667, 240, 360];
if (this._saved_color_index >= hues.length) {
this._saved_color_index = 0;
}
var color = hues[this._saved_color_index]
} else if (this._options.singleColor) {
var color = this._options.singleColor
} else {
var color = Math.round(random(0, 360));
}
var ribbon = [],
point1 = new Point(startx, starty),
point2 = new Point(startx, starty),
point3 = null,
//color = this._options.singleColor ? this._options.singleColor : Math.round(random(0, 360)),
//color = Math.round(random(0, 360)),
// randomize delay between each ribbon appearance
delay = Math.random() * 4;
// buils ribbon sections
while (true) {
if (stop <= 0) break;
stop--;
movex = Math.round((Math.random() * 1 - 0.2) * this._options.horizontalSpeed);
movey = Math.round((Math.random() * 1 - 0.5) * (this._height * 0.35));
point3 = new Point();
point3.copy(point2);
if (dir === "right") {
point3.add(movex, movey);
if (point2.x >= max) break;
} else if (dir === "left") {
point3.subtract(movex, movey);
if (point2.x <= min) break;
}
// point3.clampY( 0, this._height );
ribbon.push({ // single ribbon section
point1: new Point(point1.x, point1.y),
point2: new Point(point2.x, point2.y),
point3: point3,
color: color,
delay: delay,
dir: dir,
alpha: 0,
phase: 0,
});
point1.copy(point2);
point2.copy(point3);
// affects ribbon appear speed
//delay += 4;
//delay += 0.2;
delay += this._options.delay;
color += this._options.colorCycleSpeed;
}
// tricolor
if (this._options.singleColor === 667 ) {
this._saved_color_index++;
}
this._ribbons.push(ribbon);
},
_drawRibbonSection: function (section) {
if (section) {
if (section.phase >= 1 && section.alpha <= 0) {
return true; // done
}
if (section.delay <= 0) {
// section.phase affects animation speed
//section.phase += 0.014;
section.phase += this._options.phase;
section.alpha = Math.sin(section.phase) * 1;
section.alpha = (section.alpha <= 0) ? 0 : section.alpha;
section.alpha = (section.alpha >= 1) ? 1 : section.alpha;
if (this._options.animateSections) {
var mod = (Math.sin(1 + section.phase * Math.PI / 2) * 0.1);
if (section.dir === "right") {
section.point1.add(mod, 0);
section.point2.add(mod, 0);
section.point3.add(mod, 0);
} else {
section.point1.subtract(mod, 0);
section.point2.subtract(mod, 0);
section.point3.subtract(mod, 0);
}
section.point1.add(0, mod);
section.point2.add(0, mod);
section.point3.add(0, mod);
}
} else {
// affects ribbon dissapear speed
section.delay -= 0.5;
}
if (section.color === 667) {
var s = "0%";
var l = "100%"
} else {
var s = this._options.colorSaturation;
var l = this._options.colorBrightness;
}
var //s = this._options.colorSaturation,
//l = this._options.colorBrightness,
c = "hsla(" + section.color + ", " + s + ", " + l + ", " + section.alpha + " )";
this._context.save();
if (this._options.parallaxAmount !== 0) {
this._context.translate(0, this._scroll * this._options.parallaxAmount);
}
this._context.beginPath();
this._context.moveTo(section.point1.x, section.point1.y);
this._context.lineTo(section.point2.x, section.point2.y);
this._context.lineTo(section.point3.x, section.point3.y);
// set blur
this._context.shadowBlur = this._options.ribbonBlur;
this._context.shadowColor = c;
this._context.fillStyle = c;
this._context.fill();
if (this._options.strokeSize > 0) {
this._context.lineWidth = this._options.strokeSize;
this._context.strokeStyle = c;
this._context.lineCap = "round";
this._context.stroke();
}
this._context.restore();
}
return false; // not done yet
},
// Draw ribbons
_onDraw: function () {
for (var i = 0; i < this._ribbons.length; ++i) {
if (!this._ribbons[i]) {
this._ribbons.splice(i, 1);
i--;
}
}
this._context.clearRect(0, 0, this._width, this._height);
for (var a = 0; a < this._ribbons.length; ++a) {
var ribbon = this._ribbons[a];
if (!ribbon || !Array.isArray(ribbon) || ribbon.length === 0) {
this._ribbons[a] = null;
continue;
}
var numSections = ribbon.length;
var numDone = 0;
for (var b = 0; b < numSections; ++b) {
if (this._drawRibbonSection(ribbon[b])) {
numDone++;
}
}
if (numDone >= numSections) {
this._ribbons[a] = null;
}
}
while (this._ribbons.length < this._options.ribbonCount) {
this.addRibbon();
}
AnimationId = requestAnimationFrame(this._onDraw.bind(this));
},
// Update container size info
_onResize: function(e) {
var screen = screenInfo(e);
this._width = screen.width;
this._height = screen.height;
if (this._canvas) {
this._canvas.width = this._width;
this._canvas.height = this._height;
if (this._context) {
this._context.globalAlpha = this._options.colorAlpha;
}
}
},
// Update container size info
_onScroll: function(e) {
var screen = screenInfo(e);
this._scroll = screen.scrolly;
},
};
// export
return Factory;
});
// electron related stuff
// the screensaver glue: making sure we quit when needed
const { ipcRenderer } = require('electron')
let Ribbons_instance = null;
var colorCycleSpeed = null;
var colorSaturation = null;
var options = [];
var AnimationId = null;
function start_Ribbons(colorSaturation, colorCycleSpeed, options) {
// ribbon appearance section
Ribbons_instance = new Ribbons({
colorSaturation: colorSaturation, // set saturation
colorBrightness: "50%", // set brightness
colorAlpha: 1, // set semitransparency
colorCycleSpeed: colorCycleSpeed, // set color cycle speed
verticalPosition: "random", // make vertical position random
horizontalSpeed: options["horizontalSpeed"], // set speed
delay: options["delay"], //set delay
phase: options["phase"], //set phase
ribbonCount: options["ribbonCount"], // max onscreen visible ribbons
strokeSize: 1, // add stroke
parallaxAmount: -0.3, // lightn parallax
animateSections: true, // section animation
ribbonBlur: 30, // set ribbon blur intensity
singleColor: options["singleColor"] // set ribbon single color in HSL(Hue, Saturation, Lightness) model
});
}
ipcRenderer.on('send-stop', (event, flag) => {
// stop animation and clear canvas and context from memory
Ribbons_instance._context.clearRect(0, 0, Ribbons_instance._canvas.width, Ribbons_instance._canvas.height);
cancelAnimationFrame(AnimationId);
Ribbons_instance._canvas.remove();
Ribbons_instance = null;
//Ribbons_instance.destroy();
});
ipcRenderer.on('send-options', (event, got_options) => {
options = got_options;
//console.log(options["horizontalSpeed"])
document.body.classList.add(options["theme"]);
var colorCycleSpeed = options["colorCycleSpeed"];
var colorSaturation = "60%";
if ((options["singleColor"]) || (options["singleColor"] == 666) || (options["singleColor"] == 667)) {
colorCycleSpeed = 0;
}
if (options["singleColor"] == 666) {
colorSaturation = "0%";
}
if (options["horizontalSpeed"] == "v_fast") {
options["delay"] = 1.5;
options["phase"] = 0.07;
}
else if (options["horizontalSpeed"] == "fast") {
options["delay"] = 2;
options["phase"] = 0.04;
}
else if (options["horizontalSpeed"] == "normal") {
options["delay"] = 4;
options["phase"] = 0.014;
}
else if (options["horizontalSpeed"] == "slow") {
options["delay"] = 6;
options["phase"] = 0.007;
}
options["horizontalSpeed"] = 400;
start_Ribbons(colorSaturation, colorCycleSpeed, options);
});
// force hide mouse cursor
document.body.style.cursor = "none";