# Matrix rain background

> Columns of characters fall with a glowing trail, drawn on a canvas. Full‑screen, with HTML, CSS and a few lines of JS.

- Category: [Backgrounds](https://library.aimran.es/components/patterns)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/pt-matrix
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

The Matrix rain background still works whenever a site wants to look like it lives inside a terminal: a cybersecurity firm, a hackathon or CTF page, a developer’s portfolio, a tongue‑in‑cheek 404. Katakana, numbers and symbols pour down in columns with a bright head and a fading trail, and all three colours are yours to change.

It keeps running for visitors who asked for less motion, so add that check yourself if it covers a large area. Density changes the size of the characters. With text on top, lower the opacity so the rain stays in the background.

## Code

### HTML

```html
<div class="pt-matrix"><canvas></canvas></div>
```

### CSS

```css
.pt-matrix {
  --c1: #34d399;
  --c2: #d1fae5;
  --c3: #052e1a;
  --k: 1;
  /* velocidad */
  --d: 1;
  /* densidad / escala */
  --o: 1;
  /* opacidad */
  --bg: #05060a;
  --fg: #ffffff;
  --blend: screen;
  min-height: 100vh;
}

.pt-matrix {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-matrix>* {
  opacity: var(--o);
}

.pt-matrix {
  background: radial-gradient(ellipse at 50% 0%,color-mix(in srgb,var(--c3) 70%,var(--bg)),var(--bg))!important;
}

.pt-matrix canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
```

### JavaScript

```js
(function (el) {
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
const CH="アイウエオカキクケコサシスセソタチツテト0123456789ABCDEF<>/*+=";let cols=[],last=0,acc=0;
const loop=(t)=>{raf=requestAnimationFrame(loop);const v=V();acc+=(t-last)*v.k;last=t;if(acc<55)return;acc=0;
const fs=Math.max(8,Math.min(40,Math.round(18/v.d))),n=Math.ceil(W/fs);if(cols.length!==n)cols=Array.from({length:n},()=>Math.random()*-60);
x.globalAlpha=.1;x.fillStyle=v.bg;x.fillRect(0,0,W,H);x.globalAlpha=1;x.font=fs+"px ui-monospace,monospace";
for(let i=0;i<n;i++){const y=cols[i]*fs;x.fillStyle=v.c1;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y-fs);x.fillStyle=v.c2;x.shadowColor=v.c1;x.shadowBlur=8;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y);x.shadowBlur=0;
if(y>H&&Math.random()>.975)cols[i]=0;cols[i]++}};raf=requestAnimationFrame(loop);
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
})(document.querySelector(".pt-matrix"));
```

### React

```tsx
// @ts-nocheck
import { useEffect, useRef } from "react";

const css = `
.pt-matrix {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-matrix>* {
  opacity: var(--o);
}

.pt-matrix {
  background: radial-gradient(ellipse at 50% 0%,color-mix(in srgb,var(--c3) 70%,var(--bg)),var(--bg))!important;
}

.pt-matrix canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

`;

export default function PatternMatrix() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
const CH="アイウエオカキクケコサシスセソタチツテト0123456789ABCDEF<>/*+=";let cols=[],last=0,acc=0;
const loop=(t)=>{raf=requestAnimationFrame(loop);const v=V();acc+=(t-last)*v.k;last=t;if(acc<55)return;acc=0;
const fs=Math.max(8,Math.min(40,Math.round(18/v.d))),n=Math.ceil(W/fs);if(cols.length!==n)cols=Array.from({length:n},()=>Math.random()*-60);
x.globalAlpha=.1;x.fillStyle=v.bg;x.fillRect(0,0,W,H);x.globalAlpha=1;x.font=fs+"px ui-monospace,monospace";
for(let i=0;i<n;i++){const y=cols[i]*fs;x.fillStyle=v.c1;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y-fs);x.fillStyle=v.c2;x.shadowColor=v.c1;x.shadowBlur=8;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y);x.shadowBlur=0;
if(y>H&&Math.random()>.975)cols[i]=0;cols[i]++}};raf=requestAnimationFrame(loop);
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-matrix"
        style={{
          "--c1": "#34d399",
          "--c2": "#d1fae5",
          "--c3": "#052e1a",
          "--k": 1,
          "--d": 1,
          "--o": 1,
          "--bg": "#05060a",
          "--fg": "#ffffff",
          "--blend": "screen",
          minHeight: "100vh",
        }}
      >
        <canvas></canvas>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Matrix rain background” block (Backgrounds) from AIMRAN Library to my project.

## What it is
Columns of characters fall with a glowing trail, drawn on a canvas. Full‑screen, with HTML, CSS and a few lines of JS.

## Chosen settings
- Color 1: #34d399
- Color 2: #d1fae5
- Color 3: #052e1a
- Speed: 1x (from 0.2 to 3x)
- Density / scale: 1x (from 0.5 to 2x)
- Opacity: 1 (from 0.2 to 1)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck
import { useEffect, useRef } from "react";

const css = `
.pt-matrix {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-matrix>* {
  opacity: var(--o);
}

.pt-matrix {
  background: radial-gradient(ellipse at 50% 0%,color-mix(in srgb,var(--c3) 70%,var(--bg)),var(--bg))!important;
}

.pt-matrix canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

`;

export default function PatternMatrix() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2);W=el.clientWidth;H=el.clientHeight;cv.width=W*r;cv.height=H*r;x.setTransform(r,0,0,r,0,0)};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const V=()=>{const s=getComputedStyle(el),g=(n)=>s.getPropertyValue(n).trim();return{c1:g("--c1"),c2:g("--c2"),c3:g("--c3"),bg:g("--bg"),k:+g("--k")||1,d:+g("--d")||1}};
// Movimiento reducido: se calculan de golpe unos fotogramas (con tiempo simulado) y la escena se queda quieta
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;let RF=0;
const requestAnimationFrame=(f)=>{if(!RM)return window.requestAnimationFrame(f);if(++RF<400)f(RF*60);return 0};
const CH="アイウエオカキクケコサシスセソタチツテト0123456789ABCDEF<>/*+=";let cols=[],last=0,acc=0;
const loop=(t)=>{raf=requestAnimationFrame(loop);const v=V();acc+=(t-last)*v.k;last=t;if(acc<55)return;acc=0;
const fs=Math.max(8,Math.min(40,Math.round(18/v.d))),n=Math.ceil(W/fs);if(cols.length!==n)cols=Array.from({length:n},()=>Math.random()*-60);
x.globalAlpha=.1;x.fillStyle=v.bg;x.fillRect(0,0,W,H);x.globalAlpha=1;x.font=fs+"px ui-monospace,monospace";
for(let i=0;i<n;i++){const y=cols[i]*fs;x.fillStyle=v.c1;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y-fs);x.fillStyle=v.c2;x.shadowColor=v.c1;x.shadowBlur=8;x.fillText(CH[Math.floor(Math.random()*CH.length)],i*fs,y);x.shadowBlur=0;
if(y>H&&Math.random()>.975)cols[i]=0;cols[i]++}};raf=requestAnimationFrame(loop);
return()=>{cancelAnimationFrame(raf);ro.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-matrix"
        style={{
          "--c1": "#34d399",
          "--c2": "#d1fae5",
          "--c3": "#052e1a",
          "--k": 1,
          "--d": 1,
          "--o": 1,
          "--bg": "#05060a",
          "--fg": "#ffffff",
          "--blend": "screen",
          minHeight: "100vh",
        }}
      >
        <canvas></canvas>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
