cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              o<body>
   <svg></svg>
   <script src="https://d3js.org/d3.v3.min.js"></script> 
   <script src="https://d3js.org/topojson.v1.min.js"></script>
   <script src="https://polyshrink.s3.amazonaws.com/texas.js"></script>
</body>
            
          
!
            
              svg,body {
   background-color: black;
   margin:0px;
   position:absolute;
   height:100%;
   width:100%;
   overflow:hidden;
}
path.state {
   stroke: #ffffff;
   stroke-width: 2px;
   fill: #ffffff;
   fill-opacity: 0.2;
   -webkit-transform: translate3d(0px,0px,0px);
   -moz-transform: translate3d(0px,0px,0px);
   -ms-transform: translate3d(0px,0px,0px);
   -o-transform: translate3d(0px,0px,0px);
   transform: translate3d(0px,0px,0px);
}

            
          
!
            
              var pageHeight = window.innerHeight; // Grab the page height.
var pageWidth = window.innerWidth; // Grab the page width.

/*
 * The projection
 */
var proj = new d3.geo.mercator().
   center([-100.084,30.8885]).  // Centre over Prince George
   translate([(pageWidth/2),pageHeight/2]). // Centre in window.
   scale(1 << 11); // zoom level ten shows the whole province

/*
 * The path generator. Insert the projection
 */
var path = d3.geo.path().
   projection(proj);

var svg = d3.select("svg"); 

/*
 * Convert the topojson object to geojson
 */
var texasData = topojson.
  feature(texasBnd,texasBnd.objects.texas_exploded).
  features;

/*
 * Insert the path 
 */
var texas = svg.selectAll("path.state").
   data(texasData).
   enter().append("path").
   attr("id","texas").
   attr("class","state").
   attr("d",path);

var state = path(texasData[0]);
var centroid = path.centroid(texasData[0]);

/*
 * Create array of nodes
 */
var nodes = path(texasData[0]).
   slice(1,-1). // Remove start and end marks
   split(/L/). // Split the nodes
   map(function(d,i) {
      return d.split(/,/). // split x and y
         map(function(d) { // Convert to integers.
            return parseInt(d,10);
         });
   });

/*
 * The lines connecting the nodes to the centroid
 */
var splines = svg.selectAll("path.spline").
   data(nodes).
   enter().append("path").
   attr("class","spline").
   attr("d",function(d) {
      return "M "+centroid[0]+" "+centroid[1]+" L "+d[0]+" "+d[1];
   }).
   style("display","none");


var circle = d3.selectAll("path.spline")[0].
   map(function (d,i) {
      var point = d.getPointAtLength(10);
      return point.x+" "+point.y;
   }).join(" L ");
circle = "M "+circle+" Z";

texas.datum().circle = circle; // Add to shape

splines.remove();

shrinkyGrow();

function shrinkyGrow () {
   var texas = d3.select("#texas");
   if (texas.classed("shrunk")) {
      texas.
         classed("shrunk",false).
         transition().
         ease("elastic").
         delay(1000).
         duration(2000).
         attr("d",path).
         each("end",shrinkyGrow);
   } else {
      texas.
         classed("shrunk",true).
         transition().
         duration(2000).
         ease("bounce").
         attr("d",function (d) {
            return d.circle;
         }).
         each("end",shrinkyGrow);
   }
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console