# Leaded stained glass

> Panes of colored glass joined by lead came drift slowly while a beam of sunlight sweeps across them, on canvas.

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

## Where to use it

A stained glass window built from a Voronoi diagram. Every pixel finds its two nearest seeds: close to the border between them it becomes lead, otherwise it takes the color of its pane, brighter in the middle and with a little hammered texture. The seeds drift, so the panes slowly change shape, and a diagonal sunbeam lights them as it passes.

The three colors give the glass, and each pane gets a lighter or darker shade for variety. Density changes the number of panes. It suits churches and heritage sites, glass studios, music artwork or any page that needs warm, backlit color.

## Code

### HTML

```html
<div class="pt-uvq16-stained-glass"><canvas></canvas></div>
```

### CSS

```css
.pt-uvq16-stained-glass {
  --c1: #1f4fbf;
  --c2: #c2183c;
  --c3: #e9b23a;
  --k: 1;
  /* velocidad */
  --d: 1;
  /* densidad / escala */
  --o: 1;
  /* opacidad */
  --bg: #05060a;
  --fg: #ffffff;
  --blend: screen;
  min-height: 100vh;
}

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

.pt-uvq16-stained-glass>* {
  opacity: var(--o);
}

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

.pt-uvq16-stained-glass {
  background: #1a1918;
}
```

### JavaScript

```js
(function (el) {
const cv=el.querySelector("canvas"),x=cv.getContext("2d");let W=0,H=0,raf=0,LF=null,pend=null,vis=true,dead=false,RF=0;
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2),w=el.clientWidth,h=el.clientHeight;if(w===W&&h===H&&cv.width===Math.round(w*r))return;W=w;H=h;cv.width=Math.round(w*r);cv.height=Math.round(h*r);x.setTransform(r,0,0,r,0,0);if(RM&&LF){RF=0;LF()}};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const io=new IntersectionObserver(e=>{vis=e[e.length-1].isIntersecting;if(vis&&pend&&!dead){const f=pend;pend=null;raf=window.requestAnimationFrame(f)}});io.observe(el);
const requestAnimationFrame=f=>{LF=f;if(dead)return 0;if(RM){if(++RF<90)f();return 0}if(!vis){pend=f;return 0}return window.requestAnimationFrame(f)};
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")||"#05060a",k:+g("--k")||1,d:+g("--d")||1}};
const pc=document.createElement("canvas").getContext("2d"),CC={};
const rgb=c=>{if(CC[c])return CC[c];pc.fillStyle="#000";pc.fillStyle=c;const s=pc.fillStyle;return CC[c]=s[0]==="#"?[1,3,5].map(i=>parseInt(s.slice(i,i+2),16)):s.slice(s.indexOf("(")+1).split(",").slice(0,3).map(parseFloat)};
const mix=(a,b,p)=>[a[0]+(b[0]-a[0])*p,a[1]+(b[1]-a[1])*p,a[2]+(b[2]-a[2])*p];
const css=(a,o)=>o===undefined?"rgb("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+")":"rgba("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+","+o+")";
const lum=a=>(a[0]*.299+a[1]*.587+a[2]*.114)/255;
const o=document.createElement("canvas"),g=o.getContext("2d"),Pz=3;let S=[],img,buf,NZ,gw=0,gh=0,t=0,fr=0;
const loop=()=>{const v=V();t+=.004*v.k;const nw=Math.max(4,Math.ceil(W/Pz)),nh=Math.max(4,Math.ceil(H/Pz));
if(nw!==gw||nh!==gh||!img){gw=nw;gh=nh;o.width=gw;o.height=gh;img=g.createImageData(gw,gh);buf=new Uint32Array(img.data.buffer);NZ=new Float32Array(gw*gh);for(let k=0;k<NZ.length;k++)NZ[k]=(Math.random()-.5)*.1}
const n=Math.round(14+12*v.d),rr=Math.random;while(S.length<n)S.push({x:rr(),y:rr(),vx:rr()-.5,vy:rr()-.5,c:S.length%3,tn:rr()*.55-.2});S.length=n;
if(fr++%2===0||RM){const C=[rgb(v.c1),rgb(v.c2),rgb(v.c3)],sx=new Float32Array(n),sy=new Float32Array(n),cr=new Float32Array(n),cg=new Float32Array(n),cb=new Float32Array(n);
for(let q=0;q<n;q++){const s=S[q];s.x+=s.vx*.0007*v.k;s.y+=s.vy*.0007*v.k;if(s.x<0||s.x>1)s.vx*=-1;if(s.y<0||s.y>1)s.vy*=-1;s.x=Math.min(1,Math.max(0,s.x));s.y=Math.min(1,Math.max(0,s.y));sx[q]=s.x*gw;sy[q]=s.y*gh;const c=s.tn>0?mix(C[s.c],[255,255,255],s.tn):mix(C[s.c],[0,0,0],-s.tn);cr[q]=c[0];cg[q]=c[1];cb[q]=c[2]}
const lw=1.1,bp=(t*.35)%1.8-.4;
for(let j=0;j<gh;j++)for(let i=0;i<gw;i++){let d1=1e9,d2=1e9,a=0,b=0;for(let q=0;q<n;q++){const dx=i-sx[q],dy=j-sy[q],d=dx*dx+dy*dy;if(d<d1){d2=d1;b=a;d1=d;a=q}else if(d<d2){d2=d;b=q}}
const ex=sx[b]-sx[a],ey=sy[b]-sy[a],e=(d2-d1)/(2*Math.sqrt(ex*ex+ey*ey)+1e-6),k=j*gw+i,u=(i+j*.55)/gw-bp;
const L=(.82+.62*Math.exp(-u*u*7))*(.92+NZ[k])*(1.18-Math.min(.42,Math.sqrt(d1)/38));const mm=Math.min(1,Math.max(0,(e-lw)/.9)),hl=e<lw?(1-e/lw)*34:0;
const R=26+hl+(Math.min(255,cr[a]*L)-26-hl)*mm,G=25+hl+(Math.min(255,cg[a]*L)-25-hl)*mm,Bb=24+hl+(Math.min(255,cb[a]*L)-24-hl)*mm;buf[k]=((255<<24)|((Bb|0)<<16)|((G|0)<<8)|(R|0))>>>0}
g.putImageData(img,0,0);x.imageSmoothingEnabled=true;x.drawImage(o,0,0,gw*Pz,gh*Pz)}
raf=requestAnimationFrame(loop)};loop();
return()=>{dead=true;cancelAnimationFrame(raf);ro.disconnect();io.disconnect()};
})(document.querySelector(".pt-uvq16-stained-glass"));
```

### React

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

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

.pt-uvq16-stained-glass>* {
  opacity: var(--o);
}

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

.pt-uvq16-stained-glass {
  background: #1a1918;
}

`;

export default function PatternUvq16StainedGlass() {
  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,LF=null,pend=null,vis=true,dead=false,RF=0;
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2),w=el.clientWidth,h=el.clientHeight;if(w===W&&h===H&&cv.width===Math.round(w*r))return;W=w;H=h;cv.width=Math.round(w*r);cv.height=Math.round(h*r);x.setTransform(r,0,0,r,0,0);if(RM&&LF){RF=0;LF()}};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const io=new IntersectionObserver(e=>{vis=e[e.length-1].isIntersecting;if(vis&&pend&&!dead){const f=pend;pend=null;raf=window.requestAnimationFrame(f)}});io.observe(el);
const requestAnimationFrame=f=>{LF=f;if(dead)return 0;if(RM){if(++RF<90)f();return 0}if(!vis){pend=f;return 0}return window.requestAnimationFrame(f)};
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")||"#05060a",k:+g("--k")||1,d:+g("--d")||1}};
const pc=document.createElement("canvas").getContext("2d"),CC={};
const rgb=c=>{if(CC[c])return CC[c];pc.fillStyle="#000";pc.fillStyle=c;const s=pc.fillStyle;return CC[c]=s[0]==="#"?[1,3,5].map(i=>parseInt(s.slice(i,i+2),16)):s.slice(s.indexOf("(")+1).split(",").slice(0,3).map(parseFloat)};
const mix=(a,b,p)=>[a[0]+(b[0]-a[0])*p,a[1]+(b[1]-a[1])*p,a[2]+(b[2]-a[2])*p];
const css=(a,o)=>o===undefined?"rgb("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+")":"rgba("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+","+o+")";
const lum=a=>(a[0]*.299+a[1]*.587+a[2]*.114)/255;
const o=document.createElement("canvas"),g=o.getContext("2d"),Pz=3;let S=[],img,buf,NZ,gw=0,gh=0,t=0,fr=0;
const loop=()=>{const v=V();t+=.004*v.k;const nw=Math.max(4,Math.ceil(W/Pz)),nh=Math.max(4,Math.ceil(H/Pz));
if(nw!==gw||nh!==gh||!img){gw=nw;gh=nh;o.width=gw;o.height=gh;img=g.createImageData(gw,gh);buf=new Uint32Array(img.data.buffer);NZ=new Float32Array(gw*gh);for(let k=0;k<NZ.length;k++)NZ[k]=(Math.random()-.5)*.1}
const n=Math.round(14+12*v.d),rr=Math.random;while(S.length<n)S.push({x:rr(),y:rr(),vx:rr()-.5,vy:rr()-.5,c:S.length%3,tn:rr()*.55-.2});S.length=n;
if(fr++%2===0||RM){const C=[rgb(v.c1),rgb(v.c2),rgb(v.c3)],sx=new Float32Array(n),sy=new Float32Array(n),cr=new Float32Array(n),cg=new Float32Array(n),cb=new Float32Array(n);
for(let q=0;q<n;q++){const s=S[q];s.x+=s.vx*.0007*v.k;s.y+=s.vy*.0007*v.k;if(s.x<0||s.x>1)s.vx*=-1;if(s.y<0||s.y>1)s.vy*=-1;s.x=Math.min(1,Math.max(0,s.x));s.y=Math.min(1,Math.max(0,s.y));sx[q]=s.x*gw;sy[q]=s.y*gh;const c=s.tn>0?mix(C[s.c],[255,255,255],s.tn):mix(C[s.c],[0,0,0],-s.tn);cr[q]=c[0];cg[q]=c[1];cb[q]=c[2]}
const lw=1.1,bp=(t*.35)%1.8-.4;
for(let j=0;j<gh;j++)for(let i=0;i<gw;i++){let d1=1e9,d2=1e9,a=0,b=0;for(let q=0;q<n;q++){const dx=i-sx[q],dy=j-sy[q],d=dx*dx+dy*dy;if(d<d1){d2=d1;b=a;d1=d;a=q}else if(d<d2){d2=d;b=q}}
const ex=sx[b]-sx[a],ey=sy[b]-sy[a],e=(d2-d1)/(2*Math.sqrt(ex*ex+ey*ey)+1e-6),k=j*gw+i,u=(i+j*.55)/gw-bp;
const L=(.82+.62*Math.exp(-u*u*7))*(.92+NZ[k])*(1.18-Math.min(.42,Math.sqrt(d1)/38));const mm=Math.min(1,Math.max(0,(e-lw)/.9)),hl=e<lw?(1-e/lw)*34:0;
const R=26+hl+(Math.min(255,cr[a]*L)-26-hl)*mm,G=25+hl+(Math.min(255,cg[a]*L)-25-hl)*mm,Bb=24+hl+(Math.min(255,cb[a]*L)-24-hl)*mm;buf[k]=((255<<24)|((Bb|0)<<16)|((G|0)<<8)|(R|0))>>>0}
g.putImageData(img,0,0);x.imageSmoothingEnabled=true;x.drawImage(o,0,0,gw*Pz,gh*Pz)}
raf=requestAnimationFrame(loop)};loop();
return()=>{dead=true;cancelAnimationFrame(raf);ro.disconnect();io.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-uvq16-stained-glass"
        style={{
          "--c1": "#1f4fbf",
          "--c2": "#c2183c",
          "--c3": "#e9b23a",
          "--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 “Leaded stained glass” block (Backgrounds) from AIMRAN Library to my project.

## What it is
Panes of colored glass joined by lead came drift slowly while a beam of sunlight sweeps across them, on canvas.

## Chosen settings
- Color 1: #1f4fbf
- Color 2: #c2183c
- Color 3: #e9b23a
- 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-uvq16-stained-glass {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg);
  container-type: size;
  isolation: isolate;
}

.pt-uvq16-stained-glass>* {
  opacity: var(--o);
}

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

.pt-uvq16-stained-glass {
  background: #1a1918;
}

`;

export default function PatternUvq16StainedGlass() {
  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,LF=null,pend=null,vis=true,dead=false,RF=0;
const RM=matchMedia("(prefers-reduced-motion: reduce)").matches;
const fit=()=>{const r=Math.min(window.devicePixelRatio||1,2),w=el.clientWidth,h=el.clientHeight;if(w===W&&h===H&&cv.width===Math.round(w*r))return;W=w;H=h;cv.width=Math.round(w*r);cv.height=Math.round(h*r);x.setTransform(r,0,0,r,0,0);if(RM&&LF){RF=0;LF()}};
const ro=new ResizeObserver(fit);ro.observe(el);fit();
const io=new IntersectionObserver(e=>{vis=e[e.length-1].isIntersecting;if(vis&&pend&&!dead){const f=pend;pend=null;raf=window.requestAnimationFrame(f)}});io.observe(el);
const requestAnimationFrame=f=>{LF=f;if(dead)return 0;if(RM){if(++RF<90)f();return 0}if(!vis){pend=f;return 0}return window.requestAnimationFrame(f)};
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")||"#05060a",k:+g("--k")||1,d:+g("--d")||1}};
const pc=document.createElement("canvas").getContext("2d"),CC={};
const rgb=c=>{if(CC[c])return CC[c];pc.fillStyle="#000";pc.fillStyle=c;const s=pc.fillStyle;return CC[c]=s[0]==="#"?[1,3,5].map(i=>parseInt(s.slice(i,i+2),16)):s.slice(s.indexOf("(")+1).split(",").slice(0,3).map(parseFloat)};
const mix=(a,b,p)=>[a[0]+(b[0]-a[0])*p,a[1]+(b[1]-a[1])*p,a[2]+(b[2]-a[2])*p];
const css=(a,o)=>o===undefined?"rgb("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+")":"rgba("+(a[0]|0)+","+(a[1]|0)+","+(a[2]|0)+","+o+")";
const lum=a=>(a[0]*.299+a[1]*.587+a[2]*.114)/255;
const o=document.createElement("canvas"),g=o.getContext("2d"),Pz=3;let S=[],img,buf,NZ,gw=0,gh=0,t=0,fr=0;
const loop=()=>{const v=V();t+=.004*v.k;const nw=Math.max(4,Math.ceil(W/Pz)),nh=Math.max(4,Math.ceil(H/Pz));
if(nw!==gw||nh!==gh||!img){gw=nw;gh=nh;o.width=gw;o.height=gh;img=g.createImageData(gw,gh);buf=new Uint32Array(img.data.buffer);NZ=new Float32Array(gw*gh);for(let k=0;k<NZ.length;k++)NZ[k]=(Math.random()-.5)*.1}
const n=Math.round(14+12*v.d),rr=Math.random;while(S.length<n)S.push({x:rr(),y:rr(),vx:rr()-.5,vy:rr()-.5,c:S.length%3,tn:rr()*.55-.2});S.length=n;
if(fr++%2===0||RM){const C=[rgb(v.c1),rgb(v.c2),rgb(v.c3)],sx=new Float32Array(n),sy=new Float32Array(n),cr=new Float32Array(n),cg=new Float32Array(n),cb=new Float32Array(n);
for(let q=0;q<n;q++){const s=S[q];s.x+=s.vx*.0007*v.k;s.y+=s.vy*.0007*v.k;if(s.x<0||s.x>1)s.vx*=-1;if(s.y<0||s.y>1)s.vy*=-1;s.x=Math.min(1,Math.max(0,s.x));s.y=Math.min(1,Math.max(0,s.y));sx[q]=s.x*gw;sy[q]=s.y*gh;const c=s.tn>0?mix(C[s.c],[255,255,255],s.tn):mix(C[s.c],[0,0,0],-s.tn);cr[q]=c[0];cg[q]=c[1];cb[q]=c[2]}
const lw=1.1,bp=(t*.35)%1.8-.4;
for(let j=0;j<gh;j++)for(let i=0;i<gw;i++){let d1=1e9,d2=1e9,a=0,b=0;for(let q=0;q<n;q++){const dx=i-sx[q],dy=j-sy[q],d=dx*dx+dy*dy;if(d<d1){d2=d1;b=a;d1=d;a=q}else if(d<d2){d2=d;b=q}}
const ex=sx[b]-sx[a],ey=sy[b]-sy[a],e=(d2-d1)/(2*Math.sqrt(ex*ex+ey*ey)+1e-6),k=j*gw+i,u=(i+j*.55)/gw-bp;
const L=(.82+.62*Math.exp(-u*u*7))*(.92+NZ[k])*(1.18-Math.min(.42,Math.sqrt(d1)/38));const mm=Math.min(1,Math.max(0,(e-lw)/.9)),hl=e<lw?(1-e/lw)*34:0;
const R=26+hl+(Math.min(255,cr[a]*L)-26-hl)*mm,G=25+hl+(Math.min(255,cg[a]*L)-25-hl)*mm,Bb=24+hl+(Math.min(255,cb[a]*L)-24-hl)*mm;buf[k]=((255<<24)|((Bb|0)<<16)|((G|0)<<8)|(R|0))>>>0}
g.putImageData(img,0,0);x.imageSmoothingEnabled=true;x.drawImage(o,0,0,gw*Pz,gh*Pz)}
raf=requestAnimationFrame(loop)};loop();
return()=>{dead=true;cancelAnimationFrame(raf);ro.disconnect();io.disconnect()};
  }, []);
  return (
    <>
      <style>{css}</style>
      <div
        ref={ref}
        className="pt-uvq16-stained-glass"
        style={{
          "--c1": "#1f4fbf",
          "--c2": "#c2183c",
          "--c3": "#e9b23a",
          "--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.
````
