Built with blockbuilder.org
Last active
December 8, 2018 02:09
-
-
Save max-l/cb28e26e32ee93b7dfe2261cec03ce55 to your computer and use it in GitHub Desktop.
fresh block
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: mit |
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> | |
<head> | |
<meta charset="utf-8"> | |
<!-- script src="https://d3js.org/d3.v3.min.js"></script--> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
path { | |
fill: none; | |
stroke: #000; | |
stroke-width: 3px; | |
} | |
circle { | |
fill: steelblue; | |
stroke: #fff; | |
stroke-width: 3px; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
//http://bl.ocks.org/KoGor/8162640 | |
var points = [ | |
[480, 200], | |
[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.line() | |
.curve( | |
//d3.curveCatmullRom.alpha(0) | |
d3.curveCardinal.tension(0) | |
) | |
//.tension(0) // Catmull–Rom | |
//.interpolate("cardinal") | |
); | |
svg.selectAll(".point") | |
.data(points) | |
.enter().append("circle") | |
.attr("r", 2) | |
.attr("transform", function(d) { return "translate(" + d + ")"; }); | |
var circle = svg.append("circle") | |
.attr("r", 9) | |
.attr("transform", "translate(" + points[0] + ")"); | |
transition(); | |
function transition() { | |
circle.transition() | |
.duration(10000) | |
.attrTween("transform", translateAlong(path.node())) | |
.each("end", 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