Live pointillism
A generated picture rebuilt from thousands of SVG dots, with an optional camera mode that never leaves your browser.
What you are seeing
A picture made entirely of round dots on a plain ground, showing a sunset sky with a pale sun, dark hills, a bouncing magenta ball and a drifting cloud. A status line below explains whether the camera is off, on, denied or missing.
How it works
A hidden canvas the size of the dot grid draws a moving picture. Each pixel becomes one SVG circle, whose colour and radius come from that pixel. At 64 by 48 that is 3,072 dots, and only attributes that changed are rewritten each frame.
The three styles reuse the same data. Colour dots keep the colour, ink on paper makes dark pixels large, and glow on dark makes bright pixels large, which is the idea behind halftone printing.
The camera is off until you press Use my camera. Frames are read into the same hidden canvas and never saved or sent anywhere, and Stop camera ends every track. If permission is denied or no camera exists, a message says so and the generated picture carries on.
Source
/lab/live-pointillism/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Live pointillism</title>
<meta name="description" content="A generated picture rebuilt from thousands of SVG dots, with an optional camera mode that stays in your browser.">
<link rel="stylesheet" href="../lab.css">
<style>
#cam{position:absolute;width:2px;height:2px;opacity:0;pointer-events:none}
#status{flex:1 1 100%;color:var(--muted);font-size:12px}
</style>
</head>
<body>
<main class="demo">
<div class="stage">
<svg id="svg" viewBox="0 0 400 300" preserveAspectRatio="xMidYMid meet" role="img" aria-labelledby="t d">
<title id="t">Live pointillism</title>
<desc id="d">A moving picture of a sun setting over hills with a bouncing ball, drawn as a grid of several thousand round dots. Each dot takes its colour and size from the picture underneath. An optional camera mode draws the live camera image the same way.</desc>
<rect id="bg" width="400" height="300" fill="#f1f2f4"/>
<g id="dots"></g>
</svg>
<video id="cam" muted playsinline aria-hidden="true"></video>
</div>
<div class="bar">
<button id="play" type="button">Pause</button>
<label><span>Dots</span><select id="dens"><option value="40">1,200</option><option value="64" selected>3,072</option><option value="88">5,808</option></select></label>
<label><span>Style</span><select id="style"><option value="color">Colour dots</option><option value="ink">Ink on paper</option><option value="glow">Glow on dark</option></select></label>
<button id="camOn" type="button">Use my camera</button>
<button id="camOff" type="button" disabled>Stop camera</button>
<p id="status" role="status">Camera is off. If you turn it on, frames stay in this page and nothing is stored or sent.</p>
</div>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/live-pointillism/demo.js
(function () {
'use strict';
var $ = Lab.$, dots = $('#dots'), bg = $('#bg'), video = $('#cam'), playBtn = $('#play'), status = $('#status');
var cols = 64, rows = 48, cell = 6.25, circles = [], last = [], canvas = document.createElement('canvas'), ctx = canvas.getContext('2d', { willReadFrequently: true });
var stream = null, source = 'generated', t = 0, playing = false, acc = 0;
function build() {
cols = +$('#dens').value; rows = Math.round(cols * 3 / 4); cell = 400 / cols;
canvas.width = cols; canvas.height = rows;
dots.textContent = ''; circles = []; last = [];
var frag = document.createDocumentFragment();
for (var j = 0; j < rows; j++) for (var i = 0; i < cols; i++) {
var c = document.createElementNS(Lab.NS, 'circle');
c.setAttribute('cx', ((i + 0.5) * cell).toFixed(2)); c.setAttribute('cy', ((j + 0.5) * cell).toFixed(2)); c.setAttribute('r', 0);
frag.appendChild(c); circles.push(c); last.push('');
}
dots.appendChild(frag);
}
// Procedural picture, drawn straight at dot resolution (the canvas is cols by rows pixels).
function paint() {
var w = cols, h = rows, k = w / 320;
ctx.save(); ctx.scale(k, k);
var g = ctx.createLinearGradient(0, 0, 0, 240); g.addColorStop(0, '#2f4be0'); g.addColorStop(0.6, '#e8639f'); g.addColorStop(1, '#ffc46b');
ctx.fillStyle = g; ctx.fillRect(0, 0, 320, 240);
var sy = 120 + Math.sin(t * 0.5) * 22;
ctx.fillStyle = '#fff3b0'; ctx.beginPath(); ctx.arc(220, sy, 40, 0, 7); ctx.fill();
ctx.fillStyle = '#2d2a6b'; ctx.beginPath(); ctx.moveTo(0, 190); ctx.quadraticCurveTo(80, 120, 160, 190); ctx.quadraticCurveTo(240, 130, 320, 185); ctx.lineTo(320, 240); ctx.lineTo(0, 240); ctx.fill();
ctx.fillStyle = '#1c1a44'; ctx.beginPath(); ctx.moveTo(0, 215); ctx.quadraticCurveTo(160, 180, 320, 212); ctx.lineTo(320, 240); ctx.lineTo(0, 240); ctx.fill();
var bx = 160 + Math.sin(t * 0.9) * 110, by = 130 - Math.abs(Math.sin(t * 1.8)) * 80;
ctx.fillStyle = '#d4006a'; ctx.beginPath(); ctx.arc(bx, by, 17, 0, 7); ctx.fill();
ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.ellipse(70 + ((t * 8) % 340) - 20, 50, 34, 11, 0, 0, 7); ctx.fill();
ctx.restore();
}
function paintCamera() {
var vw = video.videoWidth, vh = video.videoHeight;
if (!vw || !vh) return false;
var sw = vw, sh = vw * 3 / 4;
if (sh > vh) { sh = vh; sw = vh * 4 / 3; }
ctx.save(); ctx.translate(cols, 0); ctx.scale(-1, 1);
ctx.drawImage(video, (vw - sw) / 2, (vh - sh) / 2, sw, sh, 0, 0, cols, rows);
ctx.restore(); return true;
}
function render() {
var style = $('#style').value;
bg.setAttribute('fill', style === 'glow' ? '#24272c' : style === 'ink' ? '#f4f1ea' : '#f1f2f4');
if (source === 'camera') { if (!paintCamera()) paint(); } else paint();
var px = ctx.getImageData(0, 0, cols, rows).data, max = cell * 0.56;
for (var i = 0; i < circles.length; i++) {
var r = px[i * 4], g = px[i * 4 + 1], b = px[i * 4 + 2], lum = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255, rad, fill;
if (style === 'color') { rad = max * (0.62 + 0.38 * lum); fill = 'rgb(' + r + ',' + g + ',' + b + ')'; }
else if (style === 'ink') { rad = max * (1 - lum) * 1.05; fill = '#24272c'; }
else { rad = max * lum * 1.05; fill = 'rgb(' + Math.min(255, r * 1.15 | 0) + ',' + Math.min(255, g * 1.15 | 0) + ',' + Math.min(255, b * 1.15 | 0) + ')'; }
var key = rad.toFixed(1) + fill;
if (last[i] !== key) { last[i] = key; circles[i].setAttribute('r', rad.toFixed(2)); circles[i].setAttribute('fill', fill); }
}
}
var loop = Lab.loop(function (dt) { t += dt; acc += dt; if (acc >= 1 / 14) { acc = 0; render(); } });
function setPlay(on) { playing = on; playBtn.textContent = on ? 'Pause' : 'Play'; if (on) loop.start(); else loop.stop(); }
function say(m) { status.textContent = m; }
function startCam() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { say('This browser or page cannot use a camera, so the generated picture stays on.'); return; }
$('#camOn').disabled = true; say('Waiting for camera permission.');
navigator.mediaDevices.getUserMedia({ video: { width: { ideal: 320 }, height: { ideal: 240 }, facingMode: 'user' }, audio: false }).then(function (s) {
stream = s; video.srcObject = s;
return video.play();
}).then(function () {
source = 'camera'; $('#camOff').disabled = false;
say('Camera is on. Frames are processed in this page only and nothing is stored or sent. Press Stop camera to end it.');
setPlay(true);
}).catch(function (e) {
var n = e && e.name;
if (stream) { stream.getTracks().forEach(function (tr) { tr.stop(); }); stream = null; }
$('#camOn').disabled = false; source = 'generated';
if (n === 'NotAllowedError' || n === 'SecurityError' || n === 'PermissionDeniedError') say('Camera permission was denied, so the generated picture stays on. You can allow the camera in your browser settings and try again.');
else if (n === 'NotFoundError' || n === 'DevicesNotFoundError' || n === 'OverconstrainedError') say('No camera was found on this device, so the generated picture stays on.');
else say('The camera could not be started (' + (n || 'unknown error') + '), so the generated picture stays on.');
});
}
function stopCam() {
if (stream) stream.getTracks().forEach(function (tr) { tr.stop(); });
stream = null; video.srcObject = null; source = 'generated';
$('#camOn').disabled = false; $('#camOff').disabled = true;
say('Camera stopped. Nothing was recorded.');
}
$('#camOn').addEventListener('click', startCam);
$('#camOff').addEventListener('click', stopCam);
window.addEventListener('pagehide', function () { if (stream) stopCam(); });
playBtn.addEventListener('click', function () { setPlay(!playing); });
$('#dens').addEventListener('change', function () { build(); render(); });
$('#style').addEventListener('change', render);
build(); render();
if (Lab.reduced) setPlay(false); else setPlay(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}}