OrbitControls.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. window.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. switch ( event.button ) {
  464. case 0:
  465. switch ( scope.mouseButtons.LEFT ) {
  466. case MOUSE.ROTATE:
  467. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  468. if ( scope.enablePan === false ) return;
  469. handleMouseDownPan( event );
  470. state = STATE.PAN;
  471. } else {
  472. if ( scope.enableRotate === false ) return;
  473. handleMouseDownRotate( event );
  474. state = STATE.ROTATE;
  475. }
  476. break;
  477. case MOUSE.PAN:
  478. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  479. if ( scope.enableRotate === false ) return;
  480. handleMouseDownRotate( event );
  481. state = STATE.ROTATE;
  482. } else {
  483. if ( scope.enablePan === false ) return;
  484. handleMouseDownPan( event );
  485. state = STATE.PAN;
  486. }
  487. break;
  488. default:
  489. state = STATE.NONE;
  490. }
  491. break;
  492. case 1:
  493. switch ( scope.mouseButtons.MIDDLE ) {
  494. case MOUSE.DOLLY:
  495. if ( scope.enableZoom === false ) return;
  496. handleMouseDownDolly( event );
  497. state = STATE.DOLLY;
  498. break;
  499. default:
  500. state = STATE.NONE;
  501. }
  502. break;
  503. case 2:
  504. switch ( scope.mouseButtons.RIGHT ) {
  505. case MOUSE.ROTATE:
  506. if ( scope.enableRotate === false ) return;
  507. handleMouseDownRotate( event );
  508. state = STATE.ROTATE;
  509. break;
  510. case MOUSE.PAN:
  511. if ( scope.enablePan === false ) return;
  512. handleMouseDownPan( event );
  513. state = STATE.PAN;
  514. break;
  515. default:
  516. state = STATE.NONE;
  517. }
  518. break;
  519. }
  520. if ( state !== STATE.NONE ) {
  521. document.addEventListener( 'mousemove', onMouseMove, false );
  522. document.addEventListener( 'mouseup', onMouseUp, false );
  523. scope.dispatchEvent( startEvent );
  524. }
  525. }
  526. function onMouseMove( event ) {
  527. if ( scope.enabled === false ) return;
  528. event.preventDefault();
  529. switch ( state ) {
  530. case STATE.ROTATE:
  531. if ( scope.enableRotate === false ) return;
  532. handleMouseMoveRotate( event );
  533. break;
  534. case STATE.DOLLY:
  535. if ( scope.enableZoom === false ) return;
  536. handleMouseMoveDolly( event );
  537. break;
  538. case STATE.PAN:
  539. if ( scope.enablePan === false ) return;
  540. handleMouseMovePan( event );
  541. break;
  542. }
  543. }
  544. function onMouseUp( event ) {
  545. if ( scope.enabled === false ) return;
  546. handleMouseUp( event );
  547. document.removeEventListener( 'mousemove', onMouseMove, false );
  548. document.removeEventListener( 'mouseup', onMouseUp, false );
  549. scope.dispatchEvent( endEvent );
  550. state = STATE.NONE;
  551. }
  552. function onMouseWheel( event ) {
  553. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  554. event.preventDefault();
  555. event.stopPropagation();
  556. scope.dispatchEvent( startEvent );
  557. handleMouseWheel( event );
  558. scope.dispatchEvent( endEvent );
  559. }
  560. function onKeyDown( event ) {
  561. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  562. handleKeyDown( event );
  563. }
  564. function onTouchStart( event ) {
  565. if ( scope.enabled === false ) return;
  566. event.preventDefault();
  567. switch ( event.touches.length ) {
  568. case 1:
  569. switch ( scope.touches.ONE ) {
  570. case TOUCH.ROTATE:
  571. if ( scope.enableRotate === false ) return;
  572. handleTouchStartRotate( event );
  573. state = STATE.TOUCH_ROTATE;
  574. break;
  575. case TOUCH.PAN:
  576. if ( scope.enablePan === false ) return;
  577. handleTouchStartPan( event );
  578. state = STATE.TOUCH_PAN;
  579. break;
  580. default:
  581. state = STATE.NONE;
  582. }
  583. break;
  584. case 2:
  585. switch ( scope.touches.TWO ) {
  586. case TOUCH.DOLLY_PAN:
  587. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  588. handleTouchStartDollyPan( event );
  589. state = STATE.TOUCH_DOLLY_PAN;
  590. break;
  591. case TOUCH.DOLLY_ROTATE:
  592. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  593. handleTouchStartDollyRotate( event );
  594. state = STATE.TOUCH_DOLLY_ROTATE;
  595. break;
  596. default:
  597. state = STATE.NONE;
  598. }
  599. break;
  600. default:
  601. state = STATE.NONE;
  602. }
  603. if ( state !== STATE.NONE ) {
  604. scope.dispatchEvent( startEvent );
  605. }
  606. }
  607. function onTouchMove( event ) {
  608. if ( scope.enabled === false ) return;
  609. event.preventDefault();
  610. event.stopPropagation();
  611. switch ( state ) {
  612. case STATE.TOUCH_ROTATE:
  613. if ( scope.enableRotate === false ) return;
  614. handleTouchMoveRotate( event );
  615. scope.update();
  616. break;
  617. case STATE.TOUCH_PAN:
  618. if ( scope.enablePan === false ) return;
  619. handleTouchMovePan( event );
  620. scope.update();
  621. break;
  622. case STATE.TOUCH_DOLLY_PAN:
  623. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  624. handleTouchMoveDollyPan( event );
  625. scope.update();
  626. break;
  627. case STATE.TOUCH_DOLLY_ROTATE:
  628. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  629. handleTouchMoveDollyRotate( event );
  630. scope.update();
  631. break;
  632. default:
  633. state = STATE.NONE;
  634. }
  635. }
  636. function onTouchEnd( event ) {
  637. if ( scope.enabled === false ) return;
  638. handleTouchEnd( event );
  639. scope.dispatchEvent( endEvent );
  640. state = STATE.NONE;
  641. }
  642. function onContextMenu( event ) {
  643. if ( scope.enabled === false ) return;
  644. event.preventDefault();
  645. }
  646. //
  647. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  648. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  649. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  650. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  651. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  652. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  653. window.addEventListener( 'keydown', onKeyDown, false );
  654. // force an update at start
  655. this.update();
  656. };
  657. OrbitControls.prototype = Object.create( EventDispatcher.prototype );
  658. OrbitControls.prototype.constructor = OrbitControls;
  659. // This set of controls performs orbiting, dollying (zooming), and panning.
  660. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  661. // This is very similar to OrbitControls, another set of touch behavior
  662. //
  663. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  664. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  665. // Pan - left mouse, or arrow keys / touch: one-finger move
  666. var MapControls = function ( object, domElement ) {
  667. OrbitControls.call( this, object, domElement );
  668. this.mouseButtons.LEFT = MOUSE.PAN;
  669. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  670. this.touches.ONE = TOUCH.PAN;
  671. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  672. };
  673. MapControls.prototype = Object.create( EventDispatcher.prototype );
  674. MapControls.prototype.constructor = MapControls;
  675. export { OrbitControls, MapControls };