GithubHelp home page GithubHelp logo

jrpat / ssvg Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2 KB

A wafer-thin wrapper around native SVGElement that makes creating/manipulating SVGs as painless as possible

License: MIT License

JavaScript 100.00%

ssvg's Introduction

Simple SVG

A wafer-thin wrapper around the built-in SVGElement which adds some creature comforts that make creating SVGs a lot less verbose.

This uses proxies, classes, modules, and template strings and thus only works in modern browsers.

Idea

SVG is really not that hard to understand, and in fact it's pretty pleasant to work with, conceptually. In practice, however, it involves a ton of very verbose method calls with arcane namespace strings, and some other annoyances.

By minimizing that boilerplate, we can bring practice closer to concept and provide a pretty decent way of creating SVGs.

Usage

To start, create an instance:

let svg = new SSVG()

All of the SVGElement types can be created and immediately appended to the SVGElement by calling their name as a method:

svg.circle()  // appends & returns a new <circle> (SVGCircleElement)
svg.line()    // appends & returns a new <line> (SVGLineElement)
// etc...

To just create an element without appending, use the create* variation:

svg.createCircle()  // returns a new <circle>
svg.createLine()    // returns a new <line>
// etc...

Created elements also have some syntax sugar applied. Normally, the short syntax (circle.cx = 10) doesn't work because those properties are read-only. Instead you have to do circle.setAttribute('cx', 10)

SSVG makes those attributes writable:

let circle = svg.circle()
circle.cx = 100
circle.cy = 100
circle.r = 50
circle.fill = 'red'
circle.stroke = 'blue'
// etc...

You can also set attributes when you create the element:

let circle = svg.circle({ 
    cx: 100, 
    cy: 100, 
    r: 50,
    fill: 'red',
    stroke: 'blue',
    // etc...
})

Elements can also create/append nested children. This only makes sense to do with the g (group) element:

let g = svg.g({fill: red})
g.circle({cx:100, cy:100, r:50})
g.rect({x:10, y:10, width:50, height:50})

You can apply classes to elements, but use .class instead of .className:

let circle = svg.circle(...)
circle.class = 'myClass'

The actual Element instance can be gotten from the .root property:

svg.root      // SVGElement
circle.root   // SVGCircleElement

Other Userful Documentation:

License

MIT

ssvg's People

Contributors

jrpat avatar

Watchers

 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.