Demonstrates how to use the getTotalLength and getPointAtLength methods on SVG path elements to interpolate a point along an existing path. Built with D3.
Last active
January 10, 2017 16:37
-
-
Save mbostock/1313857 to your computer and use it in GitHub Desktop.
Point-Along-Path Interpolation
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
license: gpl-3.0 |
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> | |
path { | |
cursor: pointer; | |
fill: #eee; | |
stroke: #666; | |
stroke-width: 1.5px; | |
} | |
circle { | |
fill: steelblue; | |
stroke: white; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); | |
var path = svg.append("path") | |
.attr("d", d3.svg.arc() | |
.innerRadius(height / 4) | |
.outerRadius(height / 3) | |
.startAngle(0) | |
.endAngle(Math.PI)); | |
var circle = svg.append("circle") | |
.attr("r", 6.5) | |
.attr("transform", "translate(0," + -height / 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made a fiddle to see it for myself: http://jsfiddle.net/6286X/