OrbitControls.js 24 KB

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