Created
September 14, 2018 14:53
-
-
Save IbrahimTanyalcin/65ecb6e98b36b97051e1c91e076ad426 to your computer and use it in GitHub Desktop.
Performance comparison of SVG point along path - default
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"> | |
<body> | |
<style> | |
path { | |
fill: none; | |
stroke: #000; | |
stroke-width: 3px; | |
} | |
circle { | |
stroke: #fff; | |
stroke-width: 3px; | |
} | |
</style> | |
<script src="https://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var points = [ | |
[480, 200], | |
[580, 400], | |
[680, 100], | |
[780, 300], | |
[180, 300], | |
[280, 100], | |
[380, 400] | |
]; | |
var svg = d3.select("body").append("svg") | |
.attr("width", 960) | |
.attr("height", 500); | |
var path = svg.append("path") | |
.data([points]) | |
.attr("d", d3.svg.line() | |
.tension(0) // Catmull–Rom | |
.interpolate("cardinal-closed")); | |
svg.selectAll(".point") | |
.data(points) | |
.enter().append("circle") | |
.attr("r", 4) | |
.attr("transform", function(d) { return "translate(" + d + ")"; }); | |
var colorScale = d3.scale.category20().domain(Array.apply(null,Array(20)).map(function(d,i){return i})); | |
var circle = svg.selectAll("some").data(d3.range(100)).enter().append("circle") | |
.attr("r", function(d,i){return 10+Math.random()*10}) | |
.attr("fill",function(d,i){return colorScale(i)}) | |
.attr("transform", "translate(" + points[0] + ")"); | |
transition(); | |
function transition() { | |
var counter = 0; | |
circle.each(function(d,i){ | |
counter++; | |
d3.select(this).transition() | |
.duration(function(){return 2080 + i * 80}) | |
.attrTween("transform", translateAlong(path.node())) | |
.each("end", function(){ | |
--counter; | |
if(!counter){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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment