Mutate SVG Paths in the DOM
npm install metamorphernew Path(element).scale, rotate, translate and transform methods.paint method.Achieve complex path animation with a simple loop and the transform and interpolate methods. Suggested to use a minimal easing / animation package to add easings to your loops. Recommend to use VelocityJS since you may want to combine this with CSS animations.
For a full tutorial, see: Animating SVGs with Metamorpher and VelocityJS.
yarn add metamorpher
`Include
Metamorpher supports AMD, CommonJS, ES6 import methods in addition to global script include. Since, Metamorpher has multiple exported classes and no default export, you have the option to selectively import classes from Metamorpher based on your needs. Most consumers of this package will only require the Path class and possibly the Point class. $3
`js
import {Path, Instruction, Point, Edge} from 'metamorpher';
`$3
`js
var Path = require('metamorpher').Path;
var Instruction = require('metamorpher').Instruction;
var Point = require('metamorpher').Point;
var Edge = require('metamorpher').Edge;
`$3
`html
`Path Usage
Path, the main class that most consumers of this package will use, represents an SVG Path as a collection of Instructions. To start working with a Path, use the constructor: new Path().$3
Constructor takes as input one of:
1. A Path SVGElement (in the DOM),
2. Another Path (this class) or,
3. A String representing SVG Path data (the d parameter of an SVG Path).If the first option, Path SVGElement, is used then the path will already be attached to the same DOM element that was passed in for the pruposes of the
paint method. If the other options are used, they can later be attached to a DOM element with the attach method or used as a parameter to the interpolate method.$3
[Chainable] Attach a path to the DOM so it can be painted.$3
[Chainable] Detatch a path from the DOM.$3
[Chainable] Update the rendered path in the DOM by setting the DOM Path element's d parameter. If the path is currently detached from the DOM, then no action will be performed.$3
[Chainable] Scale each instruction in the path by factor relative to the origin Point.$3
[Chainable] Rotate each instruction in the path by degrees relative to the origin Point.$3
[Chainable] Translate each instruction in the path by x in the x-coordinate and y in the y-coordinate.$3
[Chainable] Transform each instruction in the path by copying the point data from the corresponding instruction the path parameter. Use in conjunction with interpolate to morph one path into another. Unsupported behavior if path instruction lengths or point lengths don't match.$3
[Chainable] Calculate and set the path to be the interpolated progress between startPath and endPath. Progress should be a decimal between 0 and 1 representing the percentage of interpolation. Critical for animating one path to become another path. Unsupported behavior if path lengths don't match.$3
Returns the longest straight Edge of the path.Instruction Usage
$3
Constructor takes as arguments one of:
1. Another Instruction (this class) or,
2. An optional String representing the instruction type and zero or more Points.The instruction
type should be a single letter (ex: M, L, C, Z, etc.). See the Path Data Spec for more information on Path instructions.$3
[Chainable] Scale each point in the instruction by factor relative to the origin Point.$3
[Chainable] Rotate each point in the instruction by degrees relative to the origin Point.$3
[Chainable] Translate each point in the instruction by x in the x-coordinate and y in the y-coordinate.$3
[Chainable] Transform each point in the instruction to the same place in the SVG as corresponding point the instruction parameter. Use in conjunction with interpolate to morph one instruction into another. Unsupported behavior if instruction point lengths don't match.$3
[Chainable] Calculate and set the instruction to be the interpolated progress between startPath and endPath. Progress should be a decimal between 0 and 1 representing the percentage of interpolation. Unsupported behavior if instruction point lengths don't match.Point Usage
$3
Points can be constructed from:
1. Another Point or,
2. Zero, one or two Numbers representing x and y, respectively.x and y are simply coordinates in the SVG viewbox.$3
Scale the point by factor relative to the origin Point.$3
Rotate the point by degrees relative to the origin Point.$3
Translate the point by x in the x-coordinate and y in the y-coordinate.$3
Copy the x and y coordinate from the point parameter.$3
Calculate and set the point to be the interpolated progress between startPoint and endPoint`. Progress should be a decimal between 0 and 1 representing the percentage of interpolation.| Degrees | Result |
|---|---|
| 0 | 0 |
| 30 | 0.5 |
| 45 | 1 |
| 60 | 2 |
| 90 | NaN |
| 135 | -1 |
| 180 | 0 |
| 225 | 1 |
| 270 | NaN |
| 315 | -1 |