Select Page

Here is a code with a demo for matrix raindrop-like animation from top to bottom with 0 and 1 as its digits.

DEMO

See the Pen Untitled by Puneet Sharma (@webdevpuneet) on CodePen.

HTML

<canvas class="canvas">

Code

// Initialising the canvas
var canvas = document.querySelector('.canvas'),
    ctx = canvas?.getContext('2d');
if (canvas) {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    // Setting up the letters
    var letters = '1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010';
    letters = letters.split('');

    // Setting up the columns
    var fontSize = 10,
        columns = canvas.width / fontSize;

    // Setting up the drops
    var drops = [];
    for (var i = 0; i < columns; i++) {
        drops[i] = 1;
    }

    // Setting up the draw function
    function draw() {
        ctx.fillStyle = 'rgba(0, 0, 0, .1)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        for (var i = 0; i < drops.length; i++) {
            var text = letters[Math.floor(Math.random() * letters.length)];
            ctx.fillStyle = '#cfcfcf95';
            ctx.fillText(text, i * fontSize, drops[i] * fontSize);
            drops[i]++;
            if (drops[i] * fontSize > canvas.height && Math.random() > .95) {
                drops[i] = 0;
            }
        }
    }

    // Loop the animation
    setInterval(draw, 33);

}
// road map section