DEV Community

Cover image for Matrix raining code effect using JavaScript
Adam Nagy
Adam Nagy

Posted on • Updated on

Matrix raining code effect using JavaScript

Motivation

The new Matrix movie is coming this year, and I'm so hyped for it that I had to create this iconic effect with my tools. The implementation itself is really simple and I only used HTML, CSS and vanilla javascript for it.

If you prefer a video format, you can check my tutorial on it on YouTube:

Implementation

I'll use HTML canvas to create the "Digital rain" effect, and we will have all the login in javascript.

HTML

The HTML file will be really simple. In the body we will only have a <canvas>, and I also included the javascript file here at the bottom, you can also include it in the head with the defer attribute.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./styles.css">
    <title>Matrix digital rain</title>
</head>
<body>
    <canvas id="Matrix"></canvas>
    <script src="./index.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS

This will be really short too. In CSS I just basically make the body fill the whole viewport and set a black background color for it.

html {
    background: black;
    height: 100%;
    overflow: hidden;
}

body {
    margin: 0;
    padding: 0;
    height: 100%;
}
Enter fullscreen mode Exit fullscreen mode

Javascript

This implementation will be the 🦁 lionshare of the project.
First we have to initialise our canvas with a 2D context. I also set the canvas to take up the whole viewport by setting it's width and height:

const canvas = document.getElementById('Matrix');
const context = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
Enter fullscreen mode Exit fullscreen mode

Next I create the alphabet from which we will pick our characters for the rain. I'll use katakana characters (A variation of Katakana symbols was used in the Matrix movie itself.), the latin alphabet and arabic numbers. The concatenation of these will create the alphabet.

const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン';
const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const nums = '0123456789';

const alphabet = katakana + latin + nums;
Enter fullscreen mode Exit fullscreen mode

We have to calculate how many "rain columns" will fit on the screen and to do that I'll set a fixed fontsize and divide the width on the window's innerWidth to get the exact column count.

const fontSize = 16;
const columns = canvas.width/fontSize;
Enter fullscreen mode Exit fullscreen mode

I'll use an array to store and render the raindrops. The index of a given element will be the coordinate of the raindrop on the X axis, and the value of a given element will hold it's coordinate on the Y axis. This way we don't need a 2D array. To initialise the array I fill it up with with ones, so aftert the initialization we will have the same exact height for every column: 1.

const rainDrops = [];

for( let x = 0; x < columns; x++ ) {
    rainDrops[x] = 1;
}
Enter fullscreen mode Exit fullscreen mode

Now it's time to implement the hearth of our effect the draw function. First we will paint the whole canvas with a transparent black color. This will give us the trail effect on the raindrops, when the drop falls the already drown characters will slowly fade out. Next I'll set the fontsize and the color (of course it is green 😎). And now comes the 🔑 key. I'll loop through the raindrop array, and for every element I'll pick a random character from our alphabet and render that in the next position of the column. The important thing here is that you have to multiply the coordinates (elemnt value and index) with the font size to get the perfect spacing. Lastly we have to move our raindrops which dropped below the viewport height, to the top of that column. To get the raining effect I don't put it to the top right away, but add a little randomness by adding a random chance to do that.

const draw = () => {
    context.fillStyle = 'rgba(0, 0, 0, 0.05)';
    context.fillRect(0, 0, canvas.width, canvas.height);

    context.fillStyle = '#0F0';
    context.font = fontSize + 'px monospace';

    for(let i = 0; i < rainDrops.length; i++)
    {
        const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length));
        context.fillText(text, i*fontSize, rainDrops[i]*fontSize);

        if(rainDrops[i]*fontSize > canvas.height && Math.random() > 0.975){
            rainDrops[i] = 0;
        }
        rainDrops[i]++;
    }
};
Enter fullscreen mode Exit fullscreen mode

As a last step I need to call the draw function in an interval and this will call the draw function in every 30ms.

setInterval(draw, 30);
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is a really fun project to create and I tried to keep the implementation as beginner friendly as I could. Hope you enjoyed it, if you seek educational content on web development follow me, I create educational YouTube videos, and Instagram posts too.

Happy Hacking!

Where can you learn more from me?

I create education content covering web-development on several platforms, feel free to 👀 check them out.

I also create a newsletter where I share the week's or 2 week's educational content that I created. No bull💩 just educational content.

🔗 Links:

Discussion (11)

Collapse
marzelin profile image
Marc Ziel

Nice, but the start of the animation is a bit ugly, imho. Here's a quick fix:

const rainDrops = Array.from({ length: columns }).fill(canvas.height);
Enter fullscreen mode Exit fullscreen mode
Collapse
javascriptacademy profile image
Adam Nagy Author

Yes, this is a nice way to start it 😊. I left it because it is felt like a cool intro to me, something that signals that something big and awesome is coming.

Collapse
lpm0073 profile image
Lawrence McDaniel

likewise. i originally added Marc's fix but then changed it back to your original bc i like the flicker effect.

Collapse
lpm0073 profile image
Lawrence McDaniel • Edited on

You're welcome to do a PR on npmjs.com/package/react-mdr with your fix :)

Collapse
ecfaria profile image
Emílio Faria

I remember trying doing this circa 2004, it was MUCH MUCH MUCH more difficult than this. Impressive how web technologies evolved in the last ~20 years.

Collapse
javascriptacademy profile image
Adam Nagy Author

Yes it is evolved a lot, and now it is evolving even rapidly. I’m really excited how it will look in ^10, ^20 years 🙂

Collapse
linuxuserin profile image
L

Don't know but 4 years old code looks same as yours.

code.sololearn.com/WpFa2Yz9WP8g

Collapse
lpm0073 profile image
Lawrence McDaniel • Edited on

This is awesome Adam, thanks for sharing. I liked it so much that i ported it to ReactJS and created an NPM package based on your code sample: npmjs.com/package/react-mdr

Collapse
javascriptacademy profile image
Adam Nagy Author

Awesome work Lawrance!

Collapse
erickvivas415 profile image
Erick Vivas

Pretty cool!

Collapse
javascriptacademy profile image
Adam Nagy Author

Thanks for the feedback, appreciated. 😊