当 z-index=-1 时获取画布元素中的鼠标位置

发布于 2024-10-02 08:31:30 字数 411 浏览 6 评论 0原文

这似乎是一个简单的问题。我有一个正在使用Processing.js 编写的画布应用程序,我希望它出现在网页的背景中。为了实现这一点,我有以下 css:

#canvas-back {
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
}

其中 canvas-back 是包含实际画布的 div 的 id。画布本身设置为具有 html body 元素的宽度和高度。这对我来说是正确渲染的(背景中的画布,在我的 html 内容下方),但我无法再捕获变量 mouseX 和 mouseY(processing.js 用于获取鼠标坐标的变量)。

我该如何解决这个问题?这是不同的CSS问题还是有其他方法在processing.js中获取鼠标坐标?谢谢。

this seems like a simple problem. I have a canvas application that I am writing using Processing.js and I want it to appear in the background of a webpage. To accomplish this I have the following css:

#canvas-back {
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
}

Where canvas-back is the id of a div that contains the actual canvas. The canvas itself is set to have the width and height of the html body element. This renders correctly for me (the canvas in the background, beneath my html content) but I can no longer capture the variables mouseX and mouseY (what processing.js uses to get the mouse coordinates).

How can I fix this problem? Is it a matter of different css or is there another way to get mouse coordinates in processing.js? Thanks.

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

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

发布评论

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

评论(2

请别遗忘我 2024-10-09 08:31:30

我想我会发布我的解决方案,以防有人遇到同样的问题。由于我使用的是 jquery,所以我继续将新变量 jmouseX 和 jmouseY 设置为鼠标位置。

jQuery(document).ready(function(){
    $(document).mousemove(function(e){
        jmouseX = e.pageX;
        jmouseY = e.pageY;
    }); 
})

然后,每当我在processingjs应用程序中需要mouseX或mouseY时,我只需使用jmouseX和jmouseY。

Figured I'd post my solution in case anyone has the same problem. Since I was using jquery I went ahead and set new variables, jmouseX and jmouseY, to the mouse position.

jQuery(document).ready(function(){
    $(document).mousemove(function(e){
        jmouseX = e.pageX;
        jmouseY = e.pageY;
    }); 
})

Then whenever I need mouseX or mouseY in my processingjs app I just use jmouseX and jmouseY.

云醉月微眠 2024-10-09 08:31:30

好吧,由于您将 z-index 更改为 -1 并且您的 body 具有 z-index auto(在本例中您可以将其视为 0),因此您将无法在画布上注册任何点击,因为它位于 body 下方。你的身体会受到所有的点击等等。在这种情况下,您必须在主体本身上注册鼠标事件。

如果您必须在画布上点击,我唯一可以建议的就是将其设置为 z-index=1 并使其完全透明。但在这种情况下,您将无法单击正文中的任何元素,例如按钮、链接和文本框。否则,您必须捕获身体上的事件并手动将坐标提交到您正在使用的库中。

Well, since you change the z-index to -1 and your body has z-index auto, which you can think of as 0 in this case, you will not be able to register any clicks on your canvas, since it's under the body. Your body gets all the clicks and so on. You have to register your mouse events on the body itself in this case.

If you have to get clicks on the canvas, this only thing I can suggest is to make it z-index=1 and make it fully transparent. But in that case you will not be able to click on any elements that are in the body, like buttons, links and textboxes. Otherwise, you'll have to capture events on the body and manually submit the coordinats into the library that you are using.

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