Applies an operation over each field of a rectangle.
Applies an joint operation field-wise on two rectangles, returning a single rectangle. This is used to support operations like summing two rectangles.
Uses op
with param
to width and height.
Returns the area of rect
Returns a point on cardinal direction, or 'center' for the middle.
Returns the center of a rectangle as a Point.
If the rectangle lacks a position and origin
parameter is not provided, 0,0 is used instead.
Returns the four corners of a rectangle as an array of Points.
Return the distance of pt
to the center of rect
.
Returns the distance from the perimeter of rect
to pt
.
If the point is within the rectangle, 0 is returned.
Divides positioned rect
by width/height. Useful for normalising a value.
x & y value of second parameter are ignored
Returns a function that divides numbers or points by the largest dimension of rect
.
Divides all components of rect
by amount
.
This includes x,y if present.
Returns four lines based on each corner. Lines are given in order: top, right, bottom, left
Returns a copy of rect
with rect
resized so it also encompasses points
.
If provided point(s) are within bounds of rect
, a copy of rect
is returned.
Initialises a rectangle based on its center, a width and height
Initialise a rectangle based on the width and height of a HTML element.
Returns a rectangle from a series of numbers: x, y, width, height OR width, height
Creates a rectangle from its top-left coordinate, a width and height.
Returns a point on the edge of rectangle
const r1 = {x: 10, y: 10, width: 100, height: 50};
Rects.getEdgeX(r1, `right`); // Yields: 110
Rects.getEdgeX(r1, `bottom`);
Returns a point on the edge of rectangle
Returns a positioned rect or if it's not possible, throws an error.
Accepts:
Throws an error if rectangle is missing fields or they are not valid.
Throws an error if the dimensions of the rectangle are undefined, NaN or negative.
Throws an error if rect
is does not have a position, or
is an invalid rectangle
Returns true if point is within or on boundary of rect
.
Returns true if rect
has width and height values of 0.
Use Rects.Empty or Rects.EmptyPositioned to generate an empty rectangle.
Returns true if two rectangles have identical values. Both rectangles must be positioned or not.
Returns true if the width & height of the two rectangles is the same.
Returns true if a
or b
overlap, are equal, or a
contains b
.
A rectangle can be checked for intersections with another RectPositioned, CirclePositioned or Point.
Returns true if rect
is a placeholder, with both width and height values of NaN.
Use Rects.Placeholder or Rects.PlaceholderPositioned to generate a placeholder.
Returns true if rect
has position (x,y) fields.
Returns true if rect
has width and height fields.
Returns true if rect
is a positioned rectangle
Having width, height, x and y properties.
Returns the length of each side of the rectangle (top, right, bottom, left)
Returns a rectangle based on provided four corners.
Multiplies positioned rect
by width/height. Useful for denormalising a value.
x/y value of second parameter are ignored.
// Normalised rectangle
const r = { x:0.5, y:0.5, width: 0.5, height: 0.5
Multiplies only the width/height of rect
, leaving x
and y
as they are.
Multiplies all components of rect
by amount
.
This includes x,y if present.
If p
is inside of rect
, a copy of p
is returned.
If p
is outside of rect
, a point is returned closest to p
on the edge
of the rectangle.
Returns the perimeter of rect
(ie. sum of all edges)
*
Returns a random positioned Rect on a 0..1 scale.
Returns a random point within a rectangle.
Subtracts width/height from a
.
Subtracts A-B. Applies to x, y, width & height
Subtracts a width & height from a
. Leaves x & y as-is.
Sums width/height of b
with a
(ie: a + b), returning result.
Sums x,y,width,height of a+b.
Converts a rectangle to an array of numbers. See fromNumbers for the opposite conversion.
An operation between two fields of a rectangle. Used in the context of applyMerge
Rectangle as array: [width, height]
Positioned rectangle as array: [x, y, width, height]
Applies
op
withparam
torect
's width and height.