Menu

How to Auto-Trim Invisible Whitespace From SVG ViewBoxes in Pure TypeScript
πŸ“°

How to Auto-Trim Invisible Whitespace From SVG ViewBoxes in Pure TypeScript

DEV Community: frontendΒ·Hazel FenwickΒ·about 6 hours ago
#v09T2WLz
#devtofrontend#devto#dev#number#browser
Long article (820 words) β€” no AI summary yet.
Reading 0:00
15s threshold

Every frontend developer has inherited a company logo .svg exported from Illustrator, Figma, or an online vectorizer where the actual mark sits in the middle of a giant 800Γ—500 invisible box.

Drop that uncropped SVG into a navbar, favicon generator, or footer grid and everything looks misaligned β€” height="32" renders a tiny 16px logo floating inside invisible margins.

When we built the free browser-native SVG Cropper & ViewBox Trimmer at LogoNib, we wanted tight, exact trimming in pure client-side TypeScript without parsing every esoteric SVG transform matrix, <clipPath> , stroke width, and <use> reference by hand.

Here is the clean, deterministic algorithm that handles arbitrary SVGs β€” including thick strokes, <g transform="..."> , <defs> , and curves β€” in under 20 milliseconds in the browser.

Why Naive Path-Coordinate Parsing Fails

Parsing d="..." commands alone frequently miscalculates the true visible bounding box of a real-world logo:

Strokes extend outside path coordinates: A stroke-width="14"…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.