Last active
December 15, 2015 22:09
-
-
Save michiel/5331049 to your computer and use it in GitHub Desktop.
Visualize Math.random() in a canvas element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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