Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2013 19:08
What would you like to do?
Marker animation along SVG <path> element with D3.js II

Marker animation along SVG "path" element with D3.js: animating "path" and marker movement synchronously.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>SVG path animation</title>
<link href="style.css" rel="stylesheet">
<script src=""></script>
<script src=""></script>
<!-- start -->
<div id="pathAnimation">
<script src="pathFollowAnimation.js"></script>
<!-- end -->
.defer(d3.xml, "wiggle.svg", "image/svg+xml")
function ready(error, xml) {
//Adding our svg file to HTML document
var importedNode = document.importNode(xml.documentElement, true);"#pathAnimation").node().appendChild(importedNode);
var svg ="svg");
var path ="path#wiggle").call(transition);
var startPoint = pathStartPoint(path);
var marker = svg.append("circle");
marker.attr("r", 7)
.attr("id", "marker")
.attr("transform", "translate(" + startPoint + ")");
//Get path start point for placing marker
function pathStartPoint(path) {
var d = path.attr("d"),
dsplitted = d.split(" ");
return dsplitted[1];
function transition(path) {
.attrTween("stroke-dasharray", tweenDash)
.each("end", function() {; });// infinite loop
function tweenDash() {
var l = path.node().getTotalLength();
var i = d3.interpolateString("0," + l, l + "," + l); // interpolation of stroke-dasharray style attr
return function(t) {
var marker ="#marker");
var p = path.node().getPointAtLength(t * l);
marker.attr("transform", "translate(" + p.x + "," + p.y + ")");//move marker
return i(t);
path {
fill: none;
stroke: #000;
stroke-width: 1px;
circle {
fill: red;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link

ghost commented Oct 26, 2014

Hello, I viewed your demo with several browsers.
The animation looks great on Chrome, Safari, but on Firefox the red point is much ahead of its path. Could you please check whether this problem can be fixed? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment