Resize an SVG design while optionally keeping its holes and slots at their original physical size. Built for laser-cut parts, mounting plates, slotted panels, tags, and the tools that edit them.
Try the interactive demo → · API reference · Report an issue
- Small: approximately 12 KB minified + gzipped, with a 13 KB budget enforced in CI.
- Independent: zero runtime dependencies. No DOM, canvas, framework, or WASM.
- Engraving-aware: cut and engrave layers stay distinct; outlined text scales with the design.
- Selective: preserve holes, whole slots, just slot width, or individual features.
- Real geometry: handles compound paths, nested transforms, rotated slots, and separate X/Y scale.
- Portable: TypeScript types, ESM, and CommonJS. Browser and Node.js 20+.
- Inspectable: detected features, output measurements, and conflict reports are part of the API.
The artwork above is generated by running this package against the example SVG. All three panels use the same physical drawing scale.
npm install @mylasertools/svg-smart-resizeimport { resizeSvg } from '@mylasertools/svg-smart-resize';
const result = resizeSvg(svgString, {
width: 180, // millimetres; omitted height follows the source aspect ratio
preserve: {
holes: true,
slots: 'width', // keep width, scale length/depth
},
});
console.log(result.svg); // the resized SVG, with physical dimensions
console.log(result.features); // measured output features
console.log(result.issues); // potential overlaps or escaped featuresFor a 120 mm panel resized to 180 mm:
| Dimension | Original | Ordinary resize | Smart resize |
|---|---|---|---|
| Panel width | 120 mm | 180 mm | 180 mm |
| Hole diameter | 6 mm | 9 mm | 6 mm |
| Slot width | 4 mm | 6 mm | 4 mm |
| Slot depth | 24 mm | 36 mm | 36 mm |
By default, everything scales normally. Set preserve.holes: true to keep detected holes. Set preserve.slots: true to keep the entire slot, or 'width' to preserve just its width. Source annotations can also request preservation.
// Make the whole design 25% smaller, keeping all hole and slot dimensions.
resizeSvg(svgString, {
scale: 0.75,
preserve: { holes: true, slots: true },
});
// Change the aspect ratio while keeping round holes round.
resizeSvg(svgString, {
width: 180,
height: 70,
preserve: { holes: true, slots: 'width' },
strict: true, // throw if a preserved feature no longer fits
});import { analyzeSvg, resizeSvg } from '@mylasertools/svg-smart-resize';
const analysis = analyzeSvg(svgString);
const mountingHole = analysis.features.find((f) => f.elementId === 'mount');
if (mountingHole) {
const result = resizeSvg(svgString, {
scale: 1.5,
preserve: { features: { [mountingHole.id]: 'all' } },
});
}Features have an ID, source element ID when available, kind, physical bounds, and center. Slots also have width, length, and axis. Use feature IDs from the same source string you resize.
You can mark geometry directly:
<!-- Explicitly identify a standalone hole and keep it fixed. -->
<circle id="mount" cx="12" cy="12" r="3"
data-resize="hole" data-preserve="all" />
<!-- Exclude a decorative closed contour from automatic detection. -->
<path data-resize="scale" d="..." />Per-feature options override data-preserve, which overrides the global holes/slots settings. Feature markers can be inherited from groups. Use data-resize="scale" on an engraving group to scale every descendant normally; child markers can override it. Marking a compound path applies to all its closed subpaths; use feature IDs when selecting just one.
The analyzer respects inherited data-operation="engrave" and data-operation="score" labels, excluding those layers from cut-feature detection. Filled, unstroked artwork inside a stroke-only part is also excluded by default; explicit data-operation="cut" or feature markers override this heuristic.
The analyzer converts geometry into millimetres, resolves transforms, and builds a contour containment tree. Odd nesting levels are treated as cutouts; enclosed rectangles are classified as slots. It also recognizes rectangular U-shaped openings on straight polygon outlines.
When resizing, the outer design and feature positions follow the requested scale. A preserved enclosed feature stays centered at its scaled position. An open slot stays anchored at its scaled mouth. In width-only mode, the slot's long axis scales with the design and its short axis remains perpendicular and the same width.
Output keeps Bézier curves and elliptical arcs as curves. Flattened samples are used only for detection and conflict checks.
This is a geometry tool for plain fabrication SVG, with explicit errors for unsupported input.
| Supported | Details |
|---|---|
| Geometry | path, circle, ellipse, rect, polygon, polyline, line |
| Paths | All standard path commands, compound paths, cubic/quadratic curves, elliptical arcs |
| Transforms | Nested groups, matrix, translate, scale, rotate, skew, root transforms |
| Units | Root mm, cm, in, px, pt, pc, q; unitless lengths use 96 dpi |
| Enclosed slots | Orthogonal rectangles and rounded rect elements, including rotated ones |
| Open slots | Rectangular U-shaped notches on closed outlines made entirely of straight segments |
| Finger joints | Box-style finger edges: depth stays the material thickness, spacing scales |
| Presentation | Basic fill/stroke attributes and drawing styles; title, description, metadata |
Detection uses geometry plus available fabrication intent and paint styles. Operation labels are preferred. Unlabelled stroke-only lettering can still resemble cutouts: mark its group with data-operation="engrave" or data-resize="scale". This is not a font-recognition algorithm. A standalone circle needs data-resize="hole" if there is no enclosing part outline.
Detected holes are hardware-like cutouts: at least 0.5 mm, no more than half the part's smaller side, convex, and point-symmetric (circles, ovals, rounded rectangles, hex nut traps). Detected slots are no wider than half the part. Large or irregular cutouts, such as the openings in decorative ornaments, scale with the design. Mark any other shape with data-resize="hole" to keep it fixed.
Text, use, nested SVGs, style sheets, percentage lengths, referenced resources, clipping, masks, filters, and active content are rejected. Export plain paths first. Curved slots are not detected; mark them with data-resize="hole" for whole-feature preservation. Width-only slot preservation requires the supported rectangular forms.
Stroke widths follow SVG transform behavior; preserving a cut path does not preserve its stroke width. Output dimensions and measurements are always in mm, irrespective of input or target units. The resized viewport is the source viewport scaled; it does not automatically crop to the drawing.
Conflict checks use sampled contours (default tolerance 0.02 mm). They flag preserved features escaping their containing outline, touching neighboring contours, or causing intersecting notch edges. They are advisory geometry checks, not a kerf, material thickness, or manufacturability solver. Overlapping/self-intersecting source contours and exact tangencies can make automatic classification ambiguous.
Read the full options, result schema, errors, and limits →
MIT © MyLaserTools.