Last active
April 8, 2016 18:30
-
-
Save arnicas/4461a6c60bbbc4f6da84dffc461699ac to your computer and use it in GitHub Desktop.
Animate a dot with a few transitions
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> | |
<html> | |
<style> | |
circle { | |
fill: blue; | |
} | |
svg { | |
background-color: parchment; | |
} | |
</style> | |
<body> | |
<div id="vis"></div> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script> | |
var points = [{start: 10, mid: 30, end: 50, id: "A"}]; | |
var width = 500; | |
var height = 200; | |
var svg = d3.select("#vis").append("svg").attr("width", width).attr("height",height); | |
// make a scale for the min start and max end as your domain | |
var locScale = d3.scale.linear().range([10, width-10]).domain([20,50]); | |
var circles = svg.selectAll("circle") | |
.data(points, function(d) {return d.id;}); | |
circles | |
.enter() | |
.append("circle") | |
.attr("cx", function(d) { | |
return locScale(d.start); | |
}) | |
.attr("cy", height/2) | |
.attr("r", 10); | |
circles.transition() | |
.duration(1000) | |
.attr("cx", function(d) { | |
return locScale(d.mid); | |
}) | |
.transition() | |
.duration(200) | |
.attr("r", 30) | |
.transition() | |
.duration(200) | |
.attr("r", 10) | |
.transition() | |
.duration(1000) | |
.attr("cx", function(d) { | |
return locScale(d.end); | |
}); | |
circles.exit().remove(); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment