简体   繁体   English

如何在 OrbitControls 中编写自定义鼠标滚轮事件处理程序?

[英]How to write a custom mouse wheel event handler in OrbitControls?

I'm working on a WebGL application using ThreeJS and OrbitControls.我正在使用 ThreeJS 和 OrbitControls 开发 WebGL 应用程序。 How do I write and make use of a custom event handler for wheel spin events?如何为车轮旋转事件编写和使用自定义事件处理程序?

Suppose my custom handler is假设我的自定义处理程序是

function custom_handleMouseWheel(event) { ... }

I tried adding it as a listener:我尝试将其添加为侦听器:

window.addEventListener("wheel", custom_handleMouseWheel);

but I suspect this adds my handler to a list of existing handlers (probably only one), and I'd have to remove the original handler.但我怀疑这会将我的处理程序添加到现有处理程序列表中(可能只有一个),并且我必须删除原始处理程序。 Not sure how to do that.不知道该怎么做。 And anyway, my handler never was called, which I checked for by adding a console.log("Wheel.") line to my handler.无论如何,我的处理程序从未被调用,我通过向我的处理程序添加 console.log("Wheel.") 行来检查它。

Another thing I tried is to replace the handleWheelMouse method in the controls object, like this:我尝试的另一件事是替换控件 object 中的 handleWheelMouse 方法,如下所示:

let original_handleMouseWheel;

function custom_handleMouseWheel(event) {
  console.log("Custom Wheel!");
  ... fancy geometry calculations ...
  original_handleMouseWheel(event);
}

// somewhere after defining scene, camera, renderer, etc...
controls = new THREE.OrbitControls( camera );
original_handleMouseWheel = controls.handleMouseWheel;
controls.handleMouseWheel = custom_handleMouseWheel;

but again the console.log line never executes.但 console.log 行再次从不执行。

What is the right way to go about doing this? go 这样做的正确方法是什么?

Just setting controls.enableZoom = false will stop the default zoom .只需设置controls.enableZoom = false即可停止默认缩放
The default "wheel" listener is added to the "domElement" which should be the second parameter sent to the OrbitControls constructor.默认的“wheel”侦听器被添加到“domElement”,它应该是发送到 OrbitControls 构造函数的第二个参数。
Try renderer.domElement.addEventListener('wheel', custom_handleMouseWheel);试试renderer.domElement.addEventListener('wheel', custom_handleMouseWheel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM