Giter Club home page Giter Club logo

svgdom's Introduction

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 v2.x (x>5)

npm install svg.js svgdom
// returns a window with a document and an svg root node
const { createSVGWindow } = require('svgdom')
const window = createSVGWindow()
const SVG = require('svg.js')(window)
const document = window.document

// create svg.js instance
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)

Get started with svg.js v3.x

npm install @svgdotjs/svg.js svgdom
// returns a window with a document and an svg root node
const { createSVGWindow } = require('svgdom')
const window = createSVGWindow()
const document = window.document
const { SVG, registerWindow } = require('@svgdotjs/svg.js')

// 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
const { createHTMLWindow } = require('svgdom')
const window = createHTMLWindow()

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

Use svgdom as esm module

svgdom is used best as esm module. So in case you already did the step to esm modules on node, you can just go ahead and import svgdom:

import { createSVGWindow } from 'svgdom'

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:

const { config } = require(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

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

svgdom's People

Contributors

apla avatar danielsogl avatar dbrnz avatar dependabot[bot] avatar fuzzyma avatar guseyn avatar jroper avatar jtbrinkmann avatar kimmobrunfeldt avatar michael-brade avatar rssh avatar salarelv avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.