Example
Create and update a rect.
const rect = new NutrientViewer.Geometry.Rect({
left: 10,
top: 20,
width: 30,
height: 40
});
rect = rect.set("left", 20);
rect.left; // => 20
Default Value
{ left: 0, top: 0, width: 0, height: 0 }Hierarchy
Constructors
Parameters
options{ height?: number; left?: number; top?: number; width?: number }Optional
Properties
height
The height of the rect. This is equivalent to height of a NutrientViewer.Geometry.Size.
Default Value
0left
The left distance of the rect. This is equivalent to x of a
NutrientViewer.Geometry.Point.
Default Value
0top
The top distance of the rect. This is equivalent to y of a
NutrientViewer.Geometry.Point.
Default Value
0width
The width of the rect. This is equivalent to width of a NutrientViewer.Geometry.Size.
Default Value
0Accessors
- get bottom(): number
Computes the bottom point in the rect by adding
topandheight.Returns
number
- get right(): number
Computes the right point in the rect by adding
leftandwidth.Returns
number
Methods
Applies a transformation to the rect. We will translate [top, left] like a 2D vector but only apply the scaling to the dimension [width, height]
Parameters
matrixTransformationMatrixReturns
Returns the record's
[key, value]pairs, in the order its fields were defined.Returns
IterableIterator<[string, unknown]>An iterator over the record's fields.
- expandToIncludePoints(
...points: NutrientViewer.Geometry.Point<{ x: number; y: number }>[],
): NutrientViewer.Geometry.RectExpand the rect to include the list of points.
Parameters
...pointsNutrientViewer.Geometry.Point<{ x: number; y: number }>[]Returns
A new
Rect.Example
const rect = NutrientViewer.Geometry.Rect({
left: 10,
top: 10,
width: 10,
height: 10
})
const newRect = rect.expandToIncludePoints(new NutrientViewer.Geometry.Point({ x: 30, y: 30 }));
// => Rect {left: 10, top: 10, width: 30, height: 30}
Returns the NutrientViewer.Geometry.Point that is the center of this rect.
Returns
A point that is on the center of this rect.
Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.getCenter(); // => Point {left: 15, top: 15}
Returns the NutrientViewer.Geometry.Point that is the upper-left corner of this rect.
Returns
A point that is on the upper-left corner of this rect.
Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.getLocation(); // => Point {left: 10, top: 10}
Returns the NutrientViewer.Geometry.Size of the rect.
Returns
The size of the rect.
Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.getSize(); // => Size {width: 10, height: 10}
Grows the rect by
growthon every side but keeps the center of the Rect at the same position.Parameters
growthnumberThe growth factor. It will be applied on every side, so the new
widthandheightwill increase by two times this factor.Returns
A new
Rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.grow(5); // => Rect {left: 5, top: 5, width: 20, height: 20}
Test if a point is within the rect. This can be used for hit testing.
Parameters
pointNutrientViewer.Geometry.PointThe point that should be tested.
Returns
booleantrueif the point is inside,falseotherwise.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.isPointInside(new NutrientViewer.Geometry.Point({ x: 15, y: 15 })); // => true
rect.isPointInside(new NutrientViewer.Geometry.Point({ x: 25, y: 25 })); // => false
Test if a rect is completely inside this rect.
Parameters
otherRectNutrientViewer.Geometry.RectThe rect that should be tested.
Returns
booleantrueif the rect is inside,falseotherwise.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
const insideRect = new NutrientViewer.Geometry.Rect({ left: 12, top: 12, width: 5, height: 5 });
const overlappingRect = new NutrientViewer.Geometry.Rect({ left: 5, top: 5, width: 10, height: 10 });
const outsideRect = new NutrientViewer.Geometry.Rect({ left: 0, top: 0, width: 5, height: 5 });
rect.isRectInside(insideRect); // => true
rect.isRectInside(overlappingRect); // => false
rect.isRectInside(outsideRect); // => false
Test if the union area of two rects is greater than zero.
Parameters
otherNutrientViewer.Geometry.RectThe rect that should be tested.
Returns
booleantrueif the rect is overlapping,falseotherwise.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
const insideRect = new NutrientViewer.Geometry.Rect({ left: 12, top: 12, width: 5, height: 5 });
const overlappingRect = new NutrientViewer.Geometry.Rect({ left: 5, top: 5, width: 10, height: 10 });
const outsideRect = new NutrientViewer.Geometry.Rect({ left: 0, top: 0, width: 5, height: 5 });
rect.isRectOverlapping(insideRect); // => true
rect.isRectOverlapping(overlappingRect); // => true
rect.isRectOverlapping(outsideRect); // => false
Normalizes the Rect. In case of either a negative width or a negative height, the position will be updated so that the location is again the top, left point of the rectangle.
Returns
Rounds all coordinates to whole numbers. This implementation uses
Math.roundfor all coordinates. The resultingRectmight no longer overlap the sourceRect.Returns
A new
rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10.5, top: 15.5, width: 20.5, height: 25.5 });
rect.round(); // => Rect {left: 11, top: 16, width: 21, height: 26}
Rounds all coordinates to whole numbers. The resulting
Rectwill always overlap the sourceRect.The location (
leftandtop) will be rounded to a number which is smaller than or equal to the current value.The size (
widthandheight) will be rounded to a number which is greater than or equal to the current value.Returns
A new
rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10.5, top: 15.5, width: 20.5, height: 25.5 });
rect.roundOverlap(); // => Rect {left: 10, top: 15, width: 21, height: 26}
Scales all values by the given
sxandsyfactor. If onlysxis set andsynot defined, it will scale the values bysx.Parameters
sxnumberScale value for the
leftandwidthvalue. Ifsyis not set, this scale will also be applied totopandheight.synumberOptionalScale value for the
topanheightvalue.Returns
A new
Rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
rect.scale(2); // => Rect {left: 20, top: 20, width: 20, height: 20}
Updates the location of the rect by modifying
leftandtop.Parameters
locationNutrientViewer.Geometry.PointThe new location for the rect.
Returns
A new
Rectwithleftandtopupdated.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10, width: 10, height: 10 });
var nextLocation = new NutrientViewer.Geometry.Point({ x: 20, y: 30 });
rect.setLocation(nextLocation); // => Rect {left: 20, top: 30, width: 10, height: 10}
Translates the location of the rect by a point.
Parameters
pointNutrientViewer.Geometry.PointA point that describes the translation distance.
Returns
A new
Rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10 });
rect.translate(new NutrientViewer.Geometry.Point({ x: 5, y: -5})); // => Rect {left: 15, top: 5, width: 0, height: 0}
Translates the horizontal location of the rect by a number.
Parameters
txnumberA number to translate the
leftvalue.Returns
A new
Rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10 });
rect.translateX(5); // => Rect {left: 15, top: 10, width: 0, height: 0}
Translates the vertical location of the rect by a number.
Parameters
tynumberA number to translate the
topvalue.Returns
A new
Rect.Example
const rect = new NutrientViewer.Geometry.Rect({ left: 10, top: 10 });
point.translateY(5); // => Rect {left: 10, top: 15, width: 0, height: 0}
areRectsCloserThan
- areRectsCloserThan(
a: NutrientViewer.Geometry.Rect,
b: NutrientViewer.Geometry.Rect,
distance: number,
): booleanParameters
Returns
boolean
areVerticallyAligned
- areVerticallyAligned(
a: NutrientViewer.Geometry.Rect,
b: NutrientViewer.Geometry.Rect,
thresholdDistance: number,
): booleanParameters
Returns
boolean
fromClientRect
Creates a new rect from a DOM rect.
Parameters
rectDOMRectA DOM rect, such as the result of
Element.getBoundingClientRect().Returns
A new
Rect.Example
const rect = NutrientViewer.Geometry.Rect.fromClientRect(
element.getBoundingClientRect()
);
fromInset
Parameters
insetNutrientViewer.Geometry.InsetReturns
fromPoints
- fromPoints(
...points: NutrientViewer.Geometry.Point<{ x: number; y: number }>[],
): NutrientViewer.Geometry.RectCreates a new rect from four points.
Parameters
...pointsNutrientViewer.Geometry.Point<{ x: number; y: number }>[]An array of four points.
Returns
A new
Rect.Example
const rect = NutrientViewer.Geometry.Rect.fromPoints(
new NutrientViewer.Geometry.Point({ x: 10, y: 10 }),
new NutrientViewer.Geometry.Point({ x: 20, y: 10 }),
new NutrientViewer.Geometry.Point({ x: 20, y: 20 }),
new NutrientViewer.Geometry.Point({ x: 10, y: 20 })
);
@public
getCenteredRect
- getCenteredRect(
inner: NutrientViewer.Geometry.Size,
outer: NutrientViewer.Geometry.Size,
): NutrientViewer.Geometry.RectParameters
innerNutrientViewer.Geometry.SizeouterNutrientViewer.Geometry.SizeReturns
union
- union(
rects: NutrientViewer.Immutable.List<NutrientViewer.Geometry.Rect>,
): NutrientViewer.Geometry.RectCreates a rect that surrounds all rects in the supplied NutrientViewer.Immutable.List.
This can be used to calculate the bounding box of a list of rects.
Parameters
An immutable list of rects.
Returns
A new
Rect.Example
const rects = NutrientViewer.Immutable.List([
new NutrientViewer.Geometry.Rect({ left: 14, top: 50, width: 50, height: 50 }),
new NutrientViewer.Geometry.Rect({ left: 70, top: 20, width: 98, height: 99 }),
new NutrientViewer.Geometry.Rect({ left: 14, top: 13, width: 15, height: 16 })
]);
const unionRect = NutrientViewer.Geometry.Rect.union(rects); // => Rect {left: 14, top: 13, width: 154, height: 106}
Immutable Record API
27
Iterates the record's
[key, value]pairs, in the order its fields were defined.Returns
IterableIterator<[string, unknown]>
Returns a record with every key reset to the default it was defined with.
Returns
thisA new record holding only default values.
- delete<
K extends
keyof { height: number; left: number; top: number; width: number },
>(
key: K & ("left" | "top" | "width" | "height"),
): thisReturns a record with
keyreset to the default it was defined with.Type Parameters
Kextends keyof { height: number; left: number; top: number; width: number }Parameters
keyK & ("left" | "top" | "width" | "height")One of the keys this record was defined with.
Returns
thisA new record, or this one when the value already equals the default.
Returns a record with the value at
keyPathremoved. A field of this record is reset to its default; a key of a nested collection or plain object is deleted.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
thisA new record, or this one when nothing was removed.
equals
Reports whether
otheris a record holding the same keys with equal values, whatever its class.Parameters
otherunknownThe value to compare against.
Returns
booleantruewhen both hold the same keys and every value matches.Deprecated
Records of different classes with identical values compare equal. Use
isEqual(), which also requires both records to be of the same class.
- get<
K extends
keyof { height: number; left: number; top: number; width: number },
N = never,
>(
key: K & ("left" | "top" | "width" | "height"),
notSetValue?: N,
): N | { height: number; left: number; top: number; width: number }[K]Returns the value stored under
key.Type Parameters
Kextends keyof { height: number; left: number; top: number; width: number }N = neverParameters
Returns the value at
keyPath, walking nested records and collections.A literal key path is checked against the record's fields and gives the value's type; a computed one gives
unknown.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
unknownThe value at that path, or
undefinedwhen it does not resolve.Returns the value at
keyPath, walking nested records and collections.A literal key path is checked against the record's fields and gives the value's type; a computed one gives
unknown.
Reports whether
keyis one of the keys this record was defined with.A literal key must name a field; a key typed as
stringis narrowed to the field names.Parameters
keystringThe key to look for.
Returns
key is "left" | "top" | "width" | "height"truewhen the record defines that key, even if its value isnull.
Computes a hash of this record from its contents, the same way Immutable.js does. Equal records have equal hashes; records with equal hashes are not necessarily equal. Plain objects, functions and symbols hash by identity, so a hash is only meaningful within the page that computed it.
Returns
numberAn integer hash.
Reports whether
keyPathresolves, walking nested records and collections.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
booleantruewhen every key along the path is present.
Reports whether
otheris a record of the same class holding equal values. Only the two records' own classes are compared; nested records compare asequals()does.Parameters
otherunknownThe value to compare against.
Returns
booleantruewhen both are the same class and every value matches.
- mergeDeep(
...collections: (
| Partial<{ left: number; top: number; width: number; height: number }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in, recursing into nested collections and plain objects instead of replacing them.
Keys the record was not defined with are ignored.
Returns a record with each collection merged deeply into the value at
keyPath.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
...collectionsunknown[]Collections or plain objects to merge into the value there.
Returns
thisA new record, or this one when the value is unchanged.
- mergeDeepWith(
merger: (previous: unknown, next: unknown, key: unknown) => unknown,
...collections: (
| Partial<{ left: number; top: number; width: number; height: number }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in deeply, calling
mergerwhere a nested value is not a collection to decide the value that is kept.Keys the record was not defined with are ignored.
Parameters
merger(previous: unknown, next: unknown, key: unknown) => unknownReceives the current value, the incoming value and the key.
Returns
thisA new record, or this one when no value changed.
Returns a record with each collection merged into the value at
keyPath, one level at a time.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
...collectionsunknown[]Collections or plain objects to merge into the value there.
Returns
thisA new record, or this one when the value is unchanged.
- mergeWith(
merger: <K extends string | number | symbol>(
previous: { height: number; left: number; top: number; width: number }[K],
next: { height: number; left: number; top: number; width: number }[K],
key: K,
) => { height: number; left: number; top: number; width: number }[K],
...collections: (
| Partial<{ height: number; left: number; top: number; width: number }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in, calling
mergerfor each key the record already holds to decide the value that is kept.Keys the record was not defined with are ignored.
Parameters
merger<K extends string | number | symbol>(
previous: { height: number; left: number; top: number; width: number }[K],
next: { height: number; left: number; top: number; width: number }[K],
key: K
) => { height: number; left: number; top: number; width: number }[K]Receives the current value, the incoming value and the key.
Returns
thisA new record, or this one when no value changed.
- set<
K extends
keyof { height: number; left: number; top: number; width: number },
>(
key: K & ("left" | "top" | "width" | "height"),
value: { height: number; left: number; top: number; width: number }[K],
): thisReturns a record with
keyset tovalue.Setting a key to the value it already holds returns the same instance, so a record can be compared by reference to detect a change.
Type Parameters
Kextends keyof { height: number; left: number; top: number; width: number }Parameters
Returns
thisA new record, or this one when the value is unchanged or the key is not defined.
Returns a record with
valuewritten atkeyPath.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
valueunknownThe value to store.
Returns
thisA new record, or this one when the value is unchanged.
Converts this record and everything nested inside it to plain JavaScript values.
Returns
{ left: number; top: number; width: number; height: number }A plain object holding this record's values, converted all the way down.
left: number
The
leftdistance of the rect. This is equivalent toxof a NutrientViewer.Geometry.Point.Default Value
0top: number
The
topdistance of the rect. This is equivalent toyof a NutrientViewer.Geometry.Point.Default Value
0width: number
The
widthof the rect. This is equivalent towidthof a NutrientViewer.Geometry.Size.Default Value
0height: number
The
heightof the rect. This is equivalent toheightof a NutrientViewer.Geometry.Size.Default Value
0
An alias for toObject, called by
JSON.stringify.Returns
{ height: number; left: number; top: number; width: number }A plain object holding this record's values.
height: number
The
heightof the rect. This is equivalent toheightof a NutrientViewer.Geometry.Size.Default Value
0left: number
The
leftdistance of the rect. This is equivalent toxof a NutrientViewer.Geometry.Point.Default Value
0top: number
The
topdistance of the rect. This is equivalent toyof a NutrientViewer.Geometry.Point.Default Value
0width: number
The
widthof the rect. This is equivalent towidthof a NutrientViewer.Geometry.Size.Default Value
0
Converts this record to a plain object, one level deep. Nested records are left as they are.
Returns
{ height: number; left: number; top: number; width: number }A plain object holding this record's values.
height: number
The
heightof the rect. This is equivalent toheightof a NutrientViewer.Geometry.Size.Default Value
0left: number
The
leftdistance of the rect. This is equivalent toxof a NutrientViewer.Geometry.Point.Default Value
0top: number
The
topdistance of the rect. This is equivalent toyof a NutrientViewer.Geometry.Point.Default Value
0width: number
The
widthof the rect. This is equivalent towidthof a NutrientViewer.Geometry.Size.Default Value
0
- update<
K extends
keyof { height: number; left: number; top: number; width: number },
>(
key: K & ("left" | "top" | "width" | "height"),
updater: (
value: { height: number; left: number; top: number; width: number }[K],
) => { height: number; left: number; top: number; width: number }[K],
): thisReturns a record with
keyreplaced by the result of applyingupdaterto its current value.Type Parameters
Kextends keyof { height: number; left: number; top: number; width: number }Parameters
Returns
thisA new record, or this one when the value is unchanged.
- updateIn<N>(
keyPath: Iterable<unknown>,
notSetValue: N,
updater: (value: unknown) => unknown,
): thisReturns a record with the value at
keyPathreplaced by the result ofupdater. Returns a record with the value at
keyPathreplaced by the result ofupdater.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
updater(value: unknown) => unknownReceives the current value and returns the new one.
Returns
thisA new record, or this one when the value is unchanged.
Applies several changes as one, so only a single new record is allocated.
The record handed to
mutatormay be written to withset; it is frozen again oncemutatorreturns.Parameters
mutator(mutable: this) => unknownReceives a temporarily writable copy of this record.
Returns
thisA new record holding the applied changes, or this one when nothing changed.
A rect describes a rectangle in 2D space. It consists of a location (
leftandtop) and dimensions (widthandheight). Provided values are defined in same units used by the page, point units. Point units are only equal to pixels when zoom value is1.It is an immutable object and thus can be updated using
set(key, value), for example:rect.set("left", 20).