Skip to content

Draw SVG images - #202

Open
brainkim wants to merge 4 commits into
mainfrom
feat/svg-images
Open

brainkim wants to merge 4 commits into
mainfrom
feat/svg-images

Conversation

@brainkim

Copy link
Copy Markdown
Member

An SVG file now decodes like any other image: in an <img>, through createImageBitmap, or as a canvas drawImage source. It is drawn with the 2D canvas's existing rasterizer, so there is no new dependency (the file is parsed with parse5, which the DOM already uses).

What is drawn

  • path, rect, circle, ellipse, line, polyline, polygon, groups, and <use> of an element in the same file
  • viewBox, preserveAspectRatio, transform, and lengths in CSS units
  • fill, stroke and their widths, caps, joins, dashes, opacities and fill rule, as attributes, in a style attribute, or in a <style> sheet of type, class and id selectors
  • linearGradient and radialGradient, in bounding-box or user-space units, with gradientTransform and href inheritance
  • clip-path to the shapes of a <clipPath>
  • entities declared in the doctype, which older Illustrator files use for every style

Not drawn: text, embedded images, patterns, filters, masks, markers, animation. Group opacity is applied to each shape, which is wrong where a group's shapes overlap. Selectors with combinators, attributes or pseudo-classes set nothing.

An inline <svg> element in a page is still not drawn; this is files only.

How it behaves as an image

It is drawn the way a browser draws an SVG in an <img>: on its own. currentColor is black, scripts do not run, and nothing outside the file is fetched.

The decode does not know the box the image will be shown in, so it picks a resolution up front. The natural size is the file's (width/height, else the viewBox, else 300 by 150), and the bitmap is drawn with more pixels than that: up to 16 to a side, aiming at about 512 by 512. A 24 by 24 icon is kept at 384 by 384, so it is sharp under kitty graphics at any size it is likely to be shown. An image too large to keep is drawn smaller, as other formats are kept. Every consumer already told a bitmap's kept size from its natural size, so nothing else changed for this.

In cells mode an SVG is half blocks like any other image. No special rule was added for it.

Limits

A file is held to 4 MB, 131,072 shapes once every <use> is followed, 64 levels of nesting, and <use> 8 deep. Drawing yields to the event loop as the other decoders do and stops at the decode deadline. Entity expansion is one pass and counts toward the 4 MB.

Rasterizer change

rasterize and paintMask in src/internal/canvas.ts walked the whole bitmap for every fill and stroke. They now keep to the box of the shape's polygons, except for the composite operations that clear what the source does not cover. This is what makes a drawing of hundreds of shapes practical, and it speeds up every canvas.

File Size drawn Before After
Ghostscript tiger (240 paths) 900 by 900 2,300 ms 590 ms
Wikipedia globe (266 paths, 43 gradients) 536 by 489 890 ms 370 ms
GitHub mark 512 by 512 38 ms 28 ms
A 20 by 20 icon 320 by 320 7 ms 6 ms

Timed on a loaded machine, on the main thread; in a page the decode runs on the worker.

Tests

tests/svg.test.ts has 20 tests: shapes and transforms, natural size, resolution, preserveAspectRatio, the style cascade, both gradients, clipping, <use>, entities, what draws nothing, the limits and the deadline, sniffing, and an <img> and a canvas in a page. The built bundle was checked by hand on Node and Bun, decoding through the worker.

In the full suite on a machine under heavy load, one file on each of Node and Bun did not complete (tests/nesting-depth.test.ts, tests/incremental.test.ts); both pass alone on both runtimes. Everything else passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig

brainkim and others added 4 commits November 20, 2026 11:17
An SVG file in an <img>, or anywhere else an image decodes, is drawn
with the 2d canvas's rasterizer: shapes and paths, groups, <use>,
transforms, viewBox and preserveAspectRatio, fills and strokes, linear
and radial gradients, clip-path, and <style> sheets of simple selectors.
It is drawn on its own, as a browser draws one in an <img>, so nothing
outside the file is loaded and currentColor is black.

The image's natural size is its own, and it is drawn with more pixels
than that, up to sixteen to a side, so an icon stays sharp when the
terminal shows it at its real size. A file is held to 4 MB and 131,072
shapes, and to the time any image may take to decode.

The canvas's rasterizer now looks only at the box of the shape it is
painting, where it walked the whole bitmap for every fill.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
A 1 ms interval does not always tick three times in 60 ms on Windows.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant