• Stars
    star
    315
  • Rank 132,951 (Top 3 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interpolates path `d` attribute smoothly when A and B have different number of points.

d3-interpolate-path

npm version

d3-interpolate-path is a zero-dependency that adds an interpolator optimized for SVG <path> elements. It can also work directly with object representations of path commands that can be later interpreted for use with canvas or WebGL.

Note this package no longer has a dependency on d3 or d3-interpolate, but in web usage adds itself to the d3 namespace like other d3 plugins do.

Blog: Improving D3 Path Animation

Demo: https://pbeshai.github.io/d3-interpolate-path/

d3-interpolate-path demo

Example Usage

var line = d3.line()
  .curve(d3.curveLinear)
  .x(function (d) { return x(d.x); })
  .y(function (d) { return y(d.y); });

d3.select('path.my-path')
  .transition()
  .duration(2000)
  .attrTween('d', function (d) {
    var previous = d3.select(this).attr('d');
    var current = line(d);
    return d3.interpolatePath(previous, current);
  });

If you're using it in a module environment, you can import it as follows:

import { interpolatePath } from 'd3-interpolate-path';

Otherwise, you can use it via a <script> tag as follows:

<script src="https://unpkg.com/d3-interpolate-path/build/d3-interpolate-path.min.js"></script>

Development

Get rollup watching for changes and rebuilding

npm run watch

Run a web server in the docs directory

cd docs
php -S localhost:8000

Go to http://localhost:8000

Installing

If you use NPM, npm install d3-interpolate-path. Otherwise, download the latest release.

API Reference

# interpolatePath(a, b, excludeSegment)

Returns an interpolator between two path attribute d strings a and b. The interpolator extends a and b to have the same number of points before applying linear interpolation on the values. It uses De Castlejau's algorithm for handling bezier curves.

var pathInterpolator = interpolatePath('M0,0 L10,10', 'M10,10 L20,20 L30,30')
pathInterpolator(0)   // 'M0,0 L10,10 L10,10'
pathInterpolator(0.5) // 'M5,5 L15,15 L20,20'
pathInterpolator(1)   // 'M10,10 L20,20 L30,30'

You can optionally provide a function excludeSegment that takes two adjacent path commands and returns true if that segment should be excluded when splitting the line. A command object has form { type, x, y } (with possibly more attributes depending on type). An example object:

// equivalent to M0,150 in a path `d` string
{
  type: 'M',
  x: 0,
  y: 150
}

This is most useful when working with d3-area. Excluding the final segment (i.e. the vertical line at the end) from being split ensures a nice transition. If you know that highest x value in the path, you can exclude the final segment by passing an excludeSegment function similar to:

function excludeSegment(a, b) {
  return a.x === b.x && a.x === 300; // here 300 is the max X
}

# interpolatePathCommands(aCommands, bCommands, excludeSegment)

Returns an interpolator between two paths defined as arrays of command objects a and b. The interpolator extends a and b to have the same number of points if they differ. This can be useful if you want to work with paths in other formats besides SVG (e.g. canvas or WebGL).

Command objects take the following form:

| { type: 'M', x: number, y: number },
| { type: 'L', x, y }
| { type: 'H', x }
| { type: 'V', y }
| { type: 'C', x1, y1, x2, y2, x, y }
| { type: 'S', x2, y2, x, y }
| { type: 'Q', x1, y1, x, y }
| { type: 'T', x, y }
| { type: 'A', rx, ry, xAxisRotation, largeArcFlag, sweepFlag, x, y }
| { type: 'Z' }

Example usage:

const a = [
  { type: 'M', x: 0, y: 0 },
  { type: 'L', x: 10, y: 10 },
];
const b = [
  { type: 'M', x: 10, y: 10 },
  { type: 'L', x: 20, y: 20 },
  { type: 'L', x: 200, y: 200 },
];

const interpolator = interpolatePathCommands(a, b);

> interpolator(0);
[
  { type: 'M', x: 0, y: 0 },
  { type: 'L', x: 5, y: 5 },
  { type: 'L', x: 10, y: 10 },
]

> interpolator(0.5);
[
  { type: 'M', x: 5, y: 5 },
  { type: 'L', x: 12.5, y: 12.5 },
  { type: 'L', x: 105, y: 105 },
]

# pathCommandsFromString(pathDString)

Converts a path d string into an array of path command objects to work with interpolatePathCommands.

Example usage:

const a = 'M0,0L10,10';

> pathCommandsFromString(a)
[
  { type: 'M', x: 0, y: 0 },
  { type: 'L', x: 10, y: 10 },
]

More Repositories

1

use-query-params

React Hook for managing state in URL query parameters with easy serialization.
TypeScript
2,133
star
2

tidy

Tidy up your data with JavaScript, inspired by dplyr and the tidyverse
TypeScript
723
star
3

react-url-query

A library for managing state through query parameters in the URL in React
JavaScript
195
star
4

d3-line-chunked

Create lines that indicate where data is missing with gaps or differently styled chunks/line segments.
JavaScript
120
star
5

3dpie

gaudy 3d pie generator in react and three.js
JavaScript
115
star
6

serialize-query-params

A javascript library for simplifying encoding and decoding URL query parameters
TypeScript
71
star
7

vis-utils

A collection of utility functions for helping with data visualization
JavaScript
51
star
8

linked-highlighting-react-vega-redux

Example of doing linked highlighting with React, Vega, and Redux
JavaScript
51
star
9

react-express-example

A basic example of using Express and Facebook's React for both client-side and server-side rendering.
JavaScript
45
star
10

p5js-ccapture

An example of using p5.js with CCapture.js for exporting frames
HTML
44
star
11

deckgl-point-animation

Demo of animating points in deck.gl
JavaScript
40
star
12

linked-highlighting-react-d3-reflux

An example of doing linked highlighting using React, d3.js and Reflux
JavaScript
39
star
13

d3-scale-interactive

An interactive UI for editing d3 v4 scales in your browser
JavaScript
36
star
14

react-map-demo

Created for ReactJS Boston Meetup, a demo of using react-map-gl and deck.gl to draw a basic map with some different layers.
JavaScript
30
star
15

react-taco-table

A table component for React based on column configuration 🌮
JavaScript
30
star
16

react-basic-vis-examples

A few basic visualization examples that build on each other, using React and D3.js.
JavaScript
28
star
17

react-computed-props

A higher-order component for React to add computed or derived props to the wrapped component for better performance.
JavaScript
26
star
18

peterbeshai.com

HTML
15
star
19

cra-snowpack

Example of using Snowpack's dev server in a Create-React-App app
JavaScript
12
star
20

beshai-makes-code

Code for BeshaiMakes.com Articles
HTML
8
star
21

shots3D

Render basketball shots from the NBA using WebGL
JavaScript
6
star
22

recreating-the-past

Sketches for the Recreating the Past class at the MIT Media Lab (Fall 2019)
C++
5
star
23

stats

Examples for doing stats on experimental data
R
4
star
24

geo-explore

Simple demonstrative examples of mapping techniques for the web
HTML
3
star
25

d3-webpack-loader

Automatically bundle D3 v4 modules under a single `d3` import with D3 Webpack Loader.
JavaScript
3
star
26

rhombus

Rhombus Classroom Synchronous Participation System
Shell
2
star
27

react-library-starter

A starting place for creating a React library or component to share with others
JavaScript
2
star
28

react-autosize

Component to automatically provide width and height props to its child
JavaScript
2
star
29

react-auto-width

Set the width of a Reactjs component to its parent's width
JavaScript
2
star
30

nba-draft

JavaScript
2
star
31

rhombus-id-server

ID Server for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
32

rhombus-web-app-basic

Barebones Web App for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
33

rhombus-web-app-game-theory

Web App with Game Theory games for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
34

rhombus-clicker-server

Clicker Server for Rhombus Classroom Synchronous Participation System
Java
1
star
35

tree-csv

Explore a CSV containing hierarchical data as a tree
JavaScript
1
star
36

rhombus-web-framework

Web Framework for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
37

rhombus-web-app-experiment

Web App for 7-Segment Display Recognition Experiment for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
38

protoweb

Web prototype scaffolding and hot-reloading server
JavaScript
1
star
39

rhombus-web-app-sandbox

Sandbox for various apps for Rhombus Classroom Synchronous Participation System
JavaScript
1
star
40

rhombus-clicker-server-filter-multiple-instructor

Multiple Instructor filter for the Clicker Server for Rhombus Classroom Synchronous Participation System
Java
1
star
41

rhombus-grunt-socket-server

HTTP/Socket.io server used by Rhombus Classroom Synchronous Participation System
JavaScript
1
star