BoxMask.js 1.4 KB

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