gpt4 book ai didi

javascript - 如何在 Matter.js 中实现鼠标悬停行为?

转载 作者:行者123 更新时间:2023-11-30 09:15:39 26 4
gpt4 key购买 nike

我想知道是否有人知道如何使用 Matter.js 实现悬停效果.我已经检查了他们的文档,但无法弄清楚。虽然,有鼠标 - mousedown、mouseup、mousemove 和 more .

我已经研究过,但找不到任何有用的信息。我知道形状/物理是在 Canvas 内呈现的。任何信息或建议都会非常有帮助。

最佳答案

我想出了实现悬停行为的方法。为此,我使用了 Matter.Query.point 和“mousemove”(Matter.Events.on) 方法。感谢@philipp上面为我指出了正确的方向。这是简单的代码。

//Need to add MouseConstraint for mouse events
var mConstraint;
mConstraint = MouseConstraint.create(engine, options);
Matter.World.add(world, mConstraint);

//Add event with 'mousemove'
Matter.Events.on(mConstraint, 'mousemove', function (event) {
//For Matter.Query.point pass "array of bodies" and "mouse position"
var foundPhysics = Matter.Query.point(bodies, event.mouse.position);

//Your custom code here
console.log(foundPhysics[0]); //returns a shape corrisponding to the mouse position

});

编码愉快! :)

关于javascript - 如何在 Matter.js 中实现鼠标悬停行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55257829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com