TrackballControls.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. * @author Mark Lundin / http://mark-lundin.com
  4. * @author Simone Manini / http://daron1337.github.io
  5. * @author Luca Antiga / http://lantiga.github.io
  6. */
  7. import {
  8. EventDispatcher,
  9. MOUSE,
  10. Quaternion,
  11. Vector2,
  12. Vector3
  13. } from "../../../build/three.module.js";
  14. var TrackballControls = function ( object, domElement ) {
  15. if ( domElement === undefined ) console.warn( 'THREE.TrackballControls: The second parameter "domElement" is now mandatory.' );
  16. if ( domElement === document ) console.error( 'THREE.TrackballControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.' );
  17. var _this = this;
  18. var STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  19. this.object = object;
  20. this.domElement = domElement;
  21. // API
  22. this.enabled = true;
  23. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  24. this.rotateSpeed = 1.0;
  25. this.zoomSpeed = 1.2;
  26. this.panSpeed = 0.3;
  27. this.noRotate = false;
  28. this.noZoom = false;
  29. this.noPan = false;
  30. this.staticMoving = false;
  31. this.dynamicDampingFactor = 0.2;
  32. this.minDistance = 0;
  33. this.maxDistance = Infinity;
  34. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  35. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.ZOOM, RIGHT: MOUSE.PAN };
  36. // internals
  37. this.target = new Vector3();
  38. var EPS = 0.000001;
  39. var lastPosition = new Vector3();
  40. var lastZoom = 1;
  41. var _state = STATE.NONE,
  42. _keyState = STATE.NONE,
  43. _eye = new Vector3(),
  44. _movePrev = new Vector2(),
  45. _moveCurr = new Vector2(),
  46. _lastAxis = new Vector3(),
  47. _lastAngle = 0,
  48. _zoomStart = new Vector2(),
  49. _zoomEnd = new Vector2(),
  50. _touchZoomDistanceStart = 0,
  51. _touchZoomDistanceEnd = 0,
  52. _panStart = new Vector2(),
  53. _panEnd = new Vector2();
  54. // for reset
  55. this.target0 = this.target.clone();
  56. this.position0 = this.object.position.clone();
  57. this.up0 = this.object.up.clone();
  58. this.zoom0 = this.object.zoom;
  59. // events
  60. var changeEvent = { type: 'change' };
  61. var startEvent = { type: 'start' };
  62. var endEvent = { type: 'end' };
  63. // methods
  64. this.handleResize = function () {
  65. var box = this.domElement.getBoundingClientRect();
  66. // adjustments come from similar code in the jquery offset() function
  67. var d = this.domElement.ownerDocument.documentElement;
  68. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  69. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  70. this.screen.width = box.width;
  71. this.screen.height = box.height;
  72. };
  73. var getMouseOnScreen = ( function () {
  74. var vector = new Vector2();
  75. return function getMouseOnScreen( pageX, pageY ) {
  76. vector.set(
  77. ( pageX - _this.screen.left ) / _this.screen.width,
  78. ( pageY - _this.screen.top ) / _this.screen.height
  79. );
  80. return vector;
  81. };
  82. }() );
  83. var getMouseOnCircle = ( function () {
  84. var vector = new Vector2();
  85. return function getMouseOnCircle( pageX, pageY ) {
  86. vector.set(
  87. ( ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ) ),
  88. ( ( _this.screen.height + 2 * ( _this.screen.top - pageY ) ) / _this.screen.width ) // screen.width intentional
  89. );
  90. return vector;
  91. };
  92. }() );
  93. this.rotateCamera = ( function () {
  94. var axis = new Vector3(),
  95. quaternion = new Quaternion(),
  96. eyeDirection = new Vector3(),
  97. objectUpDirection = new Vector3(),
  98. objectSidewaysDirection = new Vector3(),
  99. moveDirection = new Vector3(),
  100. angle;
  101. return function rotateCamera() {
  102. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  103. angle = moveDirection.length();
  104. if ( angle ) {
  105. _eye.copy( _this.object.position ).sub( _this.target );
  106. eyeDirection.copy( _eye ).normalize();
  107. objectUpDirection.copy( _this.object.up ).normalize();
  108. objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
  109. objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
  110. objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
  111. moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
  112. axis.crossVectors( moveDirection, _eye ).normalize();
  113. angle *= _this.rotateSpeed;
  114. quaternion.setFromAxisAngle( axis, angle );
  115. _eye.applyQuaternion( quaternion );
  116. _this.object.up.applyQuaternion( quaternion );
  117. _lastAxis.copy( axis );
  118. _lastAngle = angle;
  119. } else if ( ! _this.staticMoving && _lastAngle ) {
  120. _lastAngle *= Math.sqrt( 1.0 - _this.dynamicDampingFactor );
  121. _eye.copy( _this.object.position ).sub( _this.target );
  122. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  123. _eye.applyQuaternion( quaternion );
  124. _this.object.up.applyQuaternion( quaternion );
  125. }
  126. _movePrev.copy( _moveCurr );
  127. };
  128. }() );
  129. this.zoomCamera = function () {
  130. var factor;
  131. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  132. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  133. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  134. if ( _this.object.isPerspectiveCamera ) {
  135. _eye.multiplyScalar( factor );
  136. } else if ( _this.object.isOrthographicCamera ) {
  137. _this.object.zoom *= factor;
  138. _this.object.updateProjectionMatrix();
  139. } else {
  140. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  141. }
  142. } else {
  143. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  144. if ( factor !== 1.0 && factor > 0.0 ) {
  145. if ( _this.object.isPerspectiveCamera ) {
  146. _eye.multiplyScalar( factor );
  147. } else if ( _this.object.isOrthographicCamera ) {
  148. _this.object.zoom /= factor;
  149. _this.object.updateProjectionMatrix();
  150. } else {
  151. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  152. }
  153. }
  154. if ( _this.staticMoving ) {
  155. _zoomStart.copy( _zoomEnd );
  156. } else {
  157. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  158. }
  159. }
  160. };
  161. this.panCamera = ( function () {
  162. var mouseChange = new Vector2(),
  163. objectUp = new Vector3(),
  164. pan = new Vector3();
  165. return function panCamera() {
  166. mouseChange.copy( _panEnd ).sub( _panStart );
  167. if ( mouseChange.lengthSq() ) {
  168. if ( _this.object.isOrthographicCamera ) {
  169. var scale_x = ( _this.object.right - _this.object.left ) / _this.object.zoom / _this.domElement.clientWidth;
  170. var scale_y = ( _this.object.top - _this.object.bottom ) / _this.object.zoom / _this.domElement.clientWidth;
  171. mouseChange.x *= scale_x;
  172. mouseChange.y *= scale_y;
  173. }
  174. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  175. pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
  176. pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
  177. _this.object.position.add( pan );
  178. _this.target.add( pan );
  179. if ( _this.staticMoving ) {
  180. _panStart.copy( _panEnd );
  181. } else {
  182. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  183. }
  184. }
  185. };
  186. }() );
  187. this.checkDistances = function () {
  188. if ( ! _this.noZoom || ! _this.noPan ) {
  189. if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  190. _this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
  191. _zoomStart.copy( _zoomEnd );
  192. }
  193. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  194. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  195. _zoomStart.copy( _zoomEnd );
  196. }
  197. }
  198. };
  199. this.update = function () {
  200. _eye.subVectors( _this.object.position, _this.target );
  201. if ( ! _this.noRotate ) {
  202. _this.rotateCamera();
  203. }
  204. if ( ! _this.noZoom ) {
  205. _this.zoomCamera();
  206. }
  207. if ( ! _this.noPan ) {
  208. _this.panCamera();
  209. }
  210. _this.object.position.addVectors( _this.target, _eye );
  211. if ( _this.object.isPerspectiveCamera ) {
  212. _this.checkDistances();
  213. _this.object.lookAt( _this.target );
  214. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS ) {
  215. _this.dispatchEvent( changeEvent );
  216. lastPosition.copy( _this.object.position );
  217. }
  218. } else if ( _this.object.isOrthographicCamera ) {
  219. _this.object.lookAt( _this.target );
  220. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS || lastZoom !== _this.object.zoom ) {
  221. _this.dispatchEvent( changeEvent );
  222. lastPosition.copy( _this.object.position );
  223. lastZoom = _this.object.zoom;
  224. }
  225. } else {
  226. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  227. }
  228. };
  229. this.reset = function () {
  230. _state = STATE.NONE;
  231. _keyState = STATE.NONE;
  232. _this.target.copy( _this.target0 );
  233. _this.object.position.copy( _this.position0 );
  234. _this.object.up.copy( _this.up0 );
  235. _this.object.zoom = _this.zoom0;
  236. _this.object.updateProjectionMatrix();
  237. _eye.subVectors( _this.object.position, _this.target );
  238. _this.object.lookAt( _this.target );
  239. _this.dispatchEvent( changeEvent );
  240. lastPosition.copy( _this.object.position );
  241. lastZoom = _this.object.zoom;
  242. };
  243. // listeners
  244. function keydown( event ) {
  245. if ( _this.enabled === false ) return;
  246. window.removeEventListener( 'keydown', keydown );
  247. if ( _keyState !== STATE.NONE ) {
  248. return;
  249. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
  250. _keyState = STATE.ROTATE;
  251. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
  252. _keyState = STATE.ZOOM;
  253. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
  254. _keyState = STATE.PAN;
  255. }
  256. }
  257. function keyup() {
  258. if ( _this.enabled === false ) return;
  259. _keyState = STATE.NONE;
  260. window.addEventListener( 'keydown', keydown, false );
  261. }
  262. function mousedown( event ) {
  263. if ( _this.enabled === false ) return;
  264. event.preventDefault();
  265. event.stopPropagation();
  266. if ( _state === STATE.NONE ) {
  267. switch ( event.button ) {
  268. case _this.mouseButtons.LEFT:
  269. _state = STATE.ROTATE;
  270. break;
  271. case _this.mouseButtons.MIDDLE:
  272. _state = STATE.ZOOM;
  273. break;
  274. case _this.mouseButtons.RIGHT:
  275. _state = STATE.PAN;
  276. break;
  277. default:
  278. _state = STATE.NONE;
  279. }
  280. }
  281. var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  282. if ( state === STATE.ROTATE && ! _this.noRotate ) {
  283. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  284. _movePrev.copy( _moveCurr );
  285. } else if ( state === STATE.ZOOM && ! _this.noZoom ) {
  286. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  287. _zoomEnd.copy( _zoomStart );
  288. } else if ( state === STATE.PAN && ! _this.noPan ) {
  289. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  290. _panEnd.copy( _panStart );
  291. }
  292. document.addEventListener( 'mousemove', mousemove, false );
  293. document.addEventListener( 'mouseup', mouseup, false );
  294. _this.dispatchEvent( startEvent );
  295. }
  296. function mousemove( event ) {
  297. if ( _this.enabled === false ) return;
  298. event.preventDefault();
  299. event.stopPropagation();
  300. var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  301. if ( state === STATE.ROTATE && ! _this.noRotate ) {
  302. _movePrev.copy( _moveCurr );
  303. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  304. } else if ( state === STATE.ZOOM && ! _this.noZoom ) {
  305. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  306. } else if ( state === STATE.PAN && ! _this.noPan ) {
  307. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  308. }
  309. }
  310. function mouseup( event ) {
  311. if ( _this.enabled === false ) return;
  312. event.preventDefault();
  313. event.stopPropagation();
  314. _state = STATE.NONE;
  315. document.removeEventListener( 'mousemove', mousemove );
  316. document.removeEventListener( 'mouseup', mouseup );
  317. _this.dispatchEvent( endEvent );
  318. }
  319. function mousewheel( event ) {
  320. if ( _this.enabled === false ) return;
  321. if ( _this.noZoom === true ) return;
  322. event.preventDefault();
  323. event.stopPropagation();
  324. switch ( event.deltaMode ) {
  325. case 2:
  326. // Zoom in pages
  327. _zoomStart.y -= event.deltaY * 0.025;
  328. break;
  329. case 1:
  330. // Zoom in lines
  331. _zoomStart.y -= event.deltaY * 0.01;
  332. break;
  333. default:
  334. // undefined, 0, assume pixels
  335. _zoomStart.y -= event.deltaY * 0.00025;
  336. break;
  337. }
  338. _this.dispatchEvent( startEvent );
  339. _this.dispatchEvent( endEvent );
  340. }
  341. function touchstart( event ) {
  342. if ( _this.enabled === false ) return;
  343. event.preventDefault();
  344. switch ( event.touches.length ) {
  345. case 1:
  346. _state = STATE.TOUCH_ROTATE;
  347. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  348. _movePrev.copy( _moveCurr );
  349. break;
  350. default: // 2 or more
  351. _state = STATE.TOUCH_ZOOM_PAN;
  352. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  353. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  354. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  355. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  356. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  357. _panStart.copy( getMouseOnScreen( x, y ) );
  358. _panEnd.copy( _panStart );
  359. break;
  360. }
  361. _this.dispatchEvent( startEvent );
  362. }
  363. function touchmove( event ) {
  364. if ( _this.enabled === false ) return;
  365. event.preventDefault();
  366. event.stopPropagation();
  367. switch ( event.touches.length ) {
  368. case 1:
  369. _movePrev.copy( _moveCurr );
  370. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  371. break;
  372. default: // 2 or more
  373. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  374. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  375. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  376. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  377. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  378. _panEnd.copy( getMouseOnScreen( x, y ) );
  379. break;
  380. }
  381. }
  382. function touchend( event ) {
  383. if ( _this.enabled === false ) return;
  384. switch ( event.touches.length ) {
  385. case 0:
  386. _state = STATE.NONE;
  387. break;
  388. case 1:
  389. _state = STATE.TOUCH_ROTATE;
  390. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  391. _movePrev.copy( _moveCurr );
  392. break;
  393. }
  394. _this.dispatchEvent( endEvent );
  395. }
  396. function contextmenu( event ) {
  397. if ( _this.enabled === false ) return;
  398. event.preventDefault();
  399. }
  400. this.dispose = function () {
  401. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  402. this.domElement.removeEventListener( 'mousedown', mousedown, false );
  403. this.domElement.removeEventListener( 'wheel', mousewheel, false );
  404. this.domElement.removeEventListener( 'touchstart', touchstart, false );
  405. this.domElement.removeEventListener( 'touchend', touchend, false );
  406. this.domElement.removeEventListener( 'touchmove', touchmove, false );
  407. document.removeEventListener( 'mousemove', mousemove, false );
  408. document.removeEventListener( 'mouseup', mouseup, false );
  409. window.removeEventListener( 'keydown', keydown, false );
  410. window.removeEventListener( 'keyup', keyup, false );
  411. };
  412. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  413. this.domElement.addEventListener( 'mousedown', mousedown, false );
  414. this.domElement.addEventListener( 'wheel', mousewheel, false );
  415. this.domElement.addEventListener( 'touchstart', touchstart, false );
  416. this.domElement.addEventListener( 'touchend', touchend, false );
  417. this.domElement.addEventListener( 'touchmove', touchmove, false );
  418. window.addEventListener( 'keydown', keydown, false );
  419. window.addEventListener( 'keyup', keyup, false );
  420. this.handleResize();
  421. // force an update at start
  422. this.update();
  423. };
  424. TrackballControls.prototype = Object.create( EventDispatcher.prototype );
  425. TrackballControls.prototype.constructor = TrackballControls;
  426. export { TrackballControls };