-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
172 lines (171 loc) · 5.69 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Avenger Thanos Snap Effect Using Javascript</title>
<meta
name="description"
content="Avenger Thanos snap effect using JavaScript. Libraries used - jQuery, html2canvas, chance.js, jQuery Ui for animations and transitions."
/>
<meta
property="og:image"
content="https://everythingishere.info/thanos-snap/avengers.png"
/>
<meta
property="og:image:secure_ur"
content="https://everythingishere.info/thanos-snap/avengers.png"
/>
<meta
property="og:title"
content="Avenger Thanos Snap Effect Using Javascript"
/>
<meta
property="og:description"
content="Avenger Thanos snap effect using JavaScript. Libraries used - jQuery, html2canvas, chance.js, jQuery Ui for animations and transitions."
/>
<meta
property="og:url"
content="https://everythingishere.info/thanos-snap/"
/>
<meta property="og:type" content="profile" />
<meta property="profile:first_name" content="Simon" />
<meta property="profile:last_name" content="Gomes" />
<meta property="profile:gender" content="male" />
<link rel="shortcut icon" href="thanos-logo.png" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="assets/html2canvas.min.js"></script>
<script src="assets/chance.min.js"></script>
<script src="assets/jquery-3.4.1.min.js"></script>
<script src="assets/jquery-ui.min.js"></script>
</head>
<body>
<div class="content">
<img src="avengers.png" height="600" />
<button id="start-btn">Snap!</button>
</div>
<script>
var imageDataArray = [];
var canvasCount = 35;
$('#start-btn').click(function() {
html2canvas($('.content')[0]).then(canvas => {
//capture all div data as image
ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var pixelArr = imageData.data;
createBlankImageData(imageData);
//put pixel info to imageDataArray (Weighted Distributed)
for (let i = 0; i < pixelArr.length; i += 4) {
//find the highest probability canvas the pixel should be in
let p = Math.floor((i / pixelArr.length) * canvasCount);
let a = imageDataArray[weightedRandomDistrib(p)];
a[i] = pixelArr[i];
a[i + 1] = pixelArr[i + 1];
a[i + 2] = pixelArr[i + 2];
a[i + 3] = pixelArr[i + 3];
}
//create canvas for each imageData and append to target element
for (let i = 0; i < canvasCount; i++) {
let c = newCanvasFromImageData(
imageDataArray[i],
canvas.width,
canvas.height
);
c.classList.add('dust');
$('body').append(c);
}
//clear all children except the canvas
$('.content')
.children()
.not('.dust')
.fadeOut(3500);
//apply animation
$('.dust').each(function(index) {
animateBlur($(this), 0.8, 800);
setTimeout(() => {
animateTransform(
$(this),
100,
-100,
chance.integer({ min: -15, max: 15 }),
800 + 110 * index
);
}, 70 * index);
//remove the canvas from DOM tree when faded
$(this)
.delay(70 * index)
.fadeOut(110 * index + 800, 'easeInQuint', () => {
$(this).remove();
});
});
});
});
function weightedRandomDistrib(peak) {
var prob = [],
seq = [];
for (let i = 0; i < canvasCount; i++) {
prob.push(Math.pow(canvasCount - Math.abs(peak - i), 3));
seq.push(i);
}
return chance.weighted(seq, prob);
}
function animateBlur(elem, radius, duration) {
var r = 0;
$({ rad: 0 }).animate(
{ rad: radius },
{
duration: duration,
easing: 'easeOutQuad',
step: function(now) {
elem.css({
filter: 'blur(' + now + 'px)'
});
}
}
);
}
function animateTransform(elem, sx, sy, angle, duration) {
var td = (tx = ty = 0);
$({ x: 0, y: 0, deg: 0 }).animate(
{ x: sx, y: sy, deg: angle },
{
duration: duration,
easing: 'easeInQuad',
step: function(now, fx) {
if (fx.prop == 'x') tx = now;
else if (fx.prop == 'y') ty = now;
else if (fx.prop == 'deg') td = now;
elem.css({
transform:
'rotate(' +
td +
'deg)' +
'translate(' +
tx +
'px,' +
ty +
'px)'
});
}
}
);
}
function createBlankImageData(imageData) {
for (let i = 0; i < canvasCount; i++) {
let arr = new Uint8ClampedArray(imageData.data);
for (let j = 0; j < arr.length; j++) {
arr[j] = 0;
}
imageDataArray.push(arr);
}
}
function newCanvasFromImageData(imageDataArray, w, h) {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
tempCtx = canvas.getContext('2d');
tempCtx.putImageData(new ImageData(imageDataArray, w, h), 0, 0);
return canvas;
}
</script>
</body>
</html>