forked from moovweb/harlem_shaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harlem-shake-script.js
177 lines (145 loc) · 4.46 KB
/
harlem-shake-script.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
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
(function() {
var MIN_HEIGHT = 30;
var MIN_WIDTH = 30;
var MAX_HEIGHT = 350;
var MAX_WIDTH = 350;
var PATH_TO_SONG = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
var CSS_BASE_CLASS = "mw-harlem_shake_me"
var CSS_FIRST_CLASS = "im_first";
var CSS_OTHER_CLASSES = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
var CSS_STROBE_CLASS = "mw-strobe_light";
var PATH_TO_CSS = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
var FILE_ADDED_CLASS = "mw_added_css"
function addCSS() {
var css = document.createElement("link");
css.setAttribute("type", "text/css");
css.setAttribute("rel", "stylesheet");
css.setAttribute("href", PATH_TO_CSS);
css.setAttribute("class", FILE_ADDED_CLASS);
document.body.appendChild(css);
}
function removeAddedFiles() {
var addedFiles = document.getElementsByClassName(FILE_ADDED_CLASS);
for (var i=0; i<addedFiles.length; i++) {
document.body.removeChild(addedFiles[i]);
}
}
function flashScreen() {
var flash = document.createElement("div");
flash.setAttribute("class", CSS_STROBE_CLASS);
document.body.appendChild(flash);
setTimeout(function() {
document.body.removeChild(flash);
}, 100);
}
function size(node) {
return {
height: node.offsetHeight,
width: node.offsetWidth
}
}
function withinBounds(node) {
var nodeFrame = size(node);
return (nodeFrame.height > MIN_HEIGHT &&
nodeFrame.height < MAX_HEIGHT &&
nodeFrame.width > MIN_WIDTH &&
nodeFrame.width < MAX_WIDTH);
}
function posY(elm) {
var test = elm;
var top = 0;
while (!!test) {
top += test.offsetTop;
test = test.offsetParent;
}
return top;
}
function viewPortHeight() {
var de = document.documentElement;
if (!!window.innerWidth) {
return window.innerHeight;
} else if (de && !isNaN(de.clientHeight)) {
return de.clientHeight;
}
return 0;
}
function scrollY() {
if (window.pageYOffset) {
return window.pageYOffset;
}
return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
}
var vpH = viewPortHeight();
var st = scrollY();
function isVisible(node) {
var y = posY(node);
return (y >= st && y <= (vpH + st));
}
function playSong() {
var audioTag = document.createElement("audio");
audioTag.setAttribute("class", FILE_ADDED_CLASS);
audioTag.src = PATH_TO_SONG;
audioTag.loop = false;
audioTag.addEventListener("canplay", function() {
// song started, start shaking first item
setTimeout(function() {
shakeFirst(firstNode);
}, 500);
// setTimeout
setTimeout(function() {
stopShakeAll();
flashScreen();
for (var i=0; i<allShakeableNodes.length; i++) {
shakeOther(allShakeableNodes[i]);
}
}, 15500);
}, true);
audioTag.addEventListener("ended", function() {
stopShakeAll();
removeAddedFiles();
}, true);
audioTag.innerHTML = "<p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p>"
document.body.appendChild(audioTag);
audioTag.play();
}
function shakeFirst(node) {
node.className += " "+CSS_BASE_CLASS+" "+CSS_FIRST_CLASS;
}
function shakeOther(node) {
node.className += " "+CSS_BASE_CLASS+" "+CSS_OTHER_CLASSES[Math.floor(Math.random()*CSS_OTHER_CLASSES.length)];
}
function stopShakeAll() {
var shakingNodes = document.getElementsByClassName(CSS_BASE_CLASS);
var regex = new RegExp('\\b'+CSS_BASE_CLASS+'\\b');
for (var i=0; i<shakingNodes.length; ) {
shakingNodes[i].className = shakingNodes[i].className.replace(regex, "");
}
}
// get first item
var allNodes = document.getElementsByTagName("*");
var firstNode = null;
for (var i=0; i<allNodes.length; i++) {
var thisNode = allNodes[i];
if (withinBounds(thisNode)) {
if(isVisible(thisNode)) {
firstNode = thisNode;
break;
}
}
}
if (thisNode === null) {
console.warn("Could not find a node of the right size. Please try a different page.");
return;
}
// insert CSS
addCSS();
// play song
playSong();
var allShakeableNodes = [];
for (var i=0; i<allNodes.length; i++) {
var thisNode = allNodes[i];
if (withinBounds(thisNode)) {
allShakeableNodes.push(thisNode);
}
}
})();