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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

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


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


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.






                # generate a new site, ensuring it's constrained
# by a good margin amount (1%)
newSite = (right, bottom) ->
  [left, top] = [right * 0.01, bottom * 0.01]
  maxWidth    = right - left * 2
  maxHeight   = bottom - top * 2
    x: Math.round(left + (Math.random() * maxWidth))
    y: Math.round(left + (Math.random() * maxHeight))

heightMap = 128, 128
voronoi = new Voronoi()

generateSites = (width, height) ->
  # for 512x512 => 16384 points
  sitesCount = Math.ceil Math.sqrt width * height * (width + height)
  (newSite(width, height) for i in [0...sitesCount])

# builds a voronoi diagram with the desired widht and height
buildVoronoi = (width, height) ->
  sites = generateSites(width, height)
  diagram = voronoi.compute sites,
    # maybe it isn't clear but the following are:
    # x left, x right, y top, y bottom 
    { xl: 0, xr: width, yt: 0, yb: height }
  for cell in diagram.cells
    # translate voronoi site in a range 0-128 (heightmap size)
    localPoint = 
      x: Math.round( / 4), 
      y: Math.round( / 4) 
    cell.height = heightMap.get_cell localPoint.x, localPoint.y

drawCell = (ctx, cell) ->
  vertex = cell.halfedges[0].getStartpoint()
  ctx.moveTo vertex.x, vertex.y
  for halfedge in cell.halfedges
    vertex = halfedge.getEndpoint()
    ctx.lineTo vertex.x, vertex.y

$ ->
  scene = document.createElement 'canvas'
  scene.width = scene.height = 512
  # build a tile map.
  # Tilemap size is the half of the canvas size
  diagram = buildVoronoi 512, 512
  ctx = scene.getContext '2d'

  # draw a 2px circle for each tilemap location
  for cell in diagram.cells
    height = cell.height
    ctx.fillStyle = ctx.strokeStyle = "rgb(#{height},#{height},#{height})"
    drawCell(ctx, cell)

  document.body.appendChild scene