1
resposta

Acertando o alvo

a forma do professor é achei mais dinâmica, mas fiz dessa forma para conseguir resolver o desafio

<!DOCTYPE html>
<html lang="pt-br">
<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>alvo</title>
</head>
<body>
    <canvas width="600" height="400"></canvas>

<script>

    var canvas = document.querySelector('canvas');
    var context = canvas.getContext('2d');

    context.fillStyle = 'lightgray';
    context.fillRect(0, 0, 600, 400);

    var raio = 10;

    function desenhaCirculo(x, y, raio, cor) {

        context.fillStyle = cor;
        context.beginPath();
        context.arc(x, y, raio, 0, 2 * Math.PI);
        context.fill();
    }

    desenhaCirculo(300,200, raio + 20, 'red'); // maior círculo
    desenhaCirculo(300,200, raio + 10, 'white');
    desenhaCirculo(300,200, raio, 'red'); // menor circulo

    function dispara(evento) {

        var x = evento.pageX - canvas.offsetLeft;
        var y = evento.pageY - canvas.offsetTop;
        if(x >= 290 && x <= 310 && y >= 190 && y <= 210){
            alert('Acertou')
            console.log(x,y)
        }
    }

    canvas.onclick = dispara;

</script>
</body>
</html>
1 resposta

Muito BOMMM