-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (73 loc) · 2.36 KB
/
app.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
$(document).ready(function() {
let image = document.getElementById('img');
let canvasInteractive = document.getElementById('canvas-interactive');
let canvasReference = document.getElementById('canvas-reference');
let contextInteractive = document.getElementById('canvas-interactive').getContext('2d');
let contextReference = document.getElementById('canvas-reference').getContext('2d');
let width = canvasInteractive.width = canvasReference.width = window.innerWidth;
let height = canvasInteractive.height = canvasReference.height = window.innerHeight;
let logoDimensions = {
x: 768,
y: 768
};
let center = {
x: width / 2,
y: height / 2
};
let logoLocation = {
x: center.x - logoDimensions.x / 2,
y: center.y - logoDimensions.y / 2
};
let particleArray = [];
let particleAttributes = {
friction: 0.95,
ease: 0.1,
spacing: 4,
size: 2,
color: "#ffffff"
};
function Particle(x, y) {
this.x = this.startingX = x;
this.y = this.startingY = y;
this.vy = 3 * Math.sin((Date.now() / 50 - this.startingX) / 5)
}
Particle.prototype.update = function() {
this.y = this.startingY + 3 * Math.sin((Date.now() / 50 - this.startingX) / 5);
};
function init() {
contextReference.drawImage(image, logoLocation.x, logoLocation.y);
let pixels = contextReference.getImageData(0, 0, width, height).data;
let index;
for (let y = 0; y < height; y += particleAttributes.spacing) {
for (let x = 0; x < width; x += particleAttributes.spacing) {
index = (y * width + x) * 4;
if (pixels[++index] > 0) {
particleArray.push(new Particle(x, y));
}
}
}
}
function update() {
for (let i = 0; i < particleArray.length; i++) {
let p = particleArray[i];
p.update();
}
}
function render() {
contextInteractive.clearRect(0, 0, width, height);
for (let i = 0; i < particleArray.length; i++) {
let p = particleArray[i];
contextInteractive.fillStyle = particleAttributes.color;
contextInteractive.fillRect(p.x, p.y, particleAttributes.size, particleAttributes.size);
}
}
function animate() {
update();
render();
requestAnimationFrame(animate);
}
image.onload = function() {
init();
animate();
};
});