Box.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. import {BezierCurve} from "./BezierCurve";
  5. import {Text} from "./Text";
  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. this.box = new Box2(new Vector2(-50, -50), new Vector2(50, 50));
  21. /**
  22. * Style of the object border line.
  23. *
  24. * If set null it is ignored.
  25. */
  26. this.strokeStyle = "#000000";
  27. /**
  28. * Line width, only used if a valid strokeStyle is defined.
  29. */
  30. this.lineWidth = 1;
  31. /**
  32. * Background color of the box.
  33. *
  34. * If set null it is ignored.
  35. */
  36. this.fillStyle = "#FFFFFF";
  37. }
  38. Box.prototype = Object.create(Object2D.prototype);
  39. Box.prototype.constructor = Box;
  40. Box.prototype.type = "Box";
  41. Box.prototype.onPointerEnter = function(pointer, viewport)
  42. {
  43. this.fillStyle = "#CCCCCC";
  44. };
  45. Box.prototype.onPointerLeave = function(pointer, viewport)
  46. {
  47. this.fillStyle = "#FFFFFF";
  48. };
  49. Box.prototype.isInside = function(point)
  50. {
  51. return this.box.containsPoint(point);
  52. };
  53. Box.prototype.draw = function(context, viewport, canvas)
  54. {
  55. var width = this.box.max.x - this.box.min.x;
  56. var height = this.box.max.y - this.box.min.y;
  57. if(this.fillStyle !== null)
  58. {
  59. context.fillStyle = this.fillStyle;
  60. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  61. }
  62. if(this.strokeStyle !== null)
  63. {
  64. context.lineWidth = this.lineWidth;
  65. context.strokeStyle = this.strokeStyle;
  66. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  67. }
  68. };
  69. Box.prototype.serialize = function(recursive)
  70. {
  71. var data = Object2D.prototype.serialize.call(this, recursive);
  72. data.box = this.box.toArray();
  73. data.strokeStyle = this.strokeStyle;
  74. data.lineWidth = this.lineWidth;
  75. data.fillStyle = this.fillStyle;
  76. return data;
  77. };
  78. Box.prototype.parse = function(data)
  79. {
  80. Object2D.prototype.parse.call(this, data);
  81. this.box.fromArray(data.box);
  82. this.strokeStyle = data.strokeStyle;
  83. this.lineWidth = data.lineWidth;
  84. this.fillStyle = data.fillStyle;
  85. };
  86. export {Box};