TrackballControls.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. import {
  2. EventDispatcher,
  3. MOUSE,
  4. Quaternion,
  5. Vector2,
  6. Vector3
  7. } from 'three';
  8. const _changeEvent = { type: 'change' };
  9. const _startEvent = { type: 'start' };
  10. const _endEvent = { type: 'end' };
  11. class TrackballControls extends EventDispatcher {
  12. constructor( object, domElement ) {
  13. super();
  14. const scope = this;
  15. const STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  16. this.multiTouchRotate = false;
  17. this.object = object;
  18. this.domElement = domElement;
  19. this.domElement.style.touchAction = 'none'; // disable touch scroll
  20. // API
  21. this.enabled = true;
  22. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  23. this.rotateSpeed = 1.0;
  24. this.zoomSpeed = 1.2;
  25. this.panSpeed = 0.3;
  26. this.rotateThreshold = 0.5;
  27. this.noRotate = false;
  28. this.noZoom = false;
  29. this.noPan = false;
  30. this.staticMoving = false;
  31. this.dynamicDampingFactor = 0.2;
  32. this.minDistance = 0;
  33. this.maxDistance = Infinity;
  34. this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];
  35. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  36. // internals
  37. this.target = new Vector3();
  38. const EPS = 0.000001;
  39. const lastPosition = new Vector3();
  40. let lastZoom = 1;
  41. let _state = STATE.NONE,
  42. _keyState = STATE.NONE,
  43. _touchZoomDistanceStart = 0,
  44. _touchZoomDistanceEnd = 0,
  45. _lastAngle = 0;
  46. let _touchRotationAnglePrev = 0;
  47. let _touchRotationAngleNew = 0;
  48. let _touchRotationAngleDelta = 0;
  49. let _pointersSet = {};
  50. const _eye = new Vector3(),
  51. _movePrev = new Vector2(),
  52. _moveCurr = new Vector2(),
  53. _lastAxis = new Vector3(),
  54. _zoomStart = new Vector2(),
  55. _zoomEnd = new Vector2(),
  56. _panStart = new Vector2(),
  57. _panEnd = new Vector2(),
  58. _pointers = [],
  59. _pointerPositions = {};
  60. // for reset
  61. this.target0 = this.target.clone();
  62. this.position0 = this.object.position.clone();
  63. this.up0 = this.object.up.clone();
  64. this.zoom0 = this.object.zoom;
  65. // methods
  66. this.handleResize = function () {
  67. const box = scope.domElement.getBoundingClientRect();
  68. // adjustments come from similar code in the jquery offset() function
  69. const d = scope.domElement.ownerDocument.documentElement;
  70. scope.screen.left = box.left + window.pageXOffset - d.clientLeft;
  71. scope.screen.top = box.top + window.pageYOffset - d.clientTop;
  72. scope.screen.width = box.width;
  73. scope.screen.height = box.height;
  74. };
  75. const getMouseOnScreen = ( function () {
  76. const vector = new Vector2();
  77. return function getMouseOnScreen( pageX, pageY ) {
  78. vector.set(
  79. ( pageX - scope.screen.left ) / scope.screen.width,
  80. ( pageY - scope.screen.top ) / scope.screen.height
  81. );
  82. return vector;
  83. };
  84. }() );
  85. const getMouseOnCircle = ( function () {
  86. const vector = new Vector2();
  87. return function getMouseOnCircle( pageX, pageY ) {
  88. vector.set(
  89. ( ( pageX - scope.screen.width * 0.5 - scope.screen.left ) / ( scope.screen.width * 0.5 ) ),
  90. ( ( scope.screen.height + 2 * ( scope.screen.top - pageY ) ) / scope.screen.width ) // screen.width intentional
  91. );
  92. return vector;
  93. };
  94. }() );
  95. this.rotateCamera = ( function () {
  96. const axis = new Vector3(),
  97. quaternion = new Quaternion(),
  98. eyeDirection = new Vector3(),
  99. objectUpDirection = new Vector3(),
  100. objectSidewaysDirection = new Vector3(),
  101. moveDirection = new Vector3();
  102. return function rotateCamera() {
  103. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  104. let angle = moveDirection.length();
  105. if ( this.multiTouchRotate && _state === STATE.TOUCH_ZOOM_PAN ) {
  106. _touchRotationAngleDelta = _touchRotationAngleNew - _touchRotationAnglePrev;
  107. _touchRotationAnglePrev = _touchRotationAngleNew;
  108. if (
  109. _touchRotationAngleDelta &&
  110. Math.abs( _touchRotationAngleDelta ) < this.rotateThreshold
  111. ) {
  112. angle = _touchRotationAngleDelta;
  113. _eye.copy( scope.object.position ).sub( scope.target );
  114. eyeDirection.copy( _eye ).normalize();
  115. quaternion.setFromAxisAngle( eyeDirection, angle );
  116. scope.object.up.applyQuaternion( quaternion );
  117. _lastAxis.copy( eyeDirection );
  118. _lastAngle = angle;
  119. }
  120. } else if ( angle && Math.abs( angle ) < this.rotateThreshold ) {
  121. _eye.copy( scope.object.position ).sub( scope.target );
  122. eyeDirection.copy( _eye ).normalize();
  123. objectUpDirection.copy( scope.object.up ).normalize();
  124. objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
  125. objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
  126. objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
  127. moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
  128. axis.crossVectors( moveDirection, _eye ).normalize();
  129. angle *= scope.rotateSpeed;
  130. quaternion.setFromAxisAngle( axis, angle );
  131. _eye.applyQuaternion( quaternion );
  132. scope.object.up.applyQuaternion( quaternion );
  133. _lastAxis.copy( axis );
  134. _lastAngle = angle;
  135. } else if ( ! scope.staticMoving && _lastAngle ) {
  136. _lastAngle *= Math.sqrt( 1.0 - scope.dynamicDampingFactor );
  137. _eye.copy( scope.object.position ).sub( scope.target );
  138. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  139. _eye.applyQuaternion( quaternion );
  140. scope.object.up.applyQuaternion( quaternion );
  141. }
  142. _movePrev.copy( _moveCurr );
  143. };
  144. }() );
  145. this.zoomCamera = function () {
  146. let factor;
  147. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  148. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  149. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  150. if ( scope.object.isPerspectiveCamera ) {
  151. _eye.multiplyScalar( factor );
  152. } else if ( scope.object.isOrthographicCamera ) {
  153. scope.object.zoom /= factor;
  154. scope.object.updateProjectionMatrix();
  155. } else {
  156. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  157. }
  158. } else {
  159. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * scope.zoomSpeed;
  160. if ( factor !== 1.0 && factor > 0.0 ) {
  161. if ( scope.object.isPerspectiveCamera ) {
  162. _eye.multiplyScalar( factor );
  163. } else if ( scope.object.isOrthographicCamera ) {
  164. scope.object.zoom /= factor;
  165. scope.object.updateProjectionMatrix();
  166. } else {
  167. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  168. }
  169. }
  170. if ( scope.staticMoving ) {
  171. _zoomStart.copy( _zoomEnd );
  172. } else {
  173. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  174. }
  175. }
  176. };
  177. this.panCamera = ( function () {
  178. const mouseChange = new Vector2(),
  179. objectUp = new Vector3(),
  180. pan = new Vector3();
  181. return function panCamera() {
  182. mouseChange.copy( _panEnd ).sub( _panStart );
  183. if ( mouseChange.lengthSq() ) {
  184. if ( scope.object.isOrthographicCamera ) {
  185. const scale_x = ( scope.object.right - scope.object.left ) / scope.object.zoom / scope.domElement.clientWidth;
  186. const scale_y = ( scope.object.top - scope.object.bottom ) / scope.object.zoom / scope.domElement.clientWidth;
  187. mouseChange.x *= scale_x;
  188. mouseChange.y *= scale_y;
  189. }
  190. mouseChange.multiplyScalar( _eye.length() * scope.panSpeed );
  191. pan.copy( _eye ).cross( scope.object.up ).setLength( mouseChange.x );
  192. pan.add( objectUp.copy( scope.object.up ).setLength( mouseChange.y ) );
  193. scope.object.position.add( pan );
  194. scope.target.add( pan );
  195. if ( scope.staticMoving ) {
  196. _panStart.copy( _panEnd );
  197. } else {
  198. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( scope.dynamicDampingFactor ) );
  199. }
  200. }
  201. };
  202. }() );
  203. this.checkDistances = function () {
  204. if ( ! scope.noZoom || ! scope.noPan ) {
  205. if ( _eye.lengthSq() > scope.maxDistance * scope.maxDistance ) {
  206. scope.object.position.addVectors( scope.target, _eye.setLength( scope.maxDistance ) );
  207. _zoomStart.copy( _zoomEnd );
  208. }
  209. if ( _eye.lengthSq() < scope.minDistance * scope.minDistance ) {
  210. scope.object.position.addVectors( scope.target, _eye.setLength( scope.minDistance ) );
  211. _zoomStart.copy( _zoomEnd );
  212. }
  213. }
  214. };
  215. this.update = function () {
  216. _eye.subVectors( scope.object.position, scope.target );
  217. if ( ! scope.noRotate ) {
  218. scope.rotateCamera();
  219. }
  220. if ( ! scope.noZoom ) {
  221. scope.zoomCamera();
  222. }
  223. if ( ! scope.noPan ) {
  224. scope.panCamera();
  225. }
  226. scope.object.position.addVectors( scope.target, _eye );
  227. if ( scope.object.isPerspectiveCamera ) {
  228. scope.checkDistances();
  229. scope.object.lookAt( scope.target );
  230. if ( lastPosition.distanceToSquared( scope.object.position ) > EPS ) {
  231. scope.dispatchEvent( _changeEvent );
  232. lastPosition.copy( scope.object.position );
  233. }
  234. } else if ( scope.object.isOrthographicCamera ) {
  235. scope.object.lookAt( scope.target );
  236. if ( lastPosition.distanceToSquared( scope.object.position ) > EPS || lastZoom !== scope.object.zoom ) {
  237. scope.dispatchEvent( _changeEvent );
  238. lastPosition.copy( scope.object.position );
  239. lastZoom = scope.object.zoom;
  240. }
  241. } else {
  242. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  243. }
  244. };
  245. this.reset = function () {
  246. _state = STATE.NONE;
  247. _keyState = STATE.NONE;
  248. scope.target.copy( scope.target0 );
  249. scope.object.position.copy( scope.position0 );
  250. scope.object.up.copy( scope.up0 );
  251. scope.object.zoom = scope.zoom0;
  252. scope.object.updateProjectionMatrix();
  253. _eye.subVectors( scope.object.position, scope.target );
  254. scope.object.lookAt( scope.target );
  255. scope.dispatchEvent( _changeEvent );
  256. lastPosition.copy( scope.object.position );
  257. lastZoom = scope.object.zoom;
  258. };
  259. // listeners
  260. function onPointerDown( event ) {
  261. if ( scope.enabled === false ) return;
  262. if ( _pointers.length === 0 ) {
  263. scope.domElement.setPointerCapture( event.pointerId );
  264. scope.domElement.addEventListener( 'pointermove', onPointerMove );
  265. scope.domElement.addEventListener( 'pointerup', onPointerUp );
  266. }
  267. //
  268. addPointer( event );
  269. if ( event.pointerType === 'touch' ) {
  270. onTouchStart( event );
  271. } else {
  272. onMouseDown( event );
  273. }
  274. }
  275. function onPointerMove( event ) {
  276. if ( scope.enabled === false ) return;
  277. if ( event.pointerType === 'touch' ) {
  278. onTouchMove( event );
  279. } else {
  280. onMouseMove( event );
  281. }
  282. }
  283. function onPointerUp( event ) {
  284. if ( scope.enabled === false ) return;
  285. if ( event.pointerType === 'touch' ) {
  286. onTouchEnd( event );
  287. } else {
  288. onMouseUp();
  289. }
  290. //
  291. removePointer( event );
  292. if ( _pointers.length === 0 ) {
  293. scope.domElement.releasePointerCapture( event.pointerId );
  294. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  295. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  296. }
  297. }
  298. function onPointerCancel( event ) {
  299. removePointer( event );
  300. }
  301. function keydown( event ) {
  302. if ( scope.enabled === false ) return;
  303. window.removeEventListener( 'keydown', keydown );
  304. if ( _keyState !== STATE.NONE ) {
  305. return;
  306. } else if ( event.code === scope.keys[ STATE.ROTATE ] && ! scope.noRotate ) {
  307. _keyState = STATE.ROTATE;
  308. } else if ( event.code === scope.keys[ STATE.ZOOM ] && ! scope.noZoom ) {
  309. _keyState = STATE.ZOOM;
  310. } else if ( event.code === scope.keys[ STATE.PAN ] && ! scope.noPan ) {
  311. _keyState = STATE.PAN;
  312. }
  313. }
  314. function keyup() {
  315. if ( scope.enabled === false ) return;
  316. _keyState = STATE.NONE;
  317. window.addEventListener( 'keydown', keydown );
  318. }
  319. function onMouseDown( event ) {
  320. if ( _state === STATE.NONE ) {
  321. switch ( event.button ) {
  322. case scope.mouseButtons.LEFT:
  323. _state = STATE.ROTATE;
  324. break;
  325. case scope.mouseButtons.MIDDLE:
  326. _state = STATE.ZOOM;
  327. break;
  328. case scope.mouseButtons.RIGHT:
  329. _state = STATE.PAN;
  330. break;
  331. }
  332. }
  333. const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  334. if ( state === STATE.ROTATE && ! scope.noRotate ) {
  335. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  336. _movePrev.copy( _moveCurr );
  337. } else if ( state === STATE.ZOOM && ! scope.noZoom ) {
  338. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  339. _zoomEnd.copy( _zoomStart );
  340. } else if ( state === STATE.PAN && ! scope.noPan ) {
  341. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  342. _panEnd.copy( _panStart );
  343. }
  344. scope.dispatchEvent( _startEvent );
  345. }
  346. function onMouseMove( event ) {
  347. const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  348. if ( state === STATE.ROTATE && ! scope.noRotate ) {
  349. _movePrev.copy( _moveCurr );
  350. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  351. } else if ( state === STATE.ZOOM && ! scope.noZoom ) {
  352. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  353. } else if ( state === STATE.PAN && ! scope.noPan ) {
  354. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  355. }
  356. }
  357. function onMouseUp() {
  358. _state = STATE.NONE;
  359. scope.dispatchEvent( _endEvent );
  360. }
  361. function onMouseWheel( event ) {
  362. if ( scope.enabled === false ) return;
  363. if ( scope.noZoom === true ) return;
  364. event.preventDefault();
  365. switch ( event.deltaMode ) {
  366. case 2:
  367. // Zoom in pages
  368. _zoomStart.y -= event.deltaY * 0.025;
  369. break;
  370. case 1:
  371. // Zoom in lines
  372. _zoomStart.y -= event.deltaY * 0.01;
  373. break;
  374. default:
  375. // undefined, 0, assume pixels
  376. _zoomStart.y -= event.deltaY * 0.00025;
  377. break;
  378. }
  379. scope.dispatchEvent( _startEvent );
  380. scope.dispatchEvent( _endEvent );
  381. }
  382. function onTouchStart( event ) {
  383. trackPointer( event );
  384. _pointersSet = {};
  385. gatherPointers( event );
  386. switch ( _pointers.length ) {
  387. case 1:
  388. _state = STATE.TOUCH_ROTATE;
  389. _moveCurr.copy( getMouseOnCircle( _pointers[ 0 ].pageX, _pointers[ 0 ].pageY ) );
  390. _movePrev.copy( _moveCurr );
  391. break;
  392. default: // 2 or more
  393. _state = STATE.TOUCH_ZOOM_PAN;
  394. const dx = _pointers[ 0 ].pageX - _pointers[ 1 ].pageX;
  395. const dy = _pointers[ 0 ].pageY - _pointers[ 1 ].pageY;
  396. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  397. _touchRotationAnglePrev = Math.atan2(
  398. _pointers[ 0 ].pageY - _pointers[ 1 ].pageY,
  399. _pointers[ 0 ].pageX - _pointers[ 1 ].pageX
  400. );
  401. const x = ( _pointers[ 0 ].pageX + _pointers[ 1 ].pageX ) / 2;
  402. const y = ( _pointers[ 0 ].pageY + _pointers[ 1 ].pageY ) / 2;
  403. _panStart.copy( getMouseOnScreen( x, y ) );
  404. _panEnd.copy( _panStart );
  405. break;
  406. }
  407. scope.dispatchEvent( _startEvent );
  408. }
  409. function onTouchMove( event ) {
  410. trackPointer( event );
  411. const points = gatherPointers( event );
  412. switch ( _pointers.length ) {
  413. case 1:
  414. _movePrev.copy( _moveCurr );
  415. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  416. break;
  417. default: // 2 or more
  418. const position = getSecondPointerPosition( event );
  419. const dx = event.pageX - position.x;
  420. const dy = event.pageY - position.y;
  421. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  422. if ( points ) {
  423. _touchRotationAngleNew = Math.atan2(
  424. points[ 0 ].y - points[ 1 ].y,
  425. points[ 0 ].x - points[ 1 ].x
  426. );
  427. }
  428. const x = ( event.pageX + position.x ) / 2;
  429. const y = ( event.pageY + position.y ) / 2;
  430. _panEnd.copy( getMouseOnScreen( x, y ) );
  431. break;
  432. }
  433. }
  434. function onTouchEnd( event ) {
  435. switch ( _pointers.length ) {
  436. case 0:
  437. _state = STATE.NONE;
  438. break;
  439. case 1:
  440. _state = STATE.TOUCH_ROTATE;
  441. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  442. _movePrev.copy( _moveCurr );
  443. break;
  444. case 2:
  445. _state = STATE.TOUCH_ZOOM_PAN;
  446. for ( let i = 0; i < _pointers.length; i ++ ) {
  447. if ( _pointers[ i ].pointerId !== event.pointerId ) {
  448. const position = _pointerPositions[ _pointers[ i ].pointerId ];
  449. _moveCurr.copy( getMouseOnCircle( position.x, position.y ) );
  450. _movePrev.copy( _moveCurr );
  451. break;
  452. }
  453. }
  454. break;
  455. }
  456. scope.dispatchEvent( _endEvent );
  457. }
  458. function contextmenu( event ) {
  459. if ( scope.enabled === false ) return;
  460. event.preventDefault();
  461. }
  462. function addPointer( event ) {
  463. _pointers.push( event );
  464. }
  465. function removePointer( event ) {
  466. delete _pointerPositions[ event.pointerId ];
  467. for ( let i = 0; i < _pointers.length; i ++ ) {
  468. if ( _pointers[ i ].pointerId == event.pointerId ) {
  469. _pointers.splice( i, 1 );
  470. return;
  471. }
  472. }
  473. }
  474. function trackPointer( event ) {
  475. let position = _pointerPositions[ event.pointerId ];
  476. if ( position === undefined ) {
  477. position = new Vector2();
  478. _pointerPositions[ event.pointerId ] = position;
  479. }
  480. position.set( event.pageX, event.pageY );
  481. }
  482. function gatherPointers( event ) {
  483. _pointersSet[ event.pointerId ] = event;
  484. if ( Object.keys( _pointersSet ).length === _pointers.length ) {
  485. const result = _pointers.map( function ( pointer ) {
  486. return _pointersSet[ pointer.pointerId ];
  487. } );
  488. return result;
  489. }
  490. return false;
  491. }
  492. function getSecondPointerPosition( event ) {
  493. const pointer = ( event.pointerId === _pointers[ 0 ].pointerId ) ? _pointers[ 1 ] : _pointers[ 0 ];
  494. return _pointerPositions[ pointer.pointerId ];
  495. }
  496. this.dispose = function () {
  497. scope.domElement.removeEventListener( 'contextmenu', contextmenu );
  498. scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
  499. scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );
  500. scope.domElement.removeEventListener( 'wheel', onMouseWheel );
  501. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  502. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  503. window.removeEventListener( 'keydown', keydown );
  504. window.removeEventListener( 'keyup', keyup );
  505. };
  506. this.domElement.addEventListener( 'contextmenu', contextmenu );
  507. this.domElement.addEventListener( 'pointerdown', onPointerDown );
  508. this.domElement.addEventListener( 'pointercancel', onPointerCancel );
  509. this.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  510. window.addEventListener( 'keydown', keydown );
  511. window.addEventListener( 'keyup', keyup );
  512. this.handleResize();
  513. // force an update at start
  514. this.update();
  515. }
  516. }
  517. export { TrackballControls };