OrbitControls.js 26 KB

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