简体   繁体   English

我无法在我的正方形内的 canvas 中画一个圆圈

[英]I can't draw a circle in my canvas within my square

I have a problem, I am French and I don't speak English very well.我有一个问题,我是法国人,我的英语说得不太好。 So I explain you the problem: I draw a canvas in js composed of 6 rows and 7 columns then I try to draw a circle in it with a click event.所以我向你解释这个问题:我在 js 中绘制了一个由 6 行和 7 列组成的 canvas 然后我尝试用点击事件在其中绘制一个圆圈。 However I can't manage to position in the canvas the circle click well proportioned inside.但是我无法在 canvas 的圆圈中按比例调整到 position。 I tried to get the position of the mouse click to then position my circle in the square, I failed.我试图让鼠标点击的 position 变成 position 我在广场上的圈子,我失败了。 Then I changed my strategy, I tried to store the canvas in a table then try to push in the square of the canvas the circle, imagining that it is positioned in the limits of the square of my canvas but again failed.然后我改变了策略,我尝试将 canvas 存储在一个表中,然后尝试将 canvas 的正方形推入圆圈,想象它位于我的 canvas 正方形的范围内,但再次失败。

javascript
const canvas = document.getElementById('grille');
const ccc = canvas.getContext('2d');
let i = 0,l = 0;
let x = 10,y = 10;
let tabC = [];
let tabtab = [];
let cnvx = -40,cnvy = 560;
let posiX = 0,posiY = 0;
let test;
// fonction dessine caneva
(function (){
        for (l = 0; l < 6; l++) {
            for (i = 0; i < 7; i++) {
                tabtab.push(ccc.strokeRect(x, y, 100, 100));
                tabC.push('');
                x += 100;
            }
            y += 100;
            x = 10;
        }console.log("tabtab : "+tabtab);
})();

canvas.addEventListener('click', function (e) {
    posiX = e.offsetX;
    posiY = e.offsetY;
    console.log((e.offsetX)+ ' , ' + (e.offsetY));
    for (let j = 0;j<tabC.length;j++){
        if (tabC[j]===''){
            tabC.push('0');
            ccc.beginPath();
            ccc.fillStyle = 'red';
            tabtab.push(ccc.arc(e.offsetX, e.offsetY , 40, 30, 0, 2 * Math.PI));
            ccc.fill();
            ccc.closePath();
        }
    }

enter image description here在此处输入图像描述

I've created an example that places circles in a grid by calculating their grid coordinate .我创建了一个示例,通过计算它们的网格坐标将圆放置在网格中。

It converts the pixel coordinate of the click into a grid square by applying the math.floor function to subtracting the top corner of the grid and dividing by the size of the grid:它通过应用 math.floor function 减去网格的顶角并除以网格的大小,将点击的像素坐标转换为网格正方形:

  let gridX = Math.floor((mouseX - startX) / gridSize);
  let gridY = Math.floor((mouseY - startY) / gridSize);

It then calculates the center of that grid square by converting the grid coordinate back to a pixel coordinate and adding half the size of the grid square然后,它通过将网格坐标转换回像素坐标并加上网格正方形的一半大小来计算该网格正方形的中心

  let pixelX = startX + gridX * gridSize + gridSize / 2;
  let pixelY = startY + gridY * gridSize + gridSize / 2;

As a bonus, I've also added an array that keeps track of which squares have been filled, gridState.作为奖励,我还添加了一个数组来跟踪哪些方块已被填充,gridState。 Squares that have been filled will be true, others will be false.已填充的方块为真,其他方块为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM