背景
我正在尝试实现一个东西,但它真的让我发疯。所以任何帮助都将不胜感激!
我在 Matter.js 中创建了一个场景,并将其放置在页面下方的容器中。我希望访问者能够与场景交互,拖动和放置物体。但是允许交互会产生问题,即每当鼠标位于画布上方时,Matter.js 就会阻止用户滚动。
为了解决这个问题,我正在使用以下代码:
mouseConstraint.mouse.element.removeEventListener("mousewheel", mouseConstraint.mouse.mousewheel);
mouseConstraint.mouse.element.removeEventListener("DOMMouseScroll", mouseConstraint.mouse.mousewheel);
这样可以使用户在浏览页面时仍能通过单击和拖动物体与场景进行交互,因为仅移除滚动事件侦听器。至少在桌面上是这样的。 问题 然而,在移动设备上,触摸事件使用户能够在页面上滚动,所以我还需要从Matter.js中的鼠标限制中移除“touchmove”,“touchstart”和“touchend”事件侦听器。像这样:
mouseConstraint.mouse.element.removeEventListener('touchmove', mouseConstraint.mouse.mousemove);
mouseConstraint.mouse.element.removeEventListener('touchstart', mouseConstraint.mouse.mousedown);
mouseConstraint.mouse.element.removeEventListener('touchend', mouseConstraint.mouse.mouseup);
问题出在这里。如果我移除触摸事件,用户可以通过画布滚动,但是用户不能与场景交互,因为这需要激活触摸事件。
所以我想知道是否有办法添加触摸事件,但只允许它们在场景中的某些物体上工作?我发现我可以使用mouseConstraint.body
作为布尔值来判断一个物体是否已被点击/触摸。这个基础上能否以某种方式使用它?
Matter.Events.on(mouseConstraint, "mousedown", function(event) {
if (mouseConstraint.body) {
console.log("Body clicked!");
}
});
mouseConstraint.body
包含实际的物体而不是布尔值。 - ggorlen