TrackballControls.js 14 KB

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