HTML5画布调整了绘制线的大小

发布于 2025-02-07 03:32:23 字数 2214 浏览 2 评论 0原文

我正在使用HTML5帆布白板。

我将所有元素存储在舞台上。

现在我添加了一个绘图功能。我可以绘制并选择绘制对象。 现在我想调整对象的大小。

我的油漆

class Paint {
    constructor(
        id="", coordinates =[], color = '', type="paint", x=0, y=0, width=0, height=0
    ) {
        this.id = id;
        this.coordinates = coordinates,
        this.fillColor = color;
        this.type = type;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        
    }

    update() {
        let { coordinates, color,x,y,width,height} = this;
        ctx.save();
        let xx;
        let yy;
      
        
        coordinates.map((dot,index) =>{

            if(index == 0)
            {
                x = dot.startX;
                y = dot.startY;
                xx = dot.mouseX;
                yy = dot.mouseY;
            }

            if(dot.startX < x)
            {
                x=dot.startX; 
            }
            if(dot.startY < y)
            {
                y=dot.startY; 
            }
            if(dot.mouseX>xx)
            {
                xx=dot.mouseX;
            }
            if(dot.mouseY>yy)
            {
                yy=dot.mouseY;
            }

            let startX =  (dot.startX - dx);
            let startY =  (dot.startY - dy);
            let mouseX =  (dot.mouseX - dx);
            let mouseY =  (dot.mouseY - dy);

            ctx.beginPath();
            ctx.lineWidth = 5;
            ctx.lineCap = 'round';
            ctx.strokeStyle = '#ACD3ED';
            ctx.moveTo(startX, startY);
            ctx.lineTo(mouseX, mouseY);
            ctx.stroke();

        });
        this.x = x;
        this.y = y;
        this.width = xx-x;
        this.height = yy-y;
    
        ctx.restore();
    }
}

A类结果看起来像这样:

位置以这种格式存储:

如果要调整图形比例大小,如果我拖动计算的矩形角。

我希望有人可以帮助我解决这个问题。

提前致谢 坦率

I'm working on a HTML5 Canvas whiteboard.

I store all my elements on the stage in an array.

Now I added a drawing function. I can draw and select the drawn object.
Now I want to resize the object.

My Paint Class

class Paint {
    constructor(
        id="", coordinates =[], color = '', type="paint", x=0, y=0, width=0, height=0
    ) {
        this.id = id;
        this.coordinates = coordinates,
        this.fillColor = color;
        this.type = type;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        
    }

    update() {
        let { coordinates, color,x,y,width,height} = this;
        ctx.save();
        let xx;
        let yy;
      
        
        coordinates.map((dot,index) =>{

            if(index == 0)
            {
                x = dot.startX;
                y = dot.startY;
                xx = dot.mouseX;
                yy = dot.mouseY;
            }

            if(dot.startX < x)
            {
                x=dot.startX; 
            }
            if(dot.startY < y)
            {
                y=dot.startY; 
            }
            if(dot.mouseX>xx)
            {
                xx=dot.mouseX;
            }
            if(dot.mouseY>yy)
            {
                yy=dot.mouseY;
            }

            let startX =  (dot.startX - dx);
            let startY =  (dot.startY - dy);
            let mouseX =  (dot.mouseX - dx);
            let mouseY =  (dot.mouseY - dy);

            ctx.beginPath();
            ctx.lineWidth = 5;
            ctx.lineCap = 'round';
            ctx.strokeStyle = '#ACD3ED';
            ctx.moveTo(startX, startY);
            ctx.lineTo(mouseX, mouseY);
            ctx.stroke();

        });
        this.x = x;
        this.y = y;
        this.width = xx-x;
        this.height = yy-y;
    
        ctx.restore();
    }
}

A result could look like this:
Drawing

Positions are stored in this format:
enter image description here

If want to resize the drawing proportional if I drag a corner of the calculated rectangle.

I hope someone can help me with that problem.

thanks in advance
Frank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2025-02-14 03:32:23

我认为我接近解决方案,但是现在我得到了一些形式的变形。

我计算图纸的中心,然后检查每个点,如果其位置是较低或更高的中心,然后我通过我' m拖动。

stageObjects[i].coordinates.map(dot=>{
    // upper left corner
    if(dot.startX<stageObjects[i].xCenter && dot.startY<stageObjects[i].yCenter)
    {
        dot.startX -= distance;
        dot.startY -= distance;
    }
    if(dot.mouseX<stageObjects[i].xCenter && dot.mouseY<stageObjects[i].yCenter)
    {
        dot.mouseX -= distance;
        dot.mouseY -= distance;
    }
    // upper right corner
    if(dot.startX>stageObjects[i].xCenter && dot.startY<stageObjects[i].yCenter)
    {
        dot.startX += distance;
        dot.startY -= distance;
    }
    if(dot.mouseX>stageObjects[i].xCenter && dot.mouseY<stageObjects[i].yCenter)
    {
        dot.mouseX += distance;
        dot.mouseY -= distance;
    }
    // lower left corner 
    if(dot.startX<stageObjects[i].xCenter && dot.startY>stageObjects[i].yCenter)
    {
        dot.startX -= distance;
        dot.startY += distance;
    }
    if(dot.mouseX<stageObjects[i].xCenter && dot.mouseY>stageObjects[i].yCenter)
    {
        dot.mouseX -= distance;
        dot.mouseY += distance;
    }
    // lower right corner 
    if(dot.startX>stageObjects[i].xCenter && dot.startY>stageObjects[i].yCenter)
    {
        dot.startX += distance;
        dot.startY += distance;
    }
    if(dot.mouseX>stageObjects[i].xCenter && dot.mouseY>stageObjects[i].yCenter)
    {
        dot.mouseX += distance;
        dot.mouseY += distance;
    }
})

I think I'm close to a solution, but now I get some form deformations.

I calculate the center of the drawing and then I check each point, if it's position is lower or higher of the center and then I increase or decrease the position by the value of the distance between the start and end position of the corner that I'm dragging.

stageObjects[i].coordinates.map(dot=>{
    // upper left corner
    if(dot.startX<stageObjects[i].xCenter && dot.startY<stageObjects[i].yCenter)
    {
        dot.startX -= distance;
        dot.startY -= distance;
    }
    if(dot.mouseX<stageObjects[i].xCenter && dot.mouseY<stageObjects[i].yCenter)
    {
        dot.mouseX -= distance;
        dot.mouseY -= distance;
    }
    // upper right corner
    if(dot.startX>stageObjects[i].xCenter && dot.startY<stageObjects[i].yCenter)
    {
        dot.startX += distance;
        dot.startY -= distance;
    }
    if(dot.mouseX>stageObjects[i].xCenter && dot.mouseY<stageObjects[i].yCenter)
    {
        dot.mouseX += distance;
        dot.mouseY -= distance;
    }
    // lower left corner 
    if(dot.startX<stageObjects[i].xCenter && dot.startY>stageObjects[i].yCenter)
    {
        dot.startX -= distance;
        dot.startY += distance;
    }
    if(dot.mouseX<stageObjects[i].xCenter && dot.mouseY>stageObjects[i].yCenter)
    {
        dot.mouseX -= distance;
        dot.mouseY += distance;
    }
    // lower right corner 
    if(dot.startX>stageObjects[i].xCenter && dot.startY>stageObjects[i].yCenter)
    {
        dot.startX += distance;
        dot.startY += distance;
    }
    if(dot.mouseX>stageObjects[i].xCenter && dot.mouseY>stageObjects[i].yCenter)
    {
        dot.mouseX += distance;
        dot.mouseY += distance;
    }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文