简体   繁体   English

为什么我的画布框的移动在点击时会增加?

[英]Why does the movement of my canvas box increase on click?


The Js Fiddle attached draw a box where ever the mouse is clicked. 附带的Js Fiddle会在单击鼠标的地方绘制一个框。 It works as expected for the most part. 它在大多数情况下都可以正常工作。 Except every time a new box is drawn on the canvas it speeds up more and more every time to the point where it's way too fast. 除了每次在画布上绘制一个新框外,它每次都以越来越快的速度加速。

I've console.log ed the dx/dy values and they are not increasing in value so it's not that, I don't think. 我已经用console.log编辑了dx/dy值,但是它们的值并没有增加,所以我认为不是。

The dx value is what the position of the box is moving by. dx值是盒子移动的位置。

Can any one shed some light? 有人可以照亮吗?

 var canvas = document.getElementById('myCanvas'); var logger = document.getElementById('logger'); var ctx = canvas.getContext('2d'); var x = 0; var y = 0; var dx = -2; var dy = -2; var boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var boxSize = 20; var mousex, mousey; canvas.addEventListener('click',function(e){ mousex = e.clientX; mousey = e.clientY; x = mousex - canvas.offsetLeft-(boxSize/2); y = mousey - canvas.offsetTop-(boxSize/2); draw(); }); function draw(){ logger.innerHTML = "x: " + x + "y: " + y; ctx.clearRect(0,0,canvas.width,canvas.height); collistionDetection(); ctx.beginPath(); ctx.rect(x,y,boxSize,boxSize); ctx.fillStyle=boxColour; ctx.fill(); ctx.closePath(); x+=dx; y+=dy; requestAnimationFrame(draw); }; function collistionDetection(){ if(x<0 || x>canvas.width-boxSize){ dx = -dx; boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); }; if(y<canvas.offsetTop-(boxSize/2) || y>canvas.height-boxSize){ boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); dy = -dy; }; }; 
 * { padding: 0; margin: 0; } canvas { background: #eee; display: block; margin: 0 auto; margin-top:10px; } 
 <canvas id="myCanvas" width='480' height='320'></canvas> <div id="logger"></div> 

Your help is always much appreciated. 您的帮助始终令人感激。
Thanks 谢谢
moe

I think your requestAnimationFrames are not being cleared so your animation function is just being called more and more frequently every time you click. 我认为您的requestAnimationFrames不会被清除,因此您每次单击动画函数的频率就会越来越高。 I made a small change to your code below which seems to help. 我在下面对您的代码做了一些小的更改,这似乎有所帮助。

  var canvas = document.getElementById('myCanvas'); var logger = document.getElementById('logger'); var ctx = canvas.getContext('2d'); var x = 0; var y = 0; var dx = -2; var dy = -2; var boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var boxSize = 20; var mousex, mousey; var lastAnimationFrame; canvas.addEventListener('click',function(e){ mousex = e.clientX; mousey = e.clientY; x = mousex - canvas.offsetLeft-(boxSize/2); y = mousey - canvas.offsetTop-(boxSize/2); cancelAnimationFrame(lastAnimationFrame) draw(); }); function draw(){ logger.innerHTML = "x: " + x + "y: " + y; ctx.clearRect(0,0,canvas.width,canvas.height); collistionDetection(); ctx.beginPath(); ctx.rect(x,y,boxSize,boxSize); ctx.fillStyle=boxColour; ctx.fill(); ctx.closePath(); x+=dx; y+=dy; lastAnimationFrame = requestAnimationFrame(draw); }; function collistionDetection(){ if(x<0 || x>canvas.width-boxSize){ dx = -dx; boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); }; if(y<canvas.offsetTop-(boxSize/2) || y>canvas.height-boxSize){ boxColour = '#'+(Math.random()*0xFFFFFF<<0).toString(16); dy = -dy; }; }; 
  * { padding: 0; margin: 0; } canvas { background: #eee; display: block; margin: 0 auto; margin-top:10px; 
 <canvas id="myCanvas" width='480' height='320'></canvas> <div id="logger"></div> 

Because by each mouse click, you are calling draw once again, and this call, as apparent in your code, is recursive and keeps calling itself. 因为每次单击鼠标都将再次调用draw ,并且此调用(在代码中显而易见)是递归的,并且会不断调用自身。 So, by each click, you have a chain of calls to draw . 因此,每单击一次,便会有一连串的调用来draw

You can add a boolean to check whether draw has initially been called, and if so, prevent subsequent calls inside your draw function: 您可以添加一个布尔值来检查是否最初调用了draw ,如果是,请防止在draw函数内进行后续调用:

if(!initialized)
{
    requestAnimationFrame(draw);
    initialized = true;
}

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

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