Skip to main content

Home > @jsplumb/util

util package#

This package contains various utility functions used by both the Community and Toolkit editions of jsPlumb.


EventGeneratorBase class for classes that wish to support binding and firing of events.
OptimisticEventGeneratorSubclass of EventGenerator with a default implementation of shouldFireEvent, which returns true always.


add(p1, p2)Adds the x and y values of the two points and returns a new point.
encloses(r1, r2, allowSharedEdges)Calculates whether or not r2 is completely enclosed by r1.
extend(o1, o2, keys)Equivalent of Object.assign, which IE11 does not support.
fastTrim(s)Trims whitespace from the given string.
fixPrecision(n, digits)Trim decimal points from a number. Defaults to 3 decimal points.
getsert(map, key, valueGenerator)Get, or insert then get, a value from the map.
gradient(p1, p2)Calculates the gradient of a line between the two points.
insertSorted(value, array, comparator, sortDescending)Inserts the given value into the given array at a sorted location.
intersects(r1, r2)Calculates whether or not the two rectangles intersect.
isAssignableFrom(object, cls)Returns true if the given object can be considered to be an instance of the class cls. This is done by testing the proto chain of the object and checking at each level to see if the proto is an instance of the given class.
isBoolean(s)Returns whether or not the given value is of boolean type.
isDate(o)Returns whether or not the given value is of type Date
isEmpty(o)Returns whether or not the given object - which may be ArrayLike, or an object - is empty.
isFunction(o)Returns whether or not the given value is of type Function
isNamedFunction(o)Returns whether or not the given value is of type Function and is a named Function.
isNumber(n)Returns whether or not the given value is of number type.
isObject(o)Returns whether or not the given value is of type object
isString(s)Returns whether or not the given value is of string type.
lineIntersection(l1, l2)Compute the intersection of the two lines.
lineLength(p1, p2)Calculates the length of a line between the two points.
lineRectangleIntersection(line, r)Finds all points where the given line intersects the given rectangle.
normal(p1, p2)Calculates the gradient of a normal to a line between the two points.
perpendicularLineTo(fromPoint, toPoint, length)Calculates a line of length length that is perpendicular to the line from fromPoint to toPoint and passes through toPoint.
pointOnLine(fromPoint, toPoint, distance)Calculates a point on the line from fromPoint to toPoint that is distance units along the length of the line.
quadrant(p1, p2)Calculates the quadrant in which the angle between the two points lies.
rotatePoint(point, center, rotation)Rotate the given point around the given center, by the given rotation (in degrees)
sgn(x)Replacement for Math.sign, which IE11 does not support.
snapToGrid(pos, grid, thresholdX, thresholdY)Snap the given x,y to a point on the grid defined by gridX and gridY, using the given thresholds to calculate proximity to the grid.
subtract(p1, p2)Subtracts p2 from p1, returning a new point.
theta(p1, p2)Calculates the angle between the two points.
uuid()Generate a v4 UUID.


GridDefinition of a grid - the width/height of each cell, and, optionally, a threshold value for each axis to use when trying to snap some coordinate to the grid.
PointXYDefines an x/y location.
SizeDefines the width and height of some element.


logEnabledDetermines whether or not logging is currently enabled.
TWO_PIDefinition of 2 PI

Type Aliases#

Type AliasDescription
BoundingBoxDefines the bounding box for some element - its x/y location, width and height, and optionally the computed center, but that can of course be calculated from the other values. Internally there are times when the code has this to hand so we include it here.
ConstructableDefines an object that has a constructor. Used internally to create endpoints/connectors/overlays from their names. Exposed as public for people to create their own endpoints/connectors/overlays.
LineXYDefines a line from some point to another.
RectangleXYThis is an alias for BoundingBox.