Box.js 1.7 KB

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