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(); });