简体   繁体   English

在 Javascript 中用 Canvas 轻弹一个矩形?

[英]Flick a Rectangle with the Canvas in Javascript?

I want to be able to hold mousedown on a rectangle, move in a direction, let go, and upon release have the rectangle be "flicked" towards the appropriate direction.我希望能够在一个矩形上按住鼠标,朝一个方向移动,让 go,然后在释放时让矩形朝适当的方向“轻弹”。 I want to accomplish something like this:我想完成这样的事情:

How do I flick a DIV and have it move in the right direction? 如何轻弹 DIV 并使其朝正确的方向移动?

but with a rectangle drawn in the HTML5 Canvas.但在 HTML5 Canvas 中绘制了一个矩形。 How do I achieve this?我如何实现这一目标?

With the canvas, you'll want to use the same technique as in that post, except using multiple frames and erasing/drawing the rectangle each frame.使用 canvas,您需要使用与该帖子中相同的技术,除了使用多个帧并在每个帧中擦除/绘制矩形。 Pseudocode:伪代码:

setTimeout(1000/30, function () {
  canvas.erase()
  rect.update_position()
  canvas.drawRect(rect)
})

Alternately, you can use a physics engine like Box2dWeb to do all kinds of stuff like 'flicking' - http://code.google.com/p/box2dweb/或者,您可以使用 Box2dWeb 之类的物理引擎来执行各种操作,例如“轻弹” - http://code.google.com/p/box2dweb/

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

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