1
0

OrbitControls.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>OrbitControls - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <p class="inheritance" translate="no"><a href="EventDispatcher.html">EventDispatcher</a> → <a href="Controls.html">Controls</a> → </p>
  13. <h1 translate="no">OrbitControls</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>Orbit controls allow the camera to orbit around a target.</p>
  17. <p>OrbitControls performs orbiting, dollying (zooming), and panning. Unlike <a href="TrackballControls.html">TrackballControls</a>,
  18. it maintains the &quot;up&quot; direction <code>object.up</code> (+Y by default).</p>
  19. <ul>
  20. <li>Orbit: Left mouse / touch: one-finger move.</li>
  21. <li>Zoom: Middle mouse, or mousewheel / touch: two-finger spread or squish.</li>
  22. <li>Pan: Right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move.</li>
  23. </ul></div>
  24. <h2>Code Example</h2>
  25. <div translate="no"><pre><code class="language-js">const controls = new OrbitControls( camera, renderer.domElement );
  26. // controls.update() must be called after any manual changes to the camera's transform
  27. camera.position.set( 0, 20, 100 );
  28. controls.update();
  29. function animate() {
  30. // required if controls.enableDamping or controls.autoRotate are set to true
  31. controls.update();
  32. renderer.render( scene, camera );
  33. }
  34. </code></pre></div>
  35. </header>
  36. <article>
  37. <h2 class="subsection-title">Import</h2>
  38. <p><span translate="no">OrbitControls</span> is an addon, and must be imported explicitly, see <a href="https://threejs.org/manual/#en/installation" target="_blank" rel="noopener">Installation#Addons</a>.</p>
  39. <pre><code class="language-js">import { OrbitControls } from 'three/addons/controls/OrbitControls.js';</code></pre>
  40. <div class="container-overview">
  41. <h2>Constructor</h2>
  42. <h3 class="name name-method" id="OrbitControls" translate="no">new <a href="#OrbitControls">OrbitControls</a><span class="signature">( object : <span class="param-type"><a href="Object3D.html">Object3D</a></span>, domElement : <span class="param-type">HTMLElement</span> )</span> </h3>
  43. <div class="method">
  44. <div class="description">
  45. <p>Constructs a new controls instance.</p>
  46. </div>
  47. <table class="params">
  48. <tbody>
  49. <tr>
  50. <td class="name">
  51. <strong>object</strong>
  52. </td>
  53. <td class="description last">
  54. <p>The object that is managed by the controls.</p>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td class="name">
  59. <strong>domElement</strong>
  60. </td>
  61. <td class="description last">
  62. <p>The HTML element used for event listeners.</p>
  63. <p>Default is <code>null</code>.</p>
  64. </td>
  65. </tr>
  66. </tbody>
  67. </table>
  68. </div>
  69. </div>
  70. <h2 class="subsection-title">Properties</h2>
  71. <div class="member">
  72. <h3 class="name" id="autoRotate" translate="no">.<a href="#autoRotate">autoRotate</a><span class="type-signature"> : boolean</span> </h3>
  73. <div class="description">
  74. <p>Set to true to automatically rotate around the target</p>
  75. <p>Note that if this is enabled, you must call <code>update()</code> in your animation loop.
  76. If you want the auto-rotate speed to be independent of the frame rate (the refresh
  77. rate of the display), you must pass the time <code>deltaTime</code>, in seconds, to <code>update()</code>.</p>
  78. <p>Default is <code>false</code>.</p>
  79. </div>
  80. </div>
  81. <div class="member">
  82. <h3 class="name" id="autoRotateSpeed" translate="no">.<a href="#autoRotateSpeed">autoRotateSpeed</a><span class="type-signature"> : number</span> </h3>
  83. <div class="description">
  84. <p>How fast to rotate around the target if <code>autoRotate</code> is <code>true</code>. The default equates to 30 seconds
  85. per orbit at 60fps.</p>
  86. <p>Note that if <code>autoRotate</code> is enabled, you must call <code>update()</code> in your animation loop.</p>
  87. <p>Default is <code>2</code>.</p>
  88. </div>
  89. </div>
  90. <div class="member">
  91. <h3 class="name" id="cursor" translate="no">.<a href="#cursor">cursor</a><span class="type-signature"> : <a href="Vector3.html">Vector3</a></span> </h3>
  92. <div class="description">
  93. <p>The focus point of the <code>minTargetRadius</code> and <code>maxTargetRadius</code> limits.
  94. It can be updated manually at any point to change the center of interest
  95. for the <code>target</code>.</p>
  96. </div>
  97. </div>
  98. <div class="member">
  99. <h3 class="name" id="dampingFactor" translate="no">.<a href="#dampingFactor">dampingFactor</a><span class="type-signature"> : number</span> </h3>
  100. <div class="description">
  101. <p>The damping inertia used if <code>enableDamping</code> is set to <code>true</code>.</p>
  102. <p>Note that for this to work, you must call <code>update()</code> in your animation loop.</p>
  103. <p>Default is <code>0.05</code>.</p>
  104. </div>
  105. </div>
  106. <div class="member">
  107. <h3 class="name" id="enableDamping" translate="no">.<a href="#enableDamping">enableDamping</a><span class="type-signature"> : boolean</span> </h3>
  108. <div class="description">
  109. <p>Set to <code>true</code> to enable damping (inertia), which can be used to give a sense of weight
  110. to the controls. Note that if this is enabled, you must call <code>update()</code> in your animation
  111. loop.</p>
  112. <p>Default is <code>false</code>.</p>
  113. </div>
  114. </div>
  115. <div class="member">
  116. <h3 class="name" id="enablePan" translate="no">.<a href="#enablePan">enablePan</a><span class="type-signature"> : boolean</span> </h3>
  117. <div class="description">
  118. <p>Enable or disable camera panning.</p>
  119. <p>Default is <code>true</code>.</p>
  120. </div>
  121. </div>
  122. <div class="member">
  123. <h3 class="name" id="enableRotate" translate="no">.<a href="#enableRotate">enableRotate</a><span class="type-signature"> : boolean</span> </h3>
  124. <div class="description">
  125. <p>Enable or disable horizontal and vertical rotation of the camera.</p>
  126. <p>Note that it is possible to disable a single axis by setting the min and max of the
  127. <code>minPolarAngle</code> or <code>minAzimuthAngle</code> to the same value, which will cause the vertical
  128. or horizontal rotation to be fixed at that value.</p>
  129. <p>Default is <code>true</code>.</p>
  130. </div>
  131. </div>
  132. <div class="member">
  133. <h3 class="name" id="enableZoom" translate="no">.<a href="#enableZoom">enableZoom</a><span class="type-signature"> : boolean</span> </h3>
  134. <div class="description">
  135. <p>Enable or disable zooming (dollying) of the camera.</p>
  136. <p>Default is <code>true</code>.</p>
  137. </div>
  138. </div>
  139. <div class="member">
  140. <h3 class="name" id="keyPanSpeed" translate="no">.<a href="#keyPanSpeed">keyPanSpeed</a><span class="type-signature"> : number</span> </h3>
  141. <div class="description">
  142. <p>How fast to pan the camera when the keyboard is used in
  143. pixels per keypress.</p>
  144. <p>Default is <code>7</code>.</p>
  145. </div>
  146. </div>
  147. <div class="member">
  148. <h3 class="name" id="keyRotateSpeed" translate="no">.<a href="#keyRotateSpeed">keyRotateSpeed</a><span class="type-signature"> : number</span> </h3>
  149. <div class="description">
  150. <p>How fast to rotate the camera when the keyboard is used.</p>
  151. <p>Default is <code>1</code>.</p>
  152. </div>
  153. </div>
  154. <div class="member">
  155. <h3 class="name" id="keys" translate="no">.<a href="#keys">keys</a><span class="type-signature"> : Object</span> </h3>
  156. <div class="description">
  157. <p>This object contains references to the keycodes for controlling camera panning.</p>
  158. <pre><code class="language-js">controls.keys = {
  159. LEFT: 'ArrowLeft', //left arrow
  160. UP: 'ArrowUp', // up arrow
  161. RIGHT: 'ArrowRight', // right arrow
  162. BOTTOM: 'ArrowDown' // down arrow
  163. }
  164. </code></pre>
  165. </div>
  166. <dl class="details">
  167. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Controls.html#keys">Controls#keys</a></dt>
  168. </dl>
  169. </div>
  170. <div class="member">
  171. <h3 class="name" id="maxAzimuthAngle" translate="no">.<a href="#maxAzimuthAngle">maxAzimuthAngle</a><span class="type-signature"> : number</span> </h3>
  172. <div class="description">
  173. <p>How far you can orbit horizontally, upper limit. If set, the interval <code>[ min, max ]</code>
  174. must be a sub-interval of <code>[ - 2 PI, 2 PI ]</code>, with <code>( max - min &lt; 2 PI )</code>.</p>
  175. <p>Default is <code>-Infinity</code>.</p>
  176. </div>
  177. </div>
  178. <div class="member">
  179. <h3 class="name" id="maxDistance" translate="no">.<a href="#maxDistance">maxDistance</a><span class="type-signature"> : number</span> </h3>
  180. <div class="description">
  181. <p>How far you can dolly out (perspective camera only).</p>
  182. <p>Default is <code>Infinity</code>.</p>
  183. </div>
  184. </div>
  185. <div class="member">
  186. <h3 class="name" id="maxPolarAngle" translate="no">.<a href="#maxPolarAngle">maxPolarAngle</a><span class="type-signature"> : number</span> </h3>
  187. <div class="description">
  188. <p>How far you can orbit vertically, upper limit. Range is <code>[0, Math.PI]</code> radians.</p>
  189. <p>Default is <code>Math.PI</code>.</p>
  190. </div>
  191. </div>
  192. <div class="member">
  193. <h3 class="name" id="maxTargetRadius" translate="no">.<a href="#maxTargetRadius">maxTargetRadius</a><span class="type-signature"> : number</span> </h3>
  194. <div class="description">
  195. <p>How far you can move the target from the 3D <code>cursor</code>.</p>
  196. <p>Default is <code>Infinity</code>.</p>
  197. </div>
  198. </div>
  199. <div class="member">
  200. <h3 class="name" id="maxZoom" translate="no">.<a href="#maxZoom">maxZoom</a><span class="type-signature"> : number</span> </h3>
  201. <div class="description">
  202. <p>How far you can zoom out (orthographic camera only).</p>
  203. <p>Default is <code>Infinity</code>.</p>
  204. </div>
  205. </div>
  206. <div class="member">
  207. <h3 class="name" id="minAzimuthAngle" translate="no">.<a href="#minAzimuthAngle">minAzimuthAngle</a><span class="type-signature"> : number</span> </h3>
  208. <div class="description">
  209. <p>How far you can orbit horizontally, lower limit. If set, the interval <code>[ min, max ]</code>
  210. must be a sub-interval of <code>[ - 2 PI, 2 PI ]</code>, with <code>( max - min &lt; 2 PI )</code>.</p>
  211. <p>Default is <code>-Infinity</code>.</p>
  212. </div>
  213. </div>
  214. <div class="member">
  215. <h3 class="name" id="minDistance" translate="no">.<a href="#minDistance">minDistance</a><span class="type-signature"> : number</span> </h3>
  216. <div class="description">
  217. <p>How far you can dolly in (perspective camera only).</p>
  218. <p>Default is <code>0</code>.</p>
  219. </div>
  220. </div>
  221. <div class="member">
  222. <h3 class="name" id="minPolarAngle" translate="no">.<a href="#minPolarAngle">minPolarAngle</a><span class="type-signature"> : number</span> </h3>
  223. <div class="description">
  224. <p>How far you can orbit vertically, lower limit. Range is <code>[0, Math.PI]</code> radians.</p>
  225. <p>Default is <code>0</code>.</p>
  226. </div>
  227. </div>
  228. <div class="member">
  229. <h3 class="name" id="minTargetRadius" translate="no">.<a href="#minTargetRadius">minTargetRadius</a><span class="type-signature"> : number</span> </h3>
  230. <div class="description">
  231. <p>How close you can get the target to the 3D <code>cursor</code>.</p>
  232. <p>Default is <code>0</code>.</p>
  233. </div>
  234. </div>
  235. <div class="member">
  236. <h3 class="name" id="minZoom" translate="no">.<a href="#minZoom">minZoom</a><span class="type-signature"> : number</span> </h3>
  237. <div class="description">
  238. <p>How far you can zoom in (orthographic camera only).</p>
  239. <p>Default is <code>0</code>.</p>
  240. </div>
  241. </div>
  242. <div class="member">
  243. <h3 class="name" id="mouseButtons" translate="no">.<a href="#mouseButtons">mouseButtons</a><span class="type-signature"> : Object</span> </h3>
  244. <div class="description">
  245. <p>This object contains references to the mouse actions used by the controls.</p>
  246. <pre><code class="language-js">controls.mouseButtons = {
  247. LEFT: THREE.MOUSE.ROTATE,
  248. MIDDLE: THREE.MOUSE.DOLLY,
  249. RIGHT: THREE.MOUSE.PAN
  250. }
  251. </code></pre>
  252. </div>
  253. <dl class="details">
  254. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Controls.html#mouseButtons">Controls#mouseButtons</a></dt>
  255. </dl>
  256. </div>
  257. <div class="member">
  258. <h3 class="name" id="panSpeed" translate="no">.<a href="#panSpeed">panSpeed</a><span class="type-signature"> : number</span> </h3>
  259. <div class="description">
  260. <p>Speed of panning.</p>
  261. <p>Default is <code>1</code>.</p>
  262. </div>
  263. </div>
  264. <div class="member">
  265. <h3 class="name" id="position0" translate="no">.<a href="#position0">position0</a><span class="type-signature"> : <a href="Vector3.html">Vector3</a></span> </h3>
  266. <div class="description">
  267. <p>Used internally by <code>saveState()</code> and <code>reset()</code>.</p>
  268. </div>
  269. </div>
  270. <div class="member">
  271. <h3 class="name" id="rotateSpeed" translate="no">.<a href="#rotateSpeed">rotateSpeed</a><span class="type-signature"> : number</span> </h3>
  272. <div class="description">
  273. <p>Speed of rotation.</p>
  274. <p>Default is <code>1</code>.</p>
  275. </div>
  276. </div>
  277. <div class="member">
  278. <h3 class="name" id="screenSpacePanning" translate="no">.<a href="#screenSpacePanning">screenSpacePanning</a><span class="type-signature"> : boolean</span> </h3>
  279. <div class="description">
  280. <p>Defines how the camera's position is translated when panning. If <code>true</code>, the camera pans
  281. in screen space. Otherwise, the camera pans in the plane orthogonal to the camera's up
  282. direction.</p>
  283. <p>Default is <code>true</code>.</p>
  284. </div>
  285. </div>
  286. <div class="member">
  287. <h3 class="name" id="target" translate="no">.<a href="#target">target</a><span class="type-signature"> : <a href="Vector3.html">Vector3</a></span> </h3>
  288. <div class="description">
  289. <p>The focus point of the controls, the <code>object</code> orbits around this.
  290. It can be updated manually at any point to change the focus of the controls.</p>
  291. </div>
  292. </div>
  293. <div class="member">
  294. <h3 class="name" id="target0" translate="no">.<a href="#target0">target0</a><span class="type-signature"> : <a href="Vector3.html">Vector3</a></span> </h3>
  295. <div class="description">
  296. <p>Used internally by <code>saveState()</code> and <code>reset()</code>.</p>
  297. </div>
  298. </div>
  299. <div class="member">
  300. <h3 class="name" id="touches" translate="no">.<a href="#touches">touches</a><span class="type-signature"> : Object</span> </h3>
  301. <div class="description">
  302. <p>This object contains references to the touch actions used by the controls.</p>
  303. <pre><code class="language-js">controls.mouseButtons = {
  304. ONE: THREE.TOUCH.ROTATE,
  305. TWO: THREE.TOUCH.DOLLY_PAN
  306. }
  307. </code></pre>
  308. </div>
  309. <dl class="details">
  310. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Controls.html#touches">Controls#touches</a></dt>
  311. </dl>
  312. </div>
  313. <div class="member">
  314. <h3 class="name" id="zoom0" translate="no">.<a href="#zoom0">zoom0</a><span class="type-signature"> : number</span> </h3>
  315. <div class="description">
  316. <p>Used internally by <code>saveState()</code> and <code>reset()</code>.</p>
  317. </div>
  318. </div>
  319. <div class="member">
  320. <h3 class="name" id="zoomSpeed" translate="no">.<a href="#zoomSpeed">zoomSpeed</a><span class="type-signature"> : number</span> </h3>
  321. <div class="description">
  322. <p>Speed of zooming / dollying.</p>
  323. <p>Default is <code>1</code>.</p>
  324. </div>
  325. </div>
  326. <div class="member">
  327. <h3 class="name" id="zoomToCursor" translate="no">.<a href="#zoomToCursor">zoomToCursor</a><span class="type-signature"> : boolean</span> </h3>
  328. <div class="description">
  329. <p>Setting this property to <code>true</code> allows to zoom to the cursor's position.</p>
  330. <p>Default is <code>false</code>.</p>
  331. </div>
  332. </div>
  333. <h2 class="subsection-title">Methods</h2>
  334. <h3 class="name name-method" id="getAzimuthalAngle" translate="no">.<a href="#getAzimuthalAngle">getAzimuthalAngle</a><span class="signature">()</span><span class="type-signature"> : number</span> </h3>
  335. <div class="method">
  336. <div class="description">
  337. <p>Get the current horizontal rotation, in radians.</p>
  338. </div>
  339. <dl class="details">
  340. <dt class="tag-returns"><strong>Returns:</strong> The current horizontal rotation, in radians.</dt>
  341. </dl>
  342. </div>
  343. <h3 class="name name-method" id="getDistance" translate="no">.<a href="#getDistance">getDistance</a><span class="signature">()</span><span class="type-signature"> : number</span> </h3>
  344. <div class="method">
  345. <div class="description">
  346. <p>Returns the distance from the camera to the target.</p>
  347. </div>
  348. <dl class="details">
  349. <dt class="tag-returns"><strong>Returns:</strong> The distance from the camera to the target.</dt>
  350. </dl>
  351. </div>
  352. <h3 class="name name-method" id="getPolarAngle" translate="no">.<a href="#getPolarAngle">getPolarAngle</a><span class="signature">()</span><span class="type-signature"> : number</span> </h3>
  353. <div class="method">
  354. <div class="description">
  355. <p>Get the current vertical rotation, in radians.</p>
  356. </div>
  357. <dl class="details">
  358. <dt class="tag-returns"><strong>Returns:</strong> The current vertical rotation, in radians.</dt>
  359. </dl>
  360. </div>
  361. <h3 class="name name-method" id="listenToKeyEvents" translate="no">.<a href="#listenToKeyEvents">listenToKeyEvents</a><span class="signature">( domElement : <span class="param-type">HTMLElement</span> )</span> </h3>
  362. <div class="method">
  363. <div class="description">
  364. <p>Adds key event listeners to the given DOM element.
  365. <code>window</code> is a recommended argument for using this method.</p>
  366. </div>
  367. <table class="params">
  368. <tbody>
  369. <tr>
  370. <td class="name">
  371. <strong>domElement</strong>
  372. </td>
  373. <td class="description last">
  374. <p>The DOM element</p>
  375. </td>
  376. </tr>
  377. </tbody>
  378. </table>
  379. </div>
  380. <h3 class="name name-method" id="reset" translate="no">.<a href="#reset">reset</a><span class="signature">()</span> </h3>
  381. <div class="method">
  382. <div class="description">
  383. <p>Reset the controls to their state from either the last time the <code>saveState()</code>
  384. was called, or the initial state.</p>
  385. </div>
  386. </div>
  387. <h3 class="name name-method" id="saveState" translate="no">.<a href="#saveState">saveState</a><span class="signature">()</span> </h3>
  388. <div class="method">
  389. <div class="description">
  390. <p>Save the current state of the controls. This can later be recovered with <code>reset()</code>.</p>
  391. </div>
  392. </div>
  393. <h3 class="name name-method" id="stopListenToKeyEvents" translate="no">.<a href="#stopListenToKeyEvents">stopListenToKeyEvents</a><span class="signature">()</span> </h3>
  394. <div class="method">
  395. <div class="description">
  396. <p>Removes the key event listener previously defined with <code>listenToKeyEvents()</code>.</p>
  397. </div>
  398. </div>
  399. <h2 class="subsection-title">Events</h2>
  400. <h3 class="name name-method" id="event:change" translate="no">.<a href="#event:change">change</a> </h3>
  401. <div class="method">
  402. <div class="description">
  403. <p>Fires when the camera has been transformed by the controls.</p>
  404. </div>
  405. <h5>Type:</h5>
  406. <ul>
  407. <li>
  408. <span class="param-type">Object</span>
  409. </li>
  410. </ul>
  411. </div>
  412. <h3 class="name name-method" id="event:end" translate="no">.<a href="#event:end">end</a> </h3>
  413. <div class="method">
  414. <div class="description">
  415. <p>Fires when an interaction has finished.</p>
  416. </div>
  417. <h5>Type:</h5>
  418. <ul>
  419. <li>
  420. <span class="param-type">Object</span>
  421. </li>
  422. </ul>
  423. </div>
  424. <h3 class="name name-method" id="event:start" translate="no">.<a href="#event:start">start</a> </h3>
  425. <div class="method">
  426. <div class="description">
  427. <p>Fires when an interaction was initiated.</p>
  428. </div>
  429. <h5>Type:</h5>
  430. <ul>
  431. <li>
  432. <span class="param-type">Object</span>
  433. </li>
  434. </ul>
  435. </div>
  436. <h2 class="subsection-title">Source</h2>
  437. <p>
  438. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/controls/OrbitControls.js" translate="no" target="_blank" rel="noopener">examples/jsm/controls/OrbitControls.js</a>
  439. </p>
  440. </article>
  441. </section>
  442. <script src="../scripts/linenumber.js"></script>
  443. <script src="../scripts/page.js"></script>
  444. </body>
  445. </html>
粤ICP备19079148号