Giter VIP home page Giter VIP logo

lw.svg-parser's Introduction

lw.svg-parser

SVG parser for LaserWeb/CNCWeb.

Supported tags

<svg> <title> <desc>
<g> <defs> <use>
<line> <polyline> <polygon>
<rect> <circle> <ellipse>
<path> <image> <text>

Features

  • ViewBox, PreserveAspectRatio
  • Clipping paths with Clipper.js
  • Promise mechanism
  • ES6 / UMD module

Demo

https://lautr3k.github.io/lw.svg-parser/dist/example/

Installation

Using NPM

npm install lw.svg-parser

Using GIT

git clone https://github.com/lautr3k/lw.svg-parser.git
cd svg-parser
npm install

Or download the last build from https://raw.githubusercontent.com/lautr3k/lw.svg-parser/master/dist/lw.svg-parser.js

<script src="./lw.svg-parser.js"></script>
<script>
  var parser = SVGParser.Parser();
</script>

Settings (all are optional)

let settings = {
  includes: ['svg', 'g', 'defs', 'use', 'line', 'polyline', 'polygon', 'rect', 'circle', 'ellipse', 'path', 'title', 'desc', 'image', 'text'],
  excludes: ['#text', '#comment'],
  traceSettings: { // Arc, Bezier curves only
    linear       : true, // Linear trace mode
    step         : 0.01, // Step resolution if linear mode = false
    resolution   : 100,  // Number of segments we use to approximate arc length
    segmentLength: 1     // Segment length
  },
  onTag: tag => {} // Called after a tag is parsed
}

Usages

import Parser from 'svg-parser'

let parser = new Parser(settings)

// <input> must be an raw XML string, XMLDocument, Element or File object
return parser.parse(input).then(tags => {
    console.log('tags:', tags);
    tags.forEach(tag => {
        tag.getPaths()  // return an array of Path objects (all contours + holes)
        tag.getShapes() // return an array of ExPolygons objects from Clipper.js (filled shapes)
    })
})
.catch(error => {
    console.error('error:', error);
});

After the main <svg> tag was parsed you can access this two properties on the parser instance :

parser.editor   // Editor info { name, version, fingerprint }
parser.document // Document info { width, height, viewBox }
                // where viewBox is { x, y, width, height }

lw.svg-parser's People

Contributors

jorgerobles avatar skarab42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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