Skip to content

Instantly share code, notes, and snippets.

@michiel
Last active December 15, 2015 22:09
Show Gist options
  • Select an option

  • Save michiel/5331049 to your computer and use it in GitHub Desktop.

Select an option

Save michiel/5331049 to your computer and use it in GitHub Desktop.
Visualize Math.random() in a canvas element
#
# Visualize random data on a canvas element
#
$ ->
canvas = $ "#canvas"
ctxt = canvas[0].getContext "2d"
blocksize = 32
height = 3
width = 8
id = ctxt.createImageData 1, 1
data = id.data
rand = ->
Math.floor Math.random() * 256
setData = ->
for i in [0..3]
data[i] = rand()
xOffset = 0
yOffset = 0
fillBlock = ->
for x in [0..blocksize - 1]
for y in [0..blocksize - 1]
setData()
ctxt.putImageData id, x + xOffset, y + yOffset
fillRow = ->
xOffset = 0
for x in [0..width]
fillBlock()
xOffset += blocksize
for y in [0..height]
fillRow()
yOffset += blocksize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment