GithubHelp home page GithubHelp logo

d3-ease's Introduction

d3-ease

Easing is a method of distorting time to control apparent motion in animation. It is most commonly used for slow-in, slow-out. By easing time, animated transitions are smoother and exhibit more plausible motion.

The easing types in this module implement the ease method, which takes a normalized time t and returns the corresponding “eased” time . Both the normalized time and the eased time are typically in the range [0,1], where 0 represents the start of the animation and 1 represents the end; some easing types, such as elastic, may return eased times slightly outside this range. A good easing type should return 0 if t = 0 and 1 if t = 1. See the easing explorer for a visual demonstration.

These easing types are largely based on work by Robert Penner.

Installing

If you use npm, npm install d3-ease. You can also download the latest release on GitHub. For vanilla HTML in modern browsers, import d3-ease from Skypack:

<script type="module">

import {easeCubic} from "https://cdn.skypack.dev/[email protected]";

const e = easeCubic(0.25);

</script>

For legacy environments, you can load d3-ease’s UMD bundle from an npm-based CDN such as jsDelivr; a d3 global is exported:

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script>

const e = d3.easeCubic(0.25);

</script>

Try d3-ease in your browser.

API Reference

# ease(t)

Given the specified normalized time t, typically in the range [0,1], returns the “eased” time , also typically in [0,1]. 0 represents the start of the animation and 1 represents the end. A good implementation returns 0 if t = 0 and 1 if t = 1. See the easing explorer for a visual demonstration. For example, to apply cubic easing:

const te = d3.easeCubic(t);

Similarly, to apply custom elastic easing:

// Before the animation starts, create your easing function.
const customElastic = d3.easeElastic.period(0.4);

// During the animation, apply the easing function.
const te = customElastic(t);

# d3.easeLinear(t) <>

Linear easing; the identity function; linear(t) returns t.

linear

# d3.easePolyIn(t) <>

Polynomial easing; raises t to the specified exponent. If the exponent is not specified, it defaults to 3, equivalent to cubicIn.

polyIn

# d3.easePolyOut(t) <>

Reverse polynomial easing; equivalent to 1 - polyIn(1 - t). If the exponent is not specified, it defaults to 3, equivalent to cubicOut.

polyOut

# d3.easePoly(t) <>
# d3.easePolyInOut(t) <>

Symmetric polynomial easing; scales polyIn for t in [0, 0.5] and polyOut for t in [0.5, 1]. If the exponent is not specified, it defaults to 3, equivalent to cubic.

polyInOut

# poly.exponent(e) <>

Returns a new polynomial easing with the specified exponent e. For example, to create equivalents of linear, quad, and cubic:

const linear = d3.easePoly.exponent(1);
const quad = d3.easePoly.exponent(2);
const cubic = d3.easePoly.exponent(3);

# d3.easeQuadIn(t) <>

Quadratic easing; equivalent to polyIn.exponent(2).

quadIn

# d3.easeQuadOut(t) <>

Reverse quadratic easing; equivalent to 1 - quadIn(1 - t). Also equivalent to polyOut.exponent(2).

quadOut

# d3.easeQuad(t) <>
# d3.easeQuadInOut(t) <>

Symmetric quadratic easing; scales quadIn for t in [0, 0.5] and quadOut for t in [0.5, 1]. Also equivalent to poly.exponent(2).

quadInOut

# d3.easeCubicIn(t) <>

Cubic easing; equivalent to polyIn.exponent(3).

cubicIn

# d3.easeCubicOut(t) <>

Reverse cubic easing; equivalent to 1 - cubicIn(1 - t). Also equivalent to polyOut.exponent(3).

cubicOut

# d3.easeCubic(t) <>
# d3.easeCubicInOut(t) <>

Symmetric cubic easing; scales cubicIn for t in [0, 0.5] and cubicOut for t in [0.5, 1]. Also equivalent to poly.exponent(3).

cubicInOut

# d3.easeSinIn(t) <>

Sinusoidal easing; returns sin(t).

sinIn

# d3.easeSinOut(t) <>

Reverse sinusoidal easing; equivalent to 1 - sinIn(1 - t).

sinOut

# d3.easeSin(t) <>
# d3.easeSinInOut(t) <>

Symmetric sinusoidal easing; scales sinIn for t in [0, 0.5] and sinOut for t in [0.5, 1].

sinInOut

# d3.easeExpIn(t) <>

Exponential easing; raises 2 to the exponent 10 * (t - 1).

expIn

# d3.easeExpOut(t) <>

Reverse exponential easing; equivalent to 1 - expIn(1 - t).

expOut

# d3.easeExp(t) <>
# d3.easeExpInOut(t) <>

Symmetric exponential easing; scales expIn for t in [0, 0.5] and expOut for t in [0.5, 1].

expInOut

# d3.easeCircleIn(t) <>

Circular easing.

circleIn

# d3.easeCircleOut(t) <>

Reverse circular easing; equivalent to 1 - circleIn(1 - t).

circleOut

# d3.easeCircle(t) <>
# d3.easeCircleInOut(t) <>

Symmetric circular easing; scales circleIn for t in [0, 0.5] and circleOut for t in [0.5, 1].

circleInOut

# d3.easeElasticIn(t) <>

Elastic easing, like a rubber band. The amplitude and period of the oscillation are configurable; if not specified, they default to 1 and 0.3, respectively.

elasticIn

# d3.easeElastic(t) <>
# d3.easeElasticOut(t) <>

Reverse elastic easing; equivalent to 1 - elasticIn(1 - t).

elasticOut

# d3.easeElasticInOut(t) <>

Symmetric elastic easing; scales elasticIn for t in [0, 0.5] and elasticOut for t in [0.5, 1].

elasticInOut

# elastic.amplitude(a) <>

Returns a new elastic easing with the specified amplitude a.

# elastic.period(p) <>

Returns a new elastic easing with the specified period p.

# d3.easeBackIn(t) <>

Anticipatory easing, like a dancer bending his knees before jumping off the floor. The degree of overshoot is configurable; if not specified, it defaults to 1.70158.

backIn

# d3.easeBackOut(t) <>

Reverse anticipatory easing; equivalent to 1 - backIn(1 - t).

backOut

# d3.easeBack(t) <>
# d3.easeBackInOut(t) <>

Symmetric anticipatory easing; scales backIn for t in [0, 0.5] and backOut for t in [0.5, 1].

backInOut

# back.overshoot(s) <>

Returns a new back easing with the specified overshoot s.

# d3.easeBounceIn(t) <>

Bounce easing, like a rubber ball.

bounceIn

# d3.easeBounce(t) <>
# d3.easeBounceOut(t) <>

Reverse bounce easing; equivalent to 1 - bounceIn(1 - t).

bounceOut

# d3.easeBounceInOut(t) <>

Symmetric bounce easing; scales bounceIn for t in [0, 0.5] and bounceOut for t in [0.5, 1].

bounceInOut

d3-ease's People

Contributors

dependabot[bot] avatar fil avatar mbostock avatar slreynolds avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

d3-ease's Issues

Need an ease interface.

An interface is needed to disambiguate transition.ease(ease) from transition.ease(function), where the latter is a function that returns an easing function, thereby allowing different easing functions to be used for different nodes in a transition. This is the same issue that lead to the creation of the symbol type interface in d3-shape, which is useful for applying a categorical symbol encoding to a scatterplot.

Admittedly, I don’t anticipate a lot of demand for customizing the easing function per node, but if we didn’t support it, the inconsistency is likely to lead to occasional confusion.

It’d still be nice if you could have optional parameters, though. Not sure how to do that…

transition.ease(d3.easePolyIn, 2);

Maybe if there’s more than one argument, it automatically gets converted to this?

transition.ease(d3.easePolyIn(2));

Well actually that wouldn’t work, because if you didn’t specify the optional argument, then the parameterizable easing function factory would get invoked separately for each node.

transition.ease(d3.easePolyIn); // Oops, an ambiguous function again!

Alternatively, the non-parameterizable easings could still be functions that return easing instances, so these would be equivalent…

transition.ease(d3.easeLinearIn);
transition.ease(d3.easeLinearIn());

But that means you’d need to do some manual easing, you’d need to change this:

var t = d3.easeCubicInOut(0.123);

To this:

var t = d3.easeCubicInOut().ease(0.123);

Which is a little verbose. (And slower, unless you stash the result of d3.easeCubicInOut() outside of your animation loop.) But then the transition.ease API is probably going to see wider use than the d3-ease API, and it’s not like the proposed API is bad. You can approximate the old API like so:

var cubicInOut = d3.easeCubicInOut().ease;

var t = cubicInOut(0.123);

broken Map polyfill

I mistakenly opened this issue earlier as "transpile es6 -> es5". After digging into the problem I was having more, I realized that the module is being transpiled, but that the Map polyfill is not being correctly, or maybe completely applied. Phantomjs tests break in any project this library is included in.

Return values of 0 and 1 are not always as expected

I ran into an issue animating with easeExpInOut where the end result was not exactly 1. The readme states "A good easing type should return 0 if t = 0 and 1 if t = 1", but a few of these do not.

https://observablehq.com/@danmarshall/d3-easing-at-0-1

easeBack 0: 0, 1: 1
easeBackIn 0: 0, 1: 0.9999999999999998
easeBackInOut 0: 0, 1: 1
easeBackOut 0: 2.220446049250313e-16, 1: 1
easeBounce 0: 0, 1: 1
easeBounceIn 0: 0, 1: 1
easeBounceInOut 0: 0, 1: 1
easeBounceOut 0: 0, 1: 1
easeCircle 0: 0, 1: 1
easeCircleIn 0: 0, 1: 1
easeCircleInOut 0: 0, 1: 1
easeCircleOut 0: 0, 1: 1
easeCubic 0: 0, 1: 1
easeCubicIn 0: 0, 1: 1
easeCubicInOut 0: 0, 1: 1
easeCubicOut 0: 0, 1: 1
easeElastic 0: 0, 1: 1.00048828125
easeElasticIn 0: -0.00048828124999999875, 1: 1
easeElasticInOut 0: -0.00024414062499999938, 1: 1.000244140625
easeElasticOut 0: 0, 1: 1.00048828125
easeExp 0: 0.00048828125, 1: 0.99951171875
easeExpIn 0: 0.0009765625, 1: 1
easeExpInOut 0: 0.00048828125, 1: 0.99951171875
easeExpOut 0: 0, 1: 0.9990234375
easeLinear 0: 0, 1: 1
easePoly 0: 0, 1: 1
easePolyIn 0: 0, 1: 1
easePolyInOut 0: 0, 1: 1
easePolyOut 0: 0, 1: 1
easeQuad 0: 0, 1: 1
easeQuadIn 0: 0, 1: 1
easeQuadInOut 0: 0, 1: 1
easeQuadOut 0: 0, 1: 1
easeSin 0: 0, 1: 1
easeSinIn 0: 0, 1: 0.9999999999999999
easeSinInOut 0: 0, 1: 1
easeSinOut 0: 0, 1: 1

Not sure if this is a bug or not, but hopefully this helps anyone else with this issue.

Remove non-in aliases.

I think we should remove exp as an alias for expIn and so on. For example, with cubic easing, you’re more likely to want cubicInOut than cubicIn.

README references obsolete APIs.

Right now several methods that use .exponent() have demo images that document their usage with a second argument specifying the exponent as an argument.

image

I think this refers to an older API of this module, and we'll need to update the images.

Easing/Transitioning in Chrome

I think this issue should probably be moved elsewhere, I'm just not really sure where else to put it yet. I'm more just trying to gather thoughts than anything else as I feel like other d3 users must have ran into this issue as well...

I have some code that translates a g element via d3-transition and d3-ease:

d3.select(this.container)
    .transition()
    .duration(1200)
    .ease(Easing)
    .attrTween('transform', () => {
        var i = Interpolate(0, 360)

        return function(t) {
            return `translate(105, 107.5) rotate(${i(t)})`
        })
    })
    .on('end', (d, i) => {
        if (this.props.loading) this._rotate() // Recalls this function
           else this._slideIn() // Ends the animation
    })

This code works flawlessly in Firefox and used to in Chrome as well, however after a recent Chrome update within the last year or so the transition started to look very odd. By this I mean instead of rotating 360 degrees and bouncing upon completion, it does this back and forth rocking while slowly orbiting around.

When I inspect the DOM, I see the rotate() values within the transform attribute being modified properly. Even slowing the animation doesn't help and, in fact, I've seen some other odd behaviors (such as rewinding when moving from 360 => 0) while trying to debug.

Again, I think this is probably not an issue with this repo or d3 in general and I'm happy to move elsewhere that would make more sense.

Cubic Bézier easing?

See d3/d3#1982. The only problem is that it’s kind of a pain to evaluate a cubic Bézier for y given x (rather than t), so it probably requires a numerical solution.

bounce optional params

bounce and bounceIn are documented as taking one parameter, but bounceOut and bounceInOut take up to three. Either these extra params should be documented for the first two functions, or they're copypasta from backIn[Out] and should be removed.

Thanks, and sorry for nitpicking.

Don’t ignore NaN parameters?

We currently ignore null, undefined and NaN easing parameters. I understand why we should ignore null and undefined, but ignoring NaN seems unnecessary. (I suppose it’s consistent with how d3-arrays ignores values, though.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.