Renderer.js.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: Renderer.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: Renderer.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. import {Pointer} from "./input/Pointer.js";
  21. /**
  22. * The renderer is resposible for drawing the structure into the canvas element.
  23. *
  24. * Its also resposible for managing the canvas state.
  25. *
  26. * @class
  27. */
  28. function Renderer(canvas)
  29. {
  30. /**
  31. * Canvas DOM element, has to be managed by the user.
  32. */
  33. this.canvas = canvas;
  34. /**
  35. * Canvas 2D rendering context used to draw content.
  36. */
  37. this.context = canvas.getContext("2d");
  38. this.context.imageSmoothingEnabled = true;
  39. this.context.globalCompositeOperation = "source-over";
  40. /**
  41. * Pointer input handler object.
  42. */
  43. this.pointer = new Pointer();
  44. this.pointer.setCanvas(canvas);
  45. /**
  46. * Indicates if the canvas should be automatically cleared on each new frame.
  47. */
  48. this.autoClear = true;
  49. }
  50. /**
  51. * Creates a infinite render loop to render the group into a viewport each frame.
  52. *
  53. * The render loop cannot be destroyed.
  54. *
  55. * @param {Object2D} group Group to be rendererd.
  56. * @param {Viewport} viewport Viewport into the objects.
  57. * @param {Function} onUpdate Function called before rendering the frame.
  58. */
  59. Renderer.prototype.createRenderLoop = function(group, viewport, onUpdate)
  60. {
  61. function loop()
  62. {
  63. if(onUpdate !== undefined)
  64. {
  65. onUpdate();
  66. }
  67. this.update(group, viewport);
  68. requestAnimationFrame(loop);
  69. }
  70. loop();
  71. };
  72. /**
  73. * Update the renderer state, update the input handlers, calculate the object and viewport transformation matrices.
  74. *
  75. * Render the object using the viewport into a canvas element.
  76. *
  77. * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
  78. *
  79. * Should be called at a fixed rate preferably using the requestAnimationFrame() method, its also possible to use the createRenderLoop() method, that automatically creates a infinite render loop.
  80. *
  81. * @param object Object to be updated.
  82. * @param viewport Viewport to be updated (should be the one where the objects will be rendered after).
  83. */
  84. Renderer.prototype.update = function(object, viewport)
  85. {
  86. // Get objects to be rendered
  87. var objects = []
  88. object.traverse(function(child)
  89. {
  90. if(child.visible)
  91. {
  92. objects.push(child);
  93. }
  94. });
  95. // Sort objects by layer
  96. objects.sort(function(a, b)
  97. {
  98. return b.layer - a.layer;
  99. });
  100. // Pointer object update
  101. var pointer = this.pointer;
  102. pointer.update();
  103. // Viewport transform matrix
  104. viewport.updateControls(pointer);
  105. viewport.updateMatrix();
  106. // Project pointer coordinates
  107. var point = pointer.position.clone();
  108. var viewportPoint = viewport.inverseMatrix.transformPoint(point);
  109. // Object pointer events
  110. for(var i = 0; i &lt; objects.length; i++)
  111. {
  112. var child = objects[i];
  113. //Process the
  114. if(child.pointerEvents)
  115. {
  116. var childPoint = child.inverseGlobalMatrix.transformPoint(child.ignoreViewport ? point : viewportPoint);
  117. // Check if the pointer pointer is inside
  118. if(child.isInside(childPoint))
  119. {
  120. // Pointer enter
  121. if(!child.pointerInside &amp;&amp; child.onPointerEnter !== null)
  122. {
  123. child.onPointerEnter(pointer, viewport);
  124. }
  125. // Pointer over
  126. if(child.onPointerOver !== null)
  127. {
  128. child.onPointerOver(pointer, viewport);
  129. }
  130. // Pointer pressed
  131. if(pointer.buttonPressed(Pointer.LEFT) &amp;&amp; child.onButtonPressed !== null)
  132. {
  133. child.onButtonPressed(pointer, viewport);
  134. }
  135. // Just released
  136. if(pointer.buttonJustReleased(Pointer.LEFT) &amp;&amp; child.onButtonUp !== null)
  137. {
  138. child.onButtonUp(pointer, viewport);
  139. }
  140. // Pointer just pressed
  141. if(pointer.buttonJustPressed(Pointer.LEFT))
  142. {
  143. if(child.onButtonDown !== null)
  144. {
  145. child.onButtonDown(pointer, viewport);
  146. }
  147. // Drag object and break to only start a drag operation on the top element.
  148. if(child.draggable)
  149. {
  150. child.beingDragged = true;
  151. break;
  152. }
  153. }
  154. child.pointerInside = true;
  155. }
  156. else if(child.pointerInside)
  157. {
  158. // Pointer leave
  159. if(child.onPointerLeave !== null)
  160. {
  161. child.onPointerLeave(pointer, viewport);
  162. }
  163. child.pointerInside = false;
  164. }
  165. // Stop object drag
  166. if(pointer.buttonJustReleased(Pointer.LEFT))
  167. {
  168. if(child.draggable)
  169. {
  170. child.beingDragged = false;
  171. }
  172. }
  173. }
  174. }
  175. // Object drag events and update logic
  176. for(var i = 0; i &lt; objects.length; i++)
  177. {
  178. var child = objects[i];
  179. // Pointer drag event
  180. if(child.beingDragged)
  181. {
  182. var lastPosition = pointer.position.clone();
  183. lastPosition.sub(pointer.delta);
  184. var positionWorld = viewport.inverseMatrix.transformPoint(pointer.position);
  185. var lastWorld = viewport.inverseMatrix.transformPoint(lastPosition);
  186. // Mouse delta in world coordinates
  187. positionWorld.sub(lastWorld);
  188. if(child.onPointerDrag !== null)
  189. {
  190. child.onPointerDrag(pointer, viewport, positionWorld);
  191. }
  192. }
  193. // On update
  194. if(child.onUpdate !== null)
  195. {
  196. child.onUpdate();
  197. }
  198. }
  199. // Update transformation matrices
  200. object.traverse(function(child)
  201. {
  202. child.updateMatrix();
  203. });
  204. // Sort objects by layer
  205. objects.sort(function(a, b)
  206. {
  207. return a.layer - b.layer;
  208. });
  209. // Clear canvas content
  210. if(this.autoClear)
  211. {
  212. this.context.setTransform(1, 0, 0, 1, 0, 0);
  213. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  214. }
  215. // Set viewport matrix transform
  216. viewport.matrix.setContextTransform(this.context);
  217. // Render into the canvas
  218. for(var i = 0; i &lt; objects.length; i++)
  219. {
  220. if(objects[i].saveContextState)
  221. {
  222. this.context.save();
  223. }
  224. // Set identity if the viewport transform is to be ignored
  225. if(objects[i].ignoreViewport)
  226. {
  227. this.context.setTransform(1, 0, 0, 1, 0, 0);
  228. }
  229. // Apply the object trasnform to the canvas context
  230. objects[i].transform(this.context, viewport, this.canvas);
  231. // Stencil objects
  232. if(objects[i].isStencil)
  233. {
  234. objects[i].clip(this.context, viewport, this.canvas);
  235. }
  236. // Drawable objects
  237. else
  238. {
  239. objects[i].draw(this.context, viewport, this.canvas);
  240. }
  241. if(objects[i].restoreContextState)
  242. {
  243. this.context.restore();
  244. }
  245. }
  246. };
  247. export {Renderer};
  248. </code></pre>
  249. </article>
  250. </section>
  251. </div>
  252. <nav>
  253. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxStencil.html">BoxStencil</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="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="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="Stencil.html">Stencil</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></ul>
  254. </nav>
  255. <br class="clear">
  256. <footer>
  257. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 06 2019 15:37:27 GMT+0100 (Western European Summer Time)
  258. </footer>
  259. <script> prettyPrint(); </script>
  260. <script src="scripts/linenumber.js"> </script>
  261. </body>
  262. </html>