JavaScript/webGL从一个点击事件中定期鼠标坐标,直到第二个事件

发布于 2025-01-22 04:19:13 字数 1551 浏览 4 评论 0原文

我有一个程序,可以模拟WebGL中的布(由春季和顶点制成)。该程序必须执行的操作是允许用户单击布上的一个点,在鼠标后将其拖动,如果用户再次单击,请将其释放。我需要检查哪个点击事件是启动拖动并终止它的事件,然后我使用了一个全局变量“单击”,将其设置为false,并在每次触发事件时进行互换。现在,对于拖动,我需要定期进行鼠标的坐标,但是我使用“ mousemove”事件是最好的解决方案:如果将“单击”设置为true,这意味着我们处于“拖动阶段” ,然后必须调用Mousemove事件并继续工作,直到下一次点击事件为止。问题是我不知道如何将Mousemove连接到点击事件:如果我将Mousemove事件放在点击事件之外,我们永远不会输入,如果我将Mousemove事件放在点击事件中,另外,如果“单击”变量从Trail到False更改,则始终调用Mousemove事件。代码的骨架是以下一个:

var clicked = false;


function exec() {

        let web_gl = document.getElementById("webgl-canvas").getContext('webgl2');
        web_gl.canvas.addEventListener('click', (e) => {
            clicked = !clicked;
            console.log(clicked);
            if (clicked == true) {
                web_gl.canvas.addEventListener('mousemove', (e) => {
                    //do something for dragging
                });
            };
        });
}

function main() {
    exec();
};

也在这里,如果控制台以正确的方式同时打印false和true,我们始终在IF条件下输入,似乎条件看到“单击”始终是正确的。 另一个选择是:

var clicked = false;


function exec() {

        let web_gl = document.getElementById("webgl-canvas").getContext('webgl2');
        web_gl.canvas.addEventListener('click', (e) => {
            clicked = !clicked;
            console.log(clicked);
        });
        if (clicked == true) {
            web_gl.canvas.addEventListener('click', (e) => {
                \\do something for dragging
            });
        }
}

function main() {
    exec();
};

在这种情况下,我们从未在此事件中输入,我不明白为什么。 有人对这个问题有暗示,还是一个更好的主意来处理我需要实施的内容?

I have a program that simulates a cloth (made by spring and vertices) in WebGL. What the program must do is to permit the user to click on a point in the cloth, dragging it following the mouse and if the user clicks again, release it. I need to check which click event is the one that starts the dragging and which one terminates it, then I used a global variable "clicked", set it to false at the beginning and swapped everytime the event click is triggered. Now for the dragging I need periodically to take the coordinates of the mouse, then I though using a "mousemove" event was the best solution: if the "clicked" is set to true, this means that we are in the "dragging phase", then the mousemove event must be called and continue to work until the next click event. The problem is that I don't know how to use the mousemove in connection to the click event: if I put the mousemove event outside the click event, we never enter in that, and if I put the mousemove event inside the click event, also if the "clicked" variable change from true to false, the mousemove event is always called. The skeleton of the code is the following one:

var clicked = false;


function exec() {

        let web_gl = document.getElementById("webgl-canvas").getContext('webgl2');
        web_gl.canvas.addEventListener('click', (e) => {
            clicked = !clicked;
            console.log(clicked);
            if (clicked == true) {
                web_gl.canvas.addEventListener('mousemove', (e) => {
                    //do something for dragging
                });
            };
        });
}

function main() {
    exec();
};

Here also if the console prints both false and true in the correct way, we enter always in the if condition, it seems the condition sees "clicked" always true.
The other option was this one:

var clicked = false;


function exec() {

        let web_gl = document.getElementById("webgl-canvas").getContext('webgl2');
        web_gl.canvas.addEventListener('click', (e) => {
            clicked = !clicked;
            console.log(clicked);
        });
        if (clicked == true) {
            web_gl.canvas.addEventListener('click', (e) => {
                \\do something for dragging
            });
        }
}

function main() {
    exec();
};

In this case we never enter in the event, and I don't understand why.
Does someone have an hint about this problem, or a better idea to handle what I need to implement?

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

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

发布评论

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

评论(2

月隐月明月朦胧 2025-01-29 04:19:13

对您的第二个片段进行调查:您的代码是异步执行的,事件处理程序在发出事件之前不会执行,因此单击 仍将处于其初始状态(false)时评估添加Mousemove处理程序的条件,因此不会添加,因此永远不会执行。

您要做的是在中添加两个事件处理程序,然后检查 mousemove事件处理程序,如果单击是是 code> true 。

顺便提一句。您将要实现的逻辑意味着用户必须单击(鼠标向下及向上)激活拖动并再次单击以解开接触,而不是常见的UI练习:单击(Mousedown)并保持拖动。

Adressing your second snippet: your code is executed asynchronously, the event handlers are not executed until the event is emitted, hence clicked will still be in its initial state (false) when the condition to add the mousemove handler is evaluated, thus is will not be added and hence never be executed.

What you want to do is to add both event handlers and check within the mousemove event handler if clicked is true.

Btw. the logic you're about to implement means that a user has to click(mouse down and up) to activate the dragging and click again to disengage, rather than what's common UI practice: click(mousedown) and hold to drag things.

不离久伴 2025-01-29 04:19:13

我找到了解决方案。我在Main()函数中使用了单击事件,该函数仅称为一次,设置一个标志,以便在检测到单击时(从false到true t true and vice-vice-cice-cice-cice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice)。在单击事件中,如果标志为真,这是第一次单击,我添加了Mousemove事件,选择坐标。

I found the solution. I used the click event in the main() function that is called only once, set a flag such that it is negated when a click is detected (passing from false to true and vice-versa). Inside the click event, if the flag is true, this is the first click, I add the mousemove event, picking the coordinates.

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