Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrbitControls: fix pinch zoom behaviour #27446

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions examples/jsm/controls/OrbitControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ class OrbitControls extends EventDispatcher {
const pointers = [];
const pointerPositions = {};

let controlActive = false;

function getAutoRotationAngle( deltaTime ) {

if ( deltaTime !== null ) {
Expand All @@ -499,8 +501,8 @@ class OrbitControls extends EventDispatcher {

function getZoomScale( delta ) {

const normalized_delta = Math.abs( delta ) / ( 100 * ( window.devicePixelRatio | 0 ) );
return Math.pow( 0.95, scope.zoomSpeed * normalized_delta );
const normalizedDelta = Math.abs( delta * 0.01 );
return Math.pow( 0.95, scope.zoomSpeed * normalizedDelta );

}

Expand Down Expand Up @@ -1207,12 +1209,70 @@ class OrbitControls extends EventDispatcher {

scope.dispatchEvent( _startEvent );

handleMouseWheel( event );
handleMouseWheel( customWheelEvent( event ) );

scope.dispatchEvent( _endEvent );

}

function customWheelEvent( event ) {

const mode = event.deltaMode;

// minimal wheel event altered to meet delta-zoom demand
const newEvent = {
clientX: event.clientX,
clientY: event.clientY,
deltaY: event.deltaY,
}

switch ( mode ) {

case 1: // LINE_MODE
newEvent.deltaY *= 16;
break;

case 2: // PAGE_MODE
newEvent.deltaY *= 100;
break;

}

// detect if event was triggered by pinching
if ( event.ctrlKey && !controlActive ) {

newEvent.deltaY *= 10;

}

return newEvent;

}

function interceptControlDown( event ) {

if ( event.key === "Control" ) {

controlActive = true;

document.addEventListener('keyup', interceptControlUp, { passive: true, capture: true });

}

}

function interceptControlUp( event ) {

if ( event.key === "Control" ) {

controlActive = false;

document.removeEventListener('keyup', interceptControlUp, { passive: true, capture: true });

}

}

function onKeyDown( event ) {

if ( scope.enabled === false || scope.enablePan === false ) return;
Expand Down Expand Up @@ -1421,6 +1481,8 @@ class OrbitControls extends EventDispatcher {
scope.domElement.addEventListener( 'pointercancel', onPointerUp );
scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );

document.addEventListener( 'keydown', interceptControlDown, { passive: true, capture: true } );

// force an update at start

this.update();
Expand All @@ -1429,4 +1491,4 @@ class OrbitControls extends EventDispatcher {

}

export { OrbitControls };
export { OrbitControls };