Box.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. import {Helpers} from "../utils/Helpers.js";
  5. import {Circle} from "./Circle.js";
  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, -35), new Vector2(50, 35));
  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.onPointerEnter = function(pointer, viewport)
  40. {
  41. this.fillStyle = "#CCCCCC";
  42. };
  43. Box.prototype.onPointerLeave = function(pointer, viewport)
  44. {
  45. this.fillStyle = "#FFFFFF";
  46. };
  47. Box.prototype.isInside = function(point)
  48. {
  49. return this.box.containsPoint(point);
  50. };
  51. Box.prototype.draw = function(context, viewport, canvas)
  52. {
  53. var width = this.box.max.x - this.box.min.x;
  54. var height = this.box.max.y - this.box.min.y;
  55. if(this.fillStyle !== null)
  56. {
  57. context.fillStyle = this.fillStyle;
  58. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  59. }
  60. if(this.strokeStyle !== null)
  61. {
  62. context.lineWidth = this.lineWidth;
  63. context.strokeStyle = this.strokeStyle;
  64. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  65. }
  66. };
  67. export {Box};