This method of getting interpolated attribute values during a transition uses a timer ( as opposed to a tween function ) which can be started at the beginning of a transition and repeatedly finds the value of a given element's attribute until stopped (this could be stopped by a transition end event itself). The other option using a timer is to use the timer itself to transition an object. This approach can be found in this answer on SO.
Last active
July 15, 2017 00:57
-
-
Save Andrew-Reid/57fa5658a7522aceb5140c7c2bbccea1 to your computer and use it in GitHub Desktop.
Transition Values with a Timer
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
</style> | |
<body> | |
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body").append("svg") | |
.attr("width",width) | |
.attr("height",height); | |
var text = svg.append("text") | |
.attr("x",40) | |
.attr("y",440) | |
.text("From Tween Function x:"); | |
var circle = svg.append("circle") | |
.attr("cx",20) | |
.attr("cy",20) | |
.attr("r",10) | |
.attr("fill","orange"); | |
circle.transition() | |
.attr("cx",920) | |
.attr("cy",440) | |
.duration(5000) | |
var timer = d3.timer(function(t) { | |
if (t > 5000) this.stop(); | |
text.text("x:" + circle.attr("cx")); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment