Viewport.js.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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>"use strict";
  20. import {Vector2} from "./math/Vector2.js";
  21. import {Matrix} from "./math/Matrix.js";
  22. import {UUID} from "./math/UUID.js";
  23. import {Pointer} from "./input/Pointer.js";
  24. /**
  25. * Used to indicate how the user views the content inside of the canvas.
  26. *
  27. * @class
  28. * @param {DOM} canvas Canvas DOM element where the viewport is being rendered.
  29. */
  30. function Viewport(canvas)
  31. {
  32. /**
  33. * UUID of the object.
  34. */
  35. this.uuid = UUID.generate();
  36. /**
  37. * Canvas DOM element where the viewport is being rendered.
  38. */
  39. this.canvas = canvas;
  40. /**
  41. * Position of the object.
  42. */
  43. this.position = new Vector2(0, 0);
  44. /**
  45. * Scale of the object.
  46. */
  47. this.scale = 1.0
  48. /**
  49. * Rotation of the object relative to its center.
  50. */
  51. this.rotation = 0.0;
  52. /**
  53. * Local transformation matrix applied to the object.
  54. */
  55. this.matrix = new Matrix();
  56. /**
  57. * Inverse of the local transformation matrix.
  58. */
  59. this.inverseMatrix = new Matrix();
  60. /**
  61. * If true the matrix is updated before rendering the object.
  62. */
  63. this.matrixNeedsUpdate = true;
  64. /**
  65. * Flag to indicate if the viewport should move when scalling.
  66. *
  67. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  68. */
  69. this.moveOnScale = false;
  70. /**
  71. * Value of the initial point of rotation if the viewport is being rotated.
  72. *
  73. * Is set to null when the viewport is not being rotated.
  74. */
  75. this.rotationPoint = null;
  76. }
  77. /**
  78. * Calculate and update the viewport transformation matrix.
  79. *
  80. * Also updates the inverse matrix of the viewport.
  81. */
  82. Viewport.prototype.updateMatrix = function()
  83. {
  84. if(this.matrixNeedsUpdate)
  85. {
  86. this.matrix.m = [1, 0, 0, 1, this.position.x, this.position.y];
  87. if(this.rotation !== 0)
  88. {
  89. var c = Math.cos(this.rotation);
  90. var s = Math.sin(this.rotation);
  91. this.matrix.multiply(new Matrix([c, s, -s, c, 0, 0]));
  92. }
  93. if(this.scale !== 1)
  94. {
  95. this.matrix.scale(this.scale, this.scale);
  96. }
  97. this.inverseMatrix = this.matrix.getInverse();
  98. this.matrixNeedsUpdate = false;
  99. }
  100. };
  101. /**
  102. * Center the viewport relative to a object.
  103. *
  104. * The position of the object is used a central point, this method does not consider "box" attributes or other strucures in the object.
  105. *
  106. * @param {Object2D} object Object to be centered on the viewport.
  107. * @param {DOM} canvas Canvas element where the image is drawn.
  108. */
  109. Viewport.prototype.centerObject = function(object, canvas)
  110. {
  111. var position = object.globalMatrix.transformPoint(new Vector2());
  112. position.multiplyScalar(-this.scale);
  113. position.x += canvas.width / 2;
  114. position.y += canvas.height / 2;
  115. this.position.copy(position);
  116. this.matrixNeedsUpdate = true;
  117. };
  118. export {Viewport};
  119. </code></pre>
  120. </article>
  121. </section>
  122. </div>
  123. <nav>
  124. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><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="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</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="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</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>
  125. </nav>
  126. <br class="clear">
  127. <footer>
  128. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Tue Jun 25 2019 14:27:21 GMT+0100 (Western European Summer Time)
  129. </footer>
  130. <script> prettyPrint(); </script>
  131. <script src="scripts/linenumber.js"> </script>
  132. </body>
  133. </html>