Skip to content

Commit

Permalink
Merge pull request mrdoob#11893 from chunnallu/fix-dragcontrol-not-wo…
Browse files Browse the repository at this point in the history
…rk-mobile-patch

fix DragControl not work on mobile
  • Loading branch information
mrdoob authored Aug 16, 2017
2 parents 5974160 + f9cfac4 commit f296313
Showing 1 changed file with 92 additions and 5 deletions.
97 changes: 92 additions & 5 deletions examples/js/controls/DragControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ THREE.DragControls = function ( _objects, _camera, _domElement ) {

_domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
_domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
_domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
_domElement.addEventListener( 'mouseup', onDocumentMouseCancel, false );
_domElement.addEventListener( 'mouseleave', onDocumentMouseCancel, false );
_domElement.addEventListener( 'touchmove', onDocumentTouchMove, false );
_domElement.addEventListener( 'touchstart', onDocumentTouchStart, false );
_domElement.addEventListener( 'touchend', onDocumentTouchEnd, false );

}

function deactivate() {

_domElement.removeEventListener( 'mousemove', onDocumentMouseMove, false );
_domElement.removeEventListener( 'mousedown', onDocumentMouseDown, false );
_domElement.removeEventListener( 'mouseup', onDocumentMouseUp, false );
_domElement.removeEventListener( 'mouseup', onDocumentMouseCancel, false );
_domElement.removeEventListener( 'mouseleave', onDocumentMouseCancel, false );
_domElement.removeEventListener( 'touchmove', onDocumentTouchMove, false );
_domElement.removeEventListener( 'touchstart', onDocumentTouchStart, false );
_domElement.removeEventListener( 'touchend', onDocumentTouchEnd, false );

}

Expand All @@ -54,8 +62,8 @@ THREE.DragControls = function ( _objects, _camera, _domElement ) {

var rect = _domElement.getBoundingClientRect();

_mouse.x = ( (event.clientX - rect.left) / rect.width ) * 2 - 1;
_mouse.y = - ( (event.clientY - rect.top) / rect.height ) * 2 + 1;
_mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
_mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;

_raycaster.setFromCamera( _mouse, _camera );

Expand Down Expand Up @@ -134,7 +142,86 @@ THREE.DragControls = function ( _objects, _camera, _domElement ) {

}

function onDocumentMouseUp( event ) {
function onDocumentMouseCancel( event ) {

event.preventDefault();

if ( _selected ) {

scope.dispatchEvent( { type: 'dragend', object: _selected } );

_selected = null;

}

_domElement.style.cursor = 'auto';

}

function onDocumentTouchMove( event ) {

event.preventDefault();
event = event.changedTouches[ 0 ];

var rect = _domElement.getBoundingClientRect();

_mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
_mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;

_raycaster.setFromCamera( _mouse, _camera );

if ( _selected && scope.enabled ) {

if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {

_selected.position.copy( _intersection.sub( _offset ) );

}

scope.dispatchEvent( { type: 'drag', object: _selected } );

return;

}

}

function onDocumentTouchStart( event ) {

event.preventDefault();
event = event.changedTouches[ 0 ];

var rect = _domElement.getBoundingClientRect();

_mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
_mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;

_raycaster.setFromCamera( _mouse, _camera );

var intersects = _raycaster.intersectObjects( _objects );

if ( intersects.length > 0 ) {

_selected = intersects[ 0 ].object;

_plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), _selected.position );

if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {

_offset.copy( _intersection ).sub( _selected.position );

}

_domElement.style.cursor = 'move';

scope.dispatchEvent( { type: 'dragstart', object: _selected } );

}


}

function onDocumentTouchEnd( event ) {

event.preventDefault();

Expand Down

0 comments on commit f296313

Please sign in to comment.