- Load external SVG file with d3
- Append SVG to document
- Use styletween to interpolate colors
Last active
August 29, 2015 14:14
-
-
Save tonyfast/c9d28bef6d46c1927409 to your computer and use it in GitHub Desktop.
d3 + styleTween + external SVG document
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
| <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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment