Conversation
Svg.render and Canvas.render always put the result into the document. renderToTag stops before the ScalaTags Tag is turned into a DOM node and hands it back instead, so the caller decides what to do with it. SvgRenderer.renderToTag renders via a hidden element that is removed afterwards, so nothing is left on the page. Measuring text needs a live document, which is why this can't be done with a detached node. Fixes creativescala#229
byteayan
force-pushed
the
svg-render-to-tag
branch
from
September 15, 2026 07:13
fe7e258 to
ad00fcf
Compare
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.
Fixes #229.
Svg.renderandCanvas.renderalways put the rendered picture into the document. This addsrenderToTag, which stops before the ScalaTagsTagbecomes a DOM node and hands the tag back instead, so you can add your own elements to it, or choose when and where it goes.SvgRenderer.renderToTag(frame, picture)is the entry point that leaves the page alone. It renders through a hidden element that is removed again afterwards. The indirection is there because measuring text needs a live document, so a fully detached node won't work.Canvas.renderToTagis also available if you already have a Canvas, with the caveat that such a Canvas has already added its own root<svg>.One bit I wasn't sure about:
renderToTagtakes aFramefor the size and background but ignores itsid, which reads a little oddly. Happy to reshape this if you had something else in mind.Ran locally on JDK 17:
test,headerCheckAll,scalafmtCheckAll,mimaReportBinaryIssues,doc,githubWorkflowCheckanddocs/mdocall pass.The tests run in Node and never touch a real DOM, so I also checked the new path in Chrome. A picture containing text comes out the same size rendered off screen as it does drawn on the page (554.15625 x 157 either way), and the hidden element is gone afterwards. That was the part worth confirming, since text measurement depends on
getBBoxworking inside the hidden container.