У меня есть круг и случайная точка, которую я поставил на нем. Эта случайная точка в настоящее время действительно случайна, но я хочу, чтобы она находилась в центре разделения сетки на заднем плане.
Я сделал фрагмент с текущей случайной точкой:
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const size = 512
canvas.width = size
canvas.height = size
// Draw grid
ctx.beginPath()
ctx.strokeStyle = '#000'
const gridDivisions = 10
const gridSize = size / gridDivisions
for (let i = 0; i <= gridDivisions; i++) {
ctx.moveTo(0, i * gridSize)
ctx.lineTo(size, i * gridSize)
ctx.moveTo(i * gridSize, 0)
ctx.lineTo(i * gridSize, size)
}
ctx.stroke()
ctx.closePath()
// Draw circle
const radius = 180
ctx.beginPath()
ctx.strokeStyle = '#F00'
ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
// Draw random point
const angle = Math.random() * Math.PI * 2
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#000'
ctx.translate(Math.cos(angle) * radius + size / 2, Math.sin(angle) * radius + size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
// Draw center
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#00F'
ctx.translate(size / 2, size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()<canvas id = "canvas"></canvas>и я хочу, чтобы черный крест был в центре случайное деление сетки, как это (в любом делении, которое сталкивается с кругом):



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вам просто нужно уменьшить координаты точки до размера ячейки, затем взять их целочисленные значения, уменьшить масштаб и добавить половину размера ячейки.
Проще показать, см. Фрагмент ниже:
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const size = 512
canvas.width = size
canvas.height = size
// Draw grid
ctx.beginPath()
ctx.strokeStyle = '#000'
const gridDivisions = 10
const gridSize = size / gridDivisions
for (let i = 0; i <= gridDivisions; i++) {
ctx.moveTo(0, i * gridSize)
ctx.lineTo(size, i * gridSize)
ctx.moveTo(i * gridSize, 0)
ctx.lineTo(i * gridSize, size)
}
ctx.stroke()
ctx.closePath()
// Draw circle
const radius = 180
ctx.beginPath()
ctx.strokeStyle = '#F00'
ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
// Draw random point
const angle = Math.random() * Math.PI * 2
let tx = Math.cos(angle) * radius + size / 2;
let ty = Math.sin(angle) * radius + size / 2;
[tx, ty] = [tx, ty].map(c => (c / gridSize | 0) * gridSize + gridSize / 2);
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#000'
ctx.translate(tx, ty)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()
// Draw center
ctx.save()
ctx.beginPath()
ctx.strokeStyle = '#00F'
ctx.translate(size / 2, size / 2)
ctx.moveTo(-5, 5)
ctx.lineTo(5, -5)
ctx.moveTo(5, 5)
ctx.lineTo(-5, -5)
ctx.stroke()
ctx.closePath()
ctx.restore()<canvas id = "canvas"></canvas>