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
  
relaxSites = (cells) ->
  p = 1 / cells.length * 0.1
  newSites = []
  for cell in cells
    rn = Math.random()
    continue if rn < p # probability of apoptosis
    site = cellCentroid(cell)
    dist = distance(site, cell.site)
    # don't relax too fast
    if dist > 2
      site.x = (site.x + cell.site.x) / 2
      site.y = (site.y + cell.site.y) / 2
    # probability of mytosis
    if rn > (1 - p)
      dist /= 2
      newSites.push
        x: site.x + (site.x - cell.site.x) / dist
        y: site.y + (site.y - cell.site.y) / dist
    newSites.push(site)
  newSites
  
buildRelaxedVoronoi = (width, height) ->
  sites = generateSites(width, height)
  diagram = buildVoronoi(width, height, sites)
  for i in [0...3]
    sites = relaxSites(diagram.cells)
    diagram = buildVoronoi(width, height, sites)
  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()
  
  
distance = (a, b) ->
  [dx, dy] = [a.x - b.x, a.y - b.y]
  Math.sqrt dx * dx + dy * dy

cellArea = (cell) ->
  area = 0
  for halfedge in cell.halfedges
    p1 = halfedge.getStartpoint()
    p2 = halfedge.getEndpoint()
    area += (p1.x * p2.y) - (p1.y * p2.x)
  area / 2

cellCentroid = (cell) ->
  [x, y] = [0, 0]
  for halfedge in cell.halfedges
    p1 = halfedge.getStartpoint()
    p2 = halfedge.getEndpoint()
    v  = p1.x * p2.y - p2.x * p1.y
    x += (p1.x + p2.x) * v
    y += (p1.y + p2.y) * v
  v = cellArea(cell) * 6
  {x: x / v, y: y / v}  

$ ->
  scene = document.createElement 'canvas'
  scene.width = scene.height = 512
  # build a tile map.
  # Tilemap size is the half of the canvas size
  diagram = buildRelaxedVoronoi 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