cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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.

            
              <div id="chart"></div>
<h1>Layered Voronis</h1>
<!-- luizneves.com -->
            
          
!
            
              body {
  background: rgb(28,28,28);
  position: relative;
  margin: 0;
  padding: 0;
}

h1{
  font-weight: 300;
  font-family: sans-serif;
  margin: 0;
  padding-top: 10%;
  text-align: center;
  color: whitesmoke;
}

#chart {
  top: 0;
  position: absolute;
  /* width: 100%; */
  /* height: 100%; */
}

rect {
  fill:none;
  /* pointer-events: all; */
  /*try hiding the cursor... adds cool effect*/  
  /* cursor: none; */
}
/* default path variables.. feel free to uncomment other values to play with different colors easily */
path {
  /* pointer: none; */
  stroke: grey;
  /*stroke: whitesmoke;*/
  fill: none;
  /*fill: teal;*/
  /*fill: MediumTurquoise;*/
  /*fill: darkgreen;*/
  /*fill: black;*/
  fill-opacity: .2;
  stroke-opacity: .2;
  stroke-linejoin: round;
}
/* this is the polygons that will be moving around... try giving them a color! */
g path:first-child {
  /* fill: salmon; */
  /* stroke-opacity: .4; */
}

/* Every 6th polygon fill with a color */
g path:nth-child(6n) {
  /*fill: rgba(34,34,34,0);*/
  fill: rgb(25,164,151);
  /*fill: rgb(49,49,48);*/
  /*stroke: none;*/
}
            
          
!
            
              width = verge.viewportW();
height = verge.viewportH();


buildTess = function(width,height){
    // var counter = 0;
    var svg;
    var path,path2,path3;
    var animate;
    var points = 50;
    var width = width,
      height = height;
    var vertices,
        vertices2,
        vertices3;
    var point = [1,1];
    //setting x and y speed by % of viewport seems to give better performance then hard coded pixel amounts
    var xVelocity = width * 0.002;
    var yVelocity = height * 0.0025;
 

    loadVertices(points);

    function newVertices(points) {
      return d3.range(points).map(function(d) {
      return [Math.random() * width, Math.random() * height];
      });
    }

    var voronoi = d3.geom.voronoi()
        .clipExtent([[0, 0], [width, height]]);

    function zoomView() {
          var points = vertices.length;
          var fontSize = parseInt($("div.top h1").css("font-size").split("px")[0]);
          if (points >= 20) {
            points = points-10;
            $("div.top h1").css("font-size",fontSize+10);
          }
          loadVertices(points);
          render();

    };

    var drag = d3.behavior.drag()
        .on("dragstart", function() {
            //clear auto calling animate when dragging begins
            clearInterval(animate);
        })
        .on("drag", function(){
          //touch events
          touchXY = [d3.event.x,d3.event.y];
          point = touchXY;
          render();
        })
        .on("dragend", function() {
          animate = setInterval(function(){render();}, 5);
        })


    
      svg = d3.select("#chart").append("svg")
        .attr("width", width)
        .attr("height", height)
     //remove the following listeners if you simply want animation and no interaction 
        .on("mousemove", function() {
          //stop calling animate when mouse takes control
          clearInterval(animate);
          var mouseXY = d3.mouse(this);
          point = mouseXY;
          render();
          })
        .on("click",function(){
          var mouseXY = [d3.event.x,d3.event.y];
          point = mouseXY;
          //reduce number of nodes on screen to give illusion of zooming in
          zoomView(point);
        })
        .on('mouseleave', function() {
          //restart animation after mouse lives viewport
          animate  = setInterval(function(){render();}, 5);
        })
        .call(drag);
  
  
      path = svg.append("g").selectAll("path");
      path2 = svg.append("g").selectAll("path");
      path3 = svg.append("g").selectAll("path");


  svg.append("rect")
      .attr("width", width)
      .attr("height", height);

  //kick off continuous animation... raising the interval will  trigger the browser to paint and actually reduce performance
  animate = setInterval(function(){render();}, 5);

  function render () {
    var crntX = point[0];
    var crntY = point[1];
    var newX, newY;
    //if moving point is at the right or left edges switch x direction
    if (crntX >= width || crntX <= 0 ) {
      xVelocity *= -1;
    }
    //if moving point is at the top or bottom edges switch y direction
    if (crntY >= height || crntY <= 0 ) {
      yVelocity *= -1;
    }
    
    newX = crntX + xVelocity;
    newY = crntY + yVelocity;

    redraw([newX,newY]);
    point = [newX,newY];

    
  }

  function  loadVertices(points){
    vertices = newVertices(points);
    vertices2 = newVertices(points);
    vertices3 = newVertices(points);
  };

   function redraw(mouseXY) {
      vertices[0] = mouseXY;
      path = path.data(voronoi(vertices), polygon);
      path.exit().remove();
      path.enter().append("path")
          .attr("d", polygon);
      path.order();

            vertices2[0] = mouseXY;
      path2 = path2.data(voronoi(vertices2), polygon);
      path2.exit().remove();
      path2.enter().append("path")
          .attr("d", polygon);
      path2.order();

            vertices3[0] = mouseXY;
      path3 = path3.data(voronoi(vertices3), polygon);
      path3.exit().remove();
      path3.enter().append("path")
          .attr("d", polygon);
      path3.order();
    }

  function polygon(d) {
    return "M" + d.join("L") + "Z";
  }

  function getRandom() {
    return Math.random() * (10 - 3) + 3;
  }
};

buildTess(width,height)
            
          
!
999px
Loading ..................

Console