#MonthOfCode - Day 16: square

My entry for the 16th day of the month of code. The theme for today is: square.

Code after the break.

square.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
document.addEventListener('DOMContentLoaded', function() {
var requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000/60);
};
})();

var ctx = document.getElementById('moc-16').getContext('2d');

function loop() {
ctx.fillStyle = "rgba(" + [rand(255),rand(255),rand(255),0.1 + 0.9 * Math.random()].join(',') + ')';
var size = rand(10, 100);
ctx.fillRect(
rand(500-size), rand(500-size),
size, size
);
requestAnimFrame(loop);
}

function rand(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min));
}

loop();
});