UNPKG

tav-media

Version:

Cross platform media editing framework

352 lines (351 loc) 11.7 kB
import { tav } from "../tav"; /** * Rect holds four float coordinates describing the upper and lower bounds of a rectangle. Rect may * be created from outer bounds or from position, width, and height. Rect describes an area; if its * right is less than or equal to its left, or if its bottom is less than or equal to its top, it is * considered empty. */ export class Rect { /** * Make new constructed Rect set to (0, 0, 0, 0), or create from an existing Rect. * @param nativeRect Existing native rect object. */ constructor(nativeRect) { if (nativeRect) { this._nativeRect = nativeRect; } else { this._nativeRect = this.makeNativeRect(); } } /** * Returns constructed Rect set to (0, 0, 0, 0). */ static MakeEmpty() { return new Rect(tav.TAVRect.MakeEmpty()); } /** * Returns constructed Rect set to float values (0, 0, w, h). Does not validate input; w or h may * be negative. */ static MakeWH(width, height) { return new Rect(tav.TAVRect.MakeWH(width, height)); } /** * Returns constructed Rect set to (l, t, r, b). Does not sort input; Rect may result in left * greater than right, or top greater than bottom. */ static MakeLTRB(left, top, right, bottom) { return new Rect(tav.TAVRect.MakeLTRB(left, top, right, bottom)); } /** * Returns constructed Rect set to (x, y, x + w, y + h). Does not validate input; w or h may be * negative. */ static MakeXYWH(x, y, width, height) { return new Rect(tav.TAVRect.MakeXYWH(x, y, width, height)); } /** * Returns true if a intersects b. Returns false if either a or b is empty, or do not intersect. */ static Intersects(a, b) { return tav.TAVRect.Intersects(a._nativeRect, b._nativeRect); } /** * smaller x-axis bounds. */ get left() { return this._nativeRect.left; } /** * smaller y-axis bounds. */ get top() { return this._nativeRect.top; } /** * larger x-axis bounds. */ get right() { return this._nativeRect.right; } /** * larger y-axis bounds. */ get bottom() { return this._nativeRect.bottom; } build() { return this._nativeRect; } /** * Returns true if left is equal to or greater than right, or if top is equal to or greater * than bottom. Call sort() to reverse rectangles with negative width() or height(). */ isEmpty() { return this._nativeRect.isEmpty(); } /** * Returns true if left is equal to or less than right, or if top is equal to or less than * bottom. Call sort() to reverse rectangles with negative width() or height(). */ isSorted() { return this._nativeRect.isSorted(); } /** * Returns left edge of Rect, if sorted. Call isSorted() to see if Rect is valid. Call sort() to * reverse left and right if needed. */ x() { return this._nativeRect.x(); } /** * Returns top edge of Rect, if sorted. Call isEmpty() to see if Rect may be invalid, and sort() * to reverse top and bottom if needed. */ y() { return this._nativeRect.y(); } /** * Returns span on the x-axis. This does not check if Rect is sorted, or if result fits in 32-bit * float; result may be negative or infinity. */ width() { return this._nativeRect.width(); } /** * Returns span on the y-axis. This does not check if Rect is sorted, or if result fits in 32-bit * float; result may be negative or infinity. */ height() { return this._nativeRect.height(); } /** * Returns average of left edge and right edge. Result does not change if Rect is sorted. */ centerX() { return this._nativeRect.centerX(); } /** * Returns average of top edge and bottom edge. Result does not change if Rect is sorted. */ centerY() { return this._nativeRect.centerY(); } /** * Sets Rect to (0, 0, 0, 0). */ setEmpty() { this._nativeRect.setEmpty(); } /** * Sets Rect to (left, top, right, bottom). left and right are not sorted; left is not necessarily * less than right. top and bottom are not sorted; top is not necessarily less than bottom. */ setLTRB(left, top, right, bottom) { this._nativeRect.setLTRB(left, top, right, bottom); } /** * Sets to bounds of Point array with count entries. Returns false if count is zero or smaller, or * if Point array contains an infinity or NaN; in these cases sets Rect to (0, 0, 0, 0). * Result is either empty or sorted: left is less than or equal to right, and top is less than * or equal to bottom. */ setBounds(pts) { const nativePts = new tav.TAVPoints(); for (const pt of pts) { nativePts.push_back(pt); } this._nativeRect.setBounds(nativePts); } /** * Sets Rect to (x, y, x + width, y + height). Does not validate input; width or height may be * negative. */ setXYWH(x, y, width, height) { this._nativeRect.setXYWH(x, y, width, height); } /** * Sets Rect to (0, 0, width, height). Does not validate input, width or height may be negative. */ setWH(width, height) { this._nativeRect.setWH(width, height); } /** * Returns Rect offset by (dx, dy). * If dx is negative, Rect returned is moved to the left. * If dx is positive, Rect returned is moved to the right. * If dy is negative, Rect returned is moved upward. * If dy is positive, Rect returned is moved downward. */ makeOffset(dx, dy) { return new Rect(this._nativeRect.makeOffset(dx, dy)); } /** * Returns Rect, inset by (dx, dy). * If dx is negative, Rect returned is wider. * If dx is positive, Rect returned is narrower. * If dy is negative, Rect returned is taller. * If dy is positive, Rect returned is shorter. */ makeInset(dx, dy) { return new Rect(this._nativeRect.makeInset(dx, dy)); } /** * Returns Rect, outset by (dx, dy). * If dx is negative, Rect returned is narrower. * If dx is positive, Rect returned is wider. * If dy is negative, Rect returned is shorter. * If dy is positive, Rect returned is taller. */ makeOutset(dx, dy) { return new Rect(this._nativeRect.makeOutset(dx, dy)); } /** * Offsets Rect by adding dx to left, right; and by adding dy to top, bottom. * If dx is negative, moves Rect to the left. * If dx is positive, moves Rect to the right. * If dy is negative, moves Rect upward. * If dy is positive, moves Rect downward. */ offset(dx, dy) { this._nativeRect.offset(dx, dy); } /** * Offsets Rect by adding delta.x to left, right; and by adding delta.y to top, bottom. * If delta.x is negative, moves Rect to the left. * If delta.x is positive, moves Rect to the right. * If delta.y is negative, moves Rect upward. * If delta.y is positive, moves Rect downward. */ offsetPoint(delta) { this._nativeRect.offset_point(delta); } /** * Offsets Rect so that left equals newX, and top equals newY. width and height are unchanged. */ offsetTo(x, y) { this._nativeRect.offsetTo(x, y); } /** * Insets Rect by (dx, dy). * If dx is positive, makes Rect narrower. * If dx is negative, makes Rect wider. * If dy is positive, makes Rect shorter. * If dy is negative, makes Rect taller. */ inset(dx, dy) { this._nativeRect.inset(dx, dy); } /** * Outsets Rect by (dx, dy). * If dx is positive, makes Rect wider. * If dx is negative, makes Rect narrower. * If dy is positive, makes Rect taller. * If dy is negative, makes Rect shorter. */ outset(dx, dy) { this._nativeRect.outset(dx, dy); } /** * Scale the rectangle by scaleX and scaleY. */ scale(scaleX, scaleY) { this._nativeRect.scale(scaleX, scaleY); } /** * Constructs Rect to intersect from (left, top, right, bottom). Does not sort construction. * Returns true if Rect intersects construction, and sets Rect to intersection. * Returns false if Rect does not intersect construction, and leaves Rect unchanged. * Returns false if either construction or Rect is empty, leaving Rect unchanged. */ intersect(l, t, r, b) { return this._nativeRect.intersect(l, t, r, b); } /** * Returns true if a intersects b, and sets Rect to intersection. * Returns false if a does not intersect b, and leaves Rect unchanged. * Returns false if either a or b is empty, leaving Rect unchanged. */ intersectRect(a, b) { if (b) { return this._nativeRect.intersect_rect_rect(a._nativeRect, b._nativeRect); } else { return this._nativeRect.intersect_rect(a._nativeRect); } } /** * Returns true if Rect intersects r. Returns false if either r or Rect is empty, or do not * intersect. */ intersects(l, t, r, b) { return this._nativeRect.intersects(l, t, r, b); } /** * Returns true if Rect intersects r. Returns false if either r or Rect is empty, or do not * intersect. */ intersectsRect(r) { return this._nativeRect.intersects_rect(r._nativeRect); } /** * Constructs Rect to intersect from (left, top, right, bottom). Does not sort construction. * Sets Rect to the union of itself and the construction. Has no effect if construction is empty. * Otherwise, if Rect is empty, sets Rect to construction. */ join(l, t, r, b) { this._nativeRect.join(l, t, r, b); } /** * Sets Rect to the union of itself and r. Has no effect if r is empty. Otherwise, if Rect is * empty, sets Rect to r. */ joinRect(a, b) { this._nativeRect.join_rect(a._nativeRect, b._nativeRect); } /** * Returns true if: left <= x < right && top <= y < bottom. Returns false if Rect is empty. */ contains(x, y) { return this._nativeRect.contains(x, y); } /** * Returns true if Rect contains r. Returns false if Rect is empty or r is empty. Rect contains r * when Rect area completely includes r area. */ containsRect(r) { return this._nativeRect.contains_rect(r._nativeRect); } /** * Sets Rect by rounding of left, top, right and bottom. */ round() { this._nativeRect.round(); } /** * Sets Rect by discarding the fractional portion of left and top; and rounding up right and * bottom. */ roundOut() { this._nativeRect.roundOut(); } /** * Swaps left and right if left is greater than right; and swaps top and bottom if top is * greater than bottom. Result may be empty; and width() and height() will be zero or positive. */ sort() { this._nativeRect.sort(); } /** * Returns Rect with left and right swapped if left is greater than right, and with top and * bottom swapped if top is greater than bottom. Result may be empty, and width() and height() * will be zero or positive. */ makeSorted() { return new Rect(this._nativeRect.makeSorted()); } makeNativeRect() { return tav.TAVRect.MakeEmpty(); } }