Skip to content

Commit

Permalink
easeVarying
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jul 23, 2020
2 parents 70abf61 + 60e9a09 commit fcdbb3a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -360,6 +360,10 @@ Specifies the transition [easing function](https://github.com/d3/d3-ease) for al

If a *value* is not specified, returns the current easing function for the first (non-null) element in the transition. This is generally useful only if you know that the transition contains exactly one element.

<a name="transition_easeVarying" href="#transition_easeVarying">#</a> <i>transition</i>.<b>easeVarying</b>(<i>factory</i>) [<>](https://github.com/d3/d3-transition/blob/master/src/transition/easeVarying.js "Source")

Specifies a factory for the transition [easing function](https://github.com/d3/d3-ease). The *factory* must be a function. It is invoked for each node of the selection, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element. It must return an easing function.

### Control Flow

For advanced usage, transitions provide methods for custom control flow.
Expand Down
14 changes: 14 additions & 0 deletions src/transition/easeVarying.js
@@ -0,0 +1,14 @@
import {set} from "./schedule.js";

function easeVarying(id, value) {
return function() {
var v = value.apply(this, arguments);
if (typeof v !== "function") throw new Error;
set(this, id).ease = v;
};
}

export default function(value) {
if (typeof value !== "function") throw new Error;
return this.each(easeVarying(this._id, value));
}
2 changes: 2 additions & 0 deletions src/transition/index.js
Expand Up @@ -4,6 +4,7 @@ import transition_attrTween from "./attrTween.js";
import transition_delay from "./delay.js";
import transition_duration from "./duration.js";
import transition_ease from "./ease.js";
import transition_easeVarying from "./easeVarying.js";
import transition_filter from "./filter.js";
import transition_merge from "./merge.js";
import transition_on from "./on.js";
Expand Down Expand Up @@ -64,5 +65,6 @@ Transition.prototype = transition.prototype = {
delay: transition_delay,
duration: transition_duration,
ease: transition_ease,
easeVarying: transition_easeVarying,
end: transition_end
};
43 changes: 43 additions & 0 deletions test/transition/easeVarying-test.js
@@ -0,0 +1,43 @@
var tape = require("tape"),
jsdom = require("../jsdom"),
d3_ease = require("d3-ease"),
d3_timer = require("d3-timer"),
d3_selection = require("d3-selection"),
state = require("./state");

require("../../");

tape("transition.easeVarying(factory) accepts an easing function factory", function(test) {
var document = jsdom("<h1 id='one'></h1><h1 id='two'></h1>"),
transition = d3_selection.select(document)
.selectAll("h1").data([{ exponent: 3 }, { exponent: 4 }])
.transition();
transition.easeVarying(d => d3_ease.easePolyIn.exponent(d.exponent));
test.equal(transition.ease()(.5), d3_ease.easePolyIn.exponent(3)(0.5));
test.end();
});

tape("transition.easeVarying(factory)’ factory receives datum, index, group with the node as this", function(test) {
var document = jsdom("<h1 id='one'></h1><h1 id='two'></h1>"),
transition = d3_selection.select(document)
.selectAll("h1").data([{ exponent: 3 }, { exponent: 4 }])
.transition();
transition.easeVarying(function(d, i, e) {
test.equal(e.length, 2);
test.equal(d.exponent, i + 3);
test.equal(this, e[i]);
return t => t;
});
test.end();
});

tape("transition.easeVarying() throws an error if the argument is not a function", function(test) {
var document = jsdom("<h1 id='one'></h1><h1 id='two'></h1>"),
transition = d3_selection.select(document)
.selectAll("h1").data([{ exponent: 3 }, { exponent: 4 }])
.transition();
test.throws(function() { transition.easeVarying(); });
test.throws(function() { transition.easeVarying("a"); });
test.end();
});

0 comments on commit fcdbb3a

Please sign in to comment.