Import flubber
masterDepending on your environment, use the following syntax to import flubber:
Node.js (Classic):
var flubber = require("flubber");ES6 Modules:
import { interpolate } from "flubber";import { interpolate } from "flubber"repository·master·Indexed 27 days ago
https://github.com/veltman/flubberA library for smoothly interpolating and animating between 2D shapes, such as SVG paths or arrays of points. It provides 'best-guess' interpolation to prevent jumps and inversions when shapes lack a direct one-to-one correspondence. Features include tools for interpolating between single or multiple shapes (separate, combine, interpolateAll), as well as specialized functions for transitioning to and from circles and rectangles.
Depending on your environment, use the following syntax to import flubber:
Node.js (Classic):
var flubber = require("flubber");ES6 Modules:
import { interpolate } from "flubber";import { interpolate } from "flubber"You can install flubber via NPM or include it directly in a browser using a script tag.
NPM:
npm install flubberBrowser (via unpkg):
<script src="https://unpkg.com/flubber@0.3.0"></script>Use flubber.interpolateAll(fromShapeList, toShapeList [, options]) to interpolate between two arrays of shapes of the same length. Shapes are matched by index (the first item in fromShapeList morphs into the first item in toShapeList, etc.).
Options: Same as flubber.interpolate() and flubber.separate() (including the single option).
Use flubber.separate(fromShape, toShapeList [, options]) to interpolate between a single shape and an array of multiple shapes.
Arguments:
fromShape: A ring or SVG path string.toShapeList: An array of rings or SVG path strings.Options:
single (boolean, default: false):false, returns an array of n interpolator functions (where n is the length of toShapeList).true, returns a single interpolator that combines everything into one giant path string or one big array of rings.Example (Multiple elements):
var interpolators = flubber.separate(triangle, [square, otherSquare]);
// interpolators is an array of two functionsvar interpolators = flubber.separate(triangle, [square, otherSquare]);
d3.selectAll("path")
.data(interpolators)
.transition()
.attrTween("d", function(interpolator) { return interpolator; });flubber.fromRect(x, y, width, height, toShape [, options]) to interpolate from a rectangle with the upper-left corner at [x, y] and dimensions width x height to a target shape. This is the reverse of toRect().A helper function that takes an SVG path string (which may contain multiple shapes) and splits it into an array of individual one-shape path strings.
flubber.splitPathString("M1,1 L2,1 L1.5,2Z M3,3 L4,3 L3.5,4 Z");
// Returns ["M1,1 L2,1 L1.5,2Z", "M3,3 L4,3 L3.5,4 Z"]Use flubber.toRect(fromShape, x, y, width, height [, options]) to interpolate from an arbitrary shape to a rectangle with the upper-left corner at [x, y] and dimensions width x height.
Options: Same as flubber.interpolate().
var interpolator = flubber.toRect(triangle, 10, 50, 100, 200);flubber.fromCircle(x, y, r, toShape [, options]) to interpolate from a circle centered at [x, y] with radius r to a target shape. This is the reverse of toCircle().Use flubber.toCircle(fromShape, x, y, r [, options]) to interpolate from an arbitrary shape to a circle centered at [x, y] with radius r.
Options: Same as flubber.interpolate().
var interpolator = flubber.toCircle(triangle, 100, 100, 10);Use flubber.interpolate(fromShape, toShape [, options]) to create an interpolator function. This function accepts a value t from 0 to 1 and returns the in-between shape.
Input Formats:
"M100,100 L200,100 L150,200Z")[x, y] points (a "ring") (e.g., [[100, 100], [200, 100], [150, 200]])Options:
string (boolean, default: true): Whether to output results as an SVG path string or an array of points.maxSegmentLength (number | false | Infinity, default: 10): Controls smoothness. Lower values are smoother but slower. Set to false or Infinity for no smoothing.Note: If a path string includes holes or multiple shapes, only the first outer shape is used.
A helper function that converts an array of points (a ring) into an SVG path string.
flubber.toPathString([[1, 1], [2, 1], [1.5, 2]]);
// Returns "M1,1L2,1L1.5,2Z"flubber.combine(fromShapeList, toShape [, options]) to interpolate from an array of multiple shapes to a single target shape. This is the reverse of separate().