Marker animation along SVG "path" element with D3.js Based on Mike's example: Point-Along-Path Interpolation.
Last active
March 8, 2018 07:54
Marker animation along SVG <path> element with D3.js
This file contains hidden or 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 lang="en"> | |
<meta charset="utf-8"> | |
<head> | |
<title>SVG path animation</title> | |
<link href="style.css" rel="stylesheet"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/queue.v1.min.js"></script> | |
</head> | |
<body> | |
<!-- start --> | |
<div id="pathAnimation"> | |
<script src="pathFollow.js"></script> | |
</div> | |
<!-- end --> | |
</body> | |
</html> |
This file contains hidden or 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
queue() | |
.defer(d3.xml, "wiggle.svg", "image/svg+xml") | |
.await(ready); | |
function ready(error, xml) { | |
//Adding our svg file to HTML document | |
var importedNode = document.importNode(xml.documentElement, true); | |
d3.select("#pathAnimation").node().appendChild(importedNode); | |
var svg = d3.select("svg"); | |
var path = svg.select("path#wiggle"), | |
startPoint = pathStartPoint(path); | |
var marker = svg.append("circle"); | |
marker.attr("r", 7) | |
.attr("transform", "translate(" + startPoint + ")"); | |
transition(); | |
//Get path start point for placing marker | |
function pathStartPoint(path) { | |
var d = path.attr("d"), | |
dsplitted = d.split(" "); | |
return dsplitted[1].split(","); | |
} | |
function transition() { | |
marker.transition() | |
.duration(7500) | |
.attrTween("transform", translateAlong(path.node())) | |
.each("end", transition);// infinite loop | |
} | |
function translateAlong(path) { | |
var l = path.getTotalLength(); | |
return function(i) { | |
return function(t) { | |
var p = path.getPointAtLength(t * l); | |
return "translate(" + p.x + "," + p.y + ")";//Move marker | |
} | |
} | |
} | |
} |
This file contains hidden or 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
path { | |
fill: none; | |
stroke: #000; | |
stroke-width: 1px; | |
} | |
circle { | |
fill: red; | |
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment