controls_ViewportControls.js.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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>"use strict";
  20. import {Viewport} from "../Viewport.js";
  21. import {Pointer} from "../input/Pointer.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. this.viewport = viewport;
  34. /**
  35. * Button used to drag and viewport around.
  36. *
  37. * On touch enabled devices the touch event is represented as a LEFT button.
  38. */
  39. this.dragButton = Pointer.RIGHT;
  40. /**
  41. * Is set to true allow the viewport to be scalled.
  42. *
  43. * Scaling is performed using the pointer scroll.
  44. */
  45. this.allowScale = true;
  46. /**
  47. * Flag to indicate if the viewport should move when scalling.
  48. *
  49. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  50. */
  51. this.moveOnScale = true;
  52. /**
  53. * If true allows the viewport to be rotated.
  54. *
  55. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  56. */
  57. this.allowRotation = false;
  58. /**
  59. * Value of the initial point of rotation if the viewport is being rotated.
  60. *
  61. * Is set to null when the viewport is not being rotated.
  62. */
  63. this.rotationPoint = null;
  64. /**
  65. * Initial rotation of the viewport.
  66. */
  67. this.rotationInitial = 0;
  68. }
  69. /**
  70. * Update the viewport controls using the pointer object.
  71. *
  72. * Should be called every frame before rendering.
  73. *
  74. * @param {Pointer} pointer
  75. */
  76. ViewportControls.prototype.update = function(pointer)
  77. {
  78. if(this.allowScale &amp;&amp; pointer.wheel !== 0)
  79. {
  80. this.viewport.scale -= pointer.wheel * 1e-3 * this.viewport.scale;
  81. if(this.moveOnScale)
  82. {
  83. var speed = pointer.wheel;
  84. var halfWidth = pointer.canvas.width / 2;
  85. var halfWeight = pointer.canvas.height / 2;
  86. this.viewport.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
  87. this.viewport.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
  88. }
  89. }
  90. if(this.allowRotation &amp;&amp; pointer.buttonPressed(Pointer.RIGHT) &amp;&amp; pointer.buttonPressed(Pointer.LEFT))
  91. {
  92. if(this.rotationPoint === null)
  93. {
  94. this.rotationPoint = pointer.position.clone();
  95. this.rotationInitial = this.viewport.rotation;
  96. }
  97. else
  98. {
  99. var pointer = pointer.position.clone();
  100. pointer.sub(this.rotationPoint);
  101. this.viewport.rotation = this.rotationInitial + pointer.angle();
  102. }
  103. }
  104. else
  105. {
  106. this.rotationPoint = null;
  107. if(pointer.buttonPressed(this.dragButton))
  108. {
  109. this.viewport.position.x += pointer.delta.x;
  110. this.viewport.position.y += pointer.delta.y;
  111. }
  112. }
  113. };
  114. export {ViewportControls};</code></pre>
  115. </article>
  116. </section>
  117. </div>
  118. <nav>
  119. <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>
  120. </nav>
  121. <br class="clear">
  122. <footer>
  123. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 13 2019 11:55:28 GMT+0100 (Western European Summer Time)
  124. </footer>
  125. <script> prettyPrint(); </script>
  126. <script src="scripts/linenumber.js"> </script>
  127. </body>
  128. </html>