Object2D.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * If true the matrix is updated before rendering the object.
  68. */
  69. this.matrixNeedsUpdate = true;
  70. /**
  71. * Indicates if its possible to drag the object around.
  72. *
  73. * If true the onPointerDrag callback is used to update the state of the object.
  74. */
  75. this.draggable = false;
  76. /**
  77. * Indicates if this object uses pointer events.
  78. *
  79. * Can be set false to skip the pointer interaction events.
  80. */
  81. this.pointerEvents = true;
  82. /**
  83. * Flag to indicate wheter this objet ignores the viewport transformation.
  84. */
  85. this.ignoreViewport = false;
  86. /**
  87. * Flag to indicate if the context of canvas should be saved before render.
  88. */
  89. this.saveContextState = true;
  90. /**
  91. * Flag to indicate if the context of canvas should be restored after render.
  92. */
  93. this.restoreContextState = true;
  94. /**
  95. * Flag indicating if the pointer is inside of the element.
  96. *
  97. * Used to control object event.
  98. */
  99. this.pointerInside = false;
  100. /**
  101. * Flag to indicate if the object is currently being dragged.
  102. */
  103. this.beingDragged = false;
  104. }
  105. /**
  106. * Traverse the object tree and run a function for all objects.
  107. *
  108. * @param callback Callback function that receives the object as parameter.
  109. */
  110. Object2D.prototype.traverse = function(callback)
  111. {
  112. callback(this);
  113. var children = this.children;
  114. for(var i = 0; i < children.length; i++)
  115. {
  116. children[i].traverse(callback);
  117. }
  118. };
  119. /**
  120. * Attach a children to the object.
  121. *
  122. * @param object Object to attach to this object.
  123. */
  124. Object2D.prototype.add = function(object)
  125. {
  126. object.parent = this;
  127. this.children.push(object);
  128. };
  129. /**
  130. * Remove object from the children list.
  131. *
  132. * @param object Object to be removed.
  133. */
  134. Object2D.prototype.remove = function(object)
  135. {
  136. var index = this.children.indexOf(object);
  137. if(index !== -1)
  138. {
  139. this.children[index].parent = null;
  140. this.children.splice(index, 1)
  141. }
  142. };
  143. /**
  144. * Check if a point is inside of the object.
  145. */
  146. Object2D.prototype.isInside = function(point)
  147. {
  148. return false;
  149. };
  150. /**
  151. * Update the transformation matrix of the object.
  152. */
  153. Object2D.prototype.updateMatrix = function(context)
  154. {
  155. if(this.matrixNeedsUpdate)
  156. {
  157. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  158. this.globalMatrix.copy(this.matrix);
  159. if(this.parent !== null)
  160. {
  161. this.globalMatrix.premultiply(this.parent.globalMatrix);
  162. }
  163. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  164. //this.matrixNeedsUpdate = false;
  165. }
  166. };
  167. /**
  168. * Apply the transform to the rendering context.
  169. *
  170. * It is assumed that the viewport transform is pre-applied to the context.
  171. *
  172. * Can also be used for pre rendering logic.
  173. *
  174. * @param {CanvasContext} context Canvas 2d drawing context.
  175. * @param {Viewport} viewport Viewport applied to the canvas.
  176. */
  177. Object2D.prototype.transform = function(context, viewport)
  178. {
  179. this.globalMatrix.tranformContext(context);
  180. };
  181. /**
  182. * Draw the object into the canvas.
  183. *
  184. * Has to be implemented by underlying classes.
  185. *
  186. * @param {CanvasContext} context Canvas 2d drawing context.
  187. * @param {Viewport} viewport Viewport applied to the canvas.
  188. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  189. */
  190. Object2D.prototype.draw = function(context, viewport, canvas){};
  191. /**
  192. * Callback method called every time before the object is draw into the canvas.
  193. *
  194. * Can be used to run preparation code, move the object, etc.
  195. */
  196. Object2D.prototype.onUpdate = null;
  197. /**
  198. * Callback method called when the pointer enters the object.
  199. *
  200. * Receives (pointer, viewport) as arguments.
  201. */
  202. Object2D.prototype.onPointerEnter = null;
  203. /**
  204. * Callback method called when the was inside of the object and leaves the object.
  205. *
  206. * Receives (pointer, viewport) as arguments.
  207. */
  208. Object2D.prototype.onPointerLeave = null;
  209. /**
  210. * Callback method while the pointer is over (inside) of the object.
  211. *
  212. * Receives (pointer, viewport) as arguments.
  213. */
  214. Object2D.prototype.onPointerOver = null;
  215. /**
  216. * Callback method while the object is being dragged across the screen.
  217. *
  218. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  219. */
  220. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  221. {
  222. this.position.add(delta);
  223. };
  224. /**
  225. * Callback method called while the pointer button is pressed.
  226. *
  227. * Receives (pointer, viewport) as arguments.
  228. */
  229. Object2D.prototype.onButtonPressed = null;
  230. /**
  231. * Callback method called when the pointer button is pressed down (single time).
  232. */
  233. Object2D.prototype.onButtonDown = null;
  234. /**
  235. * Callback method called when the pointer button is released (single time).
  236. */
  237. Object2D.prototype.onButtonUp = null;
  238. export {Object2D};