Skip to content

Instantly share code, notes, and snippets.

@evitolins
Last active December 23, 2015 00:19
Show Gist options
  • Save evitolins/6552508 to your computer and use it in GitHub Desktop.
Save evitolins/6552508 to your computer and use it in GitHub Desktop.
Create random colored squares... marginally useful.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Build Canvas
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
w = canvas.width,
h = canvas.height,
// Generate Random Rectangle
ww = getRandomInt(20, w),
hh = getRandomInt(20, h),
xx = getRandomInt(0, w/2),
yy = getRandomInt(0, h/2),
c = ["blue","yellow", "green", "orange", "purple", "red", "grey", "brown", "white"];
// Render Random Content
ctx.rect(xx, yy, ww, hh);
ctx.fillStyle = c[ getRandomInt (0, c.length - 1)];
ctx.fill();
document.body.appendChild(canvas);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment