You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rect.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Vec2 from "./Vec2.js";
  2. export default class Rect {
  3. constructor(pos = new Vec2(), size = new Vec2()) {
  4. this.pos = pos;
  5. this.size = size;
  6. }
  7. draw(ctx) {
  8. ctx.moveTo(this.pixelLeft, this.pixelTop);
  9. ctx.lineTo(this.pixelRight, this.pixelTop);
  10. ctx.lineTo(this.pixelRight, this.pixelBottom);
  11. ctx.lineTo(this.pixelLeft, this.pixelBottom);
  12. ctx.closePath();
  13. ctx.stroke();
  14. }
  15. intersects(other) {
  16. return (
  17. (this.left <= other.right && this.right >= other.left) &&
  18. (this.top <= other.bottom && this.bottom >= other.top));
  19. }
  20. contains(other) {
  21. return (
  22. (this.left <= other.left && this.right >= other.right) &&
  23. (this.top <= other.top && this.bottom >= other.bottom));
  24. }
  25. get top() {
  26. return this.pos.y;
  27. }
  28. set top(n) {
  29. this.pos.y = n;
  30. }
  31. get pixelTop() {
  32. return this.pos.pixelY;
  33. }
  34. set pixelTop(y) {
  35. this.pos.pixelY = y;
  36. }
  37. resizeTopTo(y) {
  38. let diff = y - this.top;
  39. this.top = y;
  40. this.size.y += diff;
  41. }
  42. pixelResizeTopTo(y) {
  43. let diff = y - this.pixelTop;
  44. this.pixelTop = y;
  45. this.size.pixelY += diff;
  46. }
  47. get bottom() {
  48. return this.pos.y + this.size.y;
  49. }
  50. set bottom(y) {
  51. this.pos.y = y - this.size.y;
  52. }
  53. get pixelBottom() {
  54. return this.pos.pixelY + this.size.pixelY;
  55. }
  56. set pixelBottom(y) {
  57. this.pos.pixelY = y + this.size.pixelY;
  58. }
  59. resizeBottomTo(y) {
  60. let diff = y - this.bottom;
  61. this.size.y += diff;
  62. }
  63. pixelResizeBottomTo(y) {
  64. let diff = y - this.pixelBottom;
  65. this.size.pixelY += diff;
  66. }
  67. get left() {
  68. return this.pos.x;
  69. }
  70. set left(x) {
  71. this.pos.x = x;
  72. }
  73. get pixelLeft() {
  74. return this.pos.pixelX;
  75. }
  76. set pixelLeft(x) {
  77. this.pos.pixelX = x;
  78. }
  79. resizeLeftTo(x) {
  80. let diff = x - this.left;
  81. this.left = x;
  82. this.size.x += diff;
  83. }
  84. pixelResizeLeftTo(x) {
  85. let diff = x - this.pixelLeft;
  86. this.pixelLeft = x;
  87. this.size.pixelX += diff;
  88. }
  89. get right() {
  90. return this.pos.x + this.size.x;
  91. }
  92. set right(n) {
  93. this.pos.x = n - this.size.x;
  94. }
  95. get pixelRight() {
  96. return this.pos.pixelX + this.size.pixelX;
  97. }
  98. set pixelRight(x) {
  99. this.pos.pixelX = x + this.size.pixelX;
  100. }
  101. resizeRightTo(x) {
  102. let diff = x - this.right;
  103. this.size.x += diff;
  104. }
  105. pixelResizeRightTo(x) {
  106. let diff = x - this.pixelRight;
  107. this.size.pixelX += diff;
  108. }
  109. }