Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

fix(audit): ignore empty SVG clip intersections

Rudy Celekli0428751b2bd6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fe3a3ed..01001d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- Exclude invisible empty clip intersections from authored SVG bounds.
+
- `scripts/drive.mjs` now fails when a browser evaluation throws or rejects, rather than reporting a clean successful session.
- The arcade cabinet now accepts coins with reduced motion enabled, preserving the meter, credit and power-on events when the coin animation is skipped.
- The turning dial stops its idle tour when reduced motion is enabled while the page is open and cancels its pending automatic restart while preserving keyboard and pointer input.
diff --git a/kit/audit.mjs b/kit/audit.mjs
index 7fa64e1..879e75d 100644
--- a/kit/audit.mjs
+++ b/kit/audit.mjs
@@ -1454,7 +1454,11 @@ const boxOfPoints = (points, m) => {
return box;
};
const unionBox = (a, b) => (!a ? b : !b ? a : [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.max(a[2], b[2]), Math.max(a[3], b[3])]);
-const meetBox = (a, b) => (!a || !b ? a ?? null : [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3])]);
+const meetBox = (a, b) => {
+ if (!a || !b) return a ?? null;
+ const box = [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3])];
+ return box[0] <= box[2] && box[1] <= box[3] ? box : null;
+};
const areaOf = (box) => (box ? Math.max(0, box[2] - box[0]) * Math.max(0, box[3] - box[1]) : 0);
export function markupBox(svg) {
@@ -1463,7 +1467,9 @@ export function markupBox(svg) {
let clipId = null;
for (const [, close, tag, attrs, self] of tags) {
if (tag === "clipPath" && !close) {
- clipId = self ? null : (attrs.match(/id="([^"]*)"/) ?? [])[1] ?? null;
+ const id = (attrs.match(/id="([^"]*)"/) ?? [])[1] ?? null;
+ if (id) clips.set(id, null);
+ clipId = self ? null : id;
continue;
}
if (clipId) {
@@ -1485,7 +1491,9 @@ export function markupBox(svg) {
const m = multiply(top.m, matrixOf((attrs.match(/transform="([^"]*)"/) ?? [])[1]));
const ref = (attrs.match(/clip-path="url\(#([^)]*)\)"/) ?? [])[1];
const own = ref && clips.get(ref) ? boxOfPoints([[clips.get(ref)[0], clips.get(ref)[1]], [clips.get(ref)[2], clips.get(ref)[3]]], m) : null;
- const frame = { m, clip: own ? meetBox(top.clip ?? own, own) : top.clip, skip: top.skip || ["defs", "clipPath", "mask", "linearGradient", "radialGradient", "pattern", "text", "title"].includes(tag) || /class="[^"]*iso-halo/.test(attrs) };
+ const clip = own ? meetBox(top.clip ?? own, own) : top.clip;
+ const empty = ref && clips.has(ref) && (!own || areaOf(own) === 0 || (top.clip && (!clip || areaOf(clip) === 0)));
+ const frame = { m, clip, skip: Boolean(empty) || top.skip || ["defs", "clipPath", "mask", "linearGradient", "radialGradient", "pattern", "text", "title"].includes(tag) || /class="[^"]*iso-halo/.test(attrs) };
if (!frame.skip) {
const shape = boxOfPoints(pointsOfShape(tag, attrs), m);
if (shape) box = unionBox(box, frame.clip ? meetBox(shape, frame.clip) : shape);
diff --git a/scripts/audit-empty-clipping.test.mjs b/scripts/audit-empty-clipping.test.mjs
new file mode 100644
index 0000000..bce2863
--- /dev/null
+++ b/scripts/audit-empty-clipping.test.mjs
@@ -0,0 +1,38 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { promisify } from "node:util";
+import test from "node:test";
+import * as A from "../kit/audit.mjs";
+import { turning } from "../kit/turn-build.mjs";
+const small='<rect x="2" y="2" width="2" height="2"/>';
+const fixtures=[
+ ["fully disjoint primitive",'<defs><clipPath id="cut"><rect width="10" height="10"/></clipPath></defs><rect x="20" y="20" width="10" height="10" clip-path="url(#cut)"/>'+small],
+ ["nested disjoint clips",'<defs><clipPath id="one"><rect width="10" height="10"/></clipPath><clipPath id="two"><rect x="20" y="20" width="10" height="10"/></clipPath></defs><g clip-path="url(#one)"><g clip-path="url(#two)"><rect width="64" height="64"/></g></g>'+small],
+ ["zero area clip",'<defs><clipPath id="cut"><rect width="0" height="10"/></clipPath></defs><rect width="64" height="64" clip-path="url(#cut)"/>'+small],
+ ["empty declared clip",'<defs><clipPath id="cut"/></defs><rect width="64" height="64" clip-path="url(#cut)"/>'+small],
+ ["ordinary intersecting clip",'<defs><clipPath id="cut"><rect x="2" y="2" width="4" height="4"/></clipPath></defs><rect width="64" height="64" clip-path="url(#cut)"/>'],
+ ["ordinary visible primitives",small+'<rect x="10" y="10" width="4" height="4"/>'],
+ ["fully invisible clipped drawing",'<defs><clipPath id="cut"><rect width="10" height="10"/></clipPath></defs><rect x="20" y="20" width="10" height="10" clip-path="url(#cut)"/>'],
+];
+let pixels;const directory=await mkdtemp(join(tmpdir(),"anatomy-empty-clipping-"));
+try{
+ const page=join(directory,"figure.html");await writeFile(page,'<!doctype html><div class="iso-plate"></div>');
+ const expression=`(async()=>{const fixtures=${JSON.stringify(fixtures.map(([,m])=>m))};const result=[];for(const m of fixtures){const svg='<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">'+m+'</svg>';const image=new Image();await new Promise((resolve,reject)=>{image.onload=resolve;image.onerror=reject;image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg)});const c=document.createElement('canvas');c.width=c.height=64;const ctx=c.getContext('2d');ctx.drawImage(image,0,0);const data=ctx.getImageData(0,0,64,64).data;let box=null;for(let y=0;y<64;y++)for(let x=0;x<64;x++)if(data[(y*64+x)*4+3])box=box?[Math.min(box[0],x),Math.min(box[1],y),Math.max(box[2],x+1),Math.max(box[3],y+1)]:[x,y,x+1,y+1];result.push(box);}return result;})()`;
+ const result=await promisify(execFile)(process.execPath,["scripts/drive.mjs",page,JSON.stringify([["eval",expression]])],{timeout:30000});pixels=JSON.parse(result.stdout.match(/eval: (.*)/)[1]);assert.equal(pixels.length,fixtures.length);
+}finally{await rm(directory,{recursive:true,force:true});}
+const P={origin:[0,0],scale:1,azimuth:45,elevation:30};
+fixtures.forEach(([name,markup],i)=>test(`public clipped bounds match actual native SVG pixels for ${name}`,()=>{
+ assert.deepEqual(A.markupBox(markup),pixels[i]);
+ if(pixels[i]){const T=turning(P);T.layer("figure");const part=T.fixed({name:"fixed",svg:markup,anchor:[0,0,0],shapes:[A.ball([0,0,0],50)]});assert.deepEqual(part.spec.box,pixels[i].map((v,j)=>v+(j<2?-.5:.5)));}
+}));
+test("coverage accepts the true visible plane without an invisible clipped outlier",()=>{
+ const cam=A.cameraBasis(P),F={o:[0,0,0],a:cam.V,u:cam.R,v:cam.D};
+ const shape=A.prism(F,[[2,2],[4,2],[4,4],[2,4]].map(p=>p.map(v=>v/cam.k)),0,.01);
+ assert.deepEqual(A.coverage([{name:"plane",svg:fixtures[0][1],shapes:[shape]}],P),[]);
+});
+test("clipped visible line bounds preserve their degenerate axis",()=>{
+ assert.deepEqual(A.markupBox('<defs><clipPath id="cut"><rect width="10" height="10"/></clipPath></defs><line x1="4" y1="2" x2="4" y2="8" clip-path="url(#cut)"/>'),[4,2,4,8]);
+});