Pattern forge
Generate seeded geometric patterns from simple motifs and copy them as standalone SVG code.
What you are seeing
A full-bleed generated pattern of simple shapes laid out in a regular grid in a limited palette, such as blue and charcoal on light grey. A bar of controls sits below, with a collapsible text box showing the matching SVG code.
How it works
A pattern here is a grid of cells, and each cell gets one small shape. A seeded random number generator decides size, colour and rotation, so the same seed and settings always give the same picture.
Motifs are plain SVG: circles, rotated squares, triangles, truchet-style quarter arcs and diagonal strokes. The palettes are small on purpose, because limited colour is what makes generated patterns look designed.
Copy SVG writes a complete standalone file to the clipboard, with the xmlns attribute and a viewBox, ready to paste into an editor. If clipboard access is blocked, the code is shown and selected instead.
Source
/lab/pattern-forge/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Pattern forge</title>
<meta name="description" content="Generate seeded SVG patterns from simple motifs and copy the result as standalone SVG code.">
<link rel="stylesheet" href="../lab.css">
<style>
details{flex:1 1 100%}
summary{cursor:pointer;color:var(--muted)}
textarea{width:100%;height:84px;margin-top:6px;border:1px solid #8a909b;border-radius:6px;padding:6px;resize:vertical}
input[type=number]{width:72px}
</style>
</head>
<body>
<main class="demo">
<div class="stage">
<svg id="svg" viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" role="img" aria-labelledby="t d">
<title id="t">Pattern forge</title>
<desc id="d">A generated tiled pattern of simple shapes in a limited palette. The motif, grid size, palette, spacing and seed are chosen with the controls, and the same settings always produce the same pattern.</desc>
<rect id="bg" width="400" height="300" fill="#f1f2f4"/>
<g id="out"></g>
</svg>
</div>
<div class="bar">
<label><span>Motif</span><select id="motif"><option>Circles</option><option>Squares</option><option>Triangles</option><option>Arcs</option><option>Lines</option><option>Mixed</option></select></label>
<label><span>Palette</span><select id="pal"><option>Cobalt and charcoal</option><option>Charcoal</option><option>Cobalt on blue</option><option>Magenta and cobalt</option></select></label>
<label><span>Grid</span><input id="cols" type="range" min="3" max="20" value="9"></label>
<label><span>Variation</span><input id="vary" type="range" min="0" max="100" value="60"></label>
<label><span>Seed</span><input id="seed" type="number" min="0" max="99999" value="12"></label>
<button id="rand" type="button">Randomize</button>
<label><input id="auto" type="checkbox"> <span>Auto</span></label>
<button id="copy" type="button">Copy SVG</button>
<span id="status" class="ro" role="status"></span>
<details id="det"><summary>SVG code</summary><textarea id="code" readonly aria-label="Generated SVG code"></textarea></details>
</div>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/pattern-forge/demo.js
(function () {
'use strict';
var $ = Lab.$;
var palettes = {
'Cobalt and charcoal': { bg: '#f1f2f4', ink: ['#1f3fe0', '#24272c', '#8ea0f2'] },
'Charcoal': { bg: '#f1f2f4', ink: ['#24272c', '#555b66', '#a7adb8'] },
'Cobalt on blue': { bg: '#1f3fe0', ink: ['#ffffff', '#8ea0f2', '#24272c'] },
'Magenta and cobalt': { bg: '#ffffff', ink: ['#d4006a', '#24272c', '#1f3fe0'] }
};
function rng(a) { return function () { a |= 0; a = a + 0x6D2B79F5 | 0; var t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; }
var n = function (v) { return +v.toFixed(1); };
function generate() {
var cols = +$('#cols').value, v = $('#vary').value / 100, motif = $('#motif').value, pal = palettes[$('#pal').value], rnd = rng(+$('#seed').value || 0);
var cell = 400 / cols, rows = Math.ceil(300 / cell), out = [];
for (var j = 0; j < rows; j++) for (var i = 0; i < cols; i++) {
var cx = n((i + 0.5) * cell), cy = n((j + 0.5) * cell), col = pal.ink[Math.floor(rnd() * pal.ink.length)];
var kind = motif === 'Mixed' ? ['Circles', 'Squares', 'Triangles', 'Arcs', 'Lines'][Math.floor(rnd() * 5)] : motif;
var size = cell * (0.8 - 0.45 * v * rnd()) / 2, rot = 90 * Math.floor(rnd() * 4 * v) , tr = ' transform="translate(' + cx + ' ' + cy + ') rotate(' + rot + ')"';
var s = n(size), h = n(cell / 2);
if (kind === 'Circles') out.push('<circle cx="' + cx + '" cy="' + cy + '" r="' + s + '" fill="' + col + '"/>');
else if (kind === 'Squares') out.push('<rect x="' + (-s) + '" y="' + (-s) + '" width="' + n(2 * s) + '" height="' + n(2 * s) + '" fill="' + col + '"' + tr + '/>');
else if (kind === 'Triangles') out.push('<path d="M' + (-s) + ' ' + s + 'L' + s + ' ' + s + 'L' + (rnd() < 0.5 ? -s : s) + ' ' + (-s) + 'Z" fill="' + col + '"' + tr + '/>');
else if (kind === 'Arcs') out.push('<path d="M' + (-h) + ' 0A' + h + ' ' + h + ' 0 0 0 0 ' + h + 'M0 ' + (-h) + 'A' + h + ' ' + h + ' 0 0 0 ' + h + ' 0" fill="none" stroke="' + col + '" stroke-width="' + n(cell * 0.18) + '" stroke-linecap="round"' + tr + '/>');
else out.push('<path d="M' + (-s) + ' ' + (-s) + 'L' + s + ' ' + s + '" stroke="' + col + '" stroke-width="' + n(cell * 0.2) + '" stroke-linecap="round"' + tr + '/>');
}
return { bg: pal.bg, body: out.join('') };
}
function standalone(g) {
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 300" width="400" height="300" role="img" aria-label="Generated pattern">\n<rect width="400" height="300" fill="' + g.bg + '"/>\n' + g.body.replace(/\/>/g, '/>\n') + '</svg>';
}
function render() {
var g = generate();
$('#bg').setAttribute('fill', g.bg);
$('#out').innerHTML = g.body;
$('#code').value = standalone(g);
}
var status = $('#status');
function say(m) { status.textContent = m; clearTimeout(say.t); say.t = setTimeout(function () { status.textContent = ''; }, 3500); }
function fallback() {
var d = $('#det'), ta = $('#code'); d.open = true; ta.focus(); ta.select();
var ok = false; try { ok = document.execCommand('copy'); } catch (_) {}
say(ok ? 'Copied SVG code.' : 'Copy is blocked here. The code is selected, press Ctrl or Cmd plus C.');
}
$('#copy').addEventListener('click', function () {
var text = $('#code').value;
if (navigator.clipboard && navigator.clipboard.writeText) navigator.clipboard.writeText(text).then(function () { say('Copied SVG code.'); }, fallback);
else fallback();
});
var auto = $('#auto'), acc = 0;
var loop = Lab.loop(function (dt) { acc += dt; if (acc > 3.5) { acc = 0; $('#seed').value = Math.floor(Math.random() * 99999); render(); } });
function manual() { if (auto.checked) { auto.checked = false; loop.stop(); } render(); }
['motif', 'pal', 'cols', 'vary', 'seed'].forEach(function (id) { $('#' + id).addEventListener('input', manual); });
$('#rand').addEventListener('click', function () { $('#seed').value = Math.floor(Math.random() * 99999); manual(); });
auto.addEventListener('change', function () { if (auto.checked) loop.start(); else loop.stop(); });
render();
if (!Lab.reduced) { auto.checked = true; loop.start(); }
})();
/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}}