Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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.

+ add another resource

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <svg viewBox="0 0 800 500" width="800" height="500">
  <defs>
    <filter id="goo">
      <feGaussianBlur in="SoureGraphic" stdDeviation="2" />
      <feColorMatrix mode="matrix"
                     values="1 0 0 0 0
                             0 1 0 0 0
                             0 0 1 0 0
                             0 0 0 5 -2" />
    </filter>
    <filter id="scratch">
      <feTurbulence baseFrequency="0.004 0.008"
                    numOctaves="8"
                    />
      <feDisplacementMap in="SourceGraphic"
                         scale="30" />
      <feGaussianBlur stdDeviation="4" />
      <feColorMatrix mode="matrix"
                     values="1 0 0 0 0
                             0 1 0 0 0
                             0 0 1 0 0
                             0 0 0 10 -5" />
    </filter>
  </defs>
  <g id="triangles"></g>
  <g id="links"></g>
</svg>
              
            
!

CSS

              
                body {
  align-items: center;
  
  // Background generated with https://components.ai/double-radial-gradients
  background-color: #D53C86;
  background-image:
    radial-gradient(ellipse at top, #f8d895 11%, transparent 70%),
    radial-gradient(ellipse at bottom, #FEB89D 11%, transparent 70%);
  
  display: flex;
  justify-content: center;
  margin: 0;
  min-height: 100vh;
}

svg {
  display: block;
}

#links {
  filter: url(#goo);
}

#triangles > polygon {
  filter: url(#scratch);
}
              
            
!

JS

              
                // Using a proxy object to back our dat.GUI controls so that it's
// easy to trigger redraws whenever the state changes.
const state = new Proxy({
  width: 800,
  height: 500,
  numPoints: 12,
  radius: 8,
  strokeWidth: 5,
  update: update
}, {
  set(state, key, value) {
    state[key] = value;
    update();
  }
});

const gui = new dat.GUI();
gui.add(state, "numPoints");
gui.add(state, "radius");
gui.add(state, "strokeWidth");

const canvasSettings = gui.addFolder("canvas");
canvasSettings.add(state, "width");
canvasSettings.add(state, "height");

gui.add(state, "update");

// Run to generate a new image.
function update() {
  // Random number generators for our x- and y-coordinates.
  const x = d3.randomUniform(0, state.width - (state.radius * 3));
  const y = d3.randomUniform(0, state.height - (state.radius * 3));

  // Cycle through these colors for each of the triangles.
  const triangleFill = d3.scaleOrdinal(
    ["#004158", "#0081AF", "transparent"]
  );

  // Generate points randomly in the SVG.
  let points = [];
  for (let i = 0; i < state.numPoints; i++) {
    points.push([x(), y()]);
  }

  // Calculate the voronoi diagram of the points, which we'll need
  // to calculate the delaunay triangles.
  const diagram = d3.voronoi(points);

  // Resize of the SVG.
  d3.select("svg")
      .attr("width", state.width)
      .attr("height", state.height)
      .attr("viewBox", `0 0 ${state.width} ${state.height}`);
  
  // Draw the triangles in their own group, which has a different
  // filter applied to it than the lines and vertices.
  d3.select("#triangles")
    .selectAll("polygon")
    .data(diagram.triangles(points))
    .join("polygon")
      .attr("points", (d) => d.map(p => p.join(",")).join(" "))
      .attr("fill", (d, i) => triangleFill(i));
  
  // Get the <g /> element that we put our lines and vertices in.
  const links = d3.select("#links");

  // Draw all of the links first as <line />s in the SVG
  links.selectAll("line")
    .data(diagram.links(points))
    .join("line")
      .attr("x1", (d) => d.source[0])
      .attr("y1", (d) => d.source[1])
      .attr("x2", (d) => d.target[0])
      .attr("y2", (d) => d.target[1])
      .attr("stroke", "#191924")
      .attr("stroke-width", state.strokeWidth);

  // Draw the vertices next, so they appear over the lines
  // (although this shouldn't matter since they are the same color)
  links.selectAll("circle")
    .data(points)
    .join("circle")
      .attr("cx", (d) => d[0])
      .attr("cy", (d) => d[1])
      .attr("r", state.radius)
      .attr("fill", "#191924");
}

// Run initial update to generate something.
update(state);
              
            
!
999px

Console