svgdom
Straightforward DOM implementation to make SVG.js run headless on Node.js
While this dom implementation was designed to run svg.js on node, it now is much more feature complete and can be used by anyone needing an xml, svg or html dom.
Get started with svg.js v3.x
for older versions of svg.js checkout older versions of svgdom
npm install @svgdotjs/svg.js svgdom
import { createSVGWindow } from 'svgdom'
import { SVG, registerWindow } from '@svgdotjs/svg.js'
const window = createSVGWindow()
const document = window.document
registerWindow(window, document)
const canvas = SVG(document.documentElement)
canvas.rect(100, 100).fill('yellow').move(50,50)
console.log(canvas.svg())
console.log(canvas.node.outerHTML)
Create an HTML Dom or XML Dom
import { createHTMLWindow } from 'svgdom'
const window = createHTMLWindow()
import { createWindow } from 'svgdom'
const window = createWindow(namespaceURI, rootNode)
Use svgdom as cjs module
svgdom is used best as esm module. However, if you still require cjs, you have to import the module via the async import function:
const main = async () => {
const { createSVGWindow } = await import('svgdom')
}
main()
Fonts
In order to calculate bounding boxes for text the font needs to be loaded first. svgdom
loads Open Sans-Regular
by default when no font file for the specified font was found.
The following options must be set in order to load your own fonts:
import { config } from 'svgdom'
config.
.setFontDir('./fonts')
.setFontFamilyMappings({'Arial': 'arial.ttf'})
.preloadFonts()
const {setFontDir, setFontFamilyMappings, preloadFonts} = require('svgdom')
setFontDir('./fonts')
setFontFamilyMappings({'Arial': 'arial.ttf'})
preloadFonts()
Limitations
Almost all functions of svg.js work properly with svgdom. However there are a few known limitations:
Using svgdom in your own projects
Albeit this dom implementation aims to work with svgjs, it is of course possible to use it in your own projects.
Keep in mind, that some functions are just not needed in svgjs and therefore not implemented or tested.
If you need a certain feature don't hesistate to open an issue or submit a pull request.
Last thing to say: childNodes is an array! (yet)
or ![Sponsor](https://img.shields.io/badge/Sponsor-svgdom-green.svg)