Skip to content

Commit

Permalink
Fix going from 2 fingers to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Elliott authored and PaulHax committed Apr 10, 2021
1 parent 757eddb commit b7d4336
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
32 changes: 12 additions & 20 deletions CameraSpinControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,30 @@ CameraSpinControls = function ( camera, domElement ) {
// Set to false to disable this control
this.enabled = true;

this.distanceFromPivot = 500;

this.object = camera;
// "target" sets the location of focus, where the object orbits around
this.targetObj = new THREE.Object3D();
this.targetObj.lookAt(camera.position);
this.target = this.targetObj.position;
this.distanceFromPivot = this.target.distanceTo( camera.position );

if ( camera.position.length() < EPS ) {
camera.position.set(0, 0, 1);
}

this.targetObj.lookAt(camera.position);

this.isTargetOffCenter = false;
this.isTargetOffCenter = true;
this.trackballToObject = new THREE.Matrix4();

this.targetObj.updateWorldMatrix(true, false);
this.object.updateWorldMatrix(true, false);
transformTo(this.trackballToObject, this.targetObj.matrixWorld, this.object.matrixWorld);

// How far you can dolly in and out ( PerspectiveCamera only )
// If isTargetOffCenter === true, will cause jumps if target is closer or further than limits
// Will cause jumps if target is moved closer or further than limits
this.minDistance = 0;
this.maxDistance = Infinity;
// TODO If rotate or pan with intersection, ignore rotation distance clamps

// How far you can zoom in and out ( OrthographicCamera only )
this.minZoom = 0;
Expand Down Expand Up @@ -166,7 +171,6 @@ CameraSpinControls = function ( camera, domElement ) {
scope.object.matrix.copy( scope.targetObj.matrixWorld );
scope.object.matrix.multiply(scope.trackballToObject);


} else {

scope.distanceFromPivot *= scale;
Expand Down Expand Up @@ -891,7 +895,8 @@ CameraSpinControls = function ( camera, domElement ) {
scope.spinControl.enabled = true;
scope.spinControl.handleTouchStart( event );

} else {
} else if( event.touches.length === 0 ) {
// } else {

state = STATE.NONE;
endEvent.state = state;
Expand Down Expand Up @@ -934,19 +939,6 @@ CameraSpinControls = function ( camera, domElement ) {
scope.dispatchEvent( changeEvent );

} );

scope.spinControl.addEventListener( 'start', function ( event ) {

scope.dispatchEvent( startEvent );

} );

scope.spinControl.addEventListener( 'end', function ( event ) {

endEvent.state = state;
scope.dispatchEvent( endEvent );

} );

// Starts touch interfaces off right
this.ajustTrackballRadius();
Expand Down
9 changes: 7 additions & 2 deletions SpinControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ var SpinControls = function ( object, trackBallRadius, camera, domElement ) {
}

function onTouchMove( event ) {

if ( _this.enabled === false || !_isPointerDown ) return;

event.preventDefault();
Expand All @@ -604,7 +604,7 @@ var SpinControls = function ( object, trackBallRadius, camera, domElement ) {

function onTouchEnd( event ) {

if( _this.enabled === false || !_isPointerDown ) return;
if( _this.enabled === false ) return;

if( !_this.hasPointerMovedThisFrame ) {

Expand All @@ -616,6 +616,11 @@ var SpinControls = function ( object, trackBallRadius, camera, domElement ) {

_this.handlePointerUp( event );

// override handlePointerUp if finger still down
if( event.touches.length > 0 ) {
_isPointerDown = true;
}

}

this.dispose = function () {
Expand Down
3 changes: 2 additions & 1 deletion example_camera_spin.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@

camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );

camera.position.set(0, 0, 500);

cameraSpinControl = new CameraSpinControls( camera, renderer.domElement );
cameraSpinControl.distanceFromPivot = 500;

// Uncomment for third person view of CameraSpinControls
// And comment out CameraSpinControls above
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h1 class="menuTitle">SpinControls</h1>
window.addEventListener( 'wheel', onMouseWheel, true );
window.addEventListener( 'wheel', onMouseWheelOff, false );

renderer.domElement.addEventListener( 'touchstart', onTouchStart, false );
renderer.domElement.addEventListener( 'touchstart', onTouchStart, true ); // catch down to enable camera control
window.addEventListener( 'touchend', onTouchEnd, false ); // get event last, so false on capture

// Spheres to spin
Expand Down

0 comments on commit b7d4336

Please sign in to comment.