TrackballControls.js 21 KB

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