Point.js is a JavaScript library for point calculations. Point.js has a general definition of "point" and most methods takes multiple parameters, allowing for complex, interdependent chaining and transformations.
npm i point.js
Point.js is exported as a named export, under P
. It can be instantiated with or without using the new
operator.
import { P } from 'point.js';
const point = P()
// const point = new P()
// Do some math with multiple parameters and different point signatures
point.add([ 20, 33 ], { x: 2, y: 5 }, 5)
// => P { x: 27, y: 43 }
// Can be chained
point.mult(1.5).floor()
// => P { x: 40, y: 64 }
// End with transformnation
point.toArray()
// => [ 40, 64 ]
- Static methods
P.add(...points: GeneralPoint[]): Point
P.sub(...points: GeneralPoint[]): Point
P.mult(...points: GeneralPoint[]): Point
P.div(...points: GeneralPoint[]): Point
P.mod(...points: GeneralPoint[]): Point
P.pow(...points: GeneralPoint[]): Point
P.random(...args: [max: number] | [min: number, max: number]): Point
P.min(...points: GeneralPoint[]): Point
P.max(...points: GeneralPoint[]): Point
- Instance methods
p.set(...point: [GeneralPoint] | [x: number, y: number] | undefined[]): this
p.add(...points: GeneralPoint[]): this
p.sub(...points: GeneralPoint[]): this
p.mult(...points: GeneralPoint[]): this
p.div(...points: GeneralPoint[]): this
p.mod(...points: GeneralPoint[]): this
p.pow(...points: GeneralPoint[]): this
p.ceil(precision?: number): thisl
p.floor(precision?: number): this
p.round(precision?: number): this
p.trunc(precision?: number): this
p.sq(): this
p.sqrt(): this
p.cb(): this
p.cbrt(): this
p.abs(): this
p.inv(): this
p.clamp([upper: number] | [lower: number, upper: number]): this
p.between(point: GeneralPoint, distance?: number): this
p.getSum(): this
p.getDistSq(point: GeneralPoint): this
p.getDist(point: GeneralPoint): this
p.clone(): this
p.copy(point: Point): this
p.random(...args: [max: number] | [min: number, max: number]): this
p.min(...points: GeneralPoint[]): this
p.max(...points: GeneralPoint[]): this
p.operation(resolver: (n: number) => number): this
p.transform<T>(resolver: (p: this) => T): T
p.check(resolver: (p: this) => boolean): boolean
p.is(point: GeneralPoint, threshold?: number): boolean
p.toObject(): {x: number, y: number}
p.eject(): {x: number, y: number}
p.toArray(): [x: number, y: number]
p.toString(): `{x: ${number}, y: ${number}}`
p.clg(): this
Adds addends, instantiating P
with the sum.
Subtracts subtrahends, instantiating P
with the difference.
Multiplies multiplicands, instantiating P
with the product.
Divides divisors, instantiating P
with the product.
Divides divisors, instantiating P
with the modulus.
Exponentiates exponents, instantiating P
with the product.
Generates a random number, instantiating P
with the result.
Resolves the min x and min y of points
, instantiating P
with the result.
Resolves the max x and max y of points
, instantiating P
with the result.
Sets this
to a given point.
Adds addends to this
, mutating this
to the sum.
Subtracts subtrahends from this
, mutating this
to the difference.
Multiplies this
by multiplicands, mutating this
to the product.
Divides this
by divisors, mutating this
to the quotient.
Divides this
by divisors, mutating this
to the modulus.
Exponentiates this
by exponents, mutating this
to the product.
Mutates this
by rounding up to precision.
Mutates this
by rounding down to precision.
Mutates this
by rounding to precision.
Mutates this
by rounding towards 0 to precision.
Mutates this
by its square.
Mutates this
by its square root.
Mutates this
by its cube.
Mutates this
by its cube root.
Mutates this
to its absolute.
Mutates this
to its inverse.
Mutates this
by clamping it within the inclusive lower
and upper
bounds.
P(10, -10).clamp(-5, 5)
// => P { x: 5, y: -5 }
Mutates this
to the point between this
and point
.
The fraction of the distance is determined by the multiplicand distance
.
distance
is expected to be a number between 0-1.
where 0 becomes the value ofthis
and 1 the value of point
P(10, -10).between(-20, 20)
// => P { x: -5, y: 5 }
Gets the sum of this
.
Get the square distance between this
and point
.
Get the distance between this
and point
.
Clones this
to a new instance of P
.
const p = P(10, -10)
const clone = p.clone()
Object.is(p, clone) // => false
p.is(clone) // => true
Copies the properties of point
to this
.
Essentially an alias for this.set
.
const p = P(10, -10)
const otherP = P(0, 0)
p.copy(otherP)
Object.is(p, clone) // => false
p.is(clone) // => true
Mutates this
to a random number.
Mutates this
to the respective min x and min y of this
and points
.
Mutates this
to the respective max x and max y of this
and points
.
Mutates this
by executing a method on its properties.
P(NaN, 10).operation(n => Number.isNaN(n) ? 0 : n)
// => P { x: 0, y: 10 }
Transforms this
to the return type of the resolver
.
P(20, 10).transform(p => ({ width: p.x, height: p.y }))
// => P { width: 20, height: 10 }
Executes a test on this
.
P(NaN, 10).operation(p => Number.isNaN(p.x) || Number.isNaN(p.y))
// => true
Compares the properties of this
with point
within the threshold
.
P(10, 10).is([11,11]) // => false
P(10, 10).is([13,7], 5) // => true
Transforms this
into an object.
Alias for p.toObject
.
Transforms this
into an array.
Transforms this
into a string.
Executes console.log on this
.
P(20, 10).clg()
/*
___In the console___
P {x: 20, y: 10}
*/