Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function textInterpolate(i) { | ||
return function(t) { | ||
this.textContent = i(t); | ||
}; | ||
} | ||
|
||
function textTween(value) { | ||
var t0, i0; | ||
function tween() { | ||
var i = value.apply(this, arguments); | ||
if (i !== i0) t0 = (i0 = i) && textInterpolate(i); | ||
return t0; | ||
} | ||
tween._value = value; | ||
return tween; | ||
} | ||
|
||
export default function(value) { | ||
var key = "text"; | ||
if (arguments.length < 1) return (key = this.tween(key)) && key._value; | ||
if (value == null) return this.tween(key, null); | ||
if (typeof value !== "function") throw new Error; | ||
return this.tween(key, textTween(value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var tape = require("tape"), | ||
jsdom = require("../jsdom"), | ||
d3_ease = require("d3-ease"), | ||
d3_timer = require("d3-timer"), | ||
d3_interpolate = require("d3-interpolate"), | ||
d3_selection = require("d3-selection"), | ||
state = require("./state"); | ||
|
||
require("../../"); | ||
|
||
tape("transition.textTween(value) defines a text tween using the interpolator returned by the specified function", function(test) { | ||
var root = jsdom().documentElement, | ||
interpolate = d3_interpolate.interpolateHcl("red", "blue"), | ||
ease = d3_ease.easeCubic, | ||
transition = d3_selection.select(root).transition().textTween(function() { return interpolate; }); | ||
|
||
d3_timer.timeout(function(elapsed) { | ||
test.deepEqual(root.textContent, interpolate(ease(elapsed / 250))); | ||
test.end(); | ||
}, 125); | ||
}); | ||
|
||
tape("transition.textTween() returns the existing text tween", function(test) { | ||
var root = jsdom().documentElement, | ||
factory = function() {}, | ||
transition = d3_selection.select(root).transition().textTween(factory); | ||
test.strictEqual(transition.textTween(), factory); | ||
test.end(); | ||
}); | ||
|
||
tape("transition.textTween(null) removes an existing text tween", function(test) { | ||
var root = jsdom().documentElement, | ||
factory = function() {}, | ||
transition = d3_selection.select(root).transition().textTween(factory); | ||
transition.textTween(undefined); | ||
test.strictEqual(transition.textTween(), null); | ||
test.end(); | ||
}); |