Skip to content

Commit

Permalink
Merge branch 'symbol'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 25, 2015
2 parents 6e8cfca + 4a7590e commit f63766e
Show file tree
Hide file tree
Showing 19 changed files with 923 additions and 693 deletions.
258 changes: 160 additions & 98 deletions README.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions index.js
@@ -1 +1,11 @@
export {default as ease} from "./src/ease";
export {linearIn as linear, linearIn, linearOut, linearInOut} from "./src/linear";
export {quadIn as quad, quadIn, quadOut, quadInOut} from "./src/quad";
export {cubicIn as cubic, cubicIn, cubicOut, cubicInOut} from "./src/cubic";
export {polyIn as poly, polyIn, polyOut, polyInOut} from "./src/poly";
export {sinIn as sin, sinIn, sinOut, sinInOut} from "./src/sin";
export {expIn as exp, expIn, expOut, expInOut} from "./src/exp";
export {circleIn as circle, circleIn, circleOut, circleInOut} from "./src/circle";
export {bounceIn as bounce, bounceIn, bounceOut, bounceInOut} from "./src/bounce";
export {backIn as back, backIn, backOut, backInOut} from "./src/back";
export {elasticIn as elastic, elasticIn, elasticOut, elasticInOut} from "./src/elastic";
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "d3-ease",
"version": "0.1.5",
"version": "0.2.0",
"description": "Easing functions for smooth animation.",
"keywords": [
"d3",
Expand Down
23 changes: 9 additions & 14 deletions src/back.js
@@ -1,19 +1,14 @@
import number from "./number";

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

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

export function backInOut(s) {
return s = number(s, 1.70158) * 1.525, function(t) {
return ((t *= 2) < 1 ? t * t * ((s + 1) * t - s) : (t -= 2) * t * ((s + 1) * t + s) + 2) / 2;
};
export function backInOut(t, s) {
s = s == null ? 2.5949095 : s * 1.525;
return ((t *= 2) < 1 ? t * t * ((s + 1) * t - s) : (t -= 2) * t * ((s + 1) * t + s) + 2) / 2;
};
84 changes: 26 additions & 58 deletions src/ease.js
@@ -1,63 +1,31 @@
import {linearIn, linearOut, linearInOut} from "./linear";
import {quadIn, quadOut, quadInOut} from "./quad";
import {cubicIn, cubicOut, cubicInOut} from "./cubic";
import {polyIn, polyOut, polyInOut} from "./poly";
import {sinIn, sinOut, sinInOut} from "./sin";
import {expIn, expOut, expInOut} from "./exp";
import {circleIn, circleOut, circleInOut} from "./circle";
import {bounceIn, bounceOut, bounceInOut} from "./bounce";
import {backIn, backOut, backInOut} from "./back";
import {elasticIn, elasticOut, elasticInOut} from "./elastic";
var slice = Array.prototype.slice;

var standardEases = {
"linear-in": linearIn,
"linear-out": linearOut,
"linear-in-out": linearInOut,
"quad-in": quadIn,
"quad-out": quadOut,
"quad-in-out": quadInOut,
"cubic-in": cubicIn,
"cubic-out": cubicOut,
"cubic-in-out": cubicInOut,
"poly-in": cubicIn,
"poly-out": cubicOut,
"poly-in-out": cubicInOut,
"sin-in": sinIn,
"sin-out": sinOut,
"sin-in-out": sinInOut,
"exp-in": expIn,
"exp-out": expOut,
"exp-in-out": expInOut,
"circle-in": circleIn,
"circle-out": circleOut,
"circle-in-out": circleInOut,
"bounce-in": bounceIn,
"bounce-out": bounceOut,
"bounce-in-out": bounceInOut,
"back-in": backIn(),
"back-out": backOut(),
"back-in-out": backInOut(),
"elastic-in": elasticIn(),
"elastic-out": elasticOut(),
"elastic-in-out": elasticInOut()
};
function curry1(type, a) {
return function(t) {
return type(t, a);
};
}

var customEases = {
"poly-in": polyIn,
"poly-out": polyOut,
"poly-in-out": polyInOut,
"back-in": backIn,
"back-out": backOut,
"back-in-out": backInOut,
"elastic-in": elasticIn,
"elastic-out": elasticOut,
"elastic-in-out": elasticInOut
};
function curry2(type, a, b) {
return function(t) {
return type(t, a, b);
};
}

function curryN(type, args) {
args = slice.call(args);
args[0] = null;
return function(t) {
args[0] = t;
return type.apply(null, args);
};
}

export default function(type, a, b) {
var i = (type += "").indexOf("-");
if (i < 0) type += "-in";
return arguments.length > 1 && customEases.hasOwnProperty(type) ? customEases[type](a, b)
: standardEases.hasOwnProperty(type) ? standardEases[type]
: linearIn;
switch (arguments.length) {
case 1: return type;
case 2: return curry1(type, a);
case 3: return curry2(type, a, b);
default: return curryN(type, arguments);
}
};
37 changes: 15 additions & 22 deletions src/elastic.js
@@ -1,29 +1,22 @@
import number from "./number";
var tau = 2 * Math.PI;

var k = 1 / (2 * Math.PI);

export function elasticIn(a, p) {
a = Math.max(1, number(a, 1)), p = number(p, .3) * k;
var s = p * Math.asin(1 / a);
return function(t) {
return a * Math.pow(2, 10 * --t) * Math.sin((s - t) / p);
};
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 function elasticOut(a, p) {
a = Math.max(1, number(a, 1)), p = number(p, .3) * k;
var s = p * Math.asin(1 / a);
return function(t) {
return a * Math.pow(2, -10 * t) * Math.sin((t - s) / p) + 1;
};
export function elasticOut(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((t - p * Math.asin(1 / a)) / p) + 1;
};

export function elasticInOut(a, p) {
a = Math.max(1, number(a, 1)), p = number(p, .3) * 1.5 * k; // Note: treatment differs from Penner!
export function elasticInOut(t, a, p) {
a = a == null ? 1 : Math.max(1, a);
p = (p == null ? 0.3 : p) * 1.5 / tau; // Note: treatment differs from Penner!
var s = p * Math.asin(1 / a);
return function(t) {
return a * ((t = t * 2 - 1) < 0
? Math.pow(2, 10 * t) * Math.sin((s - t) / p)
: Math.pow(2, -10 * t) * Math.sin((t - s) / p) + 2) / 2;
};
return a * ((t = t * 2 - 1) < 0
? Math.pow(2, 10 * t) * Math.sin((s - t) / p)
: Math.pow(2, -10 * t) * Math.sin((t - s) / p) + 2) / 2;
};
3 changes: 0 additions & 3 deletions src/number.js

This file was deleted.

23 changes: 9 additions & 14 deletions src/poly.js
@@ -1,19 +1,14 @@
import number from "./number";

export function polyIn(e) {
return e = number(e, 3), function(t) {
return Math.pow(t, e);
};
export function polyIn(t, e) {
if (e == null) e = 3;
return Math.pow(t, e);
};

export function polyOut(e) {
return e = number(e, 3), function(t) {
return 1 - Math.pow(1 - t, e);
};
export function polyOut(t, e) {
if (e == null) e = 3;
return 1 - Math.pow(1 - t, e);
};

export function polyInOut(e) {
return e = number(e, 3), function(t) {
return ((t *= 2) <= 1 ? Math.pow(t, e) : 2 - Math.pow(2 - t, e)) / 2;
};
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;
};
102 changes: 57 additions & 45 deletions test/back-test.js
@@ -1,60 +1,72 @@
var tape = require("tape"),
ease = require("../").ease;
ease = require("../");

require("./inDelta");

tape('ease("back") returns the expected results', function(test) {
var e = ease("back");
test.inDelta(e(0.0), 0.000000);
test.inDelta(e(0.1), -0.014314);
test.inDelta(e(0.2), -0.046451);
test.inDelta(e(0.3), -0.080200);
test.inDelta(e(0.4), -0.099352);
test.inDelta(e(0.5), -0.087698);
test.inDelta(e(0.6), -0.029028);
test.inDelta(e(0.7), +0.092868);
test.inDelta(e(0.8), +0.294198);
test.inDelta(e(0.9), +0.591172);
test.inDelta(e(1.0), +1.000000);
test.equal(e(".9"), e(0.9), "numeric coercion");
tape("back is an alias for backIn", function(test) {
test.equal(ease.back, ease.backIn);
test.end();
});

tape('ease("back-in") is an alias for ease("back")', function(test) {
test.equal(ease("back-in"), ease("back"));
tape("backIn(t) returns the expected results", function(test) {
test.inDelta(ease.backIn(0.0), 0.000000);
test.inDelta(ease.backIn(0.1), -0.014314);
test.inDelta(ease.backIn(0.2), -0.046451);
test.inDelta(ease.backIn(0.3), -0.080200);
test.inDelta(ease.backIn(0.4), -0.099352);
test.inDelta(ease.backIn(0.5), -0.087698);
test.inDelta(ease.backIn(0.6), -0.029028);
test.inDelta(ease.backIn(0.7), +0.092868);
test.inDelta(ease.backIn(0.8), +0.294198);
test.inDelta(ease.backIn(0.9), +0.591172);
test.inDelta(ease.backIn(1.0), +1.000000);
test.end();
});

tape('ease("back-out") returns the expected results', function(test) {
var e = ease("back-out");
test.inDelta(e(0.0), 0.000000);
test.inDelta(e(0.1), 0.408828);
test.inDelta(e(0.2), 0.705802);
test.inDelta(e(0.3), 0.907132);
test.inDelta(e(0.4), 1.029028);
test.inDelta(e(0.5), 1.087697);
test.inDelta(e(0.6), 1.099352);
test.inDelta(e(0.7), 1.080200);
test.inDelta(e(0.8), 1.046451);
test.inDelta(e(0.9), 1.014314);
test.inDelta(e(1.0), 1.000000);
test.equal(e(".9"), e(0.9), "numeric coercion");
tape("backIn(t) coerces t to a number", function(test) {
test.strictEqual(ease.backIn(".9"), ease.backIn(0.9));
test.strictEqual(ease.backIn({valueOf: function() { return 0.9; }}), ease.backIn(0.9));
test.end();
});

tape('ease("back-in-out") returns the expected results', function(test) {
var e = ease("back-in-out");
test.inDelta(e(0.0), 0.000000);
test.inDelta(e(0.1), -0.037519);
test.inDelta(e(0.2), -0.092556);
test.inDelta(e(0.3), -0.078833);
test.inDelta(e(0.4), 0.089926);
test.inDelta(e(0.5), 0.500000);
test.inDelta(e(0.6), 0.910074);
test.inDelta(e(0.7), 1.078833);
test.inDelta(e(0.8), 1.092556);
test.inDelta(e(0.9), 1.037519);
test.inDelta(e(1.0), 1.000000);
test.equal(e(".9"), e(0.9), "numeric coercion");
tape("backOut(t) returns the expected results", function(test) {
test.inDelta(ease.backOut(0.0), 0.000000);
test.inDelta(ease.backOut(0.1), 0.408828);
test.inDelta(ease.backOut(0.2), 0.705802);
test.inDelta(ease.backOut(0.3), 0.907132);
test.inDelta(ease.backOut(0.4), 1.029028);
test.inDelta(ease.backOut(0.5), 1.087697);
test.inDelta(ease.backOut(0.6), 1.099352);
test.inDelta(ease.backOut(0.7), 1.080200);
test.inDelta(ease.backOut(0.8), 1.046451);
test.inDelta(ease.backOut(0.9), 1.014314);
test.inDelta(ease.backOut(1.0), 1.000000);
test.end();
});

tape("backOut(t) coerces t to a number", function(test) {
test.strictEqual(ease.backOut(".9"), ease.backOut(0.9));
test.strictEqual(ease.backOut({valueOf: function() { return 0.9; }}), ease.backOut(0.9));
test.end();
});

tape("backInOut(t) returns the expected results", function(test) {
test.inDelta(ease.backInOut(0.0), 0.000000);
test.inDelta(ease.backInOut(0.1), -0.037519);
test.inDelta(ease.backInOut(0.2), -0.092556);
test.inDelta(ease.backInOut(0.3), -0.078833);
test.inDelta(ease.backInOut(0.4), 0.089926);
test.inDelta(ease.backInOut(0.5), 0.500000);
test.inDelta(ease.backInOut(0.6), 0.910074);
test.inDelta(ease.backInOut(0.7), 1.078833);
test.inDelta(ease.backInOut(0.8), 1.092556);
test.inDelta(ease.backInOut(0.9), 1.037519);
test.inDelta(ease.backInOut(1.0), 1.000000);
test.end();
});

tape("backInOut(t) coerces t to a number", function(test) {
test.strictEqual(ease.backInOut(".9"), ease.backInOut(0.9));
test.strictEqual(ease.backInOut({valueOf: function() { return 0.9; }}), ease.backInOut(0.9));
test.end();
});

0 comments on commit f63766e

Please sign in to comment.