How to use fillStyle() and RGB(red, green, blue) with random colors to draw a rectangle on canvas in JavaScript

1 Answer

0 votes
<canvas id="canvas" width="1024" height="600"></canvas>
<script type="text/JavaScript">   

var ctx = document.getElementById('canvas').getContext('2d');
 
var r = Math.floor((Math.random() * 256));
var g = Math.floor((Math.random() * 256));
var b = Math.floor((Math.random() * 256));
ctx.fillStyle = 'rgb(' + r + ',' + g + ', ' + b + ')';
ctx.fillRect(10, 10, 90, 120);
 

</script>

 



answered May 26, 2016 by avibootz
...