Skip to content

Add Svg.renderToTag / Canvas.renderToTag for SVG backend - #243

Open
byteayan wants to merge 1 commit into
creativescala:mainfrom
byteayan:svg-render-to-tag
Open

byteayan wants to merge 1 commit into
creativescala:mainfrom
byteayan:svg-render-to-tag

Conversation

@byteayan

@byteayan byteayan commented Sep 15, 2026

Copy link
Copy Markdown

Fixes #229.

Svg.render and Canvas.render always put the rendered picture into the document. This adds renderToTag, which stops before the ScalaTags Tag becomes 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.renderToTag is 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: renderToTag takes a Frame for the size and background but ignores its id, 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, githubWorkflowCheck and docs/mdoc all 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 getBBox working inside the hidden container.

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
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.

Render to SVG element

1 participant