TrackballControls.js 16 KB

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