Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!