Object2D.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import {Vector2} from "./math/Vector2.js";
  2. import {Matrix} from "./math/Matrix.js";
  3. import {UUID} from "./math/UUID.js";
  4. /**
  5. * Base object class, implements all the object positioning and scaling features.
  6. *
  7. * Stores all the base properties shared between all objects as the position, transformation properties, children etc.
  8. *
  9. * Object2D object can be used as a group to the other objects drawn.
  10. *
  11. * @class
  12. */
  13. function Object2D()
  14. {
  15. /**
  16. * UUID of the object.
  17. *
  18. * @type {string}
  19. */
  20. this.uuid = UUID.generate();
  21. /**
  22. * List of children objects attached to the object.
  23. *
  24. * @type {Object2D[]}
  25. */
  26. this.children = [];
  27. /**
  28. * Parent object, the object position is affected by its parent position.
  29. *
  30. * @type {Object2D}
  31. */
  32. this.parent = null;
  33. /**
  34. * Depth level in the object tree, objects with higher depth are drawn on top.
  35. *
  36. * The layer value is considered first.
  37. *
  38. * @type {number}
  39. */
  40. this.level = 0;
  41. /**
  42. * Position of the object.
  43. *
  44. * The world position of the object is affected by its parent transform.
  45. *
  46. * @type {Vector2}
  47. */
  48. this.position = new Vector2(0, 0);
  49. /**
  50. * Origin of the object used as point of rotation.
  51. *
  52. * @type {Vector2}
  53. */
  54. this.origin = new Vector2(0, 0);
  55. /**
  56. * Scale of the object.
  57. *
  58. * The world scale of the object is affected by the parent transform.
  59. *
  60. * @type {Vector2}
  61. */
  62. this.scale = new Vector2(1, 1);
  63. /**
  64. * Rotation of the object relative to its center.
  65. *
  66. * The world rotation of the object is affected by the parent transform.
  67. *
  68. * @type {number}
  69. */
  70. this.rotation = 0.0;
  71. /**
  72. * Indicates if the object is visible.
  73. *
  74. * @type {boolean}
  75. */
  76. this.visible = true;
  77. /**
  78. * Layer of this object, objects are sorted by layer value.
  79. *
  80. * Lower layer value is draw first, higher layer value is drawn on top.
  81. *
  82. * @type {number}
  83. */
  84. this.layer = 0;
  85. /**
  86. * Local transformation matrix applied to the object.
  87. *
  88. * @type {Matrix}
  89. */
  90. this.matrix = new Matrix();
  91. /**
  92. * Global transformation matrix multiplied by the parent matrix.
  93. *
  94. * Used to transform the object before projecting into screen coordinates.
  95. *
  96. * @type {Matrix}
  97. */
  98. this.globalMatrix = new Matrix();
  99. /**
  100. * Inverse of the global matrix.
  101. *
  102. * Used to convert pointer input points into object coordinates.
  103. *
  104. * @type {Matrix}
  105. */
  106. this.inverseGlobalMatrix = new Matrix();
  107. /**
  108. * Mask objects being applied to this object. Used to mask/subtract portions of this object when rendering.
  109. *
  110. * Multiple masks can be used simultaneously. Same mask might be reused for multiple objects.
  111. *
  112. * @type {Mask[]}
  113. */
  114. this.masks = [];
  115. /**
  116. * Indicates if the transform matrix should be automatically updated every frame.
  117. *
  118. * Set this false for better performance. But if you do so dont forget to set matrixNeedsUpdate every time that a transform attribute is changed.
  119. *
  120. * @type {boolean}
  121. */
  122. this.matrixAutoUpdate = true;
  123. /**
  124. * Indicates if the matrix needs to be updated, should be set true after changes to the object position, scale or rotation.
  125. *
  126. * The matrix is updated before rendering the object, after the matrix is updated this attribute is automatically reset to false.
  127. *
  128. * @type {boolean}
  129. */
  130. this.matrixNeedsUpdate = true;
  131. /**
  132. * Draggable controls if its possible to drag the object around. Set this true to enable dragging events on this object.
  133. *
  134. * The onPointerDrag callback is used to update the state of the object while being dragged, by default it just updates the object position.
  135. *
  136. * @type {boolean}
  137. */
  138. this.draggable = false;
  139. /**
  140. * Indicates if this object uses pointer events.
  141. *
  142. * Can be set false to skip the pointer interaction events, better performance if pointer events are not required.
  143. *
  144. * @type {boolean}
  145. */
  146. this.pointerEvents = true;
  147. /**
  148. * Flag to indicate whether this object ignores the viewport transformation.
  149. *
  150. * @type {boolean}
  151. */
  152. this.ignoreViewport = false;
  153. /**
  154. * Flag to indicate if the context of canvas should be saved before render.
  155. *
  156. * @type {boolean}
  157. */
  158. this.saveContextState = true;
  159. /**
  160. * Flag to indicate if the context of canvas should be restored after render.
  161. *
  162. * @type {boolean}
  163. */
  164. this.restoreContextState = true;
  165. /**
  166. * Flag indicating if the pointer is inside of the element.
  167. *
  168. * Used to control object event.
  169. *
  170. * @type {boolean}
  171. */
  172. this.pointerInside = false;
  173. /**
  174. * Flag to indicate if the object is currently being dragged.
  175. *
  176. * @type {boolean}
  177. */
  178. this.beingDragged = false;
  179. }
  180. /**
  181. * Traverse the object tree and run a function for all objects.
  182. *
  183. * @param {Function} callback Callback function that receives the object as parameter.
  184. */
  185. Object2D.prototype.traverse = function(callback)
  186. {
  187. callback(this);
  188. var children = this.children;
  189. for(var i = 0; i < children.length; i++)
  190. {
  191. children[i].traverse(callback);
  192. }
  193. };
  194. /**
  195. * Get a object from its children list by its UUID.
  196. *
  197. * @param {string} uuid UUID of the object to get.
  198. * @return {Object2D} The object that has the UUID specified, null if the object was not found.
  199. */
  200. Object2D.prototype.getChildByUUID = function(uuid)
  201. {
  202. var object = null;
  203. this.traverse(function(child)
  204. {
  205. if(child.uuid === uuid)
  206. {
  207. object = child;
  208. }
  209. });
  210. return object;
  211. };
  212. /**
  213. * Attach a children to this object.
  214. *
  215. * The object is set as children of this object and the transformations applied to this object are traversed to its children.
  216. *
  217. * @param {Object2D} object Object to attach to this object.
  218. */
  219. Object2D.prototype.add = function(object)
  220. {
  221. object.parent = this;
  222. object.level = this.level + 1;
  223. object.traverse(function(child)
  224. {
  225. if(child.onAdd !== null)
  226. {
  227. child.onAdd(this);
  228. }
  229. });
  230. this.children.push(object);
  231. };
  232. /**
  233. * Remove object from the children list.
  234. *
  235. * @param {Object2D} children Object to be removed.
  236. */
  237. Object2D.prototype.remove = function(children)
  238. {
  239. var index = this.children.indexOf(children);
  240. if(index !== -1)
  241. {
  242. var object = this.children[index];
  243. object.parent = null;
  244. object.level = 0;
  245. object.traverse(function(child)
  246. {
  247. if(child.onRemove !== null)
  248. {
  249. child.onRemove(this);
  250. }
  251. });
  252. this.children.splice(index, 1)
  253. }
  254. };
  255. /**
  256. * Check if a point is inside of the object.
  257. *
  258. * Used to update the point events attached to the object.
  259. *
  260. * @return {boolean} True if the point is inside of the object.
  261. */
  262. Object2D.prototype.isInside = function(point)
  263. {
  264. return false;
  265. };
  266. /**
  267. * Update the transformation matrix of the object.
  268. *
  269. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  270. */
  271. Object2D.prototype.updateMatrix = function(context)
  272. {
  273. if(this.matrixAutoUpdate || this.matrixNeedsUpdate)
  274. {
  275. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  276. this.globalMatrix.copy(this.matrix);
  277. if(this.parent !== null)
  278. {
  279. this.globalMatrix.premultiply(this.parent.globalMatrix);
  280. }
  281. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  282. this.matrixNeedsUpdate = false;
  283. }
  284. };
  285. /**
  286. * Apply the transform to the rendering context, it is assumed that the viewport transform is pre-applied to the context.
  287. *
  288. * This is called before style() and draw(). It can also be used for some pre-rendering logic.
  289. *
  290. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  291. * @param {Viewport} viewport Viewport applied to the canvas.
  292. */
  293. Object2D.prototype.transform = function(context, viewport)
  294. {
  295. this.globalMatrix.tranformContext(context);
  296. };
  297. /**
  298. * Style is called right before draw() it should not draw any content into the canvas, all context styling should be applied here (colors, fonts, etc).
  299. *
  300. * The draw() and style() methods can be useful for objects that share the same styling attributes but are drawing differently.
  301. *
  302. * Should be implemented by underlying classes.
  303. *
  304. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  305. * @param {Viewport} viewport Viewport used to view the canvas content.
  306. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  307. */
  308. Object2D.prototype.style = null; // function(context, viewport, canvas){};
  309. /**
  310. * Draw the object into the canvas, this is called transform() and style(), should be where the content is actually drawn into the canvas.
  311. *
  312. * Should be implemented by underlying classes.
  313. *
  314. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  315. * @param {Viewport} viewport Viewport used to view the canvas content.
  316. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  317. */
  318. Object2D.prototype.draw = null; // function(context, viewport, canvas){};
  319. /**
  320. * Callback method while the object is being dragged across the screen.
  321. *
  322. * By default is adds the delta value to the object position (making it follow the mouse movement).
  323. *
  324. * Delta is the movement of the pointer already translated into local object coordinates.
  325. *
  326. * Receives (pointer, viewport, delta) as arguments.
  327. *
  328. * @param {Pointer} pointer Pointer object that receives the user input.
  329. * @param {Viewport} viewport Viewport where the object is drawn.
  330. * @param {Vector2} delta Pointer movement in world space.
  331. */
  332. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  333. {
  334. this.position.add(delta);
  335. };
  336. /**
  337. * Callback method called when the pointer drag ends after the button has been released.
  338. *
  339. * @param {Pointer} pointer Pointer object that receives the user input.
  340. * @param {Viewport} viewport Viewport where the object is drawn.
  341. */
  342. Object2D.prototype.onPointerDragEnd = null;
  343. /**
  344. * Method called when the object its added to a parent.
  345. *
  346. * @param {Object2D} parent Parent object were it was added.
  347. */
  348. Object2D.prototype.onAdd = null;
  349. /**
  350. * Method called when the object gets removed from its parent
  351. *
  352. * @param {Object2D} parent Parent object from were the object is being removed.
  353. */
  354. Object2D.prototype.onRemove = null;
  355. /**
  356. * Callback method called every time before the object is draw into the canvas.
  357. *
  358. * Can be used to run preparation code, move the object, etc.
  359. */
  360. Object2D.prototype.onUpdate = null;
  361. /**
  362. * Callback method called when the pointer enters the object.
  363. *
  364. * Receives (pointer, viewport) as arguments.
  365. *
  366. * @param {Pointer} pointer Pointer object that receives the user input.
  367. * @param {Viewport} viewport Viewport where the object is drawn.
  368. */
  369. Object2D.prototype.onPointerEnter = null;
  370. /**
  371. * Callback method called when the was inside of the object and leaves the object.
  372. *
  373. * Receives (pointer, viewport) as arguments.
  374. *
  375. * @param {Pointer} pointer Pointer object that receives the user input.
  376. * @param {Viewport} viewport Viewport where the object is drawn.
  377. */
  378. Object2D.prototype.onPointerLeave = null;
  379. /**
  380. * Callback method while the pointer is over (inside) of the object.
  381. *
  382. * Receives (pointer, viewport) as arguments.
  383. *
  384. * @param {Pointer} pointer Pointer object that receives the user input.
  385. * @param {Viewport} viewport Viewport where the object is drawn.
  386. */
  387. Object2D.prototype.onPointerOver = null;
  388. /**
  389. * Callback method called while the pointer button is pressed.
  390. *
  391. * Receives (pointer, viewport) as arguments.
  392. *
  393. * @param {Pointer} pointer Pointer object that receives the user input.
  394. * @param {Viewport} viewport Viewport where the object is drawn.
  395. */
  396. Object2D.prototype.onButtonPressed = null;
  397. /**
  398. * Callback method called while the pointer button is double clicked.
  399. *
  400. * Receives (pointer, viewport) as arguments.
  401. *
  402. * @param {Pointer} pointer Pointer object that receives the user input.
  403. * @param {Viewport} viewport Viewport where the object is drawn.
  404. */
  405. Object2D.prototype.onDoubleClick = null;
  406. /**
  407. * Callback method called when the pointer button is pressed down (single time).
  408. *
  409. * Receives (pointer, viewport) as arguments.
  410. *
  411. * @param {Pointer} pointer Pointer object that receives the user input.
  412. * @param {Viewport} viewport Viewport where the object is drawn.
  413. */
  414. Object2D.prototype.onButtonDown = null;
  415. /**
  416. * Callback method called when the pointer button is released (single time).
  417. *
  418. * Receives (pointer, viewport) as arguments.
  419. *
  420. * @param {Pointer} pointer Pointer object that receives the user input.
  421. * @param {Viewport} viewport Viewport where the object is drawn.
  422. */
  423. Object2D.prototype.onButtonUp = null;
  424. export {Object2D};