Property inspector
Select SVG shapes, edit their attributes in a panel and see the markup update as you type.
What you are seeing
A white canvas holds a blue rounded rectangle, a charcoal circle, a pale blue ellipse and a grey polygon. A dashed magenta box surrounds the selected shape. Beside or below it, a panel lists the shape attributes as form fields with a dark box of live SVG markup underneath.
How it works
SVG shapes are described by plain attributes, and changing an attribute with setAttribute redraws the shape at once. The panel builds one input for each geometry attribute of the selected element, plus fill, stroke, stroke width and opacity.
A dashed magenta box is drawn from getBBox, which returns the geometry of the shape before any stroke is added, so the script pads it by half the stroke width.
The markup box rebuilds the start tag from the current attribute values, which shows how the picture and the code are the same thing.
Source
/lab/property-inspector/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Property inspector</title>
<meta name="description" content="Select SVG shapes, edit their attributes and watch the markup change.">
<link rel="stylesheet" href="../lab.css">
<style>
#sel{fill:none;stroke:#d4006a;stroke-width:2;stroke-dasharray:5 4;pointer-events:none}
.shape{cursor:grab;touch-action:none}
.shape:focus-visible{outline:none}
.fields{display:flex;flex-wrap:wrap;gap:6px 10px;flex:1 1 100%}
.fields input[type=number]{width:62px}
.fields input[type=text]{width:100%;min-width:180px}
pre{flex:1 1 100%;margin:0;padding:6px 8px;background:#24272c;color:#e8eaf0;border-radius:6px;white-space:pre-wrap;word-break:break-all;min-height:44px}
.none{color:#b9bec8}
</style>
</head>
<body>
<main class="demo split">
<div class="stage">
<svg id="svg" class="drag-surface" viewBox="0 0 400 300" role="group" aria-labelledby="t d">
<title id="t">Property inspector</title>
<desc id="d">A canvas with four editable shapes: a blue rounded rectangle, a charcoal circle, a light blue ellipse and a grey polygon. Selecting a shape draws a dashed magenta box around it and lists its attributes in the panel with the live markup underneath.</desc>
<rect id="bg" width="400" height="300" fill="#fff"/>
<g id="scene">
<rect id="rect1" class="shape" x="40" y="50" width="130" height="90" rx="14" fill="#1f3fe0" stroke="#24272c" stroke-width="3" opacity="1" tabindex="0" role="button" aria-label="Rectangle"/>
<circle id="circle1" class="shape" cx="280" cy="100" r="50" fill="#24272c" stroke="#24272c" stroke-width="3" opacity="1" tabindex="0" role="button" aria-label="Circle"/>
<ellipse id="ellipse1" class="shape" cx="110" cy="230" rx="70" ry="38" fill="#9fb0f6" stroke="#1f3fe0" stroke-width="3" opacity="1" tabindex="0" role="button" aria-label="Ellipse"/>
<polygon id="polygon1" class="shape" points="250,200 330,190 360,250 290,275 240,250" fill="#a7adb8" stroke="#24272c" stroke-width="3" opacity="1" tabindex="0" role="button" aria-label="Polygon"/>
</g>
<rect id="sel" x="0" y="0" width="0" height="0" visibility="hidden"/>
</svg>
</div>
<div class="bar">
<output id="name" class="dim">Select a shape</output>
<pre id="markup" aria-label="Live markup"></pre>
<div class="fields" id="fields"></div>
<button id="reset" type="button">Reset shapes</button>
</div>
</main>
<script src="../lab.js"></script>
<script src="demo.js"></script>
</body>
</html>
/lab/property-inspector/demo.js
(function () {
'use strict';
var $ = Lab.$, svg = $('#svg'), scene = $('#scene'), box = $('#sel'), fields = $('#fields'), markup = $('#markup');
var geo = { rect: ['x', 'y', 'width', 'height', 'rx'], circle: ['cx', 'cy', 'r'], ellipse: ['cx', 'cy', 'rx', 'ry'], polygon: ['points'] };
var order = ['id', 'x', 'y', 'width', 'height', 'rx', 'cx', 'cy', 'r', 'ry', 'points', 'fill', 'stroke', 'stroke-width', 'opacity'];
var initial = scene.innerHTML, cur = null;
function bind() {
Lab.$$('.shape', scene).forEach(function (el) {
var o;
el.addEventListener('focus', function () { choose(el); });
Lab.drag(el, {
space: svg,
down: function (p) { choose(el); o = { p: p, a: snap(el) }; },
move: function (p) { shift(el, o.a, p.x - o.p.x, p.y - o.p.y); update(); }
});
el.addEventListener('keydown', function (e) {
var s = e.shiftKey ? 16 : 4, d = { ArrowLeft: [-s, 0], ArrowRight: [s, 0], ArrowUp: [0, -s], ArrowDown: [0, s] }[e.key];
if (!d) return;
shift(el, snap(el), d[0], d[1]); syncFields(); update(); e.preventDefault();
});
});
}
function snap(el) {
var t = el.localName, o = {};
if (t === 'polygon') o.points = el.getAttribute('points');
else (t === 'rect' ? ['x', 'y'] : ['cx', 'cy']).forEach(function (n) { o[n] = +el.getAttribute(n); });
return o;
}
function shift(el, a, dx, dy) {
var t = el.localName;
if (t === 'polygon') {
el.setAttribute('points', a.points.trim().split(/\s+/).map(function (pr) { var q = pr.split(',').map(Number); return Math.round(q[0] + dx) + ',' + Math.round(q[1] + dy); }).join(' '));
} else {
var k = t === 'rect' ? ['x', 'y'] : ['cx', 'cy'];
el.setAttribute(k[0], Math.round(a[k[0]] + dx)); el.setAttribute(k[1], Math.round(a[k[1]] + dy));
}
syncFields();
}
function syncFields() {
Lab.$$('input[data-attr]', fields).forEach(function (i) { if (cur && document.activeElement !== i) i.value = cur.getAttribute(i.dataset.attr) || ''; });
}
function field(label, node) {
var l = document.createElement('label'), s = document.createElement('span');
s.textContent = label; l.appendChild(s); l.appendChild(node); fields.appendChild(l);
}
function input(type, attr, el, extra) {
var i = document.createElement('input'); i.type = type; i.dataset.attr = attr;
if (extra) for (var k in extra) i.setAttribute(k, extra[k]);
i.value = el.getAttribute(attr) || '';
i.addEventListener('input', function () { el.setAttribute(attr, i.value); update(); });
return i;
}
function choose(el) {
if (cur === el) return;
cur = el; fields.innerHTML = '';
$('#name').textContent = '<' + el.localName + ' id="' + el.id + '">'; $('#name').className = '';
geo[el.localName].forEach(function (a) {
if (a === 'points') field('points', input('text', 'points', el, { 'aria-label': 'points' }));
else field(a, input('number', a, el, { step: 1, 'aria-label': a }));
});
field('fill', input('color', 'fill', el)); field('stroke', input('color', 'stroke', el));
field('stroke-width', input('number', 'stroke-width', el, { min: 0, max: 30, step: 1 }));
field('opacity', input('range', 'opacity', el, { min: 0.1, max: 1, step: 0.05 }));
update();
}
function update() {
if (!cur) { markup.innerHTML = '<span class="none">Click a shape, or press Tab to focus one.</span>'; box.setAttribute('visibility', 'hidden'); return; }
var attrs = order.filter(function (n) { return cur.hasAttribute(n); }).map(function (n) { return n + '="' + cur.getAttribute(n) + '"'; });
markup.textContent = '<' + cur.localName + '\n ' + attrs.join('\n ') + '/>';
var b = cur.getBBox(), sw = (+cur.getAttribute('stroke-width') || 0) / 2 + 4;
box.setAttribute('x', b.x - sw); box.setAttribute('y', b.y - sw); box.setAttribute('width', b.width + 2 * sw); box.setAttribute('height', b.height + 2 * sw); box.setAttribute('visibility', 'visible');
}
function clear() { cur = null; fields.innerHTML = ''; $('#name').textContent = 'Select a shape'; $('#name').className = 'dim'; update(); }
$('#bg').addEventListener('pointerdown', clear);
$('#reset').addEventListener('click', function () { scene.innerHTML = initial; bind(); clear(); });
bind(); clear();
choose($('#rect1'));
})();
/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}}