Generating 1:1 Foil-Compensated Stained Glass Patterns & AutoCAD R12 DXF Files in the Browser (Zero Dependencies)
If you run a regular image posterizer or AI art filter on a photo and call it a "stained glass pattern," anyone who has actually held a carbide glass cutter will spot three physical failures in five seconds: Thick bla
If you run a regular image posterizer or AI art filter on a photo and call it a "stained glass pattern," anyone who has actually held a carbide glass cutter will spot three physical failures in five seconds:
- Thick black strokes aren't seam allowances. Drawing a 2 px border around polygons looks like solder on screen, yet leaves every glass piece full-size. Once you wrap 60 pieces in copper foil, the finished panel grows by 4β8 mm and no longer fits inside its zinc or wooden frame.
- Microscopic slivers and deep concave hooks can't be hand-scored. A 4 mm glass shard snaps at the grinder, and an acute inside V-notch under 110Β° cracks straight across the waist unless you own a diamond ring saw.
- Raster PNGs and screen-DPI SVGs drift off scale when imported into vinyl plotters or LightBurn. Cricut Design Space, Silhouette Studio, and CAD tools expect exact physical millimetersβpreferably clean closed polylines in AutoCAD R12 DXF or millimeter-attributed SVG.
While building GlassDraft Studioβa free, browser-local stained glass pattern maker with no login or server uploadsβI had to solve all three geometry problems strictly in client-side TypeScript. Here are the three parts of the pipeline that mattered most.
1. Real Polygon Insetting vs. Visual Gap Strokes
In Tiffany copper-foil work, traditional pattern shears remove a strip roughly 0.80 mm (1/32") wide between adjacent piecesβmeaning each adjoining glass piece needs a 0.40 mm inward polygon inset from the shared centerline cartoon. Lead came uses a wider channel depending on the H-channel heart thickness (typically 1.60 mm / 1/16" standard heart or 1.80 mm / 5/64" heavy heart).
Instead of faking gaps with thicker strokes, we define explicit physical construction presets and inset every closed polygon loop by kerfMm / 2 before emitting PDF, SVG, or DXF coordinates:
export const CONSTRUCTION_ALLOWANCES = [
{ id: "centerline", mm: 0.0, insetMm: 0.0 }, // Exact uncut cartoon centerline
{ id: "copper-foil", mm: 0.8, insetMm: 0.4 }, // 1/32" copper foil pattern shears
{ id: "heavy-foil", mm: 1.2, insetMm: 0.6 }, // Thick ripple / textured glass
{ id: "lead-came-16", mm: 1.6, insetMm: 0.8 }, // 1/16" standard lead came heart
{ id: "lead-came", mm: 1.8, insetMm: 0.9 }, // 5/64" heavy lead came heart
] as const;
Keeping the shared centerline and the inset cut loop cleanly separated also ensures that a piece never overlaps its neighbor when exported to vinyl cutters (for adhesive stencil cutting) or printed as a tiled 1:1 US Letter / A4 PDF with a 1.00" / 50 mm calibration ruler.
2. Detecting Uncuttable Slivers & Deep Concave Cuts in Real Millimeters
Pixel thresholds break the moment a user switches panel width from a 6-inch suncatcher to a 24-inch window panel. Every cutability check has to run in physical millimeters after scaling the normalized geometry to widthInches * 25.4 by heightInches * 25.4:
-
Sliver detection (
< 10 mmclear width): Rather than checking bounding-box width (which gets fooled by 45Β° diagonal strips), compute the pole of inaccessibility (largest inscribed circle) inside each polygon. Twice the inscribed radius gives the exact clear cuttable width (minWidthMm). Anything below10 mm(~3/8") is flagged as a sliver warning or auto-merged before export. -
Concave inside-cut detection (
< 110Β°opening with> 3.0 mmpocket depth): Naive vertex-angle math misfires on 1 mm curve facets from marching-squares/Voronoi contours. We simplify the closed loop at0.72 mmtolerance first, then probe a fixed arc length backward and forward along the perimeter from every reflex vertex so smooth curves don't trigger false alarms:
function auditConcaveCutMm(
outerLoopMm: Point[],
hasInnerHole: boolean,
areaSqIn: number
) {
if (hasInnerHole) {
return {
risk: "high" as const,
reason: "Enclosed interior hole β add a break line or use a ring saw",
};
}
const simplified = simplifyClosedLoopMm(outerLoopMm, 0.72);
// Walk fixed arc-length probe distances forward/backward from each reflex vertex
// so < 2 mm curve facets never masquerade as deep inside notches.
// Flag pockets with opening angle < 110Β° and chord-to-vertex depth > 3.0 mm.
...
}
These checks are surfaced as advisory risk indicators in a pre-export verification bar (showing closed polygon count, active seam/inset millimeters, and how many pieces need manual review before printing or cutting glass).
3. Zero-Dependency 1:1 AutoCAD R12 DXF Export in TypeScript
You don't need a heavy CAD SDK to generate a clean, universally readable DXF file. Almost every CAM toolβAutoCAD, LightBurn, Silhouette Studio, CorelDRAW, and browser DXF parsersβreads AutoCAD R12 (AC1009) ASCII DXF reliably, as long as you get four details right:
-
Declare millimeters explicitly in
HEADER:$INSUNITS = 4(millimeters) and$MEASUREMENT = 1(metric), plus accurate$EXTMIN/$EXTMAX. -
Flip the Y-axis from screen coordinates to CAD Y-up:
dy = heightMm - pt.y(forgetting this mirrors the entire pattern verticallyβa disaster for asymmetric portraits and text). -
Deduplicate near-zero vertices before closing the loop: duplicate consecutive vertices (
<= 0.002 mm) cause open-contour errors in laser and CNC path optimizers. -
Separate fabrication layers cleanly:
CUT_PIECES(closedPOLYLINEwith flag70 = 1),OUTER_FRAME,PIECE_LABELS(TEXT), andJUMP_RINGS.
Here is the core closed-polyline emitter we use in production:
function pushDxfClosedPolyline(
lines: string[],
layer: string,
points: Array<{ x: number; y: number }>,
heightMm: number
): void {
if (points.length < 3) return;
const cleaned: Array<{ x: number; y: number }> = [];
for (const pt of points) {
const prev = cleaned[cleaned.length - 1];
if (!prev || Math.hypot(pt.x - prev.x, pt.y - prev.y) > 0.002) {
cleaned.push(pt);
}
}
if (
cleaned.length > 2 &&
Math.hypot(
cleaned[0].x - cleaned[cleaned.length - 1].x,
cleaned[0].y - cleaned[cleaned.length - 1].y
) <= 0.002
) {
cleaned.pop();
}
if (cleaned.length < 3) return;
// Group code 66 = 1 (vertices follow), 70 = 1 (closed polyline)
lines.push("0", "POLYLINE", "8", layer, "66", "1", "70", "1");
for (const pt of cleaned) {
lines.push(
"0", "VERTEX", "8", layer,
"10", pt.x.toFixed(4),
"20", (heightMm - pt.y).toFixed(4) // Flip screen Y-down to CAD Y-up
);
}
lines.push("0", "SEQEND", "8", layer);
}
When I cross-validated an 8" Γ 10" exported pattern from this emitter in an independent industrial DXF parser (LaserQuote Studio), the bounding box came back at 203.20 Γ 254.00 mm (0.00 mm drift), detectedUnit = "mm", and 0 open contours across rectangle, circle, and octagon panel frames.
Try It or Inspect the Exports
Everything runs locally in browser memoryβzero photo uploads, no account required, and free 1:1 tiled PDF, Cricut/Silhouette SVG, and AutoCAD R12 DXF exports:
-
Main Pattern Generator: GlassDraft Studio (
stainedglasspatterngenerator.com) - Vinyl Cutter & Vector SVG Workflow: SVG Stained Glass Pattern Generator
If you build browser-side CAD/CAM or craft tools and have run into tricky edge cases with polygon offsetting (especially sharp acute spikes clipping during inward offsets), I'd love to hear how you handle miter limits in pure TypeScript.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.