All Projects → svgdotjs → Svgdom

svgdotjs / Svgdom

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Svgdom

Onthefly
🔗 Generate TinySVG, HTML and CSS on the fly
Stars: ✭ 37 (-75.97%)
Mutual labels:  xml, svg, dom
Dompurify
DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:
Stars: ✭ 8,177 (+5209.74%)
Mutual labels:  svg, dom
Snowflake
❄️ SVG in Swift
Stars: ✭ 924 (+500%)
Mutual labels:  xml, svg
Elementx
⚡️ Functionally create DOM elements and compose them to a tree quickly
Stars: ✭ 62 (-59.74%)
Mutual labels:  svg, dom
Leanify
lightweight lossless file minifier/optimizer
Stars: ✭ 694 (+350.65%)
Mutual labels:  xml, svg
Etree
parse and generate XML easily in go
Stars: ✭ 763 (+395.45%)
Mutual labels:  xml, dom
Xml
XML without worries
Stars: ✭ 35 (-77.27%)
Mutual labels:  xml, dom
React Svg
🎨 A React component that injects SVG into the DOM.
Stars: ✭ 536 (+248.05%)
Mutual labels:  svg, dom
Jsoup
jsoup: the Java HTML parser, built for HTML editing, cleaning, scraping, and XSS safety.
Stars: ✭ 9,184 (+5863.64%)
Mutual labels:  dom, xml
Phpstamp
The XSL-way templating library for MS Office Word DOCX documents.
Stars: ✭ 150 (-2.6%)
Mutual labels:  xml, dom
I7j Pdfhtml
pdfHTML is an iText 7 add-on for Java that allows you to easily convert HTML and CSS into standards compliant PDFs that are accessible, searchable and usable for indexing.
Stars: ✭ 104 (-32.47%)
Mutual labels:  xml, svg
Dom4j
flexible XML framework for Java
Stars: ✭ 689 (+347.4%)
Mutual labels:  xml, dom
Svg
Fork of the ms svg library (http://svg.codeplex.com/)
Stars: ✭ 676 (+338.96%)
Mutual labels:  svg, dom
Macsvg
macSVG - An open-source macOS app for designing HTML5 SVG (Scalable Vector Graphics) art and animation with a WebKit web view ➤➤➤
Stars: ✭ 789 (+412.34%)
Mutual labels:  xml, svg
Html To Image
✂️ Generates an image from a DOM node using HTML5 canvas and SVG.
Stars: ✭ 595 (+286.36%)
Mutual labels:  svg, dom
Binding.scala
Reactive data-binding for Scala
Stars: ✭ 1,539 (+899.35%)
Mutual labels:  xml, dom
Fluentdom
A fluent api for working with XML in PHP
Stars: ✭ 327 (+112.34%)
Mutual labels:  xml, dom
Plain Draggable
The simple and high performance library to allow HTML/SVG element to be dragged.
Stars: ✭ 362 (+135.06%)
Mutual labels:  svg, dom
Dom To Svg
Library to convert a given HTML DOM node into an accessible SVG "screenshot".
Stars: ✭ 67 (-56.49%)
Mutual labels:  svg, dom
Elemental2
Type checked access to browser APIs for Java code.
Stars: ✭ 115 (-25.32%)
Mutual labels:  svg, dom

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].