<canvas width="600" height="400"></canvas>
<script>
    var pantalla = document.querySelector('canvas');
    var pincel = pantalla.getContext('2d');
    pincel.fillStyle = 'grey';
    pincel.fillRect(0, 0, 600, 400);
    var puedoDibujar = false;
    var colores = ["red", "green", "blue"];
    var indiceColorActual = 0;
    function dibujarCirculo(evento) {
        //console.log(evento)
        if(puedoDibujar) {
            var x = evento.pageX - pantalla.offsetLeft;
            var y = evento.pageY - pantalla.offsetTop;
            pincel.fillStyle = colores[indiceColorActual];
            pincel.beginPath();
            pincel.arc(x, y, 5, 0, 2 * 3.14);
            pincel.fill();
        }
    }
    pantalla.onmousemove = dibujarCirculo;//permite capturar el movimiento de mouse.
    function habilitarDibujar(evento) {
        var x = evento.pageX - pantalla.offsetLeft;
        var y = evento.pageY - pantalla.offsetTop;
        console.log(x);
        console.log(y);
        if ((x > 4)&&
            (x< 50)&&
            (y > 4)&&
            (y<50)){
            puedoDibujar = false;
        } else
        if ((x > 54)&&
            (x< 100)&&
            (y > 4)&&
            (y<50)){
            puedoDibujar = false;
        } else 
        if ((x > 104)&&
            (x< 150)&&
            (y > 4)&&
            (y<50)){
            puedoDibujar = false;
        } else {
        puedoDibujar = true;
        }
    }
    function deshabilitarDibujar() {
        puedoDibujar = false;
    }
    pantalla.onmousedown = habilitarDibujar;//ejecuta código cuando el mouse esta presionado.
    pantalla.onmouseup = deshabilitarDibujar;//ejecuta código cuando el mouse el boton es soltado.
    function paletacolor (x,y, color){
        pincel.fillStyle = color;
        pincel.fillRect (x,y,50,50);
        pincel.strokeStyle = "black";
        pincel.strokeRect(x,y,50,50);
    }
    //Paleta de colores
    paletacolor(4,4,"red");
    paletacolor(54,4,"green");
    paletacolor(104,4,"blue");
    function selecionColor(evento){
        var x = evento.pageX - pantalla.offsetLeft;
        var y = evento.pageY - pantalla.offsetTop;
        console.log(x);
        console.log(y);
        if ((x > 4)&&
            (x< 50)&&
            (y > 4)&&
            (y<50)){
            indiceColorActual=0;
        } else
        if ((x > 54)&&
            (x< 100)&&
            (y > 4)&&
            (y<50)){
            indiceColorActual=1;
        } else 
        if ((x > 104)&&
            (x< 150)&&
            (y > 4)&&
            (y<50)){
            indiceColorActual=2;
    }
}
    pantalla.onclick= selecionColor;
</script>