OrbitControls.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. import {
  2. EventDispatcher,
  3. MOUSE,
  4. Quaternion,
  5. Spherical,
  6. TOUCH,
  7. Vector2,
  8. Vector3
  9. } from '../../../build/three.module.js';
  10. // This set of controls performs orbiting, dollying (zooming), and panning.
  11. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  12. //
  13. // Orbit - left mouse / touch: one-finger move
  14. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  15. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  16. const _changeEvent = { type: 'change' };
  17. const _startEvent = { type: 'start' };
  18. const _endEvent = { type: 'end' };
  19. class OrbitControls extends EventDispatcher {
  20. constructor( object, domElement ) {
  21. super();
  22. if ( domElement === undefined ) console.warn( 'THREE.OrbitControls: The second parameter "domElement" is now mandatory.' );
  23. if ( domElement === document ) console.error( 'THREE.OrbitControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.' );
  24. this.object = object;
  25. this.domElement = domElement;
  26. // Set to false to disable this control
  27. this.enabled = true;
  28. // "target" sets the location of focus, where the object orbits around
  29. this.target = new Vector3();
  30. // How far you can dolly in and out ( PerspectiveCamera only )
  31. this.minDistance = 0;
  32. this.maxDistance = Infinity;
  33. // How far you can zoom in and out ( OrthographicCamera only )
  34. this.minZoom = 0;
  35. this.maxZoom = Infinity;
  36. // How far you can orbit vertically, upper and lower limits.
  37. // Range is 0 to Math.PI radians.
  38. this.minPolarAngle = 0; // radians
  39. this.maxPolarAngle = Math.PI; // radians
  40. // How far you can orbit horizontally, upper and lower limits.
  41. // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
  42. this.minAzimuthAngle = - Infinity; // radians
  43. this.maxAzimuthAngle = Infinity; // radians
  44. // Set to true to enable damping (inertia)
  45. // If damping is enabled, you must call controls.update() in your animation loop
  46. this.enableDamping = false;
  47. this.dampingFactor = 0.05;
  48. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  49. // Set to false to disable zooming
  50. this.enableZoom = true;
  51. this.zoomSpeed = 1.0;
  52. // Set to false to disable rotating
  53. this.enableRotate = true;
  54. this.rotateSpeed = 1.0;
  55. // Set to false to disable panning
  56. this.enablePan = true;
  57. this.panSpeed = 1.0;
  58. this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
  59. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  60. // Set to true to automatically rotate around the target
  61. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  62. this.autoRotate = false;
  63. this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
  64. // The four arrow keys
  65. this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
  66. // Mouse buttons
  67. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  68. // Touch fingers
  69. this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
  70. // for reset
  71. this.target0 = this.target.clone();
  72. this.position0 = this.object.position.clone();
  73. this.zoom0 = this.object.zoom;
  74. // the target DOM element for key events
  75. this._domElementKeyEvents = null;
  76. //
  77. // public methods
  78. //
  79. this.getPolarAngle = function () {
  80. return spherical.phi;
  81. };
  82. this.getAzimuthalAngle = function () {
  83. return spherical.theta;
  84. };
  85. this.listenToKeyEvents = function ( domElement ) {
  86. domElement.addEventListener( 'keydown', onKeyDown );
  87. this._domElementKeyEvents = domElement;
  88. };
  89. this.saveState = function () {
  90. scope.target0.copy( scope.target );
  91. scope.position0.copy( scope.object.position );
  92. scope.zoom0 = scope.object.zoom;
  93. };
  94. this.reset = function () {
  95. scope.target.copy( scope.target0 );
  96. scope.object.position.copy( scope.position0 );
  97. scope.object.zoom = scope.zoom0;
  98. scope.object.updateProjectionMatrix();
  99. scope.dispatchEvent( _changeEvent );
  100. scope.update();
  101. state = STATE.NONE;
  102. };
  103. // this method is exposed, but perhaps it would be better if we can make it private...
  104. this.update = function () {
  105. const offset = new Vector3();
  106. // so camera.up is the orbit axis
  107. const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  108. const quatInverse = quat.clone().invert();
  109. const lastPosition = new Vector3();
  110. const lastQuaternion = new Quaternion();
  111. const twoPI = 2 * Math.PI;
  112. return function update() {
  113. const position = scope.object.position;
  114. offset.copy( position ).sub( scope.target );
  115. // rotate offset to "y-axis-is-up" space
  116. offset.applyQuaternion( quat );
  117. // angle from z-axis around y-axis
  118. spherical.setFromVector3( offset );
  119. if ( scope.autoRotate && state === STATE.NONE ) {
  120. rotateLeft( getAutoRotationAngle() );
  121. }
  122. if ( scope.enableDamping ) {
  123. spherical.theta += sphericalDelta.theta * scope.dampingFactor;
  124. spherical.phi += sphericalDelta.phi * scope.dampingFactor;
  125. } else {
  126. spherical.theta += sphericalDelta.theta;
  127. spherical.phi += sphericalDelta.phi;
  128. }
  129. // restrict theta to be between desired limits
  130. let min = scope.minAzimuthAngle;
  131. let max = scope.maxAzimuthAngle;
  132. if ( isFinite( min ) && isFinite( max ) ) {
  133. if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
  134. if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
  135. if ( min <= max ) {
  136. spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
  137. } else {
  138. spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
  139. Math.max( min, spherical.theta ) :
  140. Math.min( max, spherical.theta );
  141. }
  142. }
  143. // restrict phi to be between desired limits
  144. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  145. spherical.makeSafe();
  146. spherical.radius *= scale;
  147. // restrict radius to be between desired limits
  148. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  149. // move target to panned location
  150. if ( scope.enableDamping === true ) {
  151. scope.target.addScaledVector( panOffset, scope.dampingFactor );
  152. } else {
  153. scope.target.add( panOffset );
  154. }
  155. offset.setFromSpherical( spherical );
  156. // rotate offset back to "camera-up-vector-is-up" space
  157. offset.applyQuaternion( quatInverse );
  158. position.copy( scope.target ).add( offset );
  159. scope.object.lookAt( scope.target );
  160. if ( scope.enableDamping === true ) {
  161. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  162. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  163. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  164. } else {
  165. sphericalDelta.set( 0, 0, 0 );
  166. panOffset.set( 0, 0, 0 );
  167. }
  168. scale = 1;
  169. // update condition is:
  170. // min(camera displacement, camera rotation in radians)^2 > EPS
  171. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  172. if ( zoomChanged ||
  173. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  174. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  175. scope.dispatchEvent( _changeEvent );
  176. lastPosition.copy( scope.object.position );
  177. lastQuaternion.copy( scope.object.quaternion );
  178. zoomChanged = false;
  179. return true;
  180. }
  181. return false;
  182. };
  183. }();
  184. this.dispose = function () {
  185. scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
  186. scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
  187. scope.domElement.removeEventListener( 'wheel', onMouseWheel );
  188. scope.domElement.removeEventListener( 'touchstart', onTouchStart );
  189. scope.domElement.removeEventListener( 'touchend', onTouchEnd );
  190. scope.domElement.removeEventListener( 'touchmove', onTouchMove );
  191. scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  192. scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
  193. if ( scope._domElementKeyEvents !== null ) {
  194. scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
  195. }
  196. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  197. };
  198. //
  199. // internals
  200. //
  201. const scope = this;
  202. const STATE = {
  203. NONE: - 1,
  204. ROTATE: 0,
  205. DOLLY: 1,
  206. PAN: 2,
  207. TOUCH_ROTATE: 3,
  208. TOUCH_PAN: 4,
  209. TOUCH_DOLLY_PAN: 5,
  210. TOUCH_DOLLY_ROTATE: 6
  211. };
  212. let state = STATE.NONE;
  213. const EPS = 0.000001;
  214. // current position in spherical coordinates
  215. const spherical = new Spherical();
  216. const sphericalDelta = new Spherical();
  217. let scale = 1;
  218. const panOffset = new Vector3();
  219. let zoomChanged = false;
  220. const rotateStart = new Vector2();
  221. const rotateEnd = new Vector2();
  222. const rotateDelta = new Vector2();
  223. const panStart = new Vector2();
  224. const panEnd = new Vector2();
  225. const panDelta = new Vector2();
  226. const dollyStart = new Vector2();
  227. const dollyEnd = new Vector2();
  228. const dollyDelta = new Vector2();
  229. function getAutoRotationAngle() {
  230. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  231. }
  232. function getZoomScale() {
  233. return Math.pow( 0.95, scope.zoomSpeed );
  234. }
  235. function rotateLeft( angle ) {
  236. sphericalDelta.theta -= angle;
  237. }
  238. function rotateUp( angle ) {
  239. sphericalDelta.phi -= angle;
  240. }
  241. const panLeft = function () {
  242. const v = new Vector3();
  243. return function panLeft( distance, objectMatrix ) {
  244. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  245. v.multiplyScalar( - distance );
  246. panOffset.add( v );
  247. };
  248. }();
  249. const panUp = function () {
  250. const v = new Vector3();
  251. return function panUp( distance, objectMatrix ) {
  252. if ( scope.screenSpacePanning === true ) {
  253. v.setFromMatrixColumn( objectMatrix, 1 );
  254. } else {
  255. v.setFromMatrixColumn( objectMatrix, 0 );
  256. v.crossVectors( scope.object.up, v );
  257. }
  258. v.multiplyScalar( distance );
  259. panOffset.add( v );
  260. };
  261. }();
  262. // deltaX and deltaY are in pixels; right and down are positive
  263. const pan = function () {
  264. const offset = new Vector3();
  265. return function pan( deltaX, deltaY ) {
  266. const element = scope.domElement;
  267. if ( scope.object.isPerspectiveCamera ) {
  268. // perspective
  269. const position = scope.object.position;
  270. offset.copy( position ).sub( scope.target );
  271. let targetDistance = offset.length();
  272. // half of the fov is center to top of screen
  273. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  274. // we use only clientHeight here so aspect ratio does not distort speed
  275. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  276. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  277. } else if ( scope.object.isOrthographicCamera ) {
  278. // orthographic
  279. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  280. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  281. } else {
  282. // camera neither orthographic nor perspective
  283. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  284. scope.enablePan = false;
  285. }
  286. };
  287. }();
  288. function dollyOut( dollyScale ) {
  289. if ( scope.object.isPerspectiveCamera ) {
  290. scale /= dollyScale;
  291. } else if ( scope.object.isOrthographicCamera ) {
  292. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  293. scope.object.updateProjectionMatrix();
  294. zoomChanged = true;
  295. } else {
  296. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  297. scope.enableZoom = false;
  298. }
  299. }
  300. function dollyIn( dollyScale ) {
  301. if ( scope.object.isPerspectiveCamera ) {
  302. scale *= dollyScale;
  303. } else if ( scope.object.isOrthographicCamera ) {
  304. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  305. scope.object.updateProjectionMatrix();
  306. zoomChanged = true;
  307. } else {
  308. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  309. scope.enableZoom = false;
  310. }
  311. }
  312. //
  313. // event callbacks - update the object state
  314. //
  315. function handleMouseDownRotate( event ) {
  316. rotateStart.set( event.clientX, event.clientY );
  317. }
  318. function handleMouseDownDolly( event ) {
  319. dollyStart.set( event.clientX, event.clientY );
  320. }
  321. function handleMouseDownPan( event ) {
  322. panStart.set( event.clientX, event.clientY );
  323. }
  324. function handleMouseMoveRotate( event ) {
  325. rotateEnd.set( event.clientX, event.clientY );
  326. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  327. const element = scope.domElement;
  328. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  329. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  330. rotateStart.copy( rotateEnd );
  331. scope.update();
  332. }
  333. function handleMouseMoveDolly( event ) {
  334. dollyEnd.set( event.clientX, event.clientY );
  335. dollyDelta.subVectors( dollyEnd, dollyStart );
  336. if ( dollyDelta.y > 0 ) {
  337. dollyOut( getZoomScale() );
  338. } else if ( dollyDelta.y < 0 ) {
  339. dollyIn( getZoomScale() );
  340. }
  341. dollyStart.copy( dollyEnd );
  342. scope.update();
  343. }
  344. function handleMouseMovePan( event ) {
  345. panEnd.set( event.clientX, event.clientY );
  346. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  347. pan( panDelta.x, panDelta.y );
  348. panStart.copy( panEnd );
  349. scope.update();
  350. }
  351. function handleMouseUp( /*event*/ ) {
  352. // no-op
  353. }
  354. function handleMouseWheel( event ) {
  355. if ( event.deltaY < 0 ) {
  356. dollyIn( getZoomScale() );
  357. } else if ( event.deltaY > 0 ) {
  358. dollyOut( getZoomScale() );
  359. }
  360. scope.update();
  361. }
  362. function handleKeyDown( event ) {
  363. let needsUpdate = false;
  364. switch ( event.code ) {
  365. case scope.keys.UP:
  366. pan( 0, scope.keyPanSpeed );
  367. needsUpdate = true;
  368. break;
  369. case scope.keys.BOTTOM:
  370. pan( 0, - scope.keyPanSpeed );
  371. needsUpdate = true;
  372. break;
  373. case scope.keys.LEFT:
  374. pan( scope.keyPanSpeed, 0 );
  375. needsUpdate = true;
  376. break;
  377. case scope.keys.RIGHT:
  378. pan( - scope.keyPanSpeed, 0 );
  379. needsUpdate = true;
  380. break;
  381. }
  382. if ( needsUpdate ) {
  383. // prevent the browser from scrolling on cursor keys
  384. event.preventDefault();
  385. scope.update();
  386. }
  387. }
  388. function handleTouchStartRotate( event ) {
  389. if ( event.touches.length == 1 ) {
  390. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  391. } else {
  392. const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  393. const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  394. rotateStart.set( x, y );
  395. }
  396. }
  397. function handleTouchStartPan( event ) {
  398. if ( event.touches.length == 1 ) {
  399. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  400. } else {
  401. const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  402. const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  403. panStart.set( x, y );
  404. }
  405. }
  406. function handleTouchStartDolly( event ) {
  407. const dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  408. const dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  409. const distance = Math.sqrt( dx * dx + dy * dy );
  410. dollyStart.set( 0, distance );
  411. }
  412. function handleTouchStartDollyPan( event ) {
  413. if ( scope.enableZoom ) handleTouchStartDolly( event );
  414. if ( scope.enablePan ) handleTouchStartPan( event );
  415. }
  416. function handleTouchStartDollyRotate( event ) {
  417. if ( scope.enableZoom ) handleTouchStartDolly( event );
  418. if ( scope.enableRotate ) handleTouchStartRotate( event );
  419. }
  420. function handleTouchMoveRotate( event ) {
  421. if ( event.touches.length == 1 ) {
  422. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  423. } else {
  424. const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  425. const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  426. rotateEnd.set( x, y );
  427. }
  428. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  429. const element = scope.domElement;
  430. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  431. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  432. rotateStart.copy( rotateEnd );
  433. }
  434. function handleTouchMovePan( event ) {
  435. if ( event.touches.length == 1 ) {
  436. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  437. } else {
  438. const x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  439. const y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  440. panEnd.set( x, y );
  441. }
  442. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  443. pan( panDelta.x, panDelta.y );
  444. panStart.copy( panEnd );
  445. }
  446. function handleTouchMoveDolly( event ) {
  447. const dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  448. const dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  449. const distance = Math.sqrt( dx * dx + dy * dy );
  450. dollyEnd.set( 0, distance );
  451. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  452. dollyOut( dollyDelta.y );
  453. dollyStart.copy( dollyEnd );
  454. }
  455. function handleTouchMoveDollyPan( event ) {
  456. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  457. if ( scope.enablePan ) handleTouchMovePan( event );
  458. }
  459. function handleTouchMoveDollyRotate( event ) {
  460. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  461. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  462. }
  463. function handleTouchEnd( /*event*/ ) {
  464. // no-op
  465. }
  466. //
  467. // event handlers - FSM: listen for events and reset state
  468. //
  469. function onPointerDown( event ) {
  470. if ( scope.enabled === false ) return;
  471. switch ( event.pointerType ) {
  472. case 'mouse':
  473. case 'pen':
  474. onMouseDown( event );
  475. break;
  476. // TODO touch
  477. }
  478. }
  479. function onPointerMove( event ) {
  480. if ( scope.enabled === false ) return;
  481. switch ( event.pointerType ) {
  482. case 'mouse':
  483. case 'pen':
  484. onMouseMove( event );
  485. break;
  486. // TODO touch
  487. }
  488. }
  489. function onPointerUp( event ) {
  490. switch ( event.pointerType ) {
  491. case 'mouse':
  492. case 'pen':
  493. onMouseUp( event );
  494. break;
  495. // TODO touch
  496. }
  497. }
  498. function onMouseDown( event ) {
  499. // Prevent the browser from scrolling.
  500. event.preventDefault();
  501. // Manually set the focus since calling preventDefault above
  502. // prevents the browser from setting it automatically.
  503. scope.domElement.focus ? scope.domElement.focus() : window.focus();
  504. let mouseAction;
  505. switch ( event.button ) {
  506. case 0:
  507. mouseAction = scope.mouseButtons.LEFT;
  508. break;
  509. case 1:
  510. mouseAction = scope.mouseButtons.MIDDLE;
  511. break;
  512. case 2:
  513. mouseAction = scope.mouseButtons.RIGHT;
  514. break;
  515. default:
  516. mouseAction = - 1;
  517. }
  518. switch ( mouseAction ) {
  519. case MOUSE.DOLLY:
  520. if ( scope.enableZoom === false ) return;
  521. handleMouseDownDolly( event );
  522. state = STATE.DOLLY;
  523. break;
  524. case MOUSE.ROTATE:
  525. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  526. if ( scope.enablePan === false ) return;
  527. handleMouseDownPan( event );
  528. state = STATE.PAN;
  529. } else {
  530. if ( scope.enableRotate === false ) return;
  531. handleMouseDownRotate( event );
  532. state = STATE.ROTATE;
  533. }
  534. break;
  535. case MOUSE.PAN:
  536. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  537. if ( scope.enableRotate === false ) return;
  538. handleMouseDownRotate( event );
  539. state = STATE.ROTATE;
  540. } else {
  541. if ( scope.enablePan === false ) return;
  542. handleMouseDownPan( event );
  543. state = STATE.PAN;
  544. }
  545. break;
  546. default:
  547. state = STATE.NONE;
  548. }
  549. if ( state !== STATE.NONE ) {
  550. scope.domElement.ownerDocument.addEventListener( 'pointermove', onPointerMove );
  551. scope.domElement.ownerDocument.addEventListener( 'pointerup', onPointerUp );
  552. scope.dispatchEvent( _startEvent );
  553. }
  554. }
  555. function onMouseMove( event ) {
  556. if ( scope.enabled === false ) return;
  557. event.preventDefault();
  558. switch ( state ) {
  559. case STATE.ROTATE:
  560. if ( scope.enableRotate === false ) return;
  561. handleMouseMoveRotate( event );
  562. break;
  563. case STATE.DOLLY:
  564. if ( scope.enableZoom === false ) return;
  565. handleMouseMoveDolly( event );
  566. break;
  567. case STATE.PAN:
  568. if ( scope.enablePan === false ) return;
  569. handleMouseMovePan( event );
  570. break;
  571. }
  572. }
  573. function onMouseUp( event ) {
  574. scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  575. scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
  576. if ( scope.enabled === false ) return;
  577. handleMouseUp( event );
  578. scope.dispatchEvent( _endEvent );
  579. state = STATE.NONE;
  580. }
  581. function onMouseWheel( event ) {
  582. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  583. event.preventDefault();
  584. scope.dispatchEvent( _startEvent );
  585. handleMouseWheel( event );
  586. scope.dispatchEvent( _endEvent );
  587. }
  588. function onKeyDown( event ) {
  589. if ( scope.enabled === false || scope.enablePan === false ) return;
  590. handleKeyDown( event );
  591. }
  592. function onTouchStart( event ) {
  593. if ( scope.enabled === false ) return;
  594. event.preventDefault(); // prevent scrolling
  595. switch ( event.touches.length ) {
  596. case 1:
  597. switch ( scope.touches.ONE ) {
  598. case TOUCH.ROTATE:
  599. if ( scope.enableRotate === false ) return;
  600. handleTouchStartRotate( event );
  601. state = STATE.TOUCH_ROTATE;
  602. break;
  603. case TOUCH.PAN:
  604. if ( scope.enablePan === false ) return;
  605. handleTouchStartPan( event );
  606. state = STATE.TOUCH_PAN;
  607. break;
  608. default:
  609. state = STATE.NONE;
  610. }
  611. break;
  612. case 2:
  613. switch ( scope.touches.TWO ) {
  614. case TOUCH.DOLLY_PAN:
  615. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  616. handleTouchStartDollyPan( event );
  617. state = STATE.TOUCH_DOLLY_PAN;
  618. break;
  619. case TOUCH.DOLLY_ROTATE:
  620. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  621. handleTouchStartDollyRotate( event );
  622. state = STATE.TOUCH_DOLLY_ROTATE;
  623. break;
  624. default:
  625. state = STATE.NONE;
  626. }
  627. break;
  628. default:
  629. state = STATE.NONE;
  630. }
  631. if ( state !== STATE.NONE ) {
  632. scope.dispatchEvent( _startEvent );
  633. }
  634. }
  635. function onTouchMove( event ) {
  636. if ( scope.enabled === false ) return;
  637. event.preventDefault(); // prevent scrolling
  638. switch ( state ) {
  639. case STATE.TOUCH_ROTATE:
  640. if ( scope.enableRotate === false ) return;
  641. handleTouchMoveRotate( event );
  642. scope.update();
  643. break;
  644. case STATE.TOUCH_PAN:
  645. if ( scope.enablePan === false ) return;
  646. handleTouchMovePan( event );
  647. scope.update();
  648. break;
  649. case STATE.TOUCH_DOLLY_PAN:
  650. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  651. handleTouchMoveDollyPan( event );
  652. scope.update();
  653. break;
  654. case STATE.TOUCH_DOLLY_ROTATE:
  655. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  656. handleTouchMoveDollyRotate( event );
  657. scope.update();
  658. break;
  659. default:
  660. state = STATE.NONE;
  661. }
  662. }
  663. function onTouchEnd( event ) {
  664. if ( scope.enabled === false ) return;
  665. handleTouchEnd( event );
  666. scope.dispatchEvent( _endEvent );
  667. state = STATE.NONE;
  668. }
  669. function onContextMenu( event ) {
  670. if ( scope.enabled === false ) return;
  671. event.preventDefault();
  672. }
  673. //
  674. scope.domElement.addEventListener( 'contextmenu', onContextMenu );
  675. scope.domElement.addEventListener( 'pointerdown', onPointerDown );
  676. scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  677. scope.domElement.addEventListener( 'touchstart', onTouchStart, { passive: false } );
  678. scope.domElement.addEventListener( 'touchend', onTouchEnd );
  679. scope.domElement.addEventListener( 'touchmove', onTouchMove, { passive: false } );
  680. // force an update at start
  681. this.update();
  682. }
  683. }
  684. // This set of controls performs orbiting, dollying (zooming), and panning.
  685. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  686. // This is very similar to OrbitControls, another set of touch behavior
  687. //
  688. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  689. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  690. // Pan - left mouse, or arrow keys / touch: one-finger move
  691. class MapControls extends OrbitControls {
  692. constructor( object, domElement ) {
  693. super( object, domElement );
  694. this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
  695. this.mouseButtons.LEFT = MOUSE.PAN;
  696. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  697. this.touches.ONE = TOUCH.PAN;
  698. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  699. }
  700. }
  701. export { OrbitControls, MapControls };