Mask spotlight
A spotlight reveals a bright scene through an SVG mask or clipPath. Follow it with the pointer or the keys.
What you are seeing
A dark purple-grey scene hides a bright sunset landscape with a pale sun, jagged mountains, clouds and five small white gems. A round spotlight with a dashed magenta edge shows the bright scene wherever it goes. A counter at the top left reads gems found out of five.
How it works
The same scene is drawn twice. The bottom copy is covered by a dark layer. The top copy is hidden by a mask, and a white circle in the mask lets that part of the bright scene show through.
A mask uses brightness as opacity, so a radial gradient from white to black gives a soft edge. A clipPath is all or nothing, so it gives a hard edge, and it can use any shape, such as the circle, star or square offered here.
Five small gems are hidden around the scene. A counter notes each one the spotlight has uncovered.
Source
/lab/mask-spotlight/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Mask spotlight</title>
<meta name="description" content="A spotlight reveals a bright scene through an SVG mask or clipPath. Move the pointer or use the arrow keys.">
<link rel="stylesheet" href="../lab.css">
<style>
.ring{fill:rgba(212,0,106,.08);stroke:#d4006a;stroke-width:3;stroke-dasharray:6 5}
#svg:focus-visible{outline:3px solid var(--blue);outline-offset:-3px}
.gem{fill:#fff;stroke:#24272c;stroke-width:1.5}
</style>
</head>
<body>
<main class="demo">
<div class="stage">
<svg id="svg" class="drag-surface" viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" role="application" tabindex="0" aria-labelledby="t d">
<title id="t">Mask spotlight</title>
<desc id="d">A dark scene hides a bright sunset landscape with five small gems. A circular spotlight reveals the bright scene wherever it goes. Move the pointer, drag, or use the arrow keys to move it, and plus and minus to resize it. A counter shows how many gems have been found.</desc>
<defs>
<linearGradient id="sky" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#3a52e6"/><stop offset=".55" stop-color="#e45c9b"/><stop offset="1" stop-color="#ffc46b"/></linearGradient>
<radialGradient id="soft"><stop offset="0" stop-color="#fff"/><stop offset=".62" stop-color="#fff"/><stop offset="1" stop-color="#000"/></radialGradient>
<g id="scene">
<rect width="400" height="300" fill="url(#sky)"/>
<circle cx="285" cy="170" r="52" fill="#fff3b0"/>
<path d="M0 200 L70 120 L130 190 L200 100 L270 195 L330 140 L400 210 V300 H0Z" fill="#2d2a6b"/>
<path d="M0 250 Q100 200 200 245 T400 235 V300 H0Z" fill="#1c1a44"/>
<g fill="#fff" fill-opacity=".9"><ellipse cx="80" cy="60" rx="40" ry="12"/><ellipse cx="320" cy="50" rx="34" ry="10"/></g>
<g class="gem"><path d="M60 230 l10 -12 l10 12 l-10 10z"/><path d="M180 160 l9 -11 l9 11 l-9 9z"/><path d="M335 245 l10 -12 l10 12 l-10 10z"/><path d="M120 95 l8 -10 l8 10 l-8 8z"/><path d="M250 120 l9 -11 l9 11 l-9 9z"/></g>
</g>
<mask id="m" maskUnits="userSpaceOnUse" x="0" y="0" width="400" height="300">
<rect width="400" height="300" fill="#000"/>
<circle id="mc" r="70" fill="url(#soft)"/>
</mask>
<clipPath id="c"><circle id="cc" r="70"/><path id="cs" d=""/><rect id="cr"/></clipPath>
</defs>
<use href="#scene"/>
<rect width="400" height="300" fill="#24272c" fill-opacity=".8"/>
<g id="lit" mask="url(#m)"><use href="#scene"/></g>
<circle id="ring" class="ring" r="70" pointer-events="none"/>
</svg>
</div>
<div class="bar">
<label><span>Edge</span><select id="mode"><option value="mask">Soft mask</option><option value="circle">Hard clip, circle</option><option value="star">Hard clip, star</option><option value="square">Hard clip, square</option></select></label>
<label><span>Size</span><input id="size" type="range" min="30" max="150" value="70"></label>
<button id="auto" type="button">Pause</button>
<output id="found">Gems found: 0 of 5</output>
</div>
<p class="sr-only" id="live" aria-live="polite"></p>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/mask-spotlight/demo.js
(function () {
'use strict';
var $ = Lab.$, svg = $('#svg'), lit = $('#lit'), ring = $('#ring');
var gems = [[70, 228], [189, 158], [345, 243], [128, 93], [259, 118]], seen = {};
var x = 200, y = 150, r = 70, mode = 'mask', wander = false, t = 0;
function star(cx, cy, R) {
var d = '';
for (var i = 0; i < 10; i++) { var a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? R * 0.45 : R; d += (i ? 'L' : 'M') + (cx + rr * Math.cos(a)).toFixed(1) + ' ' + (cy + rr * Math.sin(a)).toFixed(1); }
return d + 'Z';
}
function apply() {
var mc = $('#mc'), cc = $('#cc'), cs = $('#cs'), cr = $('#cr');
mc.setAttribute('cx', x); mc.setAttribute('cy', y); mc.setAttribute('r', r);
cc.setAttribute('cx', x); cc.setAttribute('cy', y); cc.setAttribute('r', mode === 'circle' ? r : 0);
cs.setAttribute('d', mode === 'star' ? star(x, y, r * 1.15) : '');
cr.setAttribute('x', x - r * 0.85); cr.setAttribute('y', y - r * 0.85); cr.setAttribute('width', mode === 'square' ? r * 1.7 : 0); cr.setAttribute('height', mode === 'square' ? r * 1.7 : 0);
if (mode === 'mask') { lit.setAttribute('mask', 'url(#m)'); lit.removeAttribute('clip-path'); } else { lit.removeAttribute('mask'); lit.setAttribute('clip-path', 'url(#c)'); }
ring.setAttribute('cx', x); ring.setAttribute('cy', y); ring.setAttribute('r', mode === 'mask' ? r : 0);
var n = 0;
gems.forEach(function (g, i) { if (Math.hypot(g[0] - x, g[1] - y) < r * 0.55) { if (!seen[i]) { seen[i] = 1; Lab.say('Gem found.'); } } if (seen[i]) n++; });
$('#found').textContent = 'Gems found: ' + n + ' of 5';
}
function move(nx, ny) { x = Lab.clamp(nx, 0, 400); y = Lab.clamp(ny, 0, 300); apply(); }
function stop() { setAuto(false); }
function setAuto(on) { wander = on; $('#auto').textContent = on ? 'Pause' : 'Wander'; if (on) loop.start(); else loop.stop(); }
var loop = Lab.loop(function (dt) { t += dt * 0.6; x = 200 + Math.sin(t) * 150; y = 150 + Math.sin(t * 1.7 + 1) * 95; apply(); });
svg.addEventListener('pointermove', function (e) { if (e.pointerType === 'mouse' || e.buttons) { if (wander) stop(); var p = Lab.pt(svg, e); move(p.x, p.y); } });
svg.addEventListener('pointerdown', function (e) { stop(); var p = Lab.pt(svg, e); move(p.x, p.y); try { svg.setPointerCapture(e.pointerId); } catch (_) {} });
svg.addEventListener('keydown', function (e) {
var s = e.shiftKey ? 40 : 10, k = e.key;
if (k === 'ArrowLeft') move(x - s, y); else if (k === 'ArrowRight') move(x + s, y); else if (k === 'ArrowUp') move(x, y - s); else if (k === 'ArrowDown') move(x, y + s);
else if (k === '+' || k === '=') setR(r + 8); else if (k === '-' || k === '_') setR(r - 8); else return;
stop(); e.preventDefault();
});
function setR(v) { r = Lab.clamp(v, 30, 150); $('#size').value = r; apply(); }
$('#size').addEventListener('input', function (e) { r = +e.target.value; apply(); });
$('#mode').addEventListener('change', function (e) { mode = e.target.value; apply(); });
$('#auto').addEventListener('click', function () { setAuto(!wander); });
// Full-bleed on wide stages, whole scene on tall ones, so a phone never crops most of the picture away.
function fit() { var b = svg.getBoundingClientRect(); svg.setAttribute('preserveAspectRatio', b.width / b.height < 1 ? 'xMidYMid meet' : 'xMidYMid slice'); }
new ResizeObserver(fit).observe(svg); fit();
apply();
if (Lab.reduced) setAuto(false); else setAuto(true);
})();
/lab/lab.js
/* Small shared helpers for the SVG Wow! lab demos. No dependencies. */
(function () {
'use strict';
var NS = 'http://www.w3.org/2000/svg';
var mq = window.matchMedia('(prefers-reduced-motion: reduce)');
var Lab = (window.Lab = { NS: NS });
Object.defineProperty(Lab, 'reduced', { get: function () { return mq.matches; } });
Lab.$ = function (s, r) { return (r || document).querySelector(s); };
Lab.$$ = function (s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); };
Lab.clamp = function (v, a, b) { return Math.max(a, Math.min(b, v)); };
Lab.lerp = function (a, b, t) { return a + (b - a) * t; };
Lab.ease = function (t) { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; };
Lab.make = function (tag, attrs, parent) {
var e = document.createElementNS(NS, tag);
if (attrs) for (var k in attrs) e.setAttribute(k, attrs[k]);
if (parent) parent.appendChild(e);
return e;
};
/* Client point to the user space of `el` (any SVG graphics element). */
Lab.pt = function (el, evt) {
var m = el.getScreenCTM();
if (!m) return { x: 0, y: 0 };
var p = new DOMPoint(evt.clientX, evt.clientY).matrixTransform(m.inverse());
return { x: p.x, y: p.y };
};
/* requestAnimationFrame loop that pauses while the document is hidden. */
Lab.loop = function (fn) {
var raf = 0, last = 0, running = false, wanted = false, self = {};
function tick(t) {
if (!running) return;
raf = requestAnimationFrame(tick);
var dt = last ? Math.min((t - last) / 1000, 0.1) : 0;
last = t;
fn(dt, t / 1000);
}
function go() { if (running || document.hidden || !wanted) return; running = true; last = 0; raf = requestAnimationFrame(tick); }
function halt() { running = false; cancelAnimationFrame(raf); }
self.start = function () { wanted = true; go(); };
self.stop = function () { wanted = false; halt(); };
self.running = function () { return wanted; };
document.addEventListener('visibilitychange', function () { if (document.hidden) halt(); else go(); });
return self;
};
/* Pointer drag with capture. space: element whose user space the points use. */
Lab.drag = function (el, o) {
var active = false, space = o.space || el.ownerSVGElement || el;
el.addEventListener('pointerdown', function (e) {
if (e.button > 0) return;
active = true;
try { el.setPointerCapture(e.pointerId); } catch (_) {}
if (o.down) o.down(Lab.pt(space, e), e);
e.preventDefault();
});
el.addEventListener('pointermove', function (e) { if (active) o.move(Lab.pt(space, e), e); });
function end(e) {
if (!active) return;
active = false;
try { el.releasePointerCapture(e.pointerId); } catch (_) {}
if (o.up) o.up(e);
}
el.addEventListener('pointerup', end);
el.addEventListener('pointercancel', end);
};
Lab.say = function (msg) {
var l = document.getElementById('live');
if (l) l.textContent = msg;
};
})();
/lab/lab.css
:root{--bg:#f1f2f4;--ink:#24272c;--blue:#1f3fe0;--mag:#d4006a;--line:#c9ccd3;--panel:#fff;--muted:#555b66;font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);overflow-x:hidden}
body{font-size:13px;line-height:1.3}
.demo{display:flex;flex-direction:column;height:100%;min-height:0}
.stage{flex:1 1 auto;min-height:120px;position:relative;overflow:hidden;background:var(--bg)}
.stage>svg{position:absolute;left:0;top:0;width:100%;height:100%;display:block}
.bar{flex:0 0 auto;display:flex;flex-wrap:wrap;gap:6px 14px;align-items:center;padding:6px 10px;background:var(--panel);border-top:1px solid var(--line);max-height:46%;overflow:auto}
.bar .grp{display:flex;flex-wrap:wrap;align-items:center;gap:6px 10px}
label{display:inline-flex;align-items:center;gap:6px;white-space:nowrap}
label>span,.dim{color:var(--muted)}
output{font-variant-numeric:tabular-nums;min-width:3.2ch;display:inline-block}
button,select,input[type=number],input[type=text]{font:inherit;color:var(--ink);background:#fff;border:1px solid #8a909b;border-radius:6px;padding:3px 8px;min-height:28px}
button{border-color:var(--ink);cursor:pointer}
button:hover{background:#e7e9ef}
button[aria-pressed=true]{background:var(--ink);color:#fff}
input[type=range]{width:110px;accent-color:var(--blue);margin:0}
input[type=color]{width:34px;height:28px;padding:1px;border:1px solid #8a909b;border-radius:6px;background:#fff}
input[type=checkbox]{accent-color:var(--blue);width:16px;height:16px}
:focus-visible{outline:3px solid var(--blue);outline-offset:2px}
.grab{cursor:grab;touch-action:none}
.grab:active{cursor:grabbing}
.grab:focus-visible{outline:none;stroke:var(--blue);stroke-width:4px}
.drag-surface{touch-action:none}
.ro{font-size:11px;color:var(--muted)}
.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
svg text{font-family:inherit}
pre,textarea{font:11px/1.35 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
@media (pointer:coarse){button,select,input[type=number]{min-height:38px}input[type=range]{height:30px}}
@media (max-width:420px){.bar{gap:6px 10px;padding:6px 8px}input[type=range]{width:96px}}
@media (min-width:700px){.demo.split{flex-direction:row}.demo.split .stage{flex:1 1 auto}.demo.split .bar{width:320px;max-height:none;flex:0 0 320px;border-top:0;border-left:1px solid var(--line);align-content:flex-start}}