Skip to content

Commit

Permalink
OrbitControls: introduce zoom-to-cursor on touch events (#27384)
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Avila <3927951+sciecode@users.noreply.github.com>
  • Loading branch information
sciecode authored Dec 16, 2023
1 parent 50ec2a0 commit 6b19d22
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions examples/jsm/controls/OrbitControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ class OrbitControls extends EventDispatcher {

}

function updateMouseParameters( event ) {
function updateZoomParameters( x, y ) {

if ( ! scope.zoomToCursor ) {

Expand All @@ -632,13 +632,13 @@ class OrbitControls extends EventDispatcher {
performCursorZoom = true;

const rect = scope.domElement.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const dx = x - rect.left;
const dy = y - rect.top;
const w = rect.width;
const h = rect.height;

mouse.x = ( x / w ) * 2 - 1;
mouse.y = - ( y / h ) * 2 + 1;
mouse.x = ( dx / w ) * 2 - 1;
mouse.y = - ( dy / h ) * 2 + 1;

dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize();

Expand All @@ -662,7 +662,7 @@ class OrbitControls extends EventDispatcher {

function handleMouseDownDolly( event ) {

updateMouseParameters( event );
updateZoomParameters( event.clientX, event.clientX );
dollyStart.set( event.clientX, event.clientY );

}
Expand Down Expand Up @@ -729,7 +729,7 @@ class OrbitControls extends EventDispatcher {

function handleMouseWheel( event ) {

updateMouseParameters( event );
updateZoomParameters( event.clientX, event.clientY );

if ( event.deltaY < 0 ) {

Expand Down Expand Up @@ -957,6 +957,11 @@ class OrbitControls extends EventDispatcher {

dollyStart.copy( dollyEnd );

const centerX = ( event.pageX + position.x ) * 0.5;
const centerY = ( event.pageY + position.y ) * 0.5;

updateZoomParameters( centerX, centerY );

}

function handleTouchMoveDollyPan( event ) {
Expand Down

0 comments on commit 6b19d22

Please sign in to comment.