何かが悟れそうな画像ジェネレーター

Standard

http://bugcloud.com/worcloud/canvas/3/
 
 

class Block
  constructor:(row, col) ->
    @alive = true
    @row = row
    @col = col
  kill: () ->
    @alive = false

class Grid
  constructor: () ->
    @width = 22
    @height = 22
    winWidth = $(window).width()
    winHeight = $(window).height()
    @totalWidth = winWidth + (@width - (winWidth % @width))
    @totalHeight = winHeight + (@height - (winHeight % @height))
    @cols = Math.ceil(@totalWidth / @width) + 1
    @rows = Math.ceil(@totalHeight / @height) + 1
    @loopCount = @cols * @rows
    @totalWidth = @width * @cols
    @totalHeight = @height * @rows
    @blocks = []
    @blockCollection = []
    for i in [0..(@rows-1)]
      cols = []
      for j in [0..(@cols-1)]
        b = new Block(i, j)
        cols.push b
        @blockCollection.push b
      @blocks[i] = cols
  
  printBlocks: () ->
    str = ""
    for i in [0..(@rows-1)]
      for j in [0..(@cols-1)]
        b = @blocks[i][j]
        str += if b.alive then "□" else "■"
      str += "\n"
    console.log str
  pickupTryCount: 0
  pickup: (width, height) ->
    if @pickupTryCount > 60
      @pickupTryCount = 0
      return null
    else
      @pickupTryCount++
      result = null
      l = @blockCollection.length
      return null if l is 0
      b = @blockCollection[Math.floor(Math.random() * l)]
      if (b.col + width) <= @cols and (b.row + height) <= @rows
        for i in [b.row..(b.row+height-1)]
          for j in [b.col..(b.col+width-1)]
            block = @blocks[i][j]
            unless block.alive
              return this.pickup(width, height)
            else
              result = b
      else
        return this.pickup(width, height)
      @pickupTryCount = 0
      result

class Global
  constructor: () ->
    @canvas
    @context
  init: () ->
    me = this
    @canvas = document.getElementById 'canvas01'
    @context = @canvas.getContext '2d'

class Square
  constructor: (x, y, width, height) ->
    @x = x
    @y = y
    @width = width
    @height = height
    @radius = 3
    @radius2 = 14
    @r = Math.floor(Math.random() * 255)
  render: () ->
    _g.context.beginPath()
    _g.context.fillStyle = "hsla(#{@r}, 100%, 50%, 1)"
    _g.context.rect(@x, @y, @width, @height)
    console.log "x:#{@x}, y:#{@y}, w:#{@width}, h:#{@height}"
    _g.context.fill()
    _g.context.closePath()

_g = new Global()
_grid = new Grid()

$ ->
  $("#canvas01").attr("width", Math.floor($(document).width()))
  $("#canvas01").attr("height", Math.floor($(document).height()))
  _g.init()

  collection = []
  for i in [1.._grid.loopCount]
    collection.push {
      size: {
        col: Math.floor(1 + Math.random() * 4)
        row: Math.floor(1 + Math.random() * 4)
      }
    }
  for i in [1.._grid.loopCount]
    collection.push {
      size: {
        col: 1
        row: 1
      }
    }

  $.each collection, () ->
    block = _grid.pickup(this.size.col, this.size.row)
    unless block is null
      for i in [0..(this.size.row - 1)]
        for j in [0..(this.size.col - 1)]
          bb = _grid.blocks[block.row + i][block.col + j]
          bb.kill()
          newCollection = []
          for b in _grid.blockCollection
            newCollection.push b unless bb is b
          _grid.blockCollection = newCollection

      (new Square((_grid.width * block.col), (_grid.height * block.row), (_grid.width * this.size.col), (_grid.height * this.size.row))).render()
      _grid.printBlocks()

  img = _g.canvas.toDataURL("image/png")
  _g.context.clearRect(0, 0, _grid.totalWidth, _grid.totalHeight)
  $("body").append "<img src='#{img}' />"
  $("#canvas01").remove()

 
 
元ネタはこれです。
http://toyota.jp/86/

Facebook comments:

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>