How to make particles move in spirals?

66 Views Asked by At

I've been trying to make a pixel rain for base64 images with javascript's context.getImageData(). It already creates a lot of particles moving from left to right with changing speeds depending on the brightness of the image and a color based on their position.

I want to make the particles move in a spiral from the canvas borders to the center and then return to the borders once again.

The code snippet can't be runned because of myImage.src, it has to be a base64 image, but these are to long for presenting here (you can use this page to create the source preferably with a small image, https://onlinepngtools.com/convert-png-to-base64).

const myImage = new Image();
myImage.src = 'base64 image'

myImage.addEventListener('load', function() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 600;
canvas.height = 714;

ctx.drawImage(myImage, 0, 0, canvas.width, canvas.height);
const pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
// ctx.clearRect(0, 0, canvas.width, canvas.height);

let particlesArray = [];
const ParticlesAmount = 7000;

let mappedImage = [];

for (let y = 0; y < canvas.height; y++) {
    let row = [];
    for (let x = 0; x < canvas.width; x++) {
        const red = pixels.data[(y * 4 *pixels.width) + (x *4)];
        const green = pixels.data[(y * 4 *pixels.width) + (x *4 + 1)];
        const blue = pixels.data[(y * 4 *pixels.width) + (x *4 + 2)];
        const alpha = pixels.data[(y * 4 *pixels.width) + (x *4 + 3)];
        
        const brightness = calculateRelativeBrightness(red, green, blue);
        const cell = [
            cellBrightness = brightness,
            cellColor = 'rgb(' + red + ', ' + green + ', ' + blue + ', ' + alpha + ')'
        ]
        row.push(cell);
    }
    mappedImage.push(row);
}

// For the human's eye perception.
function calculateRelativeBrightness(red, green, blue) {
    return Math.sqrt(
        (red * red) * 0.299 +
        (green* green) * 0.587 +
        (blue * blue) * 0.114
    );
}

class Particle {
    constructor() {
        this.y = Math.random() * canvas.height;
        this.x = 0;
        this.speed = 0;
        this.velocity = Math.random() * 5;
        this.size = Math.random() * 1.5 + 1;
        this.position1 = Math.floor(this.y);
        this.position2 = Math.floor(this.x);
    }

    update() {
        this.position1 = Math.floor(this.y);
        this.position2 = Math.floor(this.x);

        let movement = this.velocity * 5;

        if (
            this.position1 >= 0 &&
            this.position1 < mappedImage.length &&
            this.position2 >= 0 &&
            this.position2 < mappedImage[0].length
        ) {
            this.speed = mappedImage[this.position1][this.position2][0];
            let movement = (2.5 - this.speed) + this.velocity;
        
        this.x += this.velocity;
        if (this.x >= canvas.width) {
            this.x = 0;
            this.y = Math.random() * canvas.width;
        }
        }
    }

    draw() {
        ctx.beginPath();
        if (
            this.position1 >= 0 &&
            this.position1 < mappedImage.length &&
            this.position2 >= 0 &&
            this.position2 < mappedImage[0].length
        ) {
            ctx.fillStyle = mappedImage[this.position1][this.position2];
        } else {ctx.fillStyle = 'white'}
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.fill();
    }
}

function init() {
    for (let i=0; i < ParticlesAmount; i++) {
        particlesArray.push(new Particle());
    }
}

init();

function animate() {
    ctx.globalAlpha = 0.05;
    ctx.fillStyle = 'rgb(0, 0, 0)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    for (let i=0; i < particlesArray.length; i++) {
        particlesArray[i].update();
        particlesArray[i].draw();
    }
    requestAnimationFrame(animate);
}
animate();
})
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: #222;
}

#canvas {
    border: 2px solid #f5f5f5;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 660px;
    height: 714px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Pixel flow</title>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src="script.js"></script>
</body>
</html>

0

There are 0 best solutions below