Building Cyberpunk Matrix Digital Rain in HTML5 Canvas & CSS: 60 FPS Glyph Streams & Phosphor Glow
Matrix digital rain is built by rendering columns of random characters (half-width Katakana, Latin, and digits) across an HTML5 Canvas at fixed column intervals. Instead of clearing the canvas each frame with clearRect(), fill the entire canvas with a translucent black rect (ctx.fillStyle = "rgba(0, 0, 0, 0.05)"), which creates fading neon phosphor decay trails behind falling characters.
Step 1: Initializing Glyph Column Arrays
Divide canvas width into column slots based on font size (typically 14px to 18px). Maintain an array storing the current vertical Y coordinate for each column.
const canvas = document.getElementById('matrixCanvas');
const ctx = canvas.getContext('2d');
const fontSize = 16;
let columns = Math.floor(window.innerWidth / fontSize);
let drops = [];
function initMatrix() {
columns = Math.floor(window.innerWidth / fontSize);
drops = Array.from({ length: columns }, () => Math.floor(Math.random() * -50));
}
initMatrix();
Step 2: Phosphor Decay Trails with Alpha Rectangles
The signature trailing decay of CRT phosphors is achieved without saving past frames in memory. Every frame, draw a semi-transparent black rectangle over the entire viewport.
const chars = 'ハミヒーウシナモニサワツオリアホテマケメエカキムユラセネスタヌヘ0123456789:・."=*+-<>¦';
function drawMatrix() {
// Translucent background wash creates trailing glow
ctx.fillStyle = 'rgba(5, 10, 5, 0.06)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = `${fontSize}px monospace`;
for (let i = 0; i < drops.length; i++) {
const text = chars.charAt(Math.floor(Math.random() * chars.length));
const x = i * fontSize;
const y = drops[i] * fontSize;
// Glowing white lead character
ctx.fillStyle = '#c7ffd5';
ctx.fillText(text, x, y);
// Green body characters
ctx.fillStyle = '#00ff66';
ctx.fillText(text, x, y - fontSize);
if (y > canvas.height && Math.random() > 0.975) {
drops[i] = 0; // reset column to top
}
drops[i]++;
}
requestAnimationFrame(drawMatrix);
}
Step 3: CRT Scanlines & Vignette Filter Overlay
Layer pure CSS scanlines and radial vignette masks on top of the canvas using CSS pseudo-elements and repeating-linear-gradient for an authentic 80s arcade monitor look.
.matrix-container {
position: relative;
background: #020603;
}
.matrix-container::after {
content: "";
position: absolute;
inset: 0;
background: repeating-linear-gradient(
0deg,
rgba(0, 0, 0, 0.15) 0px,
rgba(0, 0, 0, 0.15) 1px,
transparent 1px,
transparent 2px
), radial-gradient(circle at center, transparent 40%, rgba(0, 0, 0, 0.75) 100%);
pointer-events: none;
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
When building high-performance UI animations or particle systems, my golden rule is: never calculate layout triggers (like getBoundingClientRect() or offsetHeight) inside animation loops. Read measurements once upfront and use CSS transforms or Canvas for buttery 60fps.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why use semi-transparent fillRect instead of clearRect in Matrix rain?
Calling ctx.fillRect() with an alpha value (e.g. rgba(0, 0, 0, 0.05)) dims the previous frame slightly rather than erasing it. Over multiple frames, this creates natural fading phosphor trails without needing to store hundreds of character history objects in memory.
Which character set is used in the original Matrix film code?
The original film used custom Japanese half-width Katakana characters, Western Latin numerals, and mathematical symbols created by production designer Simon Whiteley. In HTML5 Canvas, you can use the Unicode range \uFF66 to \uFF9D.
How do you prevent Matrix rain from blurring on 4K retina displays?
Multiply the canvas width and height buffer by window.devicePixelRatio, then scale the 2D context using ctx.scale(dpr, dpr). Set CSS width and height to 100% so text renders at native hardware sharpness.
Can Matrix digital rain be rendered with pure CSS text animations?
While CSS keyframe column animations work for 5–10 columns, rendering 100+ columns with randomized glyph mutation in pure CSS causes massive DOM bloat and thousands of layout recalculations. Canvas renders all columns in a single draw call at 60fps.
How do you handle window resizing without resetting all rain drops to the top?
When resizing the window, preserve the existing drops array and re-index only the new or trimmed columns. Avoid resetting all column positions to zero so the rain flow continues seamlessly.