TrackballControls.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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();
  220. this.handleResize();
  221. }
  222. // force an update at start
  223. this.update();
  224. }
  225. connect() {
  226. window.addEventListener( 'keydown', this._onKeyDown );
  227. window.addEventListener( 'keyup', this._onKeyUp );
  228. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  229. this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
  230. this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
  231. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  232. this.domElement.style.touchAction = 'none'; // disable touch scroll
  233. }
  234. disconnect() {
  235. window.removeEventListener( 'keydown', this._onKeyDown );
  236. window.removeEventListener( 'keyup', this._onKeyUp );
  237. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  238. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  239. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  240. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  241. this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
  242. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  243. this.domElement.style.touchAction = 'auto'; // disable touch scroll
  244. }
  245. dispose() {
  246. this.disconnect();
  247. }
  248. /**
  249. * Must be called if the application window is resized.
  250. */
  251. handleResize() {
  252. const box = this.domElement.getBoundingClientRect();
  253. // adjustments come from similar code in the jquery offset() function
  254. const d = this.domElement.ownerDocument.documentElement;
  255. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  256. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  257. this.screen.width = box.width;
  258. this.screen.height = box.height;
  259. }
  260. update() {
  261. this._eye.subVectors( this.object.position, this.target );
  262. if ( ! this.noRotate ) {
  263. this._rotateCamera();
  264. }
  265. if ( ! this.noZoom ) {
  266. this._zoomCamera();
  267. }
  268. if ( ! this.noPan ) {
  269. this._panCamera();
  270. }
  271. this.object.position.addVectors( this.target, this._eye );
  272. if ( this.object.isPerspectiveCamera ) {
  273. this._checkDistances();
  274. this.object.lookAt( this.target );
  275. if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS ) {
  276. this.dispatchEvent( _changeEvent );
  277. this._lastPosition.copy( this.object.position );
  278. }
  279. } else if ( this.object.isOrthographicCamera ) {
  280. this.object.lookAt( this.target );
  281. if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS || this._lastZoom !== this.object.zoom ) {
  282. this.dispatchEvent( _changeEvent );
  283. this._lastPosition.copy( this.object.position );
  284. this._lastZoom = this.object.zoom;
  285. }
  286. } else {
  287. console.warn( 'THREE.TrackballControls: Unsupported camera type.' );
  288. }
  289. }
  290. /**
  291. * Resets the controls to its initial state.
  292. */
  293. reset() {
  294. this.state = _STATE.NONE;
  295. this.keyState = _STATE.NONE;
  296. this.target.copy( this._target0 );
  297. this.object.position.copy( this._position0 );
  298. this.object.up.copy( this._up0 );
  299. this.object.zoom = this._zoom0;
  300. this.object.updateProjectionMatrix();
  301. this._eye.subVectors( this.object.position, this.target );
  302. this.object.lookAt( this.target );
  303. this.dispatchEvent( _changeEvent );
  304. this._lastPosition.copy( this.object.position );
  305. this._lastZoom = this.object.zoom;
  306. }
  307. _panCamera() {
  308. _mouseChange.copy( this._panEnd ).sub( this._panStart );
  309. if ( _mouseChange.lengthSq() ) {
  310. if ( this.object.isOrthographicCamera ) {
  311. const scale_x = ( this.object.right - this.object.left ) / this.object.zoom / this.domElement.clientWidth;
  312. const scale_y = ( this.object.top - this.object.bottom ) / this.object.zoom / this.domElement.clientWidth;
  313. _mouseChange.x *= scale_x;
  314. _mouseChange.y *= scale_y;
  315. }
  316. _mouseChange.multiplyScalar( this._eye.length() * this.panSpeed );
  317. _pan.copy( this._eye ).cross( this.object.up ).setLength( _mouseChange.x );
  318. _pan.add( _objectUp.copy( this.object.up ).setLength( _mouseChange.y ) );
  319. this.object.position.add( _pan );
  320. this.target.add( _pan );
  321. if ( this.staticMoving ) {
  322. this._panStart.copy( this._panEnd );
  323. } else {
  324. this._panStart.add( _mouseChange.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.dynamicDampingFactor ) );
  325. }
  326. }
  327. }
  328. _rotateCamera() {
  329. _moveDirection.set( this._moveCurr.x - this._movePrev.x, this._moveCurr.y - this._movePrev.y, 0 );
  330. let angle = _moveDirection.length();
  331. if ( angle ) {
  332. this._eye.copy( this.object.position ).sub( this.target );
  333. _eyeDirection.copy( this._eye ).normalize();
  334. _objectUpDirection.copy( this.object.up ).normalize();
  335. _objectSidewaysDirection.crossVectors( _objectUpDirection, _eyeDirection ).normalize();
  336. _objectUpDirection.setLength( this._moveCurr.y - this._movePrev.y );
  337. _objectSidewaysDirection.setLength( this._moveCurr.x - this._movePrev.x );
  338. _moveDirection.copy( _objectUpDirection.add( _objectSidewaysDirection ) );
  339. _axis.crossVectors( _moveDirection, this._eye ).normalize();
  340. angle *= this.rotateSpeed;
  341. _quaternion.setFromAxisAngle( _axis, angle );
  342. this._eye.applyQuaternion( _quaternion );
  343. this.object.up.applyQuaternion( _quaternion );
  344. this._lastAxis.copy( _axis );
  345. this._lastAngle = angle;
  346. } else if ( ! this.staticMoving && this._lastAngle ) {
  347. this._lastAngle *= Math.sqrt( 1.0 - this.dynamicDampingFactor );
  348. this._eye.copy( this.object.position ).sub( this.target );
  349. _quaternion.setFromAxisAngle( this._lastAxis, this._lastAngle );
  350. this._eye.applyQuaternion( _quaternion );
  351. this.object.up.applyQuaternion( _quaternion );
  352. }
  353. this._movePrev.copy( this._moveCurr );
  354. }
  355. _zoomCamera() {
  356. let factor;
  357. if ( this.state === _STATE.TOUCH_ZOOM_PAN ) {
  358. factor = this._touchZoomDistanceStart / this._touchZoomDistanceEnd;
  359. this._touchZoomDistanceStart = this._touchZoomDistanceEnd;
  360. if ( this.object.isPerspectiveCamera ) {
  361. this._eye.multiplyScalar( factor );
  362. } else if ( this.object.isOrthographicCamera ) {
  363. this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
  364. if ( this._lastZoom !== this.object.zoom ) {
  365. this.object.updateProjectionMatrix();
  366. }
  367. } else {
  368. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  369. }
  370. } else {
  371. factor = 1.0 + ( this._zoomEnd.y - this._zoomStart.y ) * this.zoomSpeed;
  372. if ( factor !== 1.0 && factor > 0.0 ) {
  373. if ( this.object.isPerspectiveCamera ) {
  374. this._eye.multiplyScalar( factor );
  375. } else if ( this.object.isOrthographicCamera ) {
  376. this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
  377. if ( this._lastZoom !== this.object.zoom ) {
  378. this.object.updateProjectionMatrix();
  379. }
  380. } else {
  381. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  382. }
  383. }
  384. if ( this.staticMoving ) {
  385. this._zoomStart.copy( this._zoomEnd );
  386. } else {
  387. this._zoomStart.y += ( this._zoomEnd.y - this._zoomStart.y ) * this.dynamicDampingFactor;
  388. }
  389. }
  390. }
  391. _getMouseOnScreen( pageX, pageY ) {
  392. _v2.set(
  393. ( pageX - this.screen.left ) / this.screen.width,
  394. ( pageY - this.screen.top ) / this.screen.height
  395. );
  396. return _v2;
  397. }
  398. _getMouseOnCircle( pageX, pageY ) {
  399. _v2.set(
  400. ( ( pageX - this.screen.width * 0.5 - this.screen.left ) / ( this.screen.width * 0.5 ) ),
  401. ( ( this.screen.height + 2 * ( this.screen.top - pageY ) ) / this.screen.width ) // screen.width intentional
  402. );
  403. return _v2;
  404. }
  405. _addPointer( event ) {
  406. this._pointers.push( event );
  407. }
  408. _removePointer( event ) {
  409. delete this._pointerPositions[ event.pointerId ];
  410. for ( let i = 0; i < this._pointers.length; i ++ ) {
  411. if ( this._pointers[ i ].pointerId == event.pointerId ) {
  412. this._pointers.splice( i, 1 );
  413. return;
  414. }
  415. }
  416. }
  417. _trackPointer( event ) {
  418. let position = this._pointerPositions[ event.pointerId ];
  419. if ( position === undefined ) {
  420. position = new Vector2();
  421. this._pointerPositions[ event.pointerId ] = position;
  422. }
  423. position.set( event.pageX, event.pageY );
  424. }
  425. _getSecondPointerPosition( event ) {
  426. const pointer = ( event.pointerId === this._pointers[ 0 ].pointerId ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
  427. return this._pointerPositions[ pointer.pointerId ];
  428. }
  429. _checkDistances() {
  430. if ( ! this.noZoom || ! this.noPan ) {
  431. if ( this._eye.lengthSq() > this.maxDistance * this.maxDistance ) {
  432. this.object.position.addVectors( this.target, this._eye.setLength( this.maxDistance ) );
  433. this._zoomStart.copy( this._zoomEnd );
  434. }
  435. if ( this._eye.lengthSq() < this.minDistance * this.minDistance ) {
  436. this.object.position.addVectors( this.target, this._eye.setLength( this.minDistance ) );
  437. this._zoomStart.copy( this._zoomEnd );
  438. }
  439. }
  440. }
  441. }
  442. function onPointerDown( event ) {
  443. if ( this.enabled === false ) return;
  444. if ( this._pointers.length === 0 ) {
  445. this.domElement.setPointerCapture( event.pointerId );
  446. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  447. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  448. }
  449. //
  450. this._addPointer( event );
  451. if ( event.pointerType === 'touch' ) {
  452. this._onTouchStart( event );
  453. } else {
  454. this._onMouseDown( event );
  455. }
  456. }
  457. function onPointerMove( event ) {
  458. if ( this.enabled === false ) return;
  459. if ( event.pointerType === 'touch' ) {
  460. this._onTouchMove( event );
  461. } else {
  462. this._onMouseMove( event );
  463. }
  464. }
  465. function onPointerUp( event ) {
  466. if ( this.enabled === false ) return;
  467. if ( event.pointerType === 'touch' ) {
  468. this._onTouchEnd( event );
  469. } else {
  470. this._onMouseUp();
  471. }
  472. //
  473. this._removePointer( event );
  474. if ( this._pointers.length === 0 ) {
  475. this.domElement.releasePointerCapture( event.pointerId );
  476. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  477. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  478. }
  479. }
  480. function onPointerCancel( event ) {
  481. this._removePointer( event );
  482. }
  483. function onKeyUp() {
  484. if ( this.enabled === false ) return;
  485. this.keyState = _STATE.NONE;
  486. window.addEventListener( 'keydown', this._onKeyDown );
  487. }
  488. function onKeyDown( event ) {
  489. if ( this.enabled === false ) return;
  490. window.removeEventListener( 'keydown', this._onKeyDown );
  491. if ( this.keyState !== _STATE.NONE ) {
  492. return;
  493. } else if ( event.code === this.keys[ _STATE.ROTATE ] && ! this.noRotate ) {
  494. this.keyState = _STATE.ROTATE;
  495. } else if ( event.code === this.keys[ _STATE.ZOOM ] && ! this.noZoom ) {
  496. this.keyState = _STATE.ZOOM;
  497. } else if ( event.code === this.keys[ _STATE.PAN ] && ! this.noPan ) {
  498. this.keyState = _STATE.PAN;
  499. }
  500. }
  501. function onMouseDown( event ) {
  502. let mouseAction;
  503. switch ( event.button ) {
  504. case 0:
  505. mouseAction = this.mouseButtons.LEFT;
  506. break;
  507. case 1:
  508. mouseAction = this.mouseButtons.MIDDLE;
  509. break;
  510. case 2:
  511. mouseAction = this.mouseButtons.RIGHT;
  512. break;
  513. default:
  514. mouseAction = - 1;
  515. }
  516. switch ( mouseAction ) {
  517. case MOUSE.DOLLY:
  518. this.state = _STATE.ZOOM;
  519. break;
  520. case MOUSE.ROTATE:
  521. this.state = _STATE.ROTATE;
  522. break;
  523. case MOUSE.PAN:
  524. this.state = _STATE.PAN;
  525. break;
  526. default:
  527. this.state = _STATE.NONE;
  528. }
  529. const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
  530. if ( state === _STATE.ROTATE && ! this.noRotate ) {
  531. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  532. this._movePrev.copy( this._moveCurr );
  533. } else if ( state === _STATE.ZOOM && ! this.noZoom ) {
  534. this._zoomStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  535. this._zoomEnd.copy( this._zoomStart );
  536. } else if ( state === _STATE.PAN && ! this.noPan ) {
  537. this._panStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  538. this._panEnd.copy( this._panStart );
  539. }
  540. this.dispatchEvent( _startEvent );
  541. }
  542. function onMouseMove( event ) {
  543. const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
  544. if ( state === _STATE.ROTATE && ! this.noRotate ) {
  545. this._movePrev.copy( this._moveCurr );
  546. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  547. } else if ( state === _STATE.ZOOM && ! this.noZoom ) {
  548. this._zoomEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  549. } else if ( state === _STATE.PAN && ! this.noPan ) {
  550. this._panEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
  551. }
  552. }
  553. function onMouseUp() {
  554. this.state = _STATE.NONE;
  555. this.dispatchEvent( _endEvent );
  556. }
  557. function onMouseWheel( event ) {
  558. if ( this.enabled === false ) return;
  559. if ( this.noZoom === true ) return;
  560. event.preventDefault();
  561. switch ( event.deltaMode ) {
  562. case 2:
  563. // Zoom in pages
  564. this._zoomStart.y -= event.deltaY * 0.025;
  565. break;
  566. case 1:
  567. // Zoom in lines
  568. this._zoomStart.y -= event.deltaY * 0.01;
  569. break;
  570. default:
  571. // undefined, 0, assume pixels
  572. this._zoomStart.y -= event.deltaY * 0.00025;
  573. break;
  574. }
  575. this.dispatchEvent( _startEvent );
  576. this.dispatchEvent( _endEvent );
  577. }
  578. function onContextMenu( event ) {
  579. if ( this.enabled === false ) return;
  580. event.preventDefault();
  581. }
  582. function onTouchStart( event ) {
  583. this._trackPointer( event );
  584. switch ( this._pointers.length ) {
  585. case 1:
  586. this.state = _STATE.TOUCH_ROTATE;
  587. this._moveCurr.copy( this._getMouseOnCircle( this._pointers[ 0 ].pageX, this._pointers[ 0 ].pageY ) );
  588. this._movePrev.copy( this._moveCurr );
  589. break;
  590. default: // 2 or more
  591. this.state = _STATE.TOUCH_ZOOM_PAN;
  592. const dx = this._pointers[ 0 ].pageX - this._pointers[ 1 ].pageX;
  593. const dy = this._pointers[ 0 ].pageY - this._pointers[ 1 ].pageY;
  594. this._touchZoomDistanceEnd = this._touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  595. const x = ( this._pointers[ 0 ].pageX + this._pointers[ 1 ].pageX ) / 2;
  596. const y = ( this._pointers[ 0 ].pageY + this._pointers[ 1 ].pageY ) / 2;
  597. this._panStart.copy( this._getMouseOnScreen( x, y ) );
  598. this._panEnd.copy( this._panStart );
  599. break;
  600. }
  601. this.dispatchEvent( _startEvent );
  602. }
  603. function onTouchMove( event ) {
  604. this._trackPointer( event );
  605. switch ( this._pointers.length ) {
  606. case 1:
  607. this._movePrev.copy( this._moveCurr );
  608. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  609. break;
  610. default: // 2 or more
  611. const position = this._getSecondPointerPosition( event );
  612. const dx = event.pageX - position.x;
  613. const dy = event.pageY - position.y;
  614. this._touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  615. const x = ( event.pageX + position.x ) / 2;
  616. const y = ( event.pageY + position.y ) / 2;
  617. this._panEnd.copy( this._getMouseOnScreen( x, y ) );
  618. break;
  619. }
  620. }
  621. function onTouchEnd( event ) {
  622. switch ( this._pointers.length ) {
  623. case 0:
  624. this.state = _STATE.NONE;
  625. break;
  626. case 1:
  627. this.state = _STATE.TOUCH_ROTATE;
  628. this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
  629. this._movePrev.copy( this._moveCurr );
  630. break;
  631. case 2:
  632. this.state = _STATE.TOUCH_ZOOM_PAN;
  633. for ( let i = 0; i < this._pointers.length; i ++ ) {
  634. if ( this._pointers[ i ].pointerId !== event.pointerId ) {
  635. const position = this._pointerPositions[ this._pointers[ i ].pointerId ];
  636. this._moveCurr.copy( this._getMouseOnCircle( position.x, position.y ) );
  637. this._movePrev.copy( this._moveCurr );
  638. break;
  639. }
  640. }
  641. break;
  642. }
  643. this.dispatchEvent( _endEvent );
  644. }
  645. export { TrackballControls };
粤ICP备19079148号