Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save tonyfast/c9d28bef6d46c1927409 to your computer and use it in GitHub Desktop.

Select an option

Save tonyfast/c9d28bef6d46c1927409 to your computer and use it in GitHub Desktop.
d3 + styleTween + external SVG document

Demonstration

  • Load external SVG file with d3
  • Append SVG to document
  • Use styletween to interpolate colors
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<head>
<script src="//cdn.jsdelivr.net/g/d3js,zepto"></script>
</head>
<body>
<script>
;( function(){
// svg is an xml file
d3.xml('example.svg', function(d){
$(d).children().appendTo('body');
// change color
d3.select('path')
.transition()
.duration( 2000 )
.styleTween("fill", function(){ return d3.interpolate("black", "blue"); })
.transition()
.duration( 2000 )
.styleTween("fill", function(){ return d3.interpolate("blue", "yellow" ); });
});
$('body').prepend('<br>');
//UFO demo
d3.xml('ufo.svg', function(d){
$(d).children()
.prependTo('body');
color_of_interest = "#ffd200";
lights = d3.select('svg')
.selectAll('path')
.filter( function(){
var c = d3.select(this)
.style('fill');
return d3.rgb( c ).toString() == color_of_interest;
})
.call( function(s){
s.transition().duration(2000)
.styleTween("fill", function(){
return d3.interpolate(color_of_interest, "darkred" ) })
.transition().duration(2000)
.styleTween("fill", function(){
return d3.interpolate("darkred",color_of_interest );
})
.transition().duration(2000)
.styleTween("fill", function(){
return d3.interpolate(color_of_interest, "darkred" ) })
.transition().duration(2000)
.styleTween("fill", function(){
return d3.interpolate("darkred",color_of_interest );
});
});
});
})();
</script>
</body>
Display the source blob
Display the rendered blob
Raw
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