controls_ViewportControls.js.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * @class
  26. * @param {Viewport} viewport
  27. */
  28. function ViewportControls(viewport)
  29. {
  30. /**
  31. * Viewport being controlled by this object.
  32. *
  33. * @type {Viewport}
  34. */
  35. this.viewport = viewport;
  36. /**
  37. * Button used to drag and viewport around.
  38. *
  39. * On touch enabled devices the touch event is represented as a LEFT button.
  40. *
  41. * @type {number}
  42. */
  43. this.dragButton = Pointer.RIGHT;
  44. /**
  45. * Is set to true allow the viewport to be scalled.
  46. *
  47. * Scaling is performed using the pointer scroll.
  48. *
  49. * @type {boolean}
  50. */
  51. this.allowScale = true;
  52. /**
  53. * Flag to indicate if the viewport should move when scalling.
  54. *
  55. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  56. *
  57. * @type {boolean}
  58. */
  59. this.moveOnScale = false;
  60. /**
  61. * If true allows the viewport to be rotated.
  62. *
  63. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  64. *
  65. * @type {boolean}
  66. */
  67. this.allowRotation = true;
  68. /**
  69. * Value of the initial point of rotation if the viewport is being rotated.
  70. *
  71. * Is set to null when the viewport is not being rotated.
  72. *
  73. * @type {Vector2 | null}
  74. */
  75. this.rotationPoint = null;
  76. /**
  77. * Initial rotation of the viewport.
  78. *
  79. * @type {number}
  80. */
  81. this.rotationInitial = 0;
  82. }
  83. /**
  84. * Update the viewport controls using the pointer object.
  85. *
  86. * Should be called every frame before rendering.
  87. *
  88. * @param {Pointer} pointer Pointer used to control the viewport.
  89. */
  90. ViewportControls.prototype.update = function(pointer)
  91. {
  92. // Scale
  93. if(this.allowScale &amp;&amp; pointer.wheel !== 0)
  94. {
  95. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  96. this.viewport.scale -= scale;
  97. this.viewport.matrixNeedsUpdate = true;
  98. // Move on scale
  99. if(this.moveOnScale &amp;&amp; pointer.canvas !== null)
  100. {
  101. this.viewport.updateMatrix();
  102. var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position);
  103. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  104. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  105. var delta = pointerWorld.clone();
  106. delta.sub(centerWorld);
  107. delta.multiplyScalar(0.1);
  108. this.viewport.position.sub(delta);
  109. this.viewport.matrixNeedsUpdate = true;
  110. }
  111. }
  112. // Rotation
  113. if(this.allowRotation &amp;&amp; pointer.buttonPressed(Pointer.RIGHT) &amp;&amp; pointer.buttonPressed(Pointer.LEFT))
  114. {
  115. // Rotation pivot
  116. if(this.rotationPoint === null)
  117. {
  118. this.rotationPoint = pointer.position.clone();
  119. this.rotationInitial = this.viewport.rotation;
  120. }
  121. else
  122. {
  123. var point = pointer.position.clone();
  124. point.sub(this.rotationPoint);
  125. this.viewport.rotation = this.rotationInitial + point.angle();
  126. this.viewport.matrixNeedsUpdate = true;
  127. }
  128. }
  129. // Drag
  130. else
  131. {
  132. this.rotationPoint = null;
  133. if(pointer.buttonPressed(this.dragButton))
  134. {
  135. this.viewport.position.x += pointer.delta.x;
  136. this.viewport.position.y += pointer.delta.y;
  137. this.viewport.matrixNeedsUpdate = true;
  138. }
  139. }
  140. };
  141. export {ViewportControls};
  142. </code></pre>
  143. </article>
  144. </section>
  145. </div>
  146. <nav>
  147. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</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="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</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="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>
  148. </nav>
  149. <br class="clear">
  150. <footer>
  151. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Sun Jun 14 2020 20:43:33 GMT+0100 (Western European Summer Time)
  152. </footer>
  153. <script> prettyPrint(); </script>
  154. <script src="scripts/linenumber.js"> </script>
  155. </body>
  156. </html>