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 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.

            
              # 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 = HeightMap.build 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(cell.site.x / 4), 
      y: Math.round(cell.site.y / 4) 
    cell.height = heightMap.get_cell localPoint.x, localPoint.y
  diagram

drawCell = (ctx, cell) ->
  ctx.beginPath()
  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
  ctx.closePath()

$ ->
  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)
    ctx.stroke()
    ctx.fill()

  document.body.appendChild scene
            
          
!
999px
Loading ..................

Console