javascript - Drawing Random Coloured Circles on html Canvas with js? -
i'm starting js , need little help! got these 2 functions, draw circles , splattered circles, need create for loop draw 10 random coloured circles , 10 random splattered circles! how can it? thanks
<canvas id="mycanvas" width="550" height="400"></canvas> <script> var randomcolour; var randomsize; var xpos; var ypos; var i; var j; randomcolour = '#' + math.random().tostring(16).substring(4); // random colour c = document.getelementbyid("mycanvas"); ctx = c.getcontext("2d"); function drawfilledcircle(size,xpos,ypos,colour){ //draw circle ctx.beginpath(); ctx.arc(xpos,ypos,size,0,2*math.pi); ctx.fillstyle = colour; ctx.fill(); } function drawsplatter(size,xpos,ypos,colour){ // draw splattered circle for(j=0;j<10;j++){ var splatsize = size / math.round(math.random()*30); drawfilledcircle(splatsize,xpos + math.round(math.random()*50),ypos + math.round(math.random()*50),colour); } } </script>
var randomcolour; var randomsize; var xpos; var ypos; var i; var j; c = document.getelementbyid("mycanvas"); ctx = c.getcontext("2d"); function drawfilledcircle(size, xpos, ypos, colour) { //draw circle ctx.beginpath(); ctx.arc(xpos, ypos, size, 0, 2 * math.pi); ctx.fillstyle = colour; ctx.fill(); } function drawsplatter(size, xpos, ypos, colour) { // draw splattered circle (j = 0; j < 10; j++) { var splatsize = size / math.round(math.random() * 30); drawfilledcircle(splatsize, xpos + math.round(math.random() * 50), ypos + math.round(math.random() * 50), colour); } } var maxsize = 30; var minsize = 10; var maxx = c.width; var maxy = c.height; function randoms() { var size = math.ceil(math.random() * maxsize); size = math.max(size, minsize); var x = math.floor(math.random() * maxx); var y = math.floor(math.random() * maxy); var colour = '#' + math.random().tostring(16).substr(2,6); return {size:size, x:x, y:y, colour:colour}; } (var = 0; < 10; i++) { var r = randoms(); drawfilledcircle(r.size, r.x, r.y, r.colour); r = randoms(); drawsplatter(r.size, r.x, r.y, r.colour); }
body { background-color:#222222; }
<canvas id="mycanvas" width="550" height="400"></canvas>
Comments
Post a Comment