Object storing all color objects being transitioned at the moment
An array storing all the reserved unique IDS
The version of the module.
The logger module this module uses to log errors / logs
Generates a random decimal number between two numbers with a specified number of decimal places.
The first number to use for generating the random decimal number.
The second number to use for generating the random decimal number.
The number of decimal places to include in the generated random decimal number. Defaults to 1 if not provided.
A random decimal number between the two numbers with the specified number of decimal places.
Generates a random decimal number between two numbers with a specified number of decimal places.
The first number to use for generating the random decimal number.
The second number to use for generating the random decimal number.
A random decimal number between the two numbers with the specified number of decimal places.
Calculates the percentage of a value relative to a total value.
The value to calculate the percentage of.
The total value to calculate the percentage relative to.
The percentage of the value relative to the total value.
Clamps a number between a minimum and maximum value.
The number to clamp.
The minimum value to clamp the number to. Defaults to 0 if not provided.
The maximum value to clamp the number to. Defaults to 1 if not provided.
The clamped number between the minimum and maximum values.
Linearly interpolates between two values by a specified amount.
The start value to interpolate from.
The end value to interpolate to.
The amount to interpolate between the start and end values.
The interpolated value between the start and end values based on the specified amount.
Linearly interpolates between two values by a specified amount and returns the result as a floored integer.
The start value to interpolate from.
The end value to interpolate to.
The amount to interpolate between the start and end values.
The interpolated value between the start and end values based on the specified amount, rounded down to the nearest integer.
Rounds a number to a specified number of decimal places.
The number to round.
The number of decimal places to round to. Defaults to 1 if not provided.
The rounded number to the specified number of decimal places.
Normalizes a value between a minimum and maximum value.
The value to normalize.
The minimum value for normalization.
The maximum value for normalization.
The normalized value between 0 and 1 based on the input value's position between the minimum and maximum values. If the difference between pMax and pMin is 0, returns 1 to avoid dividing by zero.
Normalizes a value between a minimum and maximum value, clamped to the range of -1 to 1.
The value to normalize.
The minimum value for normalization.
The maximum value for normalization.
The normalized and clamped value between -1 and 1 based on the input value's position between the minimum and maximum values. If the difference between pMax and pMin is 0, returns 1 to avoid dividing by zero.
Checks if a value is within a range of minimum and maximum values (inclusive).
The value to check.
The minimum value of the range to check against.
The maximum value of the range to check against.
True if the value is within the range (inclusive), false otherwise.
Calculates the angle (in radians) from a given direction.
The direction to calculate the angle from.
The angle (in radians) associated with the given direction.
Throws an error if the direction is not recognized.
Centers a rectangle (defined by its dimensions) within a parent rectangle.
The width of the child rectangle.
The height of the child rectangle.
The width of the parent rectangle.
The height of the parent rectangle.
The x-coordinate of the parent rectangle.
The y-coordinate of the parent rectangle.
An object representing the new coordinates of the centered rectangle: { x: centerX, y: centerY }.
const childWidth = 50;
const childHeight = 30;
const parentWidth = 100;
const parentHeight = 80;
const parentX = 20;
const parentY = 10;
const centeredCoordinates = centerRectangleOnParent(childWidth, childHeight, parentWidth, parentHeight, parentX, parentY);
// Returns {x: 45, y: 35}
Gets the angle between two points
The starting point
The ending point
The angle between the starting point and the ending point
Gets the angle between two points but in VYLO / PIXI coordinate space. Removes 180 degrees from a raw angle
The starting point
The ending point
The angle between the starting point and the ending point
Gets the minimal value between the value to add and the maximum allowed value.
The current value.
The value to add to the current value.
The maximum value that can be reached.
The minimal value between the value to add and the remaining value to reach the maximum.
Calculates the Euclidean distance between two points in a two-dimensional space.
The starting point with x and y coordinates.
The x-coordinate of the starting point.
The y-coordinate of the starting point.
The ending point with x and y coordinates.
The x-coordinate of the ending point.
The y-coordinate of the ending point.
The Euclidean distance between the two points.
const startPoint = { x: 1, y: 2 };
const endPoint = { x: 4, y: 6 };
const distance = getDistance(startPoint, endPoint); // 5
// Returns the Euclidean distance between the points (1, 2) and (4, 6).
Calculates the new position of a point based on distance and angle.
The initial position of the point with x and y coordinates.
The initial x-coordinate of the point.
The initial y-coordinate of the point.
The distance by which to move the point.
The angle (in radians) at which to move the point.
The new position of the point after moving by the specified distance and angle.
const initialPosition = { x: 10, y: 20 };
const distance = 5;
const angleInRadians = 0.785398; // 45 degrees
const newPosition = calculateNewPositionFromDistanceAndAngle(initialPosition, distance, angleInDegrees);
// Returns the new position of the point after moving by 5 units at a 45-degree angle.
Calculates the proportional length based on a current value, a maximum value, and a specified total length.
The current value to be scaled.
The maximum value for scaling.
The specified total length.
The proportional length based on the current value, maximum value, and total length.
const current = 25;
const max = 50;
const totalLength = 100;
const proportionalLength = calculateProportionalLength(current, max, totalLength); // 50
// Returns the proportional length based on the current value, maximum value, and total length.
Calculates the compass direction based on the given angle.
The angle in radians.
The compass direction (e.g., 'east', 'southeast', 'south', etc.).
const angle = Math.PI / 4; // 45 degrees in radians
const direction = getDirection(angle); // Returns 'northeast'
Calculates the linear decay of a variable over time.
The initial value of the variable.
The current time at which to calculate the variable value.
The maximum time for the decay process.
The decay rate (default is 0.5).
The remaining value of the variable after linear decay.
const initialValue = 100;
const currentTime = 50;
const maxTime = 1000;
const decayRate = 0.3;
const remainingValue = linearDecay(initialValue, currentTime, maxTime, decayRate);
// Returns the remaining value after linear decay.
Convert a color to different formats or get a random color
A hex string representing a color (with or without the tag) A color formatted in the decimal format. Or the r value of a rgb color.
Optional pG: numberg value of a rgb color
Optional pB: numberb value of a rgb color
A color object with various different export options. hex, hexTagless, rgb, rgbArray, rgbObject, rgbNormal, decimal formats.
Gets a random color between two colors
The first color to get a color between
The second color to get a color between
The closer the random color will be to either input colors on a range of 0-1 0 to 0.5 (closer to pColor1) 0.5 to 1 (closer to pColor2)
A random color in the decimal format
Transition a color to another color in pDuration time.
The instance to transition it's color property. pInstance's color will be transitioned either via pInstance.color = newColor or pInstance.color.tint = newColor (if the color is defined as an object)
The start color
The end color
The duration of the transition
Optional pIterativeCallback: ((color) => void)Callback to call every tick of the transition
Optional pEndCallback: ((color) => void)Callback to call at the end of the transition
An ID that references this transition to be passed to cancelTransition to stop an ongoing transition.
Calculates the position of a point after rotating it around a center point by a given angle.
The rectangle object to rotate the point around. pRect.anchor.x and pRecent.anchor.y is used to control the "center" of the rectangle.
Optional anchor?: { Optional x?: numberOptional y?: numberThe angle (in radians) to rotate the point by.
The point object to rotate around the center of the rectangle.
The x-coordinate of the point to rotate.
The y-coordinate of the point to rotate.
An object with the rotated point's new x and y coordinates.
Calculates the position of a rectangle's corner points and center point after rotating it around a center point by a given angle.
The rectangle object to rotate the point around. pRect.anchor.x and pRecent.anchor.y is used to control the "center" of the rectangle.
Optional anchor?: { Optional x?: numberOptional y?: numberThe angle (in radians) to rotate the point by.
An object with the rotated rectangle's new corner points and center points.
Calculate the icon offset to compensate for a non-zero anchor.
The size of the icon with properties .x and .y.
The size of the icon's width.
The size of the icon's height'.
The anchor point with properties .x and .y.
The anchor's x value.
The anchor's y value.
The scale factor applied to the object with properties .x and .y.
The scale's y value.
The scale's y value.
The calculated icon offset with properties .x and .y.
A utilities class providing mathematical, color, and geometric utility functions.
This class contains static-like methods for common utility operations including:
UtilsSingleton
License
Utils is free software, available under the terms of a MIT style License.
Author
https://github.com/doubleactii
Example