Scaling according to the position of the cursor in the mouse wheel

I am currently working on the THREE.js project. If you use trackball control scaling, it approaches the center of the model. But I want to zoom in according to the position of the cursor with the mouse wheel. Anyone familiar please help me with this. Thank you in advance.

+4
source share
1 answer

Add this code to mousewheel to help you scale based on cursor position, and not relative to model.

If you use trackball controls, you can use it to pan and rotate. Thus, the trackball controls are included in the lie in the mouse wheel.

    renderer.domElement.addEventListener('mousewheel',
           function (e) {
                mousewheel(e);
                        },
           false);


    function mousewheel(event) {
            trackBallControls.noZoom = true;
            event.preventDefault();
            var factor = 50;
            var mX = (event.clientX / jQuery(container).width()) * 2 - 1;
            var mY = -(event.clientY / jQuery(container).height()) * 2 + 1;
            var vector = new THREE.Vector3(mX, mY, 0.5);
            //console.log(vector);
            vector.unproject(camera);
            vector.sub(camera.position);
            if (event.deltaY < 0) {
                camera.position.addVectors(camera.position, 
             vector.setLength(factor));
                trackBallControls.target.addVectors(trackBallControls.target, 
             vector.setLength(factor));
                camera.updateProjectionMatrix();
            } else {
                camera.position.subVectors(camera.position, 
                vector.setLength(factor));
                trackBallControls.target.subVectors(trackBallControls.target, 
                vector.setLength(factor));
                camera.updateProjectionMatrix();
            }
        }
+1
source

Source: https://habr.com/ru/post/1652466/


All Articles