-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglitch.js
51 lines (44 loc) · 1.51 KB
/
glitch.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
$(document).ready(function() {
var MAXHEIGHT = 7;
var MINTIME = 25;
var TIMEVARIANCE = 75;
function generateRandom(n) {
return Math.floor(Math.random() * n);
}
function generateClip(height, variance) {
var clipHeight = generateRandom(variance);
var clipTop = generateRandom(height - clipHeight);
var clipBottom = clipTop + clipHeight;
return 'rect(' + clipTop + 'px, 100vw, ' + clipBottom + 'px, 0vw)';
}
function generateShadow(colour) {
var offset = generateRandom(5) - 2;
return offset + 'px 0 ' + colour;
}
function spawnAnimation($element, colour) {
var frame = function() {
$element.css('clip', generateClip($element.height(), MAXHEIGHT))
.css('left', (generateRandom(5) - 2) + 'px')
.css('text-shadow', generateShadow(colour));
clearInterval($element.data('interval'));
$element.data('interval', setInterval(frame, generateRandom(TIMEVARIANCE) + MINTIME));
}
$element.data('interval', setInterval(frame, generateRandom(TIMEVARIANCE) + MINTIME));
return frame;
}
$.fn.glitch = function (colours) {
var $duplicates = [];
for (var i in colours) {
var $glitch = this.clone();
$glitch.removeClass('glitch');
$glitch.addClass('glitchDuplicate')
$duplicates.push($glitch);
}
for (i in $duplicates) {
this.append($duplicates[i]);
spawnAnimation($duplicates[i], colours[i])
}
return this;
};
$('.glitch').glitch(['red', 'aqua', 'lime', 'fuchsia', 'yellow']);
});