-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (75 loc) · 2.84 KB
/
script.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
window.addEventListener('load', function() {
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Particle {
constructor(effect, x, y, colour) {
this.effect = effect;
this.x = Math.random() * this.effect.width;
this.y = Math.random() * this.effect.height;
this.originX = Math.floor(x);
this.originY = Math.floor(y);
this.colour = colour;
this.size = 30;
this.vx = Math.random() * 2 - 1;
this.vy = Math.random() * 2 - 1;
}
draw(context) {
context.fillRect(this.x, this.y, this.size, this.size);
}
update(){
this.x += this.vx;
this.y += this.vy;
}
}
class Effect {
constructor(width, height){
this.width = width;
this.height = height;
this.particlesArray = [];
this.image = document.getElementById('image1');
this.centerX = this.width * 0.5;
this.centerY = this.height * 0.5;
this.x = this.centerX - this.image.width * 0.5;
this.y = this.centerY - this.image.height * 0.5;
// this.gap is used to pixelate the image to improve performance
this.gap = 5;
}
init(context){
context.drawImage(this.image, this.x, this.y);
// points to Uint8ClampedArray with the pixel data
const pixels = context.getImageData(0, 0, this.width, this.height).data;
for(let y = 0; y < this.height; y += this.gap){
for(let x = 0; x < this.width; y += this.gap){
// mapping linear number to x and y coordinates
const index = (y * this.width + x) * 4;
const red = pixels[index];
const green = pixels[index + 1];
const blue = pixels[index + 2];
const alpha = pixels[index + 3];
const colour = 'rgb('+ red + ','+ green + ','+ blue + ')';
if (alpha > 0){
this.particlesArray.push(new Particle(this, x, y, colour));
}
}
}
}
draw(context){
this.particlesArray.forEach(particle => particle.draw(context));
}
update(){
this.particlesArray.forEach(particle => particle.update());
}
}
const effect = new Effect(canvas.width, canvas.height);
effect.init(ctx);
console.log(effect);
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
effect.draw(ctx);
effect.update();
requestAnimationFrame(animate);
}
//animate();
});