Skip to content

Instantly share code, notes, and snippets.

@sabha
Last active June 25, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabha/80400598171ba9881547426e0d267d4c to your computer and use it in GitHub Desktop.
Save sabha/80400598171ba9881547426e0d267d4c to your computer and use it in GitHub Desktop.
Random Rect
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="640" height="480" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var w = c.width;
var h = c.height;
// Create gradient
/*var grd = ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
*/
// Fill with gradient
//ctx.fillStyle = grd;
function rect(width = 2,height = 2){
var x = Math.floor(Math.random()*(w-5))+5;
var y = Math.floor(Math.random()*(h-5))+5;
var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
return {x,
y,
w:width+(Math.random()*100+10*plusOrMinus),
h:height+(Math.random()*100+10*plusOrMinus)
};
}
var colors_1 = [[64,98,187], [89,195,195], [235,235,235], [82,72,156], [249,42,130]];
var colors_2 = [[38,109,211], [52,64,85], [251,54,64], [96,95,94], [29,52,97]];
var colors_3 = [[254,195,166], [255,255,255], [229,209,176], [235,102,99], [182,211,198]];
var colors_4 = [[80,81,79], [242,95,92], [255,224,102], [36,123,160], [112,193,179]];
var colors_all = [colors_1,colors_2,colors_3,colors_4];
var colors = colors_all[Math.floor(Math.random() * colors_all.length)];
for(var i = 0; i < 200; i++){
var alpha = Math.random()+.6;
var color = colors[Math.floor(Math.random() * colors.length)];
color.push(alpha);
ctx.beginPath();
ctx.fillStyle = "rgba("+color.join(',')+")";
var r = rect();
ctx.fillRect(r.x,r.y,r.w,r.h);
ctx.closePath();
color.pop();
}
</script>
</body>
</html>
@sabha
Copy link
Author

sabha commented Jun 22, 2018

screen shot 2018-06-22 at 2 38 38 pm

@sabha
Copy link
Author

sabha commented Jun 22, 2018

screen shot 2018-06-22 at 3 00 36 pm

@sabha
Copy link
Author

sabha commented Jun 22, 2018

screen shot 2018-06-22 at 3 25 40 pm

@sabha
Copy link
Author

sabha commented Jun 22, 2018

screen shot 2018-06-22 at 3 24 14 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment