DragControls.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @author zz85 / https://github.com/zz85
  3. * @author mrdoob / http://mrdoob.com
  4. * Running this will allow you to drag three.js objects around the screen.
  5. */
  6. import {
  7. EventDispatcher,
  8. Matrix4,
  9. Plane,
  10. Raycaster,
  11. Vector2,
  12. Vector3
  13. } from "../../../build/three.module.js";
  14. var DragControls = function ( _objects, _camera, _domElement ) {
  15. var _plane = new Plane();
  16. var _raycaster = new Raycaster();
  17. var _mouse = new Vector2();
  18. var _offset = new Vector3();
  19. var _intersection = new Vector3();
  20. var _worldPosition = new Vector3();
  21. var _inverseMatrix = new Matrix4();
  22. var _selected = null, _hovered = null;
  23. //
  24. var scope = this;
  25. function activate() {
  26. _domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
  27. _domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
  28. _domElement.addEventListener( 'mouseup', onDocumentMouseCancel, false );
  29. _domElement.addEventListener( 'mouseleave', onDocumentMouseCancel, false );
  30. _domElement.addEventListener( 'touchmove', onDocumentTouchMove, false );
  31. _domElement.addEventListener( 'touchstart', onDocumentTouchStart, false );
  32. _domElement.addEventListener( 'touchend', onDocumentTouchEnd, false );
  33. }
  34. function deactivate() {
  35. _domElement.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  36. _domElement.removeEventListener( 'mousedown', onDocumentMouseDown, false );
  37. _domElement.removeEventListener( 'mouseup', onDocumentMouseCancel, false );
  38. _domElement.removeEventListener( 'mouseleave', onDocumentMouseCancel, false );
  39. _domElement.removeEventListener( 'touchmove', onDocumentTouchMove, false );
  40. _domElement.removeEventListener( 'touchstart', onDocumentTouchStart, false );
  41. _domElement.removeEventListener( 'touchend', onDocumentTouchEnd, false );
  42. }
  43. function dispose() {
  44. deactivate();
  45. }
  46. function onDocumentMouseMove( event ) {
  47. event.preventDefault();
  48. var rect = _domElement.getBoundingClientRect();
  49. _mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
  50. _mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
  51. _raycaster.setFromCamera( _mouse, _camera );
  52. if ( _selected && scope.enabled ) {
  53. if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  54. _selected.position.copy( _intersection.sub( _offset ).applyMatrix4( _inverseMatrix ) );
  55. }
  56. scope.dispatchEvent( { type: 'drag', object: _selected } );
  57. return;
  58. }
  59. _raycaster.setFromCamera( _mouse, _camera );
  60. var intersects = _raycaster.intersectObjects( _objects, true );
  61. if ( intersects.length > 0 ) {
  62. var object = intersects[ 0 ].object;
  63. _plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( object.matrixWorld ) );
  64. if ( _hovered !== object ) {
  65. scope.dispatchEvent( { type: 'hoveron', object: object } );
  66. _domElement.style.cursor = 'pointer';
  67. _hovered = object;
  68. }
  69. } else {
  70. if ( _hovered !== null ) {
  71. scope.dispatchEvent( { type: 'hoveroff', object: _hovered } );
  72. _domElement.style.cursor = 'auto';
  73. _hovered = null;
  74. }
  75. }
  76. }
  77. function onDocumentMouseDown( event ) {
  78. event.preventDefault();
  79. _raycaster.setFromCamera( _mouse, _camera );
  80. var intersects = _raycaster.intersectObjects( _objects, true );
  81. if ( intersects.length > 0 ) {
  82. _selected = intersects[ 0 ].object;
  83. if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  84. _inverseMatrix.getInverse( _selected.parent.matrixWorld );
  85. _offset.copy( _intersection ).sub( _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
  86. }
  87. _domElement.style.cursor = 'move';
  88. scope.dispatchEvent( { type: 'dragstart', object: _selected } );
  89. }
  90. }
  91. function onDocumentMouseCancel( event ) {
  92. event.preventDefault();
  93. if ( _selected ) {
  94. scope.dispatchEvent( { type: 'dragend', object: _selected } );
  95. _selected = null;
  96. }
  97. _domElement.style.cursor = _hovered ? 'pointer' : 'auto';
  98. }
  99. function onDocumentTouchMove( event ) {
  100. event.preventDefault();
  101. event = event.changedTouches[ 0 ];
  102. var rect = _domElement.getBoundingClientRect();
  103. _mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
  104. _mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
  105. _raycaster.setFromCamera( _mouse, _camera );
  106. if ( _selected && scope.enabled ) {
  107. if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  108. _selected.position.copy( _intersection.sub( _offset ).applyMatrix4( _inverseMatrix ) );
  109. }
  110. scope.dispatchEvent( { type: 'drag', object: _selected } );
  111. return;
  112. }
  113. }
  114. function onDocumentTouchStart( event ) {
  115. event.preventDefault();
  116. event = event.changedTouches[ 0 ];
  117. var rect = _domElement.getBoundingClientRect();
  118. _mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
  119. _mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;
  120. _raycaster.setFromCamera( _mouse, _camera );
  121. var intersects = _raycaster.intersectObjects( _objects, true );
  122. if ( intersects.length > 0 ) {
  123. _selected = intersects[ 0 ].object;
  124. _plane.setFromNormalAndCoplanarPoint( _camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
  125. if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  126. _inverseMatrix.getInverse( _selected.parent.matrixWorld );
  127. _offset.copy( _intersection ).sub( _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
  128. }
  129. _domElement.style.cursor = 'move';
  130. scope.dispatchEvent( { type: 'dragstart', object: _selected } );
  131. }
  132. }
  133. function onDocumentTouchEnd( event ) {
  134. event.preventDefault();
  135. if ( _selected ) {
  136. scope.dispatchEvent( { type: 'dragend', object: _selected } );
  137. _selected = null;
  138. }
  139. _domElement.style.cursor = 'auto';
  140. }
  141. activate();
  142. // API
  143. this.enabled = true;
  144. this.activate = activate;
  145. this.deactivate = deactivate;
  146. this.dispose = dispose;
  147. };
  148. DragControls.prototype = Object.create( EventDispatcher.prototype );
  149. DragControls.prototype.constructor = DragControls;
  150. export { DragControls };