npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-tween

v3.0.1

Published

Animate between SVG shapes

Downloads

38

Readme

SVG tween

Animate between SVG shapes.

8.6kb gzipped.

Note: Wilderness has superseded this library and I'd highly recommend trying that instead.

Polyfill generators

However, you're currently also going to have to bring babel polyfill to the party at an additional 30.8kb gzipped. This is to support Javascript generators which a dependency of this library makes use of.

Examples

Tower example

View tower example code

Batman example

View batman example code

Line example

View line example code

Basic shapes example

View basic shapes example code

Installation

npm install svg-tween

Usage

SVG tween has two functions – tween and tweenPaths.

tween

The tween function takes all the same options as Tweening's tween function. However, the from and to options take the form of SVG shape objects.

import tween from 'svg-tween'

// The shape we want to animate from
const from = {
  type: 'path',
  d: 'M5,50L80,60v40,l-15,10l-15,-10z'
}

// The shape we want to animate to
const to = {
  type: 'rect',
  width: 100,
  height: 100,
  x: 50,
  y: 50
}

// Create a new path node
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')

// Set the node's initial d attribute to match the from shape
path.setAttribute('d', from.d)

// Add the path node to the dom
document.getElementById('svg').appendChild(path)

// Let's move!
// On each frame our next callback is run
// this is where we update our path node's d attribute
tween({
  duration: 500,
  from,
  to,
  next: d => path.setAttribute('d', d)
})

tweenPaths

The tweenPaths function is much the same as tween, except it takes d attribute strings as it's from and to options.

import { tweenPaths } from 'svg-tween'

// The path we want to animate from
const from = 'M5,50L80,60v40,l-15,10l-15,-10z'

// The path we want to animate to
const to = 'M50,50h100v100h-100Z'

// Create a new path node
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')

// Set the node's initial d attribute to match from
path.setAttribute('d', from)

// Add the path node to the dom
document.getElementById('svg').appendChild(path)

// Let's move!
// On each frame our next callback is run
// this is where we update our path node's d attribute
tweenPaths({
  duration: 500,
  from,
  to,
  next: d => path.setAttribute('d', d)
})

Morphing multiple shapes

The tween and tweenPaths functions can also accept an array of shape objects, or d attribute strings respectively. This allows us to tween groups of SVG shapes in one function call.

import { tweenPaths } from 'svg-tween'

// The paths we want to animate from
const from = [ 'M0,0h10v10h-10z', 'M10,10h10v10h-10z' ]

// The paths we want to animate to
const to = [ 'M0,0l10,5l-10,5z', 'M10,10l10,5l-10,5z' ]

// Create two new path nodes
const paths = [
  document.createElementNS( 'http://www.w3.org/2000/svg', 'path' ),
  document.createElementNS( 'http://www.w3.org/2000/svg', 'path' )
]

paths.forEach(( p, i ) => {
  // Set the node's initial d attribute to match from
  p.setAttribute('d', from[ i ])

  // Add the path node to the dom
  document.getElementById('svg').appendChild(p)
)

// Let's move!
// On each frame our next callback is run for each path
// this is where we update our path node's d attribute
tweenPaths({
  from,
  to,
  next: (d, i) => paths[ i ].setAttribute('d', d)
})

CommonJS

This is how you get to the good stuff if you're using require.

const SVGTween = require( 'svg-tween' )
const tween = SVGTween.default
const tweenPaths = SVGTween.tweenPaths

UMD

And if you just want to smash in a Javascript file you're also covered. Drop this in place ...

https://unpkg.com/svg-tween/dist/svg-tween.min.js

Then access it on the SVGTween global variable.

const tween = SVGTween.default
const tweenPaths = SVGTween.tweenPaths

Size

A size comparison of libraries that allow morphing of SVG shapes (with differing number of points).

| Library | Size | | --- | --- | | SVG tween | 6.8kb | | SVG Morpheus | 7.2kb | | SnapSVG | 26kb | | RaphaelJS | 32kb | | GreenSock TweenMax + morphSVG plugin | 41.5kb | | Bonsai | 43kb | | D3 | 52kb |

If you know of any others, please open an issue or even better – submit a pull request.

Help make this better

Issues and pull requests gratefully received!

I'm also on twitter @colinmeinke.

Thanks :star2:

License

ISC.