BoxMask.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {Mask} from "./Mask.js";
  2. import {Vector2} from "../../math/Vector2.js";
  3. import {Box2} from "../../math/Box2.js";
  4. /**
  5. * Box mask can be used to clear a box mask region.
  6. *
  7. * It will limit the drawing region to this box.
  8. *
  9. * @class
  10. * @extends {Mask}
  11. */
  12. function BoxMask()
  13. {
  14. Mask.call(this);
  15. /**
  16. * Box object containing the size of the object.
  17. */
  18. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  19. /**
  20. * If inverted the mask considers the outside of the box instead of the inside.
  21. */
  22. this.invert = false;
  23. }
  24. BoxMask.prototype = Object.create(Mask.prototype);
  25. BoxMask.prototype.isInside = function(point)
  26. {
  27. return this.box.containsPoint(point);
  28. };
  29. BoxMask.prototype.clip = function(context, viewport, canvas)
  30. {
  31. context.beginPath();
  32. var width = this.box.max.x - this.box.min.x;
  33. if(this.invert)
  34. {
  35. context.rect(this.box.min.x - 1e4, -5e3, 1e4, 1e4);
  36. context.rect(this.box.max.x, -5e3, 1e4, 1e4);
  37. context.rect(this.box.min.x, this.box.min.y - 1e4, width, 1e4);
  38. context.rect(this.box.min.x, this.box.max.y, width, 1e4);
  39. }
  40. else
  41. {
  42. var height = this.box.max.y - this.box.min.y;
  43. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  44. }
  45. context.clip();
  46. };
  47. export {BoxMask};