Hola, aqui les dejo una manera de realizar el ejercicio con tan solo 55 lineas de codigo(10 son separaciones de objetivo) ¡Espero les guste! Cualquier pregunta estoy al pendiente
<canvas width="600" height="400">
</canvas>
<script>
var screen = document.querySelector("canvas");
var pencil = screen.getContext("2d");
pencil.fillStyle = "lightgray";
pencil.fillRect(0,0,600,400);
squareDraw(0,0,"Red");
squareDraw(50,0,"green");
squareDraw(100,0,"blue");
screen.onmousemove = drawing;
var CanDraw = false;
function squareDraw(x,y,color){
pencil.fillStyle = color;
pencil.fillRect(x,y,50,50);
}
function drawing(event){
if(CanDraw==true){
var x = event.pageX - screen.offsetLeft;
var y = event.pageY - screen.offsetTop;
if((x>=0)&&(y>=55)||(x>=155)&&(y>=0)){
pencil.beginPath();
pencil.arc(x,y,10,0,2*Math.PI);
pencil.fill();
}
}
}
function chooseColor(event){
var x = event.pageX - screen.offsetLeft;
var y = event.pageY - screen.offsetTop;
if((x<50)&&(x>0)&&(y<50)&&(y>0))
pencil.fillStyle = "red";
else if((x>=50)&&(x<100)&&(y<50)&&(y>0))
pencil.fillStyle = "green";
else if((x>=100)&&(x<150)&&(y<50)&&(y>0))
pencil.fillStyle = "blue";
}
function drawingOn(){
CanDraw = true;
}
function drawingOff(){
CanDraw = false;
}
screen.onclick = chooseColor;
screen.onmousedown = drawingOn;
screen.onmouseup = drawingOff;
</script>