<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Trocando de cor</title>
</head>
<body>
<canvas
style="border-radius: 8px"
id="drawArea"
width="600"
height="400"
></canvas>
<script>
var painting = document.querySelector("#drawArea");
var brushTool = painting.getContext("2d");
brushTool.fillStyle = "lightgrey";
brushTool.fillRect(0, 0, 600, 400);
var colors = ["blue", "red", "green"];
var setColor = 0;
function drawCircle(event, color) {
var x = event.pageX - painting.offsetLeft;
var y = event.pageY - painting.offsetTop;
color = colors[setColor];
brushTool.fillStyle = color;
brushTool.beginPath();
brushTool.arc(x, y, 10, 0, 2 * Math.PI);
brushTool.fill();
}
painting.onclick = drawCircle;
function changeColor() {
setColor++;
if (setColor >= colors.length) {
setColor = 0;
}
return false;
}
painting.oncontextmenu = changeColor;
</script>
</body>
</html>