Repository navigation
Draw SVG images - #202
Open
brainkim wants to merge 4 commits into
Open
Draw SVG images#202brainkim wants to merge 4 commits into
brainkim wants to merge 4 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An SVG file now decodes like any other image: in an
<img>, throughcreateImageBitmap, or as a canvasdrawImagesource. 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 fileviewBox,preserveAspectRatio,transform, and lengths in CSS unitsfill,strokeand their widths, caps, joins, dashes, opacities and fill rule, as attributes, in astyleattribute, or in a<style>sheet of type, class and id selectorslinearGradientandradialGradient, in bounding-box or user-space units, withgradientTransformandhrefinheritanceclip-pathto the shapes of a<clipPath>Not drawn: text, embedded images, patterns, filters, masks, markers, animation. Group
opacityis 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.currentColoris 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 theviewBox, 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
rasterizeandpaintMaskinsrc/internal/canvas.tswalked 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.Timed on a loaded machine, on the main thread; in a page the decode runs on the worker.
Tests
tests/svg.test.tshas 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