Glass Refraction Lens
Move a piece of glass over a pattern and it bends the background, with distortion strength and shape controls.
AI prompt
Close-up product render of a clear glass lens resting on a colourful geometric pattern of stripes and circles, the pattern visibly bent and slightly magnified inside the lens with smooth wave-like distortion, a thin bright rim of light around the edge and a soft white highlight at the top left, a gentle shadow below, studio light, clean and tactile. Variants: a rounded rectangle glass pane and a droplet-shaped bubble. Negative: opaque blobs, noise artefacts, hard edges, text, logos. Reduced motion: static image.About this pattern
Definition
Real glass and water bend light: what you see through them is shifted and sometimes magnified, which is refraction. In CSS, an SVG displacement filter can fake it by pushing pixels of the background according to a noise map, giving a believable liquid-glass distortion.
When to use it
- feTurbulence makes a smooth noise map; feDisplacementMap shifts pixels using it.
- The scale value sets how strong the bend is.
- Clip the distorted copy to a lens shape; add a thin bright rim and a soft highlight.
- A slight magnification makes it look like a thick lens.
What the controls do
- Three shape buttons, a Distortion slider; move the pointer to move the lens.
Motion & timing
- The lens follows the pointer directly.
- Reduced motion: no easing is used.
Tips & variations
- Heavy filters are expensive; keep the filtered area small.
- Test in several browsers; filter support on backdrop layers varies, so this demo filters a copy of the background instead.
Code of this live preview
<svg width="0" height="0" aria-hidden="true" focusable="false"><defs><filter id="rf" x="-10%" y="-10%" width="120%" height="120%"><feTurbulence type="fractalNoise" baseFrequency="0.018" numOctaves="2" seed="4" result="n"/><feDisplacementMap id="dm" in="SourceGraphic" in2="n" scale="24" xChannelSelector="R" yChannelSelector="G"/></filter></defs></svg><div class="bx"><div class="pre" id="pre" role="group" aria-label="Shape"><button data-p="0" aria-pressed="true">Circle</button><button data-p="1" aria-pressed="false">Pane</button><button data-p="2" aria-pressed="false">Droplet</button></div><div class="sc" id="sc"><div class="bg" id="b0"></div><div class="lens" id="ln" data-s="0"><div class="in"><div class="bg" id="b1"></div></div></div></div><label class="rw">Distortion <b id="dv">24</b><input id="ds" type="range" min="0" max="60" value="24"></label></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sc{height:132px;border-radius:12px;position:relative;overflow:hidden;cursor:none;touch-action:none}.bg{position:absolute;left:0;top:0;width:330px;height:132px;background:repeating-linear-gradient(90deg,#f4c95d 0 18px,#2a6fdb 18px 36px),#fff}.bg::before{content:"";position:absolute;inset:0;background:radial-gradient(circle at 20% 40%,#ff5a5f 0 22px,transparent 23px),radial-gradient(circle at 62% 66%,#fff 0 26px,transparent 27px),radial-gradient(circle at 84% 28%,#2ec4b6 0 18px,transparent 19px)}.lens{position:absolute;width:var(--w);height:var(--h);margin:calc(var(--h)/-2) 0 0 calc(var(--w)/-2);overflow:hidden;box-shadow:inset 0 0 0 2px rgba(255,255,255,.85),0 8px 18px rgba(0,0,0,.3);pointer-events:none}.lens::after{content:"";position:absolute;inset:0;border-radius:inherit;background:radial-gradient(circle at 28% 24%,rgba(255,255,255,.55),transparent 38%)}.in{position:absolute;inset:0;filter:url(#rf);overflow:hidden;border-radius:inherit}.in .bg{transform-origin:var(--ox) var(--oy);transform:translate(var(--tx),var(--ty)) scale(1.25)}
.lens[data-s="0"]{--w:84px;--h:84px;border-radius:50%}.lens[data-s="1"]{--w:110px;--h:70px;border-radius:14px}.lens[data-s="2"]{--w:70px;--h:92px;border-radius:50% 50% 50% 50%/62% 62% 38% 38%}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const sc=document.getElementById('sc'),ln=document.getElementById('ln'),dm=document.getElementById('dm'),ds=document.getElementById('ds'),bs=[...document.querySelectorAll('#pre button')];let p=0,x=165,y=66;
function place(){const w=[84,110,70][p],h=[84,70,92][p];ln.style.left=x+'px';ln.style.top=y+'px';const tx=-(x-w/2),ty=-(y-h/2);ln.style.setProperty('--tx',tx+'px');ln.style.setProperty('--ty',ty+'px');ln.style.setProperty('--ox',(x)+'px');ln.style.setProperty('--oy',(y)+'px');}
function up(){ln.dataset.s=p;dm.setAttribute('scale',ds.value);document.getElementById('dv').textContent=ds.value;place();bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
sc.addEventListener('pointermove',e=>{const r=sc.getBoundingClientRect();x=(e.clientX-r.left)*330/r.width;y=(e.clientY-r.top)*132/r.height;place();});
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));ds.addEventListener('input',up);up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Glass Refraction Lens — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sc{height:132px;border-radius:12px;position:relative;overflow:hidden;cursor:none;touch-action:none}.bg{position:absolute;left:0;top:0;width:330px;height:132px;background:repeating-linear-gradient(90deg,#f4c95d 0 18px,#2a6fdb 18px 36px),#fff}.bg::before{content:"";position:absolute;inset:0;background:radial-gradient(circle at 20% 40%,#ff5a5f 0 22px,transparent 23px),radial-gradient(circle at 62% 66%,#fff 0 26px,transparent 27px),radial-gradient(circle at 84% 28%,#2ec4b6 0 18px,transparent 19px)}.lens{position:absolute;width:var(--w);height:var(--h);margin:calc(var(--h)/-2) 0 0 calc(var(--w)/-2);overflow:hidden;box-shadow:inset 0 0 0 2px rgba(255,255,255,.85),0 8px 18px rgba(0,0,0,.3);pointer-events:none}.lens::after{content:"";position:absolute;inset:0;border-radius:inherit;background:radial-gradient(circle at 28% 24%,rgba(255,255,255,.55),transparent 38%)}.in{position:absolute;inset:0;filter:url(#rf);overflow:hidden;border-radius:inherit}.in .bg{transform-origin:var(--ox) var(--oy);transform:translate(var(--tx),var(--ty)) scale(1.25)}
.lens[data-s="0"]{--w:84px;--h:84px;border-radius:50%}.lens[data-s="1"]{--w:110px;--h:70px;border-radius:14px}.lens[data-s="2"]{--w:70px;--h:92px;border-radius:50% 50% 50% 50%/62% 62% 38% 38%}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<svg width="0" height="0" aria-hidden="true" focusable="false"><defs><filter id="rf" x="-10%" y="-10%" width="120%" height="120%"><feTurbulence type="fractalNoise" baseFrequency="0.018" numOctaves="2" seed="4" result="n"/><feDisplacementMap id="dm" in="SourceGraphic" in2="n" scale="24" xChannelSelector="R" yChannelSelector="G"/></filter></defs></svg><div class="bx"><div class="pre" id="pre" role="group" aria-label="Shape"><button data-p="0" aria-pressed="true">Circle</button><button data-p="1" aria-pressed="false">Pane</button><button data-p="2" aria-pressed="false">Droplet</button></div><div class="sc" id="sc"><div class="bg" id="b0"></div><div class="lens" id="ln" data-s="0"><div class="in"><div class="bg" id="b1"></div></div></div></div><label class="rw">Distortion <b id="dv">24</b><input id="ds" type="range" min="0" max="60" value="24"></label></div>
</div>
<script>
const sc=document.getElementById('sc'),ln=document.getElementById('ln'),dm=document.getElementById('dm'),ds=document.getElementById('ds'),bs=[...document.querySelectorAll('#pre button')];let p=0,x=165,y=66;
function place(){const w=[84,110,70][p],h=[84,70,92][p];ln.style.left=x+'px';ln.style.top=y+'px';const tx=-(x-w/2),ty=-(y-h/2);ln.style.setProperty('--tx',tx+'px');ln.style.setProperty('--ty',ty+'px');ln.style.setProperty('--ox',(x)+'px');ln.style.setProperty('--oy',(y)+'px');}
function up(){ln.dataset.s=p;dm.setAttribute('scale',ds.value);document.getElementById('dv').textContent=ds.value;place();bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
sc.addEventListener('pointermove',e=>{const r=sc.getBoundingClientRect();x=(e.clientX-r.left)*330/r.width;y=(e.clientY-r.top)*132/r.height;place();});
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));ds.addEventListener('input',up);up();
</script>
</body></html>