OrbitControls.js 24 KB

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