123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import Vec2 from "./Vec2.js";
-
- export default class Rect {
- constructor(pos = new Vec2(), size = new Vec2()) {
- this.pos = pos;
- this.size = size;
- }
-
- draw(ctx) {
- ctx.moveTo(this.pixelLeft, this.pixelTop);
- ctx.lineTo(this.pixelRight, this.pixelTop);
- ctx.lineTo(this.pixelRight, this.pixelBottom);
- ctx.lineTo(this.pixelLeft, this.pixelBottom);
- ctx.closePath();
- ctx.stroke();
- }
-
- intersects(other) {
- return (
- (this.left <= other.right && this.right >= other.left) &&
- (this.top <= other.bottom && this.bottom >= other.top));
- }
-
- intersectSide(other) {
- if (this.midY < other.top)
- return "top";
- else if (this.midY > other.bottom)
- return "bottom";
- else if (this.midX < other.left)
- return "left";
- else
- return "right";
- }
-
- contains(other) {
- return (
- (this.left <= other.left && this.right >= other.right) &&
- (this.top <= other.top && this.bottom >= other.bottom));
- }
-
- get midX() {
- return this.pos.x + (this.size.x / 2);
- }
- get midY() {
- return this.pos.y + (this.size.y / 2);
- }
-
- get top() {
- return this.pos.y;
- }
- set top(n) {
- this.pos.y = n;
- }
- get pixelTop() {
- return this.pos.pixelY;
- }
- set pixelTop(y) {
- this.pos.pixelY = y;
- }
- resizeTopTo(y) {
- let diff = y - this.top;
- this.top = y;
- this.size.y += diff;
- }
- pixelResizeTopTo(y) {
- let diff = y - this.pixelTop;
- this.pixelTop = y;
- this.size.pixelY += diff;
- }
-
- get bottom() {
- return this.pos.y + this.size.y;
- }
- set bottom(y) {
- this.pos.y = y - this.size.y;
- }
- get pixelBottom() {
- return this.pos.pixelY + this.size.pixelY;
- }
- set pixelBottom(y) {
- this.pos.pixelY = y + this.size.pixelY;
- }
- resizeBottomTo(y) {
- let diff = y - this.bottom;
- this.size.y += diff;
- }
- pixelResizeBottomTo(y) {
- let diff = y - this.pixelBottom;
- this.size.pixelY += diff;
- }
-
- get left() {
- return this.pos.x;
- }
- set left(x) {
- this.pos.x = x;
- }
- get pixelLeft() {
- return this.pos.pixelX;
- }
- set pixelLeft(x) {
- this.pos.pixelX = x;
- }
- resizeLeftTo(x) {
- let diff = x - this.left;
- this.left = x;
- this.size.x += diff;
- }
- pixelResizeLeftTo(x) {
- let diff = x - this.pixelLeft;
- this.pixelLeft = x;
- this.size.pixelX += diff;
- }
-
- get right() {
- return this.pos.x + this.size.x;
- }
- set right(n) {
- this.pos.x = n - this.size.x;
- }
- get pixelRight() {
- return this.pos.pixelX + this.size.pixelX;
- }
- set pixelRight(x) {
- this.pos.pixelX = x + this.size.pixelX;
- }
- resizeRightTo(x) {
- let diff = x - this.right;
- this.size.x += diff;
- }
- pixelResizeRightTo(x) {
- let diff = x - this.pixelRight;
- this.size.pixelX += diff;
- }
- }
|