Viewport.js.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: Viewport.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: Viewport.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {Vector2} from "./math/Vector2.js";
  20. import {Matrix} from "./math/Matrix.js";
  21. import {UUID} from "./math/UUID.js";
  22. /**
  23. * Viewport defines the user view into the content being rendered, similar to a camera it defines the size of the content, rotation and position of the content.
  24. *
  25. * The viewport can be moved, rotated and scaled to navigate the virtual canvas.
  26. *
  27. * @class
  28. * @param {Element} canvas Canvas DOM element where the viewport is being rendered.
  29. */
  30. function Viewport(canvas)
  31. {
  32. /**
  33. * UUID of the object.
  34. *
  35. * @type {string}
  36. */
  37. this.uuid = UUID.generate();
  38. /**
  39. * Canvas DOM element where the viewport is being rendered.
  40. *
  41. * @type {Element}
  42. */
  43. this.canvas = canvas;
  44. /**
  45. * Position of the viewport.
  46. *
  47. * @type {Vector2}
  48. */
  49. this.position = new Vector2(0, 0);
  50. /**
  51. * Center point of the viewport. Relative to the size of the canvas.
  52. *
  53. * Rotation and zoom is applied relative to this point.
  54. *
  55. * @type {Vector2}
  56. */
  57. this.center = new Vector2(0, 0);
  58. /**
  59. * Scale of the object.
  60. *
  61. * @type {number}
  62. */
  63. this.scale = 1.0
  64. /**
  65. * Rotation of the object relative to its center.
  66. *
  67. * @type {number}
  68. */
  69. this.rotation = 0.0;
  70. /**
  71. * Local transformation matrix applied to the object.
  72. *
  73. * @type {Matrix}
  74. */
  75. this.matrix = new Matrix();
  76. /**
  77. * Inverse of the local transformation matrix.
  78. *
  79. * Used to transform points from local to global coordinates.
  80. *
  81. * @type {Matrix}
  82. */
  83. this.inverseMatrix = new Matrix();
  84. /**
  85. * If true the matrix is updated before rendering the object.
  86. *
  87. * Disable this if you want to update the matrix manually.
  88. *
  89. * @type {boolean}
  90. */
  91. this.matrixNeedsUpdate = true;
  92. /**
  93. * Flag to indicate if the viewport should move when scaling.
  94. *
  95. * For some application its easier to focus the target if the viewport moves to the pointer location while scaling.
  96. */
  97. this.centerOnPointer = false;
  98. /**
  99. * Value of the initial point of rotation if the viewport is being rotated.
  100. *
  101. * Is set to null when the viewport is not being rotated.
  102. */
  103. this.rotationPoint = null;
  104. }
  105. /**
  106. * Calculate and update the viewport transformation matrix.
  107. *
  108. * Also updates the inverse matrix of the viewport.
  109. */
  110. Viewport.prototype.updateMatrix = function()
  111. {
  112. if(this.matrixNeedsUpdate)
  113. {
  114. this.matrix.m = [1, 0, 0, 1, this.position.x , this.position.y];
  115. if(this.center.x !== 0.0 || this.center.y !== 0.0) {
  116. this.matrix.multiply(new Matrix([1, 0, 0, 1, this.center.x, this.center.y]));
  117. }
  118. if(this.rotation !== 0.0)
  119. {
  120. var c = Math.cos(this.rotation);
  121. var s = Math.sin(this.rotation);
  122. this.matrix.multiply(new Matrix([c, s, -s, c, 0, 0]));
  123. }
  124. if(this.scale !== 1.0)
  125. {
  126. this.matrix.multiply(new Matrix([this.scale, 0, 0, this.scale, 0, 0]));
  127. }
  128. if(this.center.x !== 0.0 || this.center.y !== 0.0) {
  129. this.matrix.multiply(new Matrix([1, 0, 0, 1, -this.center.x, -this.center.y]));
  130. }
  131. this.inverseMatrix = this.matrix.getInverse();
  132. this.matrixNeedsUpdate = false;
  133. }
  134. };
  135. /**
  136. * Center the viewport relative to a object.
  137. *
  138. * The position of the object is used a central point, this method does not consider "box" attributes or other strucures in the object.
  139. *
  140. * Uses the object's local transformation matrix and the canvas size to calculate the new position of the viewport.
  141. *
  142. * @param {Object2D} object Object to be centered on the viewport.
  143. * @param {Element} canvas Canvas element where the image is drawn.
  144. */
  145. Viewport.prototype.centerObject = function(object, canvas)
  146. {
  147. var position = object.globalMatrix.transformPoint(new Vector2());
  148. position.multiplyScalar(-this.scale);
  149. position.x += canvas.width / 2;
  150. position.y += canvas.height / 2;
  151. this.position.copy(position);
  152. this.matrixNeedsUpdate = true;
  153. };
  154. export {Viewport};
  155. </code></pre>
  156. </article>
  157. </section>
  158. </div>
  159. <nav>
  160. <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>
  161. </nav>
  162. <br class="clear">
  163. <footer>
  164. 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)
  165. </footer>
  166. <script> prettyPrint(); </script>
  167. <script src="scripts/linenumber.js"> </script>
  168. </body>
  169. </html>