TrackballControls.js 14 KB

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