Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
how do i place a circle on specific places in grid (canvas)

so I am trying to make thisgame called GO and I need to place circles in the edges like this. I've made the grid and now need to place the circles in there. I 'v tried different things like using the arc in canvas with mouse position and placing it but it isn't working something else I tried is to make an array that checks where the lines cross but it still didn't do anything. but i may have done it wrong. I'm not sure what's wrong so hope you guys could help me find a way to place the circles on the board every time I click the mouse. I've deleted the things that didn't work and this is my code now:

const canvas = document.getElementById("myCanvas")
const ctx = canvas.getContext("2d")
//canvas.style.border = "1px solid black"
let w = ctx.canvas.width
let h = ctx.canvas.height


goBoard = []
goCheckBoard = []

function drawGrid(w, h) {

    for (x = 0; x <= w; x += 40) {
        for (y = 0; y <= h; y += 40) {
            ctx.moveTo(x, 0);
            ctx.lineTo(x, h);
            ctx.stroke();
            ctx.moveTo(0, y);
            ctx.lineTo(w, y);
            ctx.stroke();
        }
    }
}

drawGrid(400, 400)

this just makes the grid

one example i've tried is this:

mouseClicked = function () {
    ctx.beginPath()
    ctx.strokeStyle = "black"
    ctx.ellipse(mouseX, mouseY, 20, 20);
    ctx.stroke()
};

another:

function rtn(n, r){
    return Math.round(n/r)*r;
}
canvas.onclick = function(event){
    ellipse(rtn(event.clientX, 40), rtn(event.clientY, 40), 180, 180);
};

im not sure if that is the correct way to do it

maybe make an array of all the possible places to place the circle but im not sure how

here is a fiddle if you want to test it out https://jsfiddle.net/pwe0vx7o/

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This will get you close, but you need to figure out something with your rounding function.

canvas.onclick = function (event) {
  ctx.beginPath();
  const x = rtn(event.clientX, 40);
  const y = rtn(event.clientY, 40);
  ctx.ellipse(x - 20, y - 20, 10, 10, 0, 0, 360);
  ctx.fill();
};
about 4 years ago · Juan Pablo Isaza Relatório

0

you first need to change the ellipse(...) to ctx.ellipse(...), because ellipse isn't a global function, but a metod of the CanvasRenderingContext2D.

Through the docs I also found that this function takes 7 arguments so added ones for radiusX, radiusY, rotation, startAngle, endAngle. Here is also a fiddle for the finished result: https://jsfiddle.net/c6udn9gf/8/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda