Box.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. /**
  5. * Box object draw a rectangular object.
  6. *
  7. * Can be used as a base to implement other box objects, already implements collision for pointer events.
  8. *
  9. * @class
  10. * @extends {Object2D}
  11. */
  12. function Box()
  13. {
  14. Object2D.call(this);
  15. /**
  16. * Box object containing the size of the object.
  17. */
  18. this.box = new Box2(new Vector2(-50, -50), new Vector2(50, 50));
  19. /**
  20. * Style of the object border line.
  21. *
  22. * If set null it is ignored.
  23. */
  24. this.strokeStyle = "#000000";
  25. /**
  26. * Line width, only used if a valid strokeStyle is defined.
  27. */
  28. this.lineWidth = 1;
  29. /**
  30. * Background color of the box.
  31. *
  32. * If set null it is ignored.
  33. */
  34. this.fillStyle = "#FFFFFF";
  35. }
  36. Box.prototype = Object.create(Object2D.prototype);
  37. Box.prototype.onPointerEnter = function(pointer, viewport)
  38. {
  39. this.fillStyle = "#CCCCCC";
  40. };
  41. Box.prototype.onPointerLeave = function(pointer, viewport)
  42. {
  43. this.fillStyle = "#FFFFFF";
  44. };
  45. Box.prototype.isInside = function(point)
  46. {
  47. return this.box.containsPoint(point);
  48. };
  49. Box.prototype.draw = function(context, viewport, canvas)
  50. {
  51. var width = this.box.max.x - this.box.min.x;
  52. var height = this.box.max.y - this.box.min.y;
  53. if(this.fillStyle !== null)
  54. {
  55. context.fillStyle = this.fillStyle;
  56. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  57. }
  58. if(this.strokeStyle !== null)
  59. {
  60. context.lineWidth = this.lineWidth;
  61. context.strokeStyle = this.strokeStyle;
  62. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  63. }
  64. };
  65. export {Box};