Skip to content

Commit

Permalink
Fox #13 - adopt an easing interface.
Browse files Browse the repository at this point in the history
Rather than expose bare easing functions, this module now exports instances of
an easing interface: object.ease(t). For example:

    var te = d3.easeCubicIn(t); // BEFORE
    var te = d3.easeCubicIn.ease(t); // AFTER

The primary motivation of this change is to allow D3 transitions to differ-
entiate between an easing type and a function that returns an easing type, as
when the easing type should be determined separately for each selected element.
Also, it means you can use a class to implement a configurable easing type, if
you want, rather than a closure.

This commit also changes how optional parameters are specified for configurable
easing types. Rather than binding optional arguments to the easing function,
create a new instance using method chaining. For example, to create a cubic
easing instance using generic polynomial easing:

    var cubicIn = d3.easeBind(d3.easePolyIn, 3); // BEFORE
    var cubicIn = d3.easePolyIn.exponent(3); // AFTER

Equivalently, to call the function directly:

    var te = d3.easePolyIn(t, 3); // BEFORE
    var te = d3.easePolyIn.exponent(3).ease(t); // AFTER

By giving the parameters explicit names, usage is now self-documenting! Also, we
can eliminate d3.easeBind and easing functions are slightly faster.
  • Loading branch information
mbostock committed Feb 1, 2016
1 parent 60e9d37 commit f0ea8b5
Show file tree
Hide file tree
Showing 26 changed files with 763 additions and 772 deletions.
139 changes: 77 additions & 62 deletions README.md

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export {
default as easeBind
} from "./src/bind";

export {
linearIn as easeLinearIn,
linearOut as easeLinearOut,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-ease",
"version": "0.5.2",
"version": "0.6.0",
"description": "Easing functions for smooth animation.",
"keywords": [
"d3",
Expand Down
38 changes: 26 additions & 12 deletions src/back.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
export function backIn(t, s) {
s = s == null ? 1.70158 : +s;
return t * t * ((s + 1) * t - s);
}
var overshoot = 1.70158;

export function backOut(t, s) {
s = s == null ? 1.70158 : +s;
return --t * t * ((s + 1) * t + s) + 1;
}
export var backIn = (function backIn(s) {
return s = +s, {
overshoot: backIn,
ease: function(t) {
return t * t * ((s + 1) * t - s);
}
};
})(overshoot);

export function backInOut(t, s) {
s = s == null ? 1.70158 : +s;
return ((t *= 2) < 1 ? t * t * ((s + 1) * t - s) : (t -= 2) * t * ((s + 1) * t + s) + 2) / 2;
}
export var backOut = (function backOut(s) {
return s = +s, {
overshoot: backOut,
ease: function(t) {
return --t * t * ((s + 1) * t + s) + 1;
}
};
})(overshoot);

export var backInOut = (function backInOut(s) {
return s = +s, {
overshoot: backInOut,
ease: function(t) {
return ((t *= 2) < 1 ? t * t * ((s + 1) * t - s) : (t -= 2) * t * ((s + 1) * t + s) + 2) / 2;
}
};
})(overshoot);
31 changes: 0 additions & 31 deletions src/bind.js

This file was deleted.

24 changes: 16 additions & 8 deletions src/bounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ var b1 = 4 / 11,
b9 = 63 / 64,
b0 = 1 / b1 / b1;

export function bounceIn(t) {
return 1 - bounceOut(1 - t);
function bounce(t) {
return (t = +t) < b1 ? b0 * t * t : t < b3 ? b0 * (t -= b2) * t + b4 : t < b6 ? b0 * (t -= b5) * t + b7 : b0 * (t -= b8) * t + b9;
}

export function bounceOut(t) {
return t < b1 ? b0 * t * t : t < b3 ? b0 * (t -= b2) * t + b4 : t < b6 ? b0 * (t -= b5) * t + b7 : b0 * (t -= b8) * t + b9;
}
export var bounceIn = {
ease: function(t) {
return 1 - bounce(1 - t);
}
};

export function bounceInOut(t) {
return ((t *= 2) <= 1 ? 1 - bounceOut(1 - t) : bounceOut(t - 1) + 1) / 2;
}
export var bounceOut = {
ease: bounce
};

export var bounceInOut = {
ease: function(t) {
return ((t *= 2) <= 1 ? 1 - bounce(1 - t) : bounce(t - 1) + 1) / 2;
}
};
24 changes: 15 additions & 9 deletions src/circle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export function circleIn(t) {
return 1 - Math.sqrt(1 - t * t);
}
export var circleIn = {
ease: function(t) {
return 1 - Math.sqrt(1 - t * t);
}
};

export function circleOut(t) {
return Math.sqrt(1 - --t * t);
}
export var circleOut = {
ease: function(t) {
return Math.sqrt(1 - --t * t);
}
};

export function circleInOut(t) {
return ((t *= 2) <= 1 ? 1 - Math.sqrt(1 - t * t) : Math.sqrt(1 - (t -= 2) * t) + 1) / 2;
}
export var circleInOut = {
ease: function(t) {
return ((t *= 2) <= 1 ? 1 - Math.sqrt(1 - t * t) : Math.sqrt(1 - (t -= 2) * t) + 1) / 2;
}
};
24 changes: 15 additions & 9 deletions src/cubic.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export function cubicIn(t) {
return t * t * t;
}
export var cubicIn = {
ease: function(t) {
return t * t * t;
}
};

export function cubicOut(t) {
return --t * t * t + 1;
}
export var cubicOut = {
ease: function(t) {
return --t * t * t + 1;
}
};

export function cubicInOut(t) {
return ((t *= 2) <= 1 ? t * t * t : (t -= 2) * t * t + 2) / 2;
}
export var cubicInOut = {
ease: function(t) {
return ((t *= 2) <= 1 ? t * t * t : (t -= 2) * t * t + 2) / 2;
}
};
54 changes: 35 additions & 19 deletions src/elastic.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
var tau = 2 * Math.PI;
var tau = 2 * Math.PI,
amplitude = 1,
period = 0.3;

export function elasticIn(t, a, p) {
a = a == null ? 1 : Math.max(1, a);
p = (p == null ? 0.3 : p) / tau;
return a * Math.pow(2, 10 * --t) * Math.sin((p * Math.asin(1 / a) - t) / p);
}
export var elasticIn = (function elasticIn(a, p) {
var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
return {
amplitude: function(a) { return elasticIn(a, p * tau); },
period: function(p) { return elasticIn(a, p); },
ease: function(t) {
return a * Math.pow(2, 10 * --t) * Math.sin((s - t) / p);
}
};
})(amplitude, period);

export function elasticOut(t, a, p) {
a = a == null ? 1 : Math.max(1, a);
p = (p == null ? 0.3 : p) / tau;
return 1 - a * Math.pow(2, -10 * t) * Math.sin((+t + p * Math.asin(1 / a)) / p);
}
export var elasticOut = (function elasticOut(a, p) {
var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
return {
amplitude: function(a) { return elasticOut(a, p * tau); },
period: function(p) { return elasticOut(a, p); },
ease: function(t) {
return 1 - a * Math.pow(2, -10 * (t = +t)) * Math.sin((t + s) / p);
}
};
})(amplitude, period);

export function elasticInOut(t, a, p) {
a = a == null ? 1 : Math.max(1, a);
p = (p == null ? 0.3 : p) / tau;
var s = p * Math.asin(1 / a);
return ((t = t * 2 - 1) < 0
? a * Math.pow(2, 10 * t) * Math.sin((s - t) / p)
: 2 - a * Math.pow(2, -10 * t) * Math.sin((s + t) / p)) / 2;
}
export var elasticInOut = (function elasticInOut(a, p) {
var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
return {
amplitude: function(a) { return elasticInOut(a, p * tau); },
period: function(p) { return elasticInOut(a, p); },
ease: function(t) {
return ((t = t * 2 - 1) < 0
? a * Math.pow(2, 10 * t) * Math.sin((s - t) / p)
: 2 - a * Math.pow(2, -10 * t) * Math.sin((s + t) / p)) / 2;
}
};
})(amplitude, period);
24 changes: 15 additions & 9 deletions src/exp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export function expIn(t) {
return Math.pow(2, 10 * t - 10);
}
export var expIn = {
ease: function(t) {
return Math.pow(2, 10 * t - 10);
}
};

export function expOut(t) {
return 1 - Math.pow(2, -10 * t);
}
export var expOut = {
ease: function(t) {
return 1 - Math.pow(2, -10 * t);
}
};

export function expInOut(t) {
return ((t *= 2) <= 1 ? Math.pow(2, 10 * t - 10) : 2 - Math.pow(2, 10 - 10 * t)) / 2;
}
export var expInOut = {
ease: function(t) {
return ((t *= 2) <= 1 ? Math.pow(2, 10 * t - 10) : 2 - Math.pow(2, 10 - 10 * t)) / 2;
}
};
8 changes: 5 additions & 3 deletions src/linear.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function linearIn(t) {
return +t;
}
export var linearIn = {
ease: function(t) {
return +t;
}
};

export {
linearIn as linearOut,
Expand Down
38 changes: 26 additions & 12 deletions src/poly.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
export function polyIn(t, e) {
if (e == null) e = 3;
return Math.pow(t, e);
}
var exponent = 3;

export function polyOut(t, e) {
if (e == null) e = 3;
return 1 - Math.pow(1 - t, e);
}
export var polyIn = (function polyIn(e) {
return e = +e, {
exponent: polyIn,
ease: function(t) {
return Math.pow(t, e);
}
};
})(exponent);

export function polyInOut(t, e) {
if (e == null) e = 3;
return ((t *= 2) <= 1 ? Math.pow(t, e) : 2 - Math.pow(2 - t, e)) / 2;
}
export var polyOut = (function polyOut(e) {
return e = +e, {
exponent: polyOut,
ease: function(t) {
return 1 - Math.pow(1 - t, e);
}
};
})(exponent);

export var polyInOut = (function polyInOut(e) {
return e = +e, {
exponent: polyInOut,
ease: function(t) {
return ((t *= 2) <= 1 ? Math.pow(t, e) : 2 - Math.pow(2 - t, e)) / 2;
}
};
})(exponent);
24 changes: 15 additions & 9 deletions src/quad.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export function quadIn(t) {
return t * t;
}
export var quadIn = {
ease: function(t) {
return t * t;
}
};

export function quadOut(t) {
return t * (2 - t);
}
export var quadOut = {
ease: function(t) {
return t * (2 - t);
}
};

export function quadInOut(t) {
return ((t *= 2) <= 1 ? t * t : --t * (2 - t) + 1) / 2;
}
export var quadInOut = {
ease: function(t) {
return ((t *= 2) <= 1 ? t * t : --t * (2 - t) + 1) / 2;
}
};
24 changes: 15 additions & 9 deletions src/sin.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
var pi = Math.PI,
halfPi = pi / 2;

export function sinIn(t) {
return 1 - Math.cos(t * halfPi);
}
export var sinIn = {
ease: function(t) {
return 1 - Math.cos(t * halfPi);
}
};

export function sinOut(t) {
return Math.sin(t * halfPi);
}
export var sinOut = {
ease: function(t) {
return Math.sin(t * halfPi);
}
};

export function sinInOut(t) {
return (1 - Math.cos(pi * t)) / 2;
}
export var sinInOut = {
ease: function(t) {
return (1 - Math.cos(pi * t)) / 2;
}
};
Loading

0 comments on commit f0ea8b5

Please sign in to comment.