Object2D.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. /**
  3. * Base 2D object class, implements all the object positioning and scalling features.
  4. *
  5. * @class
  6. */
  7. function Object2D()
  8. {
  9. /**
  10. * UUID of the object.
  11. */
  12. this.uuid = UUID.generate();
  13. /**
  14. * List of children objects attached to the object.
  15. */
  16. this.children = [];
  17. /**
  18. * Parent object, the object position is affected by its parent position.
  19. */
  20. this.parent = null;
  21. /**
  22. * Position of the object.
  23. */
  24. this.position = new Vector2(0, 0);
  25. /**
  26. * Scale of the object.
  27. */
  28. this.scale = new Vector2(1, 1);
  29. /**
  30. * Rotation of the object relative to its center.
  31. */
  32. this.rotation = 0.0;
  33. /**
  34. * Layer of this object, objects are sorted by layer value.
  35. *
  36. * Lower layer value is draw first.
  37. */
  38. this.layer = 0;
  39. /**
  40. * Local transformation matrix applied to the object.
  41. */
  42. this.matrix = new Matrix3();
  43. /**
  44. * Global transformation matrix used to project the object to screen space.
  45. */
  46. this.globalMatrix = new Matrix3();
  47. }
  48. /**
  49. * Attach a children to the object.
  50. */
  51. Object2D.prototype.add = function(object)
  52. {
  53. object.parent = this;
  54. this.children.push(object);
  55. };
  56. /**
  57. * Remove object from the children list.
  58. */
  59. Object2D.prototype.remove = function(object)
  60. {
  61. var index = this.children.indexOf(object);
  62. if(index !== -1)
  63. {
  64. this.children[index].parent = null;
  65. this.children.splice(index, 1)
  66. }
  67. };
  68. /**
  69. * Draw the object into the canvas.
  70. */
  71. Object2D.prototype.draw = function(context)
  72. {
  73. };