Repository navigation
Utils v1.0.0
Initial Release
Utils Module
Utils is a lightweight and versatile JavaScript utility class designed to provide commonly used functions for web development. It is a collection of various helper functions that can be used in a variety of projects.
Installation
ES Module
import { Utils } from './utils.mjs';IIFE (Immediately Invoked Function Expression)
<script src="utils.js"></script>;
// ...
window.UtilsBundle.Utils;CommonJS (CJS) Module
const { Utils } = require('./utils.cjs.js');You can then call any of the functions available in the Utils class.
API
decimalRand(pNum1, pNum2, pPlaces)
- Generates a random decimal number between
pNum1andpNum2withpPlacesdecimal places.
rand(pNum1, pNum2)
- Generates a random integer between
pNum1andpNum2.
getPercentage(pValue, pTotalValue)
- Calculates the percentage of
pValuein relation topTotalValue.
clamp(pNumber, pMin, pMax)
- Clamps
pNumberbetweenpMinandpMax.
lerp(pStart, pEnd, pAmount)
- Linearly interpolates between
pStartandpEndbypAmount.
flooredLerp(pStart, pEnd, pAmount)
- Linearly interpolates between
pStartandpEndbypAmount, then floors the result.
round(pNumber, pPlace)
- Rounds pNumber to
pPlacedecimal places.
normalize(pVal, pMin, pMax)
- Normalizes pVal between
pMinandpMax.
within(pVal, pMin, pMax)
- Checks if
pValis within the range ofpMinandpMax.
formatIntegerWithCommas(pNum)
- Formats
pNumas a string with commas between every three digits.
toRadians(pDegrees)
- Converts degrees to radians.
toDegrees(pRadians)
- Converts radians to degrees.
pick(pArray)
- Returns a random element from
pArray.
removeProperties(pObject, pExclude)
- Removes all properties from
pObject, excluding those inpExclude.
prob(pChance)
- Returns true with a probability of
pChance, and false with a probability of100 - pChance.
getInverseDirection(pDir)
- Returns the opposite direction of
pDir.
getAngleFromDirection(pDir)
- Returns the angle in radians corresponding to
pDir.
generateID(pIDLength)
- Generates a random unique ID string of length
pIDLength.
decimalToHex(pDecimal, pChars)
- Converts a decimal number to a hexadecimal color string with pChars characters.
pCharscan either be3or6.
addIntensity(pColor, pPercent)
- Increases the intensity of pColor by
pPercentpercent.
rgbToHex(pR, pG, pB)
- Converts an RGB color to a hexadecimal color string.
hexToRgb(pHex)
- Converts a hexadecimal color string to an RGB color.
rgbToDecimal(pR, pG, pB)
- Converts an RGB color to a decimal number.
hexToDecimal(pHex)
- Converts a hexadecimal color string to a decimal number.
grabColor(pSwitch, pG, pB)
- Returns a color object with a hexadecimal color string, tagless hexadecimal color string, RGB string, RGB color array, RGB normalized array, RGB object, and decimal color number. If
pSwitchis a number,pGandpBmust also be numbers and represent the green and blue components of the color respectively. IfpSwitchis a string, it can either be a hexadecimal color string or a decimal color.
getRandomColor()
- Gets a random color in the hex format.
getRandomColorBetween(pColor1, pColor2, pAmount = 0.5)
- Gets a random color between
pColor1andpColor2in the decimal format.pAmountcan be used to change how close the returned color is to eitherpColor1orpColor2.
transitionColor(pInstance, pStartColor='#000', pEndColor='#fff', pDuration=1000, pIterativeCallback, pEndCallback)
- Transition
pStartColortopEndColorinpDurationtime with callbacks to track the animation. WherepIterativeCallbackis called each frame andpEndCallbackis called at the end of the animation. This API returns theIDof the transition. So that if you want to cancel it, you can usecancelTransitionColor(ID).
cancelTransitionColor(pID)
- Cancels an ongoing transition with the
IDofpID.
getPointRotated(pRect, pTheta, pPoint)v
- Calculates the position of a point after rotating it around a center point by a given angle.
- @PARAM {object} pRect - The rectangle object to rotate the point around. pRect.anchor.x and pRecent.anchor.y is used to control the "center" of the rectangle.
- @PARAM {number} pTheta - The angle (in radians) to rotate the point by.
- @PARAM {object} pPoint - The point object to rotate around the center of the rectangle.
- @PARAM {number} pPoint.x - The x-coordinate of the point to rotate.
- @PARAM {number} pPoint.y - The y-coordinate of the point to rotate.
- @returns {object} An object with the rotated point's new x and y coordinates.
getPointsOfRotatedRect(pRect, pTheta)
- @PARAM {object} pRect - The rectangle object to rotate the point around.
pRect.anchor.xandpRecent.anchor.yis used to control the "center" of the rectangle. - @PARAM {number} pTheta - The angle (in radians) to rotate the point by.
- @returns {object} An object with the rotated rectangle's new corner points and center points.
Global Dependency
Utils relies on the VYLO variable being globally accessible.