处理中的绝对鼠标位置

发布于 2024-07-12 22:29:14 字数 412 浏览 8 评论 0原文

(这是我在处理论坛上发表的帖子的复制粘贴,到目前为止我还没有得到答案。我想我不妨在这里尝试一下)。

处理是一种非常酷的绘制内容的方式,特别是对于网页。 只是作为参考 http://processing.org

我是处理新手,我用它来制作无闪光网站,所以我几乎是在画布上画画。

我的鼠标位置有问题,尽管坐标在绘图时将左上角视为位置 0,0; 实际的鼠标坐标将 0,0 视为浏览器窗口的左上角。

所以我的问题是,画布在居中的网页上运行,每当浏览器改变大小时,画布内鼠标的坐标也会改变。

有没有办法让鼠标相对于画布的坐标? 这样我就可以更改浏览器窗口的大小并且左上角的鼠标坐标始终为 0,0 ?

(This is a copy paste of a post I made on the Processing forum, where I haven't got an answer so far. I thought I might as well try here).

Processing is a very cool way to draw stuff, specially for the webpages. Just as a reference http://processing.org

I'm new to processing, I'm using it to make a flashless website, so I'm pretty much drawing on a canvas.

I'm having a problem with the mouse position, although the coordinates, when drawing, consider the top left corner to be position 0,0; the actual mouse coordinates consider the 0,0 to be the top left corner of the browser window.

So my problem is this, the canvas is operating on a centered web-page, whenever the browser changes size, so does the coordinates of the mouse within the canvas.

Is there any way to make the coordinates of the mouse relative to the canvas? So that I can change the size of my browser window and the top left corner will be always 0,0 for the mouse coordinates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虫児飞 2024-07-19 22:29:14

我对处理不熟悉,但是你不能只计算浏览器窗口左上角和画布左上角之间的差异吗?
ie(使用jquery)

$(window).onresize = function() {
 //offset return position realive to the document.
 var offset = $('#canvas').offset();
 window.canvasLeft = offset.left;
 window.canvasTop = offset.top;
}

然后你可以做类似的事情:

 relativeMouseLeftPosition = mouseLeftPosition() - window.canvasLeft;

你应该用画布区域的css选择器替换#canvas。

另请注意,window是全局对象,我在这里使用它来处理可能的范围问题。

I'm not familiar with processing, but can't you just calculate the difference between the top left corner of the browser window and the top left corner of the canvas?
i.e. (using jquery)

$(window).onresize = function() {
 //offset return position realive to the document.
 var offset = $('#canvas').offset();
 window.canvasLeft = offset.left;
 window.canvasTop = offset.top;
}

Then you can do something like:

 relativeMouseLeftPosition = mouseLeftPosition() - window.canvasLeft;

You should replace #canvas with a css selector for your canvas area.

Also note that window is the global object, I use it here to deal with possible scope problems.

云朵有点甜 2024-07-19 22:29:14

它比 Flash 更糟糕(处理草图是 Java 小程序 - Java 不太常见,资源密集程度更高,加载速度也很慢。)

处理确实不是为了创建网页而设计的。对于网站而言, ,有 processing.js,Processing 的 Javascript 端口。

snake 示例 访问鼠标。 由于它是 Javascript,并且画布是一个 div,所以坐标应该比 Java (它存在于它自己的 VM 世界中)更理智一些,但我可能是错的..

Processing really isn't intended for creating webpages.. It's worse than Flash for sites (processing sketches being Java applets - Java being less common, far more resource-intensive and slow to load..)

That said, there is processing.js, a Javascript port of Processing.

The snake example accesses the mouse. Since it is Javascript, and the canvas is a div, the coordinates should be a bit more sane than Java (which lives in it's own VM world), but I could be wrong..

念三年u 2024-07-19 22:29:14

您可以要求用户在使用前校准系统。 这并不完全是问题的答案,而是问题的解决方案。

只需在屏幕中央、左上角和右下角绘制一个红点即可。 要求用户单击它们并检索坐标。 然后,您就知道屏幕的角在哪里了。

You can ask the user to calibrate the system, before use. It's not fully an answer to the question, but a sollution to the problem.

Just draw a red dot in the center of the screen, top left and bottem right. Ask the user to click on them, and retrieve the coordinates. Then, you know where the corners of the screen are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文