Renderer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. "use strict";
  2. import {Pointer} from "./input/Pointer.js";
  3. /**
  4. * The renderer is resposible for drawing the structure into the canvas element.
  5. *
  6. * Its also resposible for managing the canvas state.
  7. *
  8. * @class
  9. */
  10. function Renderer(canvas)
  11. {
  12. /**
  13. * Canvas DOM element, has to be managed by the user.
  14. */
  15. this.canvas = canvas;
  16. /**
  17. * Canvas 2D rendering context used to draw content.
  18. */
  19. this.context = canvas.getContext("2d");
  20. this.context.imageSmoothingEnabled = true;
  21. this.context.globalCompositeOperation = "source-over";
  22. /**
  23. * Pointer input handler object.
  24. */
  25. this.pointer = new Pointer();
  26. this.pointer.setCanvas(canvas);
  27. /**
  28. * Indicates if the canvas should be automatically cleared on each new frame.
  29. */
  30. this.autoClear = true;
  31. }
  32. /**
  33. * Creates a infinite render loop to render the group into a viewport each frame.
  34. *
  35. * The render loop cannot be destroyed.
  36. *
  37. * @param {Object2D} group Group to be rendererd.
  38. * @param {Viewport} viewport Viewport into the objects.
  39. * @param {Function} onUpdate Function called before rendering the frame.
  40. */
  41. Renderer.prototype.createRenderLoop = function(group, viewport, onUpdate)
  42. {
  43. function loop()
  44. {
  45. if(onUpdate !== undefined)
  46. {
  47. onUpdate();
  48. }
  49. this.update(group, viewport);
  50. requestAnimationFrame(loop);
  51. }
  52. loop();
  53. };
  54. /**
  55. * Update the renderer state, update the input handlers, calculate the object and viewport transformation matrices.
  56. *
  57. * Render the object using the viewport into a canvas element.
  58. *
  59. * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
  60. *
  61. * 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.
  62. *
  63. * @param object Object to be updated.
  64. * @param viewport Viewport to be updated (should be the one where the objects will be rendered after).
  65. */
  66. Renderer.prototype.update = function(object, viewport)
  67. {
  68. // Get objects to be rendered
  69. var objects = []
  70. object.traverse(function(child)
  71. {
  72. if(child.visible)
  73. {
  74. objects.push(child);
  75. }
  76. });
  77. // Sort objects by layer
  78. objects.sort(function(a, b)
  79. {
  80. return b.layer - a.layer;
  81. });
  82. // Pointer object update
  83. var pointer = this.pointer;
  84. pointer.update();
  85. // Viewport transform matrix
  86. viewport.updateControls(pointer);
  87. viewport.updateMatrix();
  88. // Project pointer coordinates
  89. var point = pointer.position.clone();
  90. var viewportPoint = viewport.inverseMatrix.transformPoint(point);
  91. // Object pointer events
  92. for(var i = 0; i < objects.length; i++)
  93. {
  94. var child = objects[i];
  95. //Process the
  96. if(child.pointerEvents)
  97. {
  98. var childPoint = child.inverseGlobalMatrix.transformPoint(child.ignoreViewport ? point : viewportPoint);
  99. // Check if the pointer pointer is inside
  100. if(child.isInside(childPoint))
  101. {
  102. // Pointer enter
  103. if(!child.pointerInside && child.onPointerEnter !== null)
  104. {
  105. child.onPointerEnter(pointer, viewport);
  106. }
  107. // Pointer over
  108. if(child.onPointerOver !== null)
  109. {
  110. child.onPointerOver(pointer, viewport);
  111. }
  112. // Pointer pressed
  113. if(pointer.buttonPressed(Pointer.LEFT) && child.onButtonPressed !== null)
  114. {
  115. child.onButtonPressed(pointer, viewport);
  116. }
  117. // Just released
  118. if(pointer.buttonJustReleased(Pointer.LEFT) && child.onButtonUp !== null)
  119. {
  120. child.onButtonUp(pointer, viewport);
  121. }
  122. // Pointer just pressed
  123. if(pointer.buttonJustPressed(Pointer.LEFT))
  124. {
  125. if(child.onButtonDown !== null)
  126. {
  127. child.onButtonDown(pointer, viewport);
  128. }
  129. // Drag object and break to only start a drag operation on the top element.
  130. if(child.draggable)
  131. {
  132. child.beingDragged = true;
  133. break;
  134. }
  135. }
  136. child.pointerInside = true;
  137. }
  138. else if(child.pointerInside)
  139. {
  140. // Pointer leave
  141. if(child.onPointerLeave !== null)
  142. {
  143. child.onPointerLeave(pointer, viewport);
  144. }
  145. child.pointerInside = false;
  146. }
  147. // Stop object drag
  148. if(pointer.buttonJustReleased(Pointer.LEFT))
  149. {
  150. if(child.draggable)
  151. {
  152. child.beingDragged = false;
  153. }
  154. }
  155. }
  156. }
  157. // Object drag events and update logic
  158. for(var i = 0; i < objects.length; i++)
  159. {
  160. var child = objects[i];
  161. // Pointer drag event
  162. if(child.beingDragged)
  163. {
  164. var lastPosition = pointer.position.clone();
  165. lastPosition.sub(pointer.delta);
  166. var positionWorld = viewport.inverseMatrix.transformPoint(pointer.position);
  167. var lastWorld = viewport.inverseMatrix.transformPoint(lastPosition);
  168. // Mouse delta in world coordinates
  169. positionWorld.sub(lastWorld);
  170. if(child.onPointerDrag !== null)
  171. {
  172. child.onPointerDrag(pointer, viewport, positionWorld);
  173. }
  174. }
  175. // On update
  176. if(child.onUpdate !== null)
  177. {
  178. child.onUpdate();
  179. }
  180. }
  181. // Update transformation matrices
  182. object.traverse(function(child)
  183. {
  184. child.updateMatrix();
  185. });
  186. // Sort objects by layer
  187. objects.sort(function(a, b)
  188. {
  189. return a.layer - b.layer;
  190. });
  191. this.context.setTransform(1, 0, 0, 1, 0, 0);
  192. // Clear canvas content
  193. if(this.autoClear)
  194. {
  195. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  196. }
  197. // Render into the canvas
  198. for(var i = 0; i < objects.length; i++)
  199. {
  200. if(objects[i].isMask)
  201. {
  202. continue;
  203. }
  204. if(objects[i].saveContextState)
  205. {
  206. this.context.save();
  207. }
  208. // Apply all masks
  209. var masks = objects[i].masks;
  210. for(var j = 0; j < masks.length; j++)
  211. {
  212. if(!masks[j].ignoreViewport)
  213. {
  214. viewport.matrix.setContextTransform(this.context);
  215. }
  216. masks[j].clip(this.context, viewport, this.canvas);
  217. }
  218. // Set the viewport transform
  219. if(!objects[i].ignoreViewport)
  220. {
  221. viewport.matrix.setContextTransform(this.context);
  222. }
  223. // Apply the object transform to the canvas context
  224. objects[i].transform(this.context, viewport, this.canvas);
  225. objects[i].draw(this.context, viewport, this.canvas);
  226. if(objects[i].restoreContextState)
  227. {
  228. this.context.restore();
  229. }
  230. }
  231. };
  232. export {Renderer};