Giter VIP home page Giter VIP logo

svg.onchain.js's Introduction

svg.onchain.js

A micro JS library (362 bytes) for manipulating and animating SVG.

This library is intended for use in environments where the available storage space is very limited; like blockchains for example. Everything is stripped down to the bare essentials.

GitHub GitHub tag (latest SemVer) GitHub Workflow Status

Why not use vanilla JS?

Building an SVG document with plain JS is pretty verbose. Let's look at the following example:

const ns = 'http://www.w3.org/2000/svg'
const svg = document.createElementNS(ns, 'svg')
svg.setAttribute('xmlns', ns)
svg.setAttribute('width', 100)
svg.setAttribute('height', 100)
document.body.appendChild(svg)
const rect = document.createElementNS(ns, 'rect')
rect.setAttribute('width', 100)
rect.setAttribute('height', 100)
rect.setAttribute('fill', '#f06')
rect.setAttribute('rx', 10)
rect.setAttribute('ry', 20)
svg.appendChild(rect)

That's the code to create an SVG canvas and draw a pink square on it. In comparison, doing the same using this library is a lot more consice:

const doc = SVG.doc(100, 100, document.body)
SVG.el('rect', doc, {width: 100, height: 100, fill: '#f06', rx: 10, ry: 20})

That's 98 bytes instead of 406, or roughly 75% less code. And the difference is only going to get bigger the more objects you add. Of course, this library takes up 362 bytes as well, but that's quickly compensated after adding a few more objects.

Usage

Create a canvas of 100x100 px:

let cvs = SVG.doc(100, 100, document.body)

Add a square, filling the canvas:

let rect = SVG.el('rect', cvs, {width: 100, height: 100})

Update the colour of the square:

SVG.at(rect, {fill: '#0fa'})

Animate the corner radius of the square:

SVG.el('animate', rect, {
  attributeName: 'rx',
  values: '0;50;0',
  dur: '5s',
  repeatCount: 'indefinite'
})

License

svg.onchain.js is licensed under the terms of the MIT License.

svg.onchain.js's People

Contributors

wout avatar

Stargazers

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