2
0

controls_ViewportControls.js.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: controls/ViewportControls.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: controls/ViewportControls.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {Viewport} from "../Viewport.js";
  20. import {Pointer} from "../input/Pointer.js";
  21. import {Vector2} from "../math/Vector2.js";
  22. /**
  23. * Viewport controls are used to allow the user to control the viewport.
  24. *
  25. * The user controls the viewport using pointer input (e.g. mouse, touchscreen)
  26. *
  27. * @class
  28. * @param {Viewport} viewport
  29. */
  30. function ViewportControls(viewport)
  31. {
  32. /**
  33. * Viewport being controlled by this object.
  34. *
  35. * @type {Viewport}
  36. */
  37. this.viewport = viewport;
  38. /**
  39. * Button used to drag and viewport around.
  40. *
  41. * On touch enabled devices the touch event is represented as a LEFT button.
  42. *
  43. * @type {number}
  44. */
  45. this.dragButton = Pointer.RIGHT;
  46. /**
  47. * Button used to rotate the viewport.
  48. *
  49. * @type {number}
  50. */
  51. this.rotateButton = Pointer.MIDDLE;
  52. /**
  53. * Is set to true allow the viewport to be scalled.
  54. *
  55. * Scaling is performed using the pointer scroll.
  56. *
  57. * @type {boolean}
  58. */
  59. this.allowScale = true;
  60. /**
  61. * Flag to indicate if the viewport should automatically be recentered.
  62. *
  63. * This will cause the viewport center property to be automatically set based on an heuristic defined by the user.
  64. *
  65. * @type {number}
  66. */
  67. this.recenterViewport = ViewportControls.RECENTER_NONE;
  68. /**
  69. * If true allows the viewport to be rotated.
  70. *
  71. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  72. *
  73. * @type {boolean}
  74. */
  75. this.allowRotation = true;
  76. /**
  77. * Value of the initial point of rotation if the viewport is being rotated.
  78. *
  79. * Is the value of the pointer position when the rotation starts.
  80. *
  81. * Is set to null when the viewport is not being rotated.
  82. *
  83. * @type {Vector2 | null}
  84. */
  85. this.rotationPoint = null;
  86. /**
  87. * Initial rotation of the viewport.
  88. *
  89. * Is set to the current rotation of the viewport when the rotation starts.
  90. *
  91. * @type {number}
  92. */
  93. this.rotationInitial = 0;
  94. }
  95. /**
  96. * Viewport is not automatically recentered.
  97. *
  98. * The center point can be set manually by the developer.
  99. *
  100. * @type {number}
  101. */
  102. ViewportControls.RECENTER_NONE = 0;
  103. /**
  104. * Recenter the viewport automatically to the canvas.
  105. *
  106. * This will ensure that rotation and scaling will not cause the viewport to move around.
  107. *
  108. * @type {number}
  109. */
  110. ViewportControls.RECENTER_CANVAS = 1;
  111. /**
  112. * Viewport should automatically cente ron the pointer position.
  113. *
  114. * The viewport will simulataniously move to the pointer position while scalling.
  115. *
  116. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  117. *
  118. * @type {number}
  119. */
  120. ViewportControls.RECENTER_POINTER = 2;
  121. /**
  122. * Update the viewport controls using the pointer object.
  123. *
  124. * Should be called every frame before rendering.
  125. *
  126. * @param {Pointer} pointer Pointer used to control the viewport.
  127. */
  128. ViewportControls.prototype.update = function(pointer)
  129. {
  130. // Scale
  131. if(this.allowScale &amp;&amp; pointer.wheel !== 0)
  132. {
  133. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  134. this.viewport.scale -= scale;
  135. this.viewport.matrixNeedsUpdate = true;
  136. }
  137. // Rotation
  138. if(this.allowRotation &amp;&amp; pointer.buttonPressed(this.rotateButton))
  139. {
  140. // Rotation pivot
  141. if(this.rotationPoint === null)
  142. {
  143. this.rotationPoint = pointer.position.clone();
  144. this.rotationInitial = this.viewport.rotation;
  145. }
  146. else
  147. {
  148. var point = pointer.position.clone();
  149. point.sub(this.rotationPoint);
  150. this.viewport.rotation = this.rotationInitial + point.angle();
  151. this.viewport.matrixNeedsUpdate = true;
  152. }
  153. return;
  154. } else {
  155. this.rotationPoint = null;
  156. }
  157. // Drag
  158. if(pointer.buttonPressed(this.dragButton))
  159. {
  160. this.viewport.position.add(pointer.delta);
  161. this.viewport.matrixNeedsUpdate = true;
  162. }
  163. if (pointer.canvas === null) {
  164. return;
  165. }
  166. // Center viewport on canvas
  167. if (this.recenterViewport === ViewportControls.RECENTER_CANVAS) {
  168. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  169. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  170. this.viewport.center.copy(centerWorld);
  171. this.viewport.matrixNeedsUpdate = true;
  172. }
  173. // Center viewport on pointer
  174. else if(this.recenterViewport === ViewportControls.RECENTER_POINTER)
  175. {
  176. var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position)
  177. this.viewport.center.copy(pointerWorld);
  178. this.viewport.matrixNeedsUpdate = true;
  179. }
  180. };
  181. export {ViewportControls};
  182. </code></pre>
  183. </article>
  184. </section>
  185. </div>
  186. <nav>
  187. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
  188. </nav>
  189. <br class="clear">
  190. <footer>
  191. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  192. </footer>
  193. <script> prettyPrint(); </script>
  194. <script src="scripts/linenumber.js"> </script>
  195. </body>
  196. </html>