Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created October 24, 2018 17:58
Show Gist options
  • Select an option

  • Save fernandoc1/bf8b764585b8fe01d38dffc6ce02981a to your computer and use it in GitHub Desktop.

Select an option

Save fernandoc1/bf8b764585b8fe01d38dffc6ce02981a to your computer and use it in GitHub Desktop.
Code for creating random color dots in HTML5 canvas.
<html>
<body>
<canvas id="mainCanvas" width="300" height="300"></canvas>
</body>
</html>
<script>
var ctx = document.getElementById("mainCanvas").getContext("2d");
function drawDot(ctx, x, y)
{
var randomColor = getRandomColor();
ctx.beginPath();
ctx.arc(x, y, 2, 0, 2 * Math.PI);
ctx.fillStyle = randomColor;
ctx.fill();
ctx.strokeStyle = randomColor;
ctx.stroke();
}
setInterval(function ()
{
drawDot(ctx, Math.random() * 300, Math.random() * 300);
}, 10);
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment