Skip to content

Instantly share code, notes, and snippets.

@miklobit
Forked from mbostock/.block
Created September 17, 2014 15:24
Show Gist options
  • Save miklobit/fc52d3d6fddc06c275db to your computer and use it in GitHub Desktop.
Save miklobit/fc52d3d6fddc06c275db to your computer and use it in GitHub Desktop.
Point-Along-Path Interpolation
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Point-Along-Path Interpolation</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.4.6"></script>
<style type="text/css">
path {
cursor: pointer;
fill: #eee;
stroke: #666;
stroke-width: 1.5px;
}
circle {
fill: steelblue;
stroke: white;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script type="text/javascript">
var w = 960,
h = 500;
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
var path = svg.append("svg:path")
.attr("d", d3.svg.arc()
.innerRadius(h / 4)
.outerRadius(h / 3)
.startAngle(0)
.endAngle(Math.PI));
var circle = svg.append("svg:circle")
.attr("r", 6.5)
.attr("transform", "translate(0," + -h / 3 + ")");
function transition() {
circle.transition()
.duration(5000)
.attrTween("transform", translateAlong(path.node()))
.each("end", transition);
}
transition();
// Returns an attrTween for translating along the specified path element.
function translateAlong(path) {
var l = path.getTotalLength();
return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment