简体   繁体   English

如何通过重用相同的功能在画布上动态绘制?

[英]How can I dynamically draw on the canvas by reusing the same function?

When a user clicks on the canvas, the coordinates of the cursor are set equal to the start point of a line on the canvas.当用户点击画布时,光标的坐标被设置为等于画布上一条线的起点。 Then when the user clicks again, the new coordinates are set for the end point of the line, and the line appears.然后当用户再次点击时,新坐标被设置为线的终点,线出现。 My problem is the function only works once.我的问题是该功能只能工作一次。 The user can create one line and that's it.用户可以创建一行,仅此而已。 I'm new to the canvas and normally I'd just do something like: document.createElement('div');我是画布的新手,通常我只会做类似的事情: document.createElement('div'); every time the target element is clicked to make the code reusable.每次单击目标元素以使代码可重用。 But in the canvas I can't do this.但是在画布中我不能这样做。

I've tried using creating document.createElement for canvas shapes, but that only works for HTML elements, not javascript canvas functions.我尝试使用为画布形状创建document.createElement ,但这仅适用于 HTML 元素,不适用于 javascript 画布函数。 I've also tried using if and statements to manipulate the current function, but that didn't lead anywhere.我还尝试使用 if 和 statements 来操作当前函数,但这并没有导致任何地方。 This is the code:这是代码:

canvas[0].addEventListener('mousedown', setPosition);
const xCoord = [];      
const yCoord = [];  
function setPosition(position) {
    let z = -1; 
    let posX = position.clientX; 
    let posY = position.clientY;
    xCoord.push(posX);
    yCoord.push(posY);  
    z++;
    draw.moveTo(xCoord[z], yCoord[z]);
    z++;
    draw.lineTo(xCoord[z], yCoord[z]);
    draw.stroke();  
}

As you can see the code only creates one line and doesn't work anymore after that because it's already been executed.正如您所看到的,代码只创建了一行,之后不再工作,因为它已经被执行了。 What I would like is code that creates javascript functions like draw.moveTo and draw.lineTo or something similar to that effect or perhaps another solution.我想要的是创建 javascript 函数的代码,如draw.moveTodraw.lineTo或类似效果的东西,或者可能是另一种解决方案。

Fixed it.修复。

It turned out that the code was executing multiple times, it just didn't appear that way because the line kept being drawn over and over in the same location.结果是代码执行了多次,只是没有那样显示,因为线一直在同一个位置被一遍又一遍地绘制。 I noticed this when it started becoming slightly darker.当它开始变暗时,我注意到了这一点。

The real issue was that I needed the index of the array to change every time the canvas was clicked.真正的问题是每次单击画布时我都需要更改数组的索引。 This would then alter the position and create a pen-like effect.这将改变位置并创建类似钢笔的效果。 Since the arrays contained the coordinates of the mouse and were being updated with each click, it made the most sense to set index equal to this value.由于数组包含鼠标的坐标并且每次单击都会更新,因此将索引设置为等于该值是最有意义的。

I did this by setting xCoord[xCoord.length - 2] for the start point and xCoord[xCoord.length - 1] for the end point.我通过设置这样做xCoord[xCoord.length - 2]为起点和xCoord[xCoord.length - 1]为结束点。 This would ensure that I obtain the updated start and end coordinates of the mouse cursor that were being pushed onto this array.这将确保我获得被推到这个数组上的鼠标光标的更新开始和结束坐标。

Then I made the if statement to make sure that the values were pushed onto the array before I could called them.然后我做了 if 语句以确保在我可以调用它们之前将值推送到数组中。

canvas[0].addEventListener('mousedown', setPosition);
const xCoord = [];      
const yCoord = [];  
let z= 0;
function setPosition (position){
    z++;
    let posX = position.clientX; 
    let posY = position.clientY;
    xCoord.push(posX);
    yCoord.push(posY);  
    if (z >= 1) {
        draw.moveTo(xCoord[xCoord.length - 2], yCoord[yCoord.length - 2]);
        draw.lineTo(xCoord[xCoord.length - 1], yCoord[yCoord.length - 1]);
        draw.stroke();
    }
}

Now I'm able to make this beauty... Drawing现在我可以创造出这种美……绘图

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

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