• Stars
    star
    267
  • Rank 153,621 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Straightforward DOM implementation to make SVG.js run headless on Node.js

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'

// returns a window with a document and an svg root node
const window = createSVGWindow()
const document = window.document

// register window and document
registerWindow(window, document)

// create canvas
const canvas = SVG(document.documentElement)

// use svg.js as normal
canvas.rect(100, 100).fill('yellow').move(50,50)

// get your svg as string
console.log(canvas.svg())
// or
console.log(canvas.node.outerHTML)

Create an HTML Dom or XML Dom

// create HTML window with a document and an html root node
import { createHTMLWindow } from 'svgdom'
const window = createHTMLWindow()

// create XML window with a document and a given xml root node
import { createWindow } from 'svgdom'
const window = createWindow(namespaceURI, rootNode)
// e.g. createWindow('http://www.w3.org/1998/Math/MathML', 'math')

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:

```js
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.
    // your font directory
    .setFontDir('./fonts')
    // map the font-family to the file
    .setFontFamilyMappings({'Arial': 'arial.ttf'})
    // you can preload your fonts to avoid the loading delay
    // when the font is used the first time
    .preloadFonts()

// Alternatively you can import the functions itself and use them
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:

  • font properties like bold, italic... are only supported when you explicitely load that font e.g.
    setFontFamilyMappings({'Arial-italic': 'arial_italic.ttf'})
  • querySelector only supports the following pseudo classes:
    • first-child
    • last-child
    • nth-child
    • nth-last-child
    • first-of-type
    • last-of-type
    • nth-of-type
    • nth-last-of-type
    • only-child
    • only-of-type
    • root
    • not
    • matches
    • scope
  • special chars in attribute values: # and . are allowed but things like : or [] will break the selector

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)

Donate or Sponsor

More Repositories

1

svg.js

The lightweight library for manipulating and animating SVG
JavaScript
11,031
star
2

svg.draggable.js

An extension for the svg.js library to make elements draggable
HTML
281
star
3

svg.draw.js

An extension of svg.js which allows to draw elements with mouse
JavaScript
236
star
4

svg.filter.js

A plugin for svg.js adding svg filters
JavaScript
218
star
5

svg.import.js

A plugin for importing raw SVG into the svg.js library
JavaScript
98
star
6

svg.select.js

An extension of svg.js which allows to select an element
JavaScript
93
star
7

svg.panzoom.js

A plugin for svg.js which enables panzoom for viewbox elements
JavaScript
93
star
8

svg.resize.js

An extension of svg.js which allows to resize elements which are selected
JavaScript
90
star
9

svg.clock.js

A clock built with the svg.js library, inspired by the swiss railway clock
JavaScript
75
star
10

svg.pathmorphing.js

A plugin for the svg.js library to enable path morphing / animation
JavaScript
55
star
11

svg.easing.js

Easing equations for the fx module in the svg.js library
JavaScript
52
star
12

svg.topoly.js

A plugin for SVG.js to convert paths to polygons at a given sample rate
JavaScript
39
star
13

svg.shapes.js

A shapes plugin for the SVG.js library
JavaScript
36
star
14

svg.topath.js

A plugin for the svg.js library to convert any other shape to a path
JavaScript
29
star
15

svg.export.js

A plugin for the svg.js library to export raw svg
JavaScript
26
star
16

svg.parser.js

A helper library for svg.js providing parser functions to adopt and import existing svg's
JavaScript
17
star
17

svg.textmorph.js

A plugin for the svg.js library to enable text morphing / animation
JavaScript
16
star
18

svg.absorb.js

A plugin for the svg.js library adding the ability to absorb an existing svg into the dynamic svg.js structure
JavaScript
14
star
19

svg.screenbbox.js

A plugin for the svgjs library which gets the bbox of a path/polygon/polyline after all transformations applied.
JavaScript
14
star
20

svg.threed.js

An SVG.js plugin to visualise 3D scenes and objects
JavaScript
13
star
21

svgdotjs.github.io

Documentation and website for SVG.js
PHP
13
star
22

svg.pathhandles.js

Add handles to svg path as known in svg programs like incscape
JavaScript
8
star
23

svg.wiml.js

A plugin for the svg.js library adding support for the wiml markup language
JavaScript
3
star
24

svg.colorAt.js

An extension for svg.js which allows to pick a color from a gradint at a specific position
JavaScript
3
star
25

saucelab-configurator

A cli tool to create configurations objects for karma-saucelabs-launcher
JavaScript
1
star
26

svg.logo

The logo for svgdotjs
HTML
1
star