基于鼠标移动的 HTML5 平移
我正在尝试在 HTML5 中实现在画布内“平移”的功能,但我不确定实现它的最佳方法。
目前 - 我正在尝试检测鼠标在画布上的位置,如果它在边缘的 10% 范围内,它将朝该方向移动,如下所示:
当前边缘检测:
canvas.onmousemove = function(e)
{
var x = e.offsetX;
var y = e.offsetY;
var cx = canvas.width;
var cy = canvas.height;
if(x <= 0.1*cx && y <= 0.1*cy)
{
alert("Upper Left");
//Move "viewport" to up and left (if possible)
}
//Additional Checks for location
}
我知道我可能可以通过在画布内创建路径并将事件附加到它们来实现这一点,但我没有太多地使用它们,所以我想我会在这里问。另外 - 如果可以进行“环绕”平移,那就太棒了(向左平移最终将到达右侧)。
摘要:我想知道在 HTML5 Canvas 中完成“平移”的最佳途径是什么。这不会使用图像,而是实际绘制的对象(如果这有什么不同的话)。如果可以的话,我很乐意回答任何问题。
演示:
I'm trying to implement functionality to "pan" inside a canvas in HTML5 and I am unsure about the best way to go about accomplishing it.
Currently - I am trying to detect where the mouse is on the canvas, and if it is within 10% of an edge, it will move in that direction, as shown:
Current Edge Detection:
canvas.onmousemove = function(e)
{
var x = e.offsetX;
var y = e.offsetY;
var cx = canvas.width;
var cy = canvas.height;
if(x <= 0.1*cx && y <= 0.1*cy)
{
alert("Upper Left");
//Move "viewport" to up and left (if possible)
}
//Additional Checks for location
}
I know I could probably accomplish this by creating paths within the canvas and attaching events to them, but I haven't worked with them much, so I thought I would ask here. Also - if a "wrapping" pan would be possible that would be awesome (panning to the left will eventually get to the right).
Summary: I am wondering what the best route is to accomplish "panning" is within the HTML5 Canvas. This won't be using images but actual drawn objects (if that makes any difference). I'll be glad to answer any questions if I can.
Demo:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您希望如何实现通过鼠标移动进行平移,但现在通常是“实时”平移,您可以四处拖动。我尝试稍微更新一下你的小提琴: http://jsfiddle.net/pimvdb/VWn6t/3/< /a>.
It depends on how you want panning with mouse movement to be implemented, but today it's often 'realtime' panning in that you can drag around. I tried to update your fiddle a little: http://jsfiddle.net/pimvdb/VWn6t/3/.
pimvdb 的小提琴很好地展示了这个概念,但实际上不起作用,至少对我来说不起作用。
这是一个固定版本: http://jsfiddle.net/VWn6t/173/
其肉质基本相同。
pimvdb's fiddle shows the concept nicely but doesn't actually work, at least not for me.
Here's a fixed version: http://jsfiddle.net/VWn6t/173/
The meat of it is basically the same.