-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
232 lines (193 loc) · 5.35 KB
/
index.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
232
(function() {
/**
* Declare Variables
*/
let IDS = []
let MEMBERS = [1, 2, 3, 4, 5, 6, 7, 8]
let REPEAT = 2;
let SPARES = 32;
/**
* Declare Doms
*/
let $snap = document.querySelector('#gauntlet-snap')
let $reverse = document.querySelector('#gauntlet-reverse')
/**
* Click Gauntlet
*/
$snap.onclick = () => {
$reverse.style.display = 'none'
playAudio('snap')
animateGauntlet('snap')
setTimeout(() => {
$snap.style.display = 'none'
$reverse.style.display = 'block'
makeRandom()
let i = 0,
l = IDS.length;
let snapAction = function() {
if (i < l) {
let itm = document.getElementById(IDS[i])
snapToDust(itm, function() {
i++
snapAction()
})
}
}
snapAction()
}, 1500)
}
$reverse.onclick = () => {
$snap.style.display = 'none'
playAudio('reverse')
animateGauntlet('reverse')
setTimeout(() => {
$reverse.style.display = 'none'
$snap.style.display = 'block'
for (var i = 0, l = IDS.length; i < l; i++) {
let itm = document.getElementById(IDS[i])
itm.style.visibility = 'visible'
addClass(itm, 'time')
}
}, 2500)
}
/**
* Play Audio Files
*/
function playAudio(type) {
switch (type) {
case 'snap':
document.querySelector('#audio-snap').play()
break
case 'reverse':
document.querySelector('#audio-reverse').play()
break
default:
return
}
}
/**
* The Infinity Gauntlet Animation
*/
function animateGauntlet(type) {
removeClass($snap, 'snaping')
removeClass($reverse, 'reversing')
if (type == 'snap') {
addClass($snap, 'snaping')
} else if (type == 'reverse') {
addClass($reverse, 'reversing')
}
}
/**
* Generates the individual subsets of pixels that are animated to create the effect
*/
function generateFrames($canvas, count = 32) {
const {
width,
height
} = $canvas;
const ctx = $canvas.getContext("2d");
const originalData = ctx.getImageData(0, 0, width, height);
const imageDatas = [...Array(count)].map(
(_, i) => ctx.createImageData(width, height)
);
for (let x = 0; x < width; ++x) {
for (let y = 0; y < height; ++y) {
for (let i = 0; i < REPEAT; ++i) {
const dataIndex = Math.floor(
count * (Math.random() + 2 * x / width) / 3
);
const pixelIndex = (y * width + x) * 4;
for (let offset = 0; offset < 4; ++offset) {
imageDatas[dataIndex].data[pixelIndex + offset] = originalData.data[pixelIndex + offset];
}
}
}
}
return imageDatas.map(data => {
const $c = $canvas.cloneNode(true);
$c.getContext("2d").putImageData(data, 0, 0);
return $c;
});
}
/**
* Insert new element, hiding the old one
*/
function replaceElementVisually($old, $new) {
const $parent = $old.offsetParent;
$new.style.top = `${$old.offsetTop}px`;
$new.style.left = `${$old.offsetLeft}px`;
$new.style.width = `${$old.offsetWidth}px`;
$new.style.height = `${$old.offsetHeight}px`;
$parent.appendChild($new);
$old.style.visibility = "hidden";
}
/**
* snapToDust
*/
function snapToDust($elm, callback) {
html2canvas($elm, {
allowTaint: true,
}).then($canvas => {
// create a container
const $container = document.createElement("div");
$container.classList.add("dust-container");
// frames for animation
const $frames = generateFrames($canvas, SPARES);
$frames.forEach(($frame, i) => {
$frame.style.transitionDelay = `${1.35 * i / $frames.length}s`;
$container.appendChild($frame);
});
// insert canvas into DOM over the element
replaceElementVisually($elm, $container);
// animate them
$container.offsetLeft;
$frames.forEach($frame => {
const randomRadian = 2 * Math.PI * (Math.random() - 0.5);
$frame.style.transform =
`rotate(${15 * (Math.random() - 0.5)}deg) translate(${60 * Math.cos(randomRadian)}px, ${30 * Math.sin(randomRadian)}px)
rotate(${15 * (Math.random() - 0.5)}deg)`;
$frame.style.opacity = 0;
});
if (callback && typeof callback == 'function') {
callback()
}
});
}
/**
* Utils
*/
function hasClass($ele, cls) {
cls = cls || ''
if (cls.replace(/\s/g, '').length == 0) {
return false
}
return new RegExp(' ' + cls + ' ').test(' ' + $ele.className + ' ')
}
function addClass($ele, cls) {
if (!hasClass($ele, cls)) {
$ele.className = $ele.className == '' ? cls : $ele.className + ' ' + cls
}
}
function removeClass($ele, cls) {
if (hasClass($ele, cls)) {
let newClass = ' ' + $ele.className.replace(/[\t\r\n]/g, '') + ' '
while (newClass.indexOf(' ' + cls + ' ') >= 0) {
newClass = newClass.replace(' ' + cls + ' ', ' ')
}
$ele.className = newClass.replace(/^\s+|\s+$/g, '')
}
}
function makeRandom() {
IDS = []
let arr = new Array()
for (var i = 0; i < MEMBERS.length; i++) {
arr[i] = i + 1;
}
arr.sort(function() {
return 0.5 - Math.random();
});
for (var i = 0; i < Math.ceil(MEMBERS.length / 2); i++) {
IDS.push(arr[i])
}
}
})()