简体   繁体   English

HTML Canvas使用mouseover事件制作线条

[英]HTML Canvas making lines using mouseover event

I am trying to write a code using HTML canvas that will create a line beginning where a mousemove event occurs. 我正在尝试使用HTML canvas编写代码,该代码将在发生mousemove事件的地方开始创建一行代码。 The line has a defined direction and should continue extending until it is off the screen. 该线具有确定的方向,应继续延伸直到离开屏幕。 The issue I am having is that every time I move the mouse a new line begins(this is good) but the previous line stops extending. 我遇到的问题是,每当我移动鼠标时,都会有新行开始(这很好),但是前一行会停止扩展。 I believe that the issue is because each new line is taking on a set of parameters with the same name as the previous line, however I am not certain that this is the issue, nor do I know how to fix it. 我认为问题是因为每个新行都具有一组与上一行相同名称的参数,但是我不确定这是问题所在,也不知道如何解决。

Here is a jsfiddle of my current code: https://jsfiddle.net/tdammon/bf8xdyzL/ 这是我当前代码的jsfiddle: https ://jsfiddle.net/tdammon/bf8xdyzL/

I start be creating an object named mouse that takes an x and y parameter. 我开始创建一个名为mouse的对象,该对象带有x和y参数。 The xbeg and ybeg will be the starting coordinates for my lines. xbegybeg将是我行的起始坐标。

let canvas = document.querySelector('canvas');
canvas.width = window.innerWidth;
canvas.height= window.innerHeight;

let c = canvas.getContext('2d');
let mouse ={
    x:undefined,
    y:undefined,
}

window.addEventListener("mousemove",function(event){
    mouse.x = event.x;
    mouse.y = event.y;
    xbeg = mouse.x;
    ybeg = mouse.y;
})

Next I create an animate function that continuously calls itself. 接下来,我创建一个动画函数,该函数连续调用自身。 I create a new line object which will take the xbeg and ybeg parameters for beginning points and xbeg+10 and ybeg+10 as ending point. 我创建一个新的线对象,该对象将以xbegybeg参数为起点,以xbeg+10ybeg+10作为终点。 The function then increments xbeg and ybeg . 然后,该函数将xbegybeg递增。 I would like this function to create new lines that do not stop extending whenever the mouse is moved. 我希望此函数创建每当鼠标移动时都不会停止扩展的新行。

function animate() {

    requestAnimationFrame(animate);

    new Line(xbeg,ybeg,xbeg+10,ybeg+10)

    c.beginPath();
    c.moveTo(xbeg,ybeg);

    c.lineTo(xbeg+10,ybeg+10);
    c.stroke();

    xbeg += 1;
    ybeg += 1;
}

I've added to your code an array for all your lines: let linesRy = []; 我在您的代码中添加了所有行的数组: let linesRy = []; and I've changed a bit your draw() function by adding this.endx++; this.endy++; 通过添加this.endx++; this.endy++; ,我对您的draw()函数进行了一些this.endx++; this.endy++; this.endx++; this.endy++;

also I'm using your commented out c.clearRect(0, 0, innerWidth, innerHeight); 我也在用你注释掉的c.clearRect(0, 0, innerWidth, innerHeight); since with every frame you redraw all the lines. 因为每帧都需要重画所有线条。 I hope this is what you need. 我希望这是您所需要的。

 let linesRy = []; let canvas = document.querySelector("canvas"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; let c = canvas.getContext("2d"); let mouse = { x: undefined, y: undefined }; let xbeg, ybeg; window.addEventListener("mousemove", function(event) { mouse.x = event.x; mouse.y = event.y; xbeg = mouse.x; ybeg = mouse.y; }); class Line { constructor(begx, begy, endx, endy, dx, dy, slope) { this.begx = begx; this.begy = begy; this.endx = endx; this.endy = endy; this.dx = endx - begx; this.dy = endy - begy; this.slope = dy / dx; } draw() { this.endx++; this.endy++; c.beginPath(); c.moveTo(this.begx, this.begy); c.lineTo(this.endx, this.endy); c.stroke(); } } //let xend = 420; //let yend = 220; function animate() { requestAnimationFrame(animate); c.clearRect(0, 0, innerWidth, innerHeight); linesRy.push(new Line(xbeg, ybeg, xbeg + 10, ybeg + 10, 10, 10, 1)); linesRy.forEach(l => { l.draw(); }); } animate(); 
 canvas{border:1px solid;} 
 <canvas></canvas> 

the variable c is taken local variable 变量c是局部变量

 function animate() { c = canvas.getContext('2d'); requestAnimationFrame(animate); new Line(xbeg,ybeg,xbeg+10,ybeg+10) c.beginPath(); c.moveTo(xbeg,ybeg); c.lineTo(xbeg+10,ybeg+10); c.stroke(); xbeg += 1; ybeg += 1; } 

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

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