I have always been a big fan of the movie series 'The Matrix Trilogy'. In this article we will make the following visual effect from the matrix series in vanilla Javascript.
This GIF here is optimized for size, so it's rather low quality and it janks. But our end result will be smooth. Let's get started.
We will render this visual effect on a HTML5 canvas
. For this article, we don't need any other elements on our page than a canvas
. Initially we can give it just any valid size (width and height), because we will be setting the actual width and height of the canvas from our JS code. And we will give it an ID to reference it easily from the JS code.
<canvas width="500" height="200" id="canv" />
Now we can get the DOM Node for this canvas
and set its width and height to fill the body
. We will also get the 2D drawing context for this canvas. The screen will be black for this effect, so we will fill the canvas with black by drawing a black rectangle with width and height same as that of the canvas.
// Get the canvas node and the drawing context
const canvas = document.getElementById('canv');
const ctx = canvas.getContext('2d');
// set the width and height of the canvas
const w = canvas.width = document.body.offsetWidth;
const h = canvas.height = document.body.offsetHeight;
// draw a black rectangle of width and height same as that of the canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, w, h);
We want to make the text fall in columns. Each column will be 20px wide. And in each frame of the animation, we will put a single character at the end of each column. Initially the end (y coordinate) of each column is at 0.
const cols = Math.floor(w / 20) + 1;
const ypos = Array(cols).fill(0);
In each frame, we need to render a semi transparent black rectangle on top of the previous frame, so that the characters rendered in previous frames can look progressively dimmed. Then we need to render the new characters at the end of each column for the current frame.
The y coordinates for each column is stored in the ypos
array. In each frame we want to randomly reset some columns to start from the top again, so that it seems like columns of varying heights are falling from the top. For the rest of the columns we will simply move the y coordinate 20px down, so that in next frame a new character appears below the current one.
function matrix () {
// Draw a semitransparent black rectangle on top of previous drawing
ctx.fillStyle = '#0001';
ctx.fillRect(0, 0, w, h);
// Set color to green and font to 15pt monospace in the drawing context
ctx.fillStyle = '#0f0';
ctx.font = '15pt monospace';
// for each column put a random character at the end
ypos.forEach((y, ind) => {
// generate a random character
const text = String.fromCharCode(Math.random() * 128);
// x coordinate of the column, y coordinate is already given
const x = ind * 20;
// render the character at (x, y)
ctx.fillText(text, x, y);
// randomly reset the end of the column if it's at least 100px high
if (y > 100 + Math.random() * 10000) ypos[ind] = 0;
// otherwise just move the y coordinate for the column 20px down,
else ypos[ind] = y + 20;
});
}
// render the animation at 20 FPS.
setInterval(matrix, 50);
That's all we need to render a matrix effect in vanilla JS. The code for this article is given in the pen below for reference.
Hoping you enjoyed reading this article and learned a few things from it.
You can find more about me at gnsp.in.
Thanks for reading !
Top comments (17)
Nice work. I don’t recall the exact matrix effects, but I think you should remove the space character from the possible result set... It looks like some columns start somewhere in the middle. Maybe some modulo/offset calculation would do the trick.
Nice. I remember doing this in Flash/Actionscript around 10 years ago
Great work, I improve a little:
you can asign a font in http; so i doit a Matrix font like: fontmeme.com/fonts/matrix-code-nfi...
I read a text in files instead a random characters, i thing is better distribution of character sets
Look my improves:
jaimecortesproject.000webhostapp.c...
feel free to use.
Me again, look the improves here:
github.com/jcortesm5681/MatrixHTML
feel free to use.
I've created a library with the Matrix effect. It's based on CodePen demo, I think it looks much better. The original code was much simpler and also looks better because it use Japanese characters.
jcubic / cmatrix
Render Matrix effect animation on Canvas in JavaScript
CMatrix - Matrix effect in JavaScript
Matrix animation effect in JavaScript using Canvas
Installation
Demos
Usage
You can use CDN:
and intialize the effect.
The matrix function return a Promise that is resolved when exit By default,
q
andESC
exit from the effect. Useexit: false
to disable ending the animation.Repo Link
Options
chars
- array of single character strings, by default Katagana and Hiragana (Japanese characters are used).exit
- by default matrix return…That is so coool !
could you please tell how I can write some text first and then run animation plz
It looks so smooth! Great job 👏👏👏
So cool, nice one
The power of vanilla js🤘
Really COOL!!