Object2D.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. "use strict";
  2. import {Vector2} from "./math/Vector2.js";
  3. import {Matrix} from "./math/Matrix.js";
  4. import {UUID} from "./math/UUID.js";
  5. /**
  6. * Base 2D object class, implements all the object positioning and scalling features.
  7. *
  8. * @class
  9. */
  10. function Object2D()
  11. {
  12. /**
  13. * UUID of the object.
  14. */
  15. this.uuid = UUID.generate();
  16. /**
  17. * List of children objects attached to the object.
  18. */
  19. this.children = [];
  20. /**
  21. * Parent object, the object position is affected by its parent position.
  22. */
  23. this.parent = null;
  24. /**
  25. * Position of the object.
  26. */
  27. this.position = new Vector2(0, 0);
  28. /**
  29. * Origin of the object used as point of rotation.
  30. */
  31. this.origin = new Vector2(0, 0);
  32. /**
  33. * Scale of the object.
  34. */
  35. this.scale = new Vector2(1, 1);
  36. /**
  37. * Rotation of the object relative to its center.
  38. */
  39. this.rotation = 0.0;
  40. /**
  41. * Indicates if the object is visible.
  42. */
  43. this.visible = true;
  44. /**
  45. * Layer of this object, objects are sorted by layer value.
  46. *
  47. * Lower layer value is draw first.
  48. */
  49. this.layer = 0;
  50. /**
  51. * Local transformation matrix applied to the object.
  52. */
  53. this.matrix = new Matrix();
  54. /**
  55. * Global transformation matrix multiplied by the parent matrix.
  56. *
  57. * Used to transform the object before projecting into screen coordinates.
  58. */
  59. this.globalMatrix = new Matrix();
  60. /**
  61. * Inverse of the global matrix.
  62. *
  63. * Used to convert pointer input points into object coordinates.
  64. */
  65. this.inverseGlobalMatrix = new Matrix();
  66. /**
  67. * Masks being applied to this object.
  68. *
  69. * Multiple masks can be used simultaneously.
  70. */
  71. this.masks = [];
  72. /**
  73. * If true the matrix is updated before rendering the object.
  74. */
  75. this.matrixNeedsUpdate = true;
  76. /**
  77. * Indicates if its possible to drag the object around.
  78. *
  79. * If true the onPointerDrag callback is used to update the state of the object.
  80. */
  81. this.draggable = false;
  82. /**
  83. * Indicates if this object uses pointer events.
  84. *
  85. * Can be set false to skip the pointer interaction events.
  86. */
  87. this.pointerEvents = true;
  88. /**
  89. * Flag to indicate wheter this objet ignores the viewport transformation.
  90. */
  91. this.ignoreViewport = false;
  92. /**
  93. * Flag to indicate if the context of canvas should be saved before render.
  94. */
  95. this.saveContextState = true;
  96. /**
  97. * Flag to indicate if the context of canvas should be restored after render.
  98. */
  99. this.restoreContextState = true;
  100. /**
  101. * Flag indicating if the pointer is inside of the element.
  102. *
  103. * Used to control object event.
  104. */
  105. this.pointerInside = false;
  106. /**
  107. * Flag to indicate if the object is currently being dragged.
  108. */
  109. this.beingDragged = false;
  110. }
  111. /**
  112. * Traverse the object tree and run a function for all objects.
  113. *
  114. * @param callback Callback function that receives the object as parameter.
  115. */
  116. Object2D.prototype.traverse = function(callback)
  117. {
  118. callback(this);
  119. var children = this.children;
  120. for(var i = 0; i < children.length; i++)
  121. {
  122. children[i].traverse(callback);
  123. }
  124. };
  125. /**
  126. * Attach a children to the object.
  127. *
  128. * @param object Object to attach to this object.
  129. */
  130. Object2D.prototype.add = function(object)
  131. {
  132. object.parent = this;
  133. this.children.push(object);
  134. };
  135. /**
  136. * Remove object from the children list.
  137. *
  138. * @param object Object to be removed.
  139. */
  140. Object2D.prototype.remove = function(object)
  141. {
  142. var index = this.children.indexOf(object);
  143. if(index !== -1)
  144. {
  145. this.children[index].parent = null;
  146. this.children.splice(index, 1)
  147. }
  148. };
  149. /**
  150. * Check if a point is inside of the object.
  151. */
  152. Object2D.prototype.isInside = function(point)
  153. {
  154. return false;
  155. };
  156. /**
  157. * Update the transformation matrix of the object.
  158. */
  159. Object2D.prototype.updateMatrix = function(context)
  160. {
  161. if(this.matrixNeedsUpdate)
  162. {
  163. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  164. this.globalMatrix.copy(this.matrix);
  165. if(this.parent !== null)
  166. {
  167. this.globalMatrix.premultiply(this.parent.globalMatrix);
  168. }
  169. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  170. //this.matrixNeedsUpdate = false;
  171. }
  172. };
  173. /**
  174. * Apply the transform to the rendering context.
  175. *
  176. * It is assumed that the viewport transform is pre-applied to the context.
  177. *
  178. * Can also be used for pre rendering logic.
  179. *
  180. * @param {CanvasContext} context Canvas 2d drawing context.
  181. * @param {Viewport} viewport Viewport applied to the canvas.
  182. */
  183. Object2D.prototype.transform = function(context, viewport)
  184. {
  185. this.globalMatrix.tranformContext(context);
  186. };
  187. /**
  188. * Draw the object into the canvas.
  189. *
  190. * Has to be implemented by underlying classes.
  191. *
  192. * @param {CanvasContext} context Canvas 2d drawing context.
  193. * @param {Viewport} viewport Viewport applied to the canvas.
  194. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  195. */
  196. Object2D.prototype.draw = function(context, viewport, canvas){};
  197. /**
  198. * Callback method called every time before the object is draw into the canvas.
  199. *
  200. * Can be used to run preparation code, move the object, etc.
  201. */
  202. Object2D.prototype.onUpdate = null;
  203. /**
  204. * Callback method called when the pointer enters the object.
  205. *
  206. * Receives (pointer, viewport) as arguments.
  207. */
  208. Object2D.prototype.onPointerEnter = null;
  209. /**
  210. * Callback method called when the was inside of the object and leaves the object.
  211. *
  212. * Receives (pointer, viewport) as arguments.
  213. */
  214. Object2D.prototype.onPointerLeave = null;
  215. /**
  216. * Callback method while the pointer is over (inside) of the object.
  217. *
  218. * Receives (pointer, viewport) as arguments.
  219. */
  220. Object2D.prototype.onPointerOver = null;
  221. /**
  222. * Callback method while the object is being dragged across the screen.
  223. *
  224. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  225. */
  226. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  227. {
  228. this.position.add(delta);
  229. };
  230. /**
  231. * Callback method called while the pointer button is pressed.
  232. *
  233. * Receives (pointer, viewport) as arguments.
  234. */
  235. Object2D.prototype.onButtonPressed = null;
  236. /**
  237. * Callback method called when the pointer button is pressed down (single time).
  238. */
  239. Object2D.prototype.onButtonDown = null;
  240. /**
  241. * Callback method called when the pointer button is released (single time).
  242. */
  243. Object2D.prototype.onButtonUp = null;
  244. export {Object2D};