Box.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. import {ColorStyle} from "./style/ColorStyle";
  5. import {Style} from "./style/Style";
  6. /**
  7. * Box object draw a rectangular object.
  8. *
  9. * Can be used as a base to implement other box objects, already implements collision for pointer events.
  10. *
  11. * @class
  12. * @extends {Object2D}
  13. */
  14. function Box()
  15. {
  16. Object2D.call(this);
  17. /**
  18. * Box object containing the size of the object.
  19. *
  20. * @type {Box2}
  21. */
  22. this.box = new Box2(new Vector2(-50, -50), new Vector2(50, 50));
  23. /**
  24. * Style of the object border line.
  25. *
  26. * If set null it is ignored.
  27. *
  28. * @type {Style}
  29. */
  30. this.strokeStyle = new ColorStyle("#000000");
  31. /**
  32. * Line width, only used if a valid strokeStyle is defined.
  33. *
  34. * @type {number}
  35. */
  36. this.lineWidth = 1;
  37. /**
  38. * Background color of the box.
  39. *
  40. * If set null it is ignored.
  41. *
  42. * @type {Style}
  43. */
  44. this.fillStyle = new ColorStyle("#FFFFFF");
  45. }
  46. Box.prototype = Object.create(Object2D.prototype);
  47. Box.prototype.constructor = Box;
  48. Box.prototype.type = "Box";
  49. Object2D.register(Box, "Box");
  50. Box.prototype.isInside = function(point)
  51. {
  52. return this.box.containsPoint(point);
  53. };
  54. Box.prototype.draw = function(context, viewport, canvas)
  55. {
  56. var width = this.box.max.x - this.box.min.x;
  57. var height = this.box.max.y - this.box.min.y;
  58. if(this.fillStyle !== null)
  59. {
  60. context.fillStyle = this.fillStyle.get(context);
  61. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  62. }
  63. if(this.strokeStyle !== null)
  64. {
  65. context.lineWidth = this.lineWidth;
  66. context.strokeStyle = this.strokeStyle.get(context);
  67. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  68. }
  69. };
  70. Box.prototype.serialize = function(recursive)
  71. {
  72. var data = Object2D.prototype.serialize.call(this, recursive);
  73. data.box = this.box.toArray();
  74. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  75. data.lineWidth = this.lineWidth;
  76. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  77. return data;
  78. };
  79. Box.prototype.parse = function(data, root)
  80. {
  81. Object2D.prototype.parse.call(this, data, root);
  82. this.box.fromArray(data.box);
  83. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  84. this.lineWidth = data.lineWidth;
  85. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  86. };
  87. export {Box};