OrthographicTrackballControls.js 14 KB

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