Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 44 ยท Transitions and Interaction

Easing and Interpolation

Control the feel of a D3 animation with easing functions and build custom interpolators: numbers, colors, strings, transforms, attrTween and tweens.

Every transition has two ingredients that decide how it looks. Easing decides how time maps to progress: starting slowly, ending slowly, bouncing. Interpolation decides how values between the start and end are computed: numbers, colors, transforms, whole path strings. Both are plain functions, so you can use the built-in ones or write your own.

Easing

An easing function takes a time t between 0 and 1 and returns an adjusted progress, also usually between 0 and 1 (some overshoot on purpose). Set it with .ease():

JavaScript
selection.transition().duration(1000).ease(d3.easeBounceOut).attr("cy", 200);

The default is d3.easeCubic (slow-fast-slow). Families come in three variants: In (slow start), Out (slow end) and InOut (both):

Family Feel
easeLinear constant speed
easeQuad, easeCubic, easePoly.exponent(n) polynomial: higher power, stronger acceleration
easeSin, easeExp, easeCircle gentle, sharp or circular curves
easeElastic, easeBack, easeBounce overshoot, spring back, bounce

Names combine as d3.easeCubicInOut, d3.easeElasticOut, d3.easeBackIn. Several have parameters: d3.easeElasticOut.amplitude(1.2).period(0.5) and d3.easeBackOut.overshoot(2).

A good rule: use Out easings for things that arrive (objects entering), In for things that leave, and InOut for things that move from one place to another. Reserve bounce and elastic for playful interfaces; they distract in serious data charts.

Compare easing functions

All six dots start and finish together, but they take different paths. .ease() takes one easing function for the whole transition, so each creates a separate transition per dot, each with the easing stored in its datum.

Interpolators

The d3-interpolate module turns two values into a function i(t) that returns the value at progress t:

JavaScript
d3.interpolateNumber(10, 20)(0.5);          // 15
d3.interpolateRgb("red", "blue")(0.5);       // "rgb(128, 0, 128)"
d3.interpolateString("5px", "25px")(0.5);    // "15px"
d3.interpolateArray([0, 0], [10, 20])(0.5);  // [5, 10]
d3.interpolateObject({a: 0}, {a: 8})(0.5);   // {a: 4}
d3.interpolateRound(0, 9)(0.5);              // 5 (rounded)

The generic d3.interpolate(a, b) chooses the right one from the type of b: a number, a color string, a date, an array, an object, or other strings. In transitions this is exactly what .attr() and .style() do for you.

Colors deserve a note: interpolateRgb blends in RGB space, which can pass through dull greys. d3.interpolateHsl, d3.interpolateLab and d3.interpolateHcl blend in other color spaces and often look nicer: for red to blue, compare them side by side.

d3.interpolateString finds numbers inside strings and interpolates only those, which is how path data like M0,0L10,10 animates to M0,0L20,30, provided both strings have the same structure. Paths with different numbers of points will glitch; use a library such as flubber, or resample.

Custom tweens

Sometimes you need an effect no built-in interpolator gives, such as counting up a number in a label. Use .tween(name, factory), or the shortcut .attrTween(name, factory) / .styleTween(...) / .textTween(...). The factory runs when the transition starts and returns a function of t.

Count-up text and an arc tween

The arc path cannot be interpolated as a plain string from a datum, so attrTween returns a function that rebuilds the path from an interpolated angle on every frame. This is the standard recipe for animating arcs, pie slices and any shape created by a generator.

Other useful interpolators

  • d3.interpolateZoom(a, b) smoothly flies a viewport between two views [cx, cy, width] (used for map and zoom animations).
  • d3.interpolateTransformSvg(a, b) interpolates transform attribute strings correctly (rotations take the short way around).
  • d3.piecewise(interpolator, array) makes one interpolator through many values; d3.quantize(interpolator, n) samples n values from it, for example turning d3.interpolateViridis into a discrete palette.

Color schemes such as d3.interpolateViridis and d3.interpolateRdYlBu are interpolators too: pass them to d3.scaleSequential (Scales) to color continuous data.

flowchart LR
  T["time t: 0 to 1"] --> E["easing: t -> t'"]
  E --> I["interpolator: i(t')"]
  I --> V["value written to attr or style"]

Easing reshapes time; interpolation turns progress into a value.

Common mistakes

  • Passing a string name to .ease("elastic"). D3 v4+ expects a function: .ease(d3.easeElastic).
  • Using a bounce or elastic easing on a bar's height, which can briefly give negative values and errors. Clamp them or use a gentler ease.
  • Interpolating paths with different numbers of points.
  • Mutating the interpolated datum without a plan: tween factories run per element, so keep state inside the closure.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an easing function receive and return?

    Easing remaps time to progress. d3.easeLinear returns t unchanged; others curve it.
  2. 2. Which easing starts slowly and ends fast?

    In variants accelerate from the start; Out variants decelerate toward the end.
  3. 3. Why does animating a pie arc need attrTween?

    Interpolating the angle and rebuilding the path each frame gives a smooth arc; interpolating two path strings would not match.
  4. 4. What does d3.interpolate(a, b) do?

    It looks at b: numbers, colors, dates, arrays, objects or strings get the matching interpolator.

Summary

  • Easing remaps time (d3.easeCubicInOut, easeBounceOut, ...); pass a function to .ease().
  • d3-interpolate provides interpolators for numbers, colors, strings, arrays and objects.
  • Use attrTween, styleTween, textTween or tween for custom effects.
  • Color spaces (interpolateHcl, interpolateLab) often look better than RGB.

Next: Mouse Events and Tooltips