Inspired by Vox Media's article: Polygon feature design: SVG animations for fun and profit and Mike Bostock's example: Stroke Dash Interpolation.
Created
December 28, 2013 15:42
-
-
Save KoGor/8160770 to your computer and use it in GitHub Desktop.
PlayStation 4: SVG animation
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> | |
<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="pathAnimation.js"></script> | |
</div> | |
<!-- end --> | |
</body> | |
</html> |
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
queue() | |
.defer(d3.xml, "PS4.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"), | |
svgWidth = svg.attr("width"), | |
svgHeight = svg.attr("height"); | |
var paths = svg.selectAll("path") | |
.call(transition); | |
function transition(path) { | |
path.transition() | |
.duration(5000) | |
.attrTween("stroke-dasharray", tweenDash) | |
.each("end", function() { d3.select(this).call(transition); }); // infinite loop | |
} | |
function tweenDash() { | |
var l = this.getTotalLength(), | |
i = d3.interpolateString("0," + l, l + "," + l); // interpolation of stroke-dasharray attr | |
return function(t) { | |
return i(t); | |
}; | |
} | |
} |
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
path, line, ellipse { | |
fill: none; | |
stroke: #000; | |
stroke-width: 1px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment