TrackballControls.js 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. import {
  2. Controls,
  3. MathUtils,
  4. MOUSE,
  5. Quaternion,
  6. Vector2,
  7. Vector3
  8. } from 'three';
  9. /**
  10. * Fires when the camera has been transformed by the controls.
  11. *
  12. * @event TrackballControls#change
  13. * @type {Object}
  14. */
  15. const _changeEvent = { type: 'change' };
  16. /**
  17. * Fires when an interaction was initiated.
  18. *
  19. * @event TrackballControls#start
  20. * @type {Object}
  21. */
  22. const _startEvent = { type: 'start' };
  23. /**
  24. * Fires when an interaction has finished.
  25. *
  26. * @event TrackballControls#end
  27. * @type {Object}
  28. */
  29. const _endEvent = { type: 'end' };
  30. const _EPS = 0.000001;
  31. const _STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  32. const _v2 = new Vector2();
  33. const _mouseChange = new Vector2();
  34. const _objectUp = new Vector3();
  35. const _pan = new Vector3();
  36. const _axis = new Vector3();
  37. const _quaternion = new Quaternion();
  38. const _eyeDirection = new Vector3();
  39. const _objectUpDirection = new Vector3();
  40. const _objectSidewaysDirection = new Vector3();
  41. const _moveDirection = new Vector3();
  42. /**
  43. * This class is similar to {@link OrbitControls}. However, it does not maintain a constant camera
  44. * `up` vector. That means if the camera orbits over the “north” and “south” poles, it does not flip
  45. * to stay "right side up".
  46. *
  47. * @augments Controls
  48. */
  49. class TrackballControls extends Controls {
  50. /**
  51. * Constructs a new controls instance.
  52. *
  53. * @param {Object3D} object - The object that is managed by the controls.
  54. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  55. */
  56. constructor( object, domElement = null ) {
  57. super( object, domElement );
  58. /**
  59. * Represents the properties of the screen. Automatically set when `handleResize()` is called.
  60. *
  61. * @type {Object}
  62. * @readonly
  63. */
  64. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  65. /**
  66. * The rotation speed.
  67. *
  68. * @type {number}
  69. * @default 1
  70. */
  71. this.rotateSpeed = 1.0;
  72. /**
  73. * The zoom speed.
  74. *
  75. * @type {number}
  76. * @default 1.2
  77. */
  78. this.zoomSpeed = 1.2;
  79. /**
  80. * The pan speed.
  81. *
  82. * @type {number}
  83. * @default 0.3
  84. */
  85. this.panSpeed = 0.3;
  86. /**
  87. * Whether rotation is disabled or not.
  88. *
  89. * @type {boolean}
  90. * @default false
  91. */
  92. this.noRotate = false;
  93. /**
  94. * Whether zooming is disabled or not.
  95. *
  96. * @type {boolean}
  97. * @default false
  98. */
  99. this.noZoom = false;
  100. /**
  101. * Whether panning is disabled or not.
  102. *
  103. * @type {boolean}
  104. * @default false
  105. */
  106. this.noPan = false;
  107. /**
  108. * Whether damping is disabled or not.
  109. *
  110. * @type {boolean}
  111. * @default false
  112. */
  113. this.staticMoving = false;
  114. /**
  115. * Defines the intensity of damping. Only considered if `staticMoving` is set to `false`.
  116. *
  117. * @type {number}
  118. * @default 0.2
  119. */
  120. this.dynamicDampingFactor = 0.2;
  121. /**
  122. * How far you can dolly in (perspective camera only).
  123. *
  124. * @type {number}
  125. * @default 0
  126. */
  127. this.minDistance = 0;
  128. /**
  129. * How far you can dolly out (perspective camera only).
  130. *
  131. * @type {number}
  132. * @default Infinity
  133. */
  134. this.maxDistance = Infinity;
  135. /**
  136. * How far you can zoom in (orthographic camera only).
  137. *
  138. * @type {number}
  139. * @default 0
  140. */
  141. this.minZoom = 0;
  142. /**
  143. * How far you can zoom out (orthographic camera only).
  144. *
  145. * @type {number}
  146. * @default Infinity
  147. */
  148. this.maxZoom = Infinity;
  149. /**
  150. * This array holds keycodes for controlling interactions.
  151. *
  152. * - When the first defined key is pressed, all mouse interactions (left, middle, right) performs orbiting.
  153. * - When the second defined key is pressed, all mouse interactions (left, middle, right) performs zooming.
  154. * - When the third defined key is pressed, all mouse interactions (left, middle, right) performs panning.
  155. *
  156. * Default is *KeyA, KeyS, KeyD* which represents A, S, D.
  157. *
  158. * @type {Array<string>}
  159. */
  160. this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];
  161. /**
  162. * This object contains references to the mouse actions used by the controls.
  163. *
  164. * ```js
  165. * controls.mouseButtons = {
  166. * LEFT: THREE.MOUSE.ROTATE,
  167. * MIDDLE: THREE.MOUSE.DOLLY,
  168. * RIGHT: THREE.MOUSE.PAN
  169. * }
  170. * ```
  171. * @type {Object}
  172. */
  173. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  174. /**
  175. * The focus point of the controls.
  176. *
  177. * @type {Vector3}
  178. */
  179. this.target = new Vector3();
  180. // internals
  181. this.state = _STATE.NONE;
  182. this.keyState = _STATE.NONE;
  183. this._lastPosition = new Vector3();
  184. this._lastZoom = 1;
  185. this._touchZoomDistanceStart = 0;
  186. this._touchZoomDistanceEnd = 0;
  187. this._lastAngle = 0;
  188. this._eye = new Vector3();
  189. this._movePrev = new Vector2();
  190. this._moveCurr = new Vector2();
  191. this._lastAxis = new Vector3();
  192. this._zoomStart = new Vector2();
  193. this._zoomEnd = new Vector2();
  194. this._panStart = new Vector2();
  195. this._panEnd = new Vector2();
  196. this._pointers = [];
  197. this._pointerPositions = {};
  198. // event listeners
  199. this._onPointerMove = onPointerMove.bind( this );
  200. this._onPointerDown = onPointerDown.bind( this );
  201. this._onPointerUp = onPointerUp.bind( this );
  202. this._onPointerCancel = onPointerCancel.bind( this );
  203. this._onContextMenu = onContextMenu.bind( this );
  204. this._onMouseWheel = onMouseWheel.bind( this );
  205. this._onKeyDown = onKeyDown.bind( this );
  206. this._onKeyUp = onKeyUp.bind( this );
  207. this._onTouchStart = onTouchStart.bind( this );
  208. this._onTouchMove = onTouchMove.bind( this );
  209. this._onTouchEnd = onTouchEnd.bind( this );
  210. this._onMouseDown = onMouseDown.bind( this );
  211. this._onMouseMove = onMouseMove.bind( this );
  212. this._onMouseUp = onMouseUp.bind( this );
  213. // for reset
  214. this._target0 = this.target.clone();
  215. this._position0 = this.object.position.clone();
  216. this._up0 = this.object.up.clone();
  217. this._zoom0 = this.object.zoom;
  218. if ( domElement !== null ) {
  219. this.connect( domElement );
  220. this.handleResize();
  221. }
  222. // force an update at start
  223. this.update();
  224. }
  225. connect( element ) {
  226. super.connect( element );
  227. window.addEventListener( 'keydown', this._onKeyDown );
  228. window.addEventListener( 'keyup', this._onKeyUp );
  229. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  230. this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
  231. this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
  232. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  233. this.domElement.style.touchAction = 'none'; // disable touch scroll
  234. }
  235. disconnect() {
  236. window.removeEventListener( 'keydown', this._onKeyDown );
  237. window.removeEventListener( 'keyup', this._onKeyUp );
  238. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  239. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  240. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  241. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  242. this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
  243. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  244. this.domElement.style.touchAction = 'auto'; // disable touch scroll
  245. }
  246. dispose() {
  247. this.disconnect();
  248. }
  249. /**
  250. * Must be called if the application window is resized.
  251. */
  252. handleResize() {
  253. const box = this.domElement.getBoundingClientRect();
  254. // adjustments come from similar code in the jquery offset() function
  255. const d = this.domElement.ownerDocument.documentElement;
  256. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  257. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  258. this.screen.width = box.width;
  259. this.screen.height = box.height;
  260. }
  261. update() {
  262. this._eye.subVectors( this.object.position, this.target );
  263. if ( ! this.noRotate ) {
  264. this._rotateCamera();
  265. }
  266. if ( ! this.noZoom ) {
  267. this._zoomCamera();
  268. }
  269. if ( ! this.noPan ) {
  270. this._panCamera();
  271. }
  272. this.object.position.addVectors( this.target, this._eye );
  273. if ( this.object.isPerspectiveCamera ) {
  274. this._checkDistances();
  275. this.object.lookAt( this.target );
  276. if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS ) {
  277. this.dispatchEvent( _changeEvent );
  278. this._lastPosition.copy( this.object.position );
  279. }
  280. } else if ( this.object.isOrthographicCamera ) {
  281. this.object.lookAt( this.target );
  282. if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS || this._lastZoom !== this.object.zoom ) {
  283. this.dispatchEvent( _changeEvent );
  284. this._lastPosition.copy( this.object.position );
  285. this._lastZoom = this.object.zoom;
  286. }
  287. } else {
  288. console.warn( 'THREE.TrackballControls: Unsupported camera type.' );
  289. }
  290. }
  291. /**
  292. * Resets the controls to its initial state.
  293. */
  294. reset() {
  295. this.state = _STATE.NONE;
  296. this.keyState = _STATE.NONE;
  297. this.target.copy( this._target0 );
  298. this.object.position.copy( this._position0 );
  299. this.object.up.copy( this._up0 );
  300. this.object.zoom = this._zoom0;
  301. this.object.updateProjectionMatrix();
  302. this._eye.subVectors( this.object.position, this.target );
  303. this.object.lookAt( this.target );
  304. this.dispatchEvent( _changeEvent );
  305. this._lastPosition.copy( this.object.position );
  306. this._lastZoom = this.object.zoom;
  307. }
  308. _panCamera() {
  309. _mouseChange.copy( this._panEnd ).sub( this._panStart );
  310. if ( _mouseChange.lengthSq() ) {
  311. if ( this.object.isOrthographicCamera ) {
  312. const scale_x = ( this.object.right - this.object.left ) / this.object.zoom / this.domElement.clientWidth;
  313. const scale_y = ( this.object.top - this.object.bottom ) / this.object.zoom / this.domElement.clientWidth;
  314. _mouseChange.x *= scale_x;
  315. _mouseChange.y *= scale_y;
  316. }
  317. _mouseChange.multiplyScalar( this._eye.length() * this.panSpeed );
  318. _pan.copy( this._eye ).cross( this.object.up ).setLength( _mouseChange.x );
  319. _pan.add( _objectUp.copy( this.object.up ).setLength( _mouseChange.y ) );
  320. this.object.position.add( _pan );
  321. this.target.add( _pan );
  322. if ( this.staticMoving ) {
  323. this._panStart.copy( this._panEnd );
  324. } else {
  325. this._panStart.add( _mouseChange.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.dynamicDampingFactor ) );
  326. }
  327. }
  328. }
  329. _rotateCamera() {
  330. _moveDirection.set( this._moveCurr.x - this._movePrev.x, this._moveCurr.y - this._movePrev.y, 0 );
  331. let angle = _moveDirection.length();
  332. if ( angle ) {
  333. this._eye.copy( this.object.position ).sub( this.target );
  334. _eyeDirection.copy( this._eye ).normalize();
  335. _objectUpDirection.copy( this.object.up ).normalize();
  336. _objectSidewaysDirection.crossVectors( _objectUpDirection, _eyeDirection ).normalize();
  337. _objectUpDirection.setLength( this._moveCurr.y - this._movePrev.y );
  338. _objectSidewaysDirection.setLength( this._moveCurr.x - this._movePrev.x );
  339. _moveDirection.copy( _objectUpDirection.add( _objectSidewaysDirection ) );
  340. _axis.crossVectors( _moveDirection, this._eye ).normalize();
  341. angle *= this.rotateSpeed;
  342. _quaternion.setFromAxisAngle( _axis, angle );
  343. this._eye.applyQuaternion( _quaternion );
  344. this.object.up.applyQuaternion( _quaternion );
  345. this._lastAxis.copy( _axis );
  346. this._lastAngle = angle;
  347. } else if ( ! this.staticMoving && this._lastAngle ) {
  348. this._lastAngle *= Math.sqrt( 1.0 - this.dynamicDampingFactor );
  349. this._eye.copy( this.object.position ).sub( this.target );
  350. _quaternion.setFromAxisAngle( this._lastAxis, this._lastAngle );
  351. this._eye.applyQuaternion( _quaternion );
  352. this.object.up.applyQuaternion( _quaternion );
  353. }
  354. this._movePrev.copy( this._moveCurr );
  355. }
  356. _zoomCamera() {
  357. let factor;
  358. if ( this.state === _STATE.TOUCH_ZOOM_PAN ) {
  359. factor = this._touchZoomDistanceStart / this._touchZoomDistanceEnd;
  360. this._touchZoomDistanceStart = this._touchZoomDistanceEnd;
  361. if ( this.object.isPerspectiveCamera ) {
  362. this._eye.multiplyScalar( factor );
  363. } else if ( this.object.isOrthographicCamera ) {
  364. this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
  365. if ( this._lastZoom !== this.object.zoom ) {
  366. this.object.updateProjectionMatrix();
  367. }
  368. } else {
  369. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  370. }
  371. } else {
  372. factor = 1.0 + ( this._zoomEnd.y - this._zoomStart.y ) * this.zoomSpeed;
  373. if ( factor !== 1.0 && factor > 0.0 ) {
  374. if ( this.object.isPerspectiveCamera ) {
  375. this._eye.multiplyScalar( factor );
  376. } else if ( this.object.isOrthographicCamera ) {
  377. this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
  378. if ( this._lastZoom !== this.object.zoom ) {
  379. this.object.updateProjectionMatrix();
  380. }
  381. } else {
  382. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  383. }
  384. }
  385. if ( this.staticMoving ) {
  386. this._zoomStart.copy( this._zoomEnd );
  387. } else {
  388. this._zoomStart.y += ( this._zoomEnd.y - this._zoomStart.y ) * this.dynamicDampingFactor;
  389. }
  390. }
  391. }
  392. _getMouseOnScreen( pageX, pageY ) {
  393. _v2.set(
  394. ( pageX - this.screen.left ) / this.screen.width,
  395. ( pageY - this.screen.top ) / this.screen.height
  396. );
  397. return _v2;
  398. }
  399. _getMouseOnCircle( pageX, pageY ) {
  400. _v2.set(
  401. ( ( pageX - this.screen.width * 0.5 - this.screen.left ) / ( this.screen.width * 0.5 ) ),
  402. ( ( this.screen.height + 2 * ( this.screen.top - pageY ) ) / this.screen.width ) // screen.width intentional
  403. );
  404. return _v2;
  405. }
  406. _addPointer( event ) {
  407. this._pointers.push( event );
  408. }
  409. _removePointer( event ) {
  410. delete this._pointerPositions[ event.pointerId ];
  411. for ( let i = 0; i < this._pointers.length; i ++ ) {
  412. if ( this._pointers[ i ].pointerId == event.pointerId ) {
  413. this._pointers.splice( i, 1 );
  414. return;
  415. }
  416. }
  417. }
  418. _trackPointer( event ) {
  419. let position = this._pointerPositions[ event.pointerId ];
  420. if ( position === undefined ) {
  421. position = new Vector2();
  422. this._pointerPositions[ event.pointerId ] = position;
  423. }
  424. position.set( event.pageX, event.pageY );
  425. }
  426. _getSecondPointerPosition( event ) {
  427. const pointer = ( event.pointerId === this._pointers[ 0 ].pointerId ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
  428. return this._pointerPositions[ pointer.pointerId ];
  429. }
  430. _checkDistances() {
  431. if ( ! this.noZoom || ! this.noPan ) {
  432. if ( this._eye.lengthSq() > this.maxDistance * this.maxDistance ) {
  433. this.object.position.addVectors( this.target, this._eye.setLength( this.maxDistance ) );
  434. this._zoomStart.copy( this._zoomEnd );
  435. }
  436. if ( this._eye.lengthSq() < this.minDistance * this.minDistance ) {
  437. this.object.position.addVectors( this.target, this._eye.setLength( this.minDistance ) );
  438. this._zoomStart.copy( this._zoomEnd );
  439. }
  440. }
  441. }
  442. }
  443. function onPointerDown( event ) {
  444. if ( this.enabled === false ) return;
  445. if ( this._pointers.length === 0 ) {
  446. this.domElement.setPointerCapture( event.pointerId );
  447. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  448. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  449. }
  450. //
  451. this._addPointer( event );
  452. if ( event.pointerType === 'touch' ) {
  453. this._onTouchStart( event );
  454. } else {
  455. this._onMouseDown( event );
  456. }
  457. }
  458. function onPointerMove( event ) {
  459. if ( this.enabled === false ) return;
  460. if ( event.pointerType === 'touch' ) {
  461. this._onTouchMove( event );
  462. } else {
  463. this._onMouseMove( event );
  464. }
  465. }
  466. function onPointerUp( event ) {
  467. if ( this.enabled === false ) return;
  468. if ( event.pointerType === 'touch' ) {
  469. this._onTouchEnd( event );
  470. } else {
  471. this._onMouseUp();
  472. }
  473. //
  474. this._removePointer( event );
  475. if ( this._pointers.length === 0 ) {
  476. this.domElement.releasePointerCapture( event.pointerId );
  477. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  478. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  479. }
  480. }
  481. function onPointerCancel( event ) {
  482. this._removePointer( event );
  483. }
  484. function onKeyUp() {
  485. if ( this.enabled === false ) return;
  486. this.keyState = _STATE.NONE;
  487. window.addEventListener( 'keydown', this._onKeyDown );
  488. }
  489. function onKeyDown( event ) {
  490. if ( this.enabled === false ) return;
  491. window.removeEventListener( 'keydown', this._onKeyDown );
  492. if ( this.keyState !== _STATE.NONE ) {
  493. return;
  494. } else if ( event.code === this.keys[ _STATE.ROTATE ] && ! this.noRotate ) {
  495. this.keyState = _STATE.ROTATE;
  496. } else if ( event.code === this.keys[ _STATE.ZOOM ] && ! this.noZoom ) {
  497. this.keyState = _STATE.ZOOM;
  498. } else if ( event.code === this.keys[ _STATE.PAN ] && ! this.noPan ) {
  499. this.keyState = _STATE.PAN;
  500. }
  501. }
  502. function onMouseDown( event ) {
  503. let mouseAction;
  504. switch ( event.button ) {
  505. case 0:
  506. mouseAction = this.mouseButtons.LEFT;
  507. break;
  508. case 1:
  509. mouseAction = this.mouseButtons.MIDDLE;
  510. break;
  511. case 2:
  512. mouseAction = this.mouseButtons.RIGHT;
  513. break;
  514. default:
  515. mouseAction = - 1;
  516. }
  517. switch ( mouseAction ) {
  518. case MOUSE.DOLLY:
  519. this.state = _STATE.ZOOM;
  520. break;
  521. case MOUSE.ROTATE:
  522. this.state = _STATE.ROTATE;
  523. break;
  524. case MOUSE.PAN:
  525. this.state = _STATE.PAN;
  526. break;
  527. default:
  528. this.state = _STATE.NONE;
  529. }
  530. const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
  531. if ( state === _STATE.ROTATE && ! this.noRotate ) {
  532. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  533. this._movePrev.copy( this._moveCurr );
  534. } else if ( state === _STATE.ZOOM && ! this.noZoom ) {
  535. this._zoomStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  536. this._zoomEnd.copy( this._zoomStart );
  537. } else if ( state === _STATE.PAN && ! this.noPan ) {
  538. this._panStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  539. this._panEnd.copy( this._panStart );
  540. }
  541. this.dispatchEvent( _startEvent );
  542. }
  543. function onMouseMove( event ) {
  544. const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
  545. if ( state === _STATE.ROTATE && ! this.noRotate ) {
  546. this._movePrev.copy( this._moveCurr );
  547. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  548. } else if ( state === _STATE.ZOOM && ! this.noZoom ) {
  549. this._zoomEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  550. } else if ( state === _STATE.PAN && ! this.noPan ) {
  551. this._panEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  552. }
  553. }
  554. function onMouseUp() {
  555. this.state = _STATE.NONE;
  556. this.dispatchEvent( _endEvent );
  557. }
  558. function onMouseWheel( event ) {
  559. if ( this.enabled === false ) return;
  560. if ( this.noZoom === true ) return;
  561. event.preventDefault();
  562. switch ( event.deltaMode ) {
  563. case 2:
  564. // Zoom in pages
  565. this._zoomStart.y -= event.deltaY * 0.025;
  566. break;
  567. case 1:
  568. // Zoom in lines
  569. this._zoomStart.y -= event.deltaY * 0.01;
  570. break;
  571. default:
  572. // undefined, 0, assume pixels
  573. this._zoomStart.y -= event.deltaY * 0.00025;
  574. break;
  575. }
  576. this.dispatchEvent( _startEvent );
  577. this.dispatchEvent( _endEvent );
  578. }
  579. function onContextMenu( event ) {
  580. if ( this.enabled === false ) return;
  581. event.preventDefault();
  582. }
  583. function onTouchStart( event ) {
  584. this._trackPointer( event );
  585. switch ( this._pointers.length ) {
  586. case 1:
  587. this.state = _STATE.TOUCH_ROTATE;
  588. this._moveCurr.copy( this._getMouseOnCircle( this._pointers[ 0 ].pageX, this._pointers[ 0 ].pageY ) );
  589. this._movePrev.copy( this._moveCurr );
  590. break;
  591. default: // 2 or more
  592. this.state = _STATE.TOUCH_ZOOM_PAN;
  593. const dx = this._pointers[ 0 ].pageX - this._pointers[ 1 ].pageX;
  594. const dy = this._pointers[ 0 ].pageY - this._pointers[ 1 ].pageY;
  595. this._touchZoomDistanceEnd = this._touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  596. const x = ( this._pointers[ 0 ].pageX + this._pointers[ 1 ].pageX ) / 2;
  597. const y = ( this._pointers[ 0 ].pageY + this._pointers[ 1 ].pageY ) / 2;
  598. this._panStart.copy( this._getMouseOnScreen( x, y ) );
  599. this._panEnd.copy( this._panStart );
  600. break;
  601. }
  602. this.dispatchEvent( _startEvent );
  603. }
  604. function onTouchMove( event ) {
  605. this._trackPointer( event );
  606. switch ( this._pointers.length ) {
  607. case 1:
  608. this._movePrev.copy( this._moveCurr );
  609. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  610. break;
  611. default: // 2 or more
  612. const position = this._getSecondPointerPosition( event );
  613. const dx = event.pageX - position.x;
  614. const dy = event.pageY - position.y;
  615. this._touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  616. const x = ( event.pageX + position.x ) / 2;
  617. const y = ( event.pageY + position.y ) / 2;
  618. this._panEnd.copy( this._getMouseOnScreen( x, y ) );
  619. break;
  620. }
  621. }
  622. function onTouchEnd( event ) {
  623. switch ( this._pointers.length ) {
  624. case 0:
  625. this.state = _STATE.NONE;
  626. break;
  627. case 1:
  628. this.state = _STATE.TOUCH_ROTATE;
  629. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  630. this._movePrev.copy( this._moveCurr );
  631. break;
  632. case 2:
  633. this.state = _STATE.TOUCH_ZOOM_PAN;
  634. for ( let i = 0; i < this._pointers.length; i ++ ) {
  635. if ( this._pointers[ i ].pointerId !== event.pointerId ) {
  636. const position = this._pointerPositions[ this._pointers[ i ].pointerId ];
  637. this._moveCurr.copy( this._getMouseOnCircle( position.x, position.y ) );
  638. this._movePrev.copy( this._moveCurr );
  639. break;
  640. }
  641. }
  642. break;
  643. }
  644. this.dispatchEvent( _endEvent );
  645. }
  646. export { TrackballControls };
粤ICP备19079148号