-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththanos.js
More file actions
243 lines (202 loc) · 6.34 KB
/
thanos.js
File metadata and controls
243 lines (202 loc) · 6.34 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
window.onload = () => {
const REPETITION_COUNT = 2; // number of times each pixel is assigned to a canvas
const NUM_FRAMES = 128;
let members = [1, 2, 3, 4, 5, 6, 7, 8]
let IDS = []
let item = document.getElementById('1')
let copyItem = item.querySelector('.item-body')
/**
* Generates the individual subsets of pixels that are animated to create the effect
* @param {HTMLCanvasElement} ctx
* @param {number} count the frame particles
* @return {HTMLCanvasElement[]} Each canvas contains a subset of the original pixels
*/
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)
);
// assign the pixels to a canvas
// each pixel is assigned to 2 canvas', based on its x-position
for (let x = 0; x < width; ++x) {
for (let y = 0; y < height; ++y) {
for (let i = 0; i < REPETITION_COUNT; ++i) {
const dataIndex = Math.floor(
count * (Math.random() + 2 * x / width) / 3
);
const pixelIndex = (y * width + x) * 4;
// copy the pixel over from the original image
for (let offset = 0; offset < 4; ++offset) {
imageDatas[dataIndex].data[pixelIndex + offset] = originalData.data[pixelIndex + offset];
}
}
}
}
// turn image datas into canvas
return imageDatas.map(data => {
const $c = $canvas.cloneNode(true);
$c.getContext("2d").putImageData(data, 0, 0);
return $c;
});
}
/**
* Inserts a new element over an old one, 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
* @param {HTMLElement} $elm
*/
function snapToDust($elm, callback) {
html2canvas($elm, {
allowTaint: true,
}).then($canvas => {
// create the container we'll use to replace the element with
const $container = document.createElement("div");
$container.classList.add("disintegration-container");
// setup the frames for animation
const $frames = generateFrames($canvas, NUM_FRAMES);
$frames.forEach(($frame, i) => {
$frame.style.transitionDelay = `${1.35 * i / $frames.length}s`;
$container.appendChild($frame);
});
// then insert them into the DOM over the element
replaceElementVisually($elm, $container);
// then animate them
$container.offsetLeft; // forces reflow, so CSS we apply below does transition
// set the values the frame should animate to
// note that this is done after reflow so the transitions trigger
$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;
});
callback()
});
}
function reverseTime($elm) {
$elm.style.visibility = "visible"
addClass($elm, 'time')
}
/**
* Click Gauntlet
*/
let snap_btn = document.querySelector('#gauntlet-snap')
let reverse_btn = document.querySelector('#gauntlet-reverse')
snap_btn.onclick = () => {
reverse_btn.style.display = 'none'
playAudio('snap')
animateGauntlet('snap')
setTimeout(() => {
snap_btn.style.display = 'none'
reverse_btn.style.display = 'block'
console.log('1111')
makeRandom()
console.log('IDS', IDS)
let i = 0,
l = IDS.length;
let snapAction = function() {
if (i < l) {
let itm = document.getElementById(IDS[i])
snapToDust(itm, function() {
i++
snapAction()
})
}
}
snapAction()
// let itm = document.getElementById('1')
// snapToDust(itm)
}, 1500)
}
reverse_btn.onclick = () => {
snap_btn.style.display = 'none'
playAudio('reverse')
animateGauntlet('reverse')
setTimeout(() => {
reverse_btn.style.display = 'none'
snap_btn.style.display = 'block'
let itm = document.getElementById('1')
reverseTime(itm)
}, 2500)
}
/**
* Play Audio Files
* @param {[snap]} type [play audio of making snap]
* @param {[reverse]} type [play audio of reversing time]
*/
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_btn, 'snaping')
removeClass(reverse_btn, 'reversing')
if (type == 'snap') {
addClass(snap_btn, 'snaping')
} else if (type == 'reverse') {
addClass(reverse_btn, 'reversing')
}
}
/**
* Utils
*/
function hasClass(ele, cls) {
cls = cls || '';
if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时,返回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)) {
var 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 = []
var 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])
}
}
}