forked from nthitz/turndownforwhatjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdfw.js
231 lines (204 loc) · 6.89 KB
/
tdfw.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
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
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
(function() {
var player;
var turndownAt = 20
var numTurntAnimations = 10
var turntDown = false;
var maxNodes = 1000;
var animationCSS = {
'tdfw_intro': 'tdfwIntro 1s infinite ease-in-out',
'turntDown': function() {
var key = ~~ ( Math.random() * numTurntAnimations)
return 'turntDown' + key + ' 5s infinite ease-in-out'
}
}
var firstAddition = true
function embedVideo() {
var parent = document.createElement('div')
parent.style.position = 'fixed'
parent.style.zIndex = 5000;
parent.style.right = 0;
parent.style.top = 0
// parent.style.opacity = 0.2
var div = document.createElement('div')
div.id = "tdfw"
parent.appendChild(div)
document.body.appendChild(parent)
parent.onmouseover = function() {
console.log('mouse')
parent.style.opacity = 1
}
parent.onmouseout = function() {
parent.style.opacity = 0.2
}
parent.style.webkitTransition = 'opacity 0.3s ease-in-out'
parent.style.transition = 'opacity 0.3s ease-in-out'
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.body.appendChild(tag)
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
function onYouTubeIframeAPIReady() {
player = new YT.Player('tdfw', {
height: '200',
width: '305',
videoId: 'HMUDVMiITOU',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady
}
function onPlayerReady(event) {
console.log('ready')
event.target.playVideo();
requestAnimationFrame(checkTime)
}
function checkTime() {
if(turntDown) {
return false;
}
requestAnimationFrame(checkTime);
if(player.getCurrentTime() > turndownAt) {
turntDown = true;
removeCurStyles();
addCurStyles()
}
}
function onPlayerStateChange(event) {
console.log(event)
if(event.data === 1) {
//started
addCurStyles()
} else if(event.data === 2) {
//paused
removeCurStyles()
}
}
//<iframe width="560" height="315" src="//www.youtube.com/embed/HMUDVMiITOU?autoplay=1" frameborder="0" allowfullscreen></iframe>
function setupAnimations() {
var numKeyFrames = 10
var introKeyFrames = '';
var turntKeyFrames = [];
var jitterAmount = 10;
for(var i = 0; i < numTurntAnimations; i++) {
turntKeyFrames[i] = ''
}
for(var i = 0; i <= numKeyFrames; i++) {
var pct = i / numKeyFrames * 100 + '%';
var x = (Math.random() - 0.5) * jitterAmount
var y = (Math.random() - 0.5) * jitterAmount
x = ~~ x
y = ~~ y
var keyframe = '-webkit-transform: translate(' + x + 'px,' + y +'px); '
keyframe += 'transform: translate(' + x + 'px,' + y + 'px);'
introKeyFrames += pct + ' { ' + keyframe + ' } '
for(var j = 0; j < numTurntAnimations; j++) {
var x = (Math.random() - 0.5) * jitterAmount
var y = (Math.random() - 0.5) * jitterAmount
x = ~~ x
y = ~~ y
var rotationAmount = i / numKeyFrames * 360;
rotationAmount = ~~ rotationAmount
var rotateDirection = String.fromCharCode(88 + ~~ (Math.random() * 2))
var keyframe = '-webkit-transform: translate(' + x + 'px,' + y +'px) rotate' + rotateDirection + '(' + rotationAmount + 'deg); '
keyframe += 'transform: translate(' + x + 'px,' + y + 'px) rotate' + rotateDirection + '(' + rotationAmount + 'deg); '
turntKeyFrames[j] += pct + ' { ' + keyframe + ' }'
}
}
var introKeyFrameDef = '@-webkit-keyframes tdfwIntro { ' + introKeyFrames + ' } '
introKeyFrameDef += '@keyframes tdfwIntro { ' + introKeyFrames + ' } '
var allStyles = introKeyFrameDef
for(var i = 0; i < turntKeyFrames.length; i++) {
var kf = turntKeyFrames[i]
allStyles += '@-webkit-keyframes turntDown' + i + ' { ' + kf + ' } '
allStyles += '@keyframes turntDown' + i + ' { ' + kf + ' } '
}
var introClass = '.tdfw_intro { -webkit-animation: tdfw 1s infinite; animation: tdfw 1s infinite; } '
//allStyles += introClass
var style = document.createElement('style')
style.textContent = allStyles
document.body.appendChild(style)
}
function addCurStyles() {
var curClass = getCurClass()
var nodes = Array.prototype.slice.call(document.querySelectorAll('img'))
nodes = nodes.concat(Array.prototype.slice.call(document.querySelectorAll('div')))
nodes = nodes.concat(Array.prototype.slice.call(document.querySelectorAll('span')))
nodes = nodes.concat(Array.prototype.slice.call(document.querySelectorAll('a')))
nodes = nodes.concat(Array.prototype.slice.call(document.querySelectorAll('section, header, footer, video, iframe, nav, article, h1, h2, h3, h4, h5, h6, footer, main, p, pre, blockquote, ol, ul, li, embed, object, canvas, svg, form, input, select, button')))
var max = maxNodes < nodes.length ? maxNodes : nodes.length;
//console.log(nodes)
//console.log(max)
for(var i = 0; i < max ; i++) {
var node = nodes[i];
node.classList.add(curClass)
var delay = Math.round(Math.random() * 1000) / 1000 + 'ms'
if(firstAddition) {
delay = ~~ (Math.random() * 10) + 's'
}
var css = animationCSS[curClass];
if(typeof css === 'function') {
css = css()
}
node.style['webkitAnimation'] = css + ' ' + delay
node.style['animation'] = css + ' ' + delay
}
firstAddition = false
}
function removeCurStyles() {
var classes = allClasses()
var nodes = document.querySelectorAll('*')
for(var i = 0; i < nodes.length ; i++) {
var node = nodes[i];
for(var j = 0; j < classes.length; j++) {
var cl = classes[j]
node.classList.remove(cl)
node.style['webkitAnimation'] = ''
node.style['animation'] = ''
}
}
}
function allClasses() {
return ['tdfw_intro', 'turntDown']
}
function getCurClass() {
if(player.getCurrentTime() > turndownAt) {
return 'turntDown'
} else {
return 'tdfw_intro'
}
}
function init() {
if(typeof window.tdfw________TDFW !== 'undefined') {
return;
}
window.tdfw________TDFW = true;
embedVideo()
setupAnimations()
}
init()
})()