Utilities Documentation
Utilities Methods
- assert
- assertDebug
- assertEquals
- checkArray
- checkBoolean
- checkFunction
- checkInstanceOf
- checkInt
- checkIsSet
- checkNumber
- checkObject
- checkString
- checkType
- clamp
- deepMerge
- dist2d
- downloadFile
- error
- event
- fatal
- intersectIntervals
- intersectRects
- loadFileAsync
- loadImageAsync
- log
- makeColorTransparent
- makeUrlPretty
- padWithZeros
- repeatArray
- warn
beep8.Utilities.assert(cond, msg)
Asserts that the given condition is true, else shows a fatal error.
Parameters
- cond (boolean): The condition that you fervently hope will be true.
- msg (string): The error message to show if the condition is false.
Returns
- (boolean): The ‘cond’ parameter.
beep8.Utilities.assertDebug(cond, msg)
Same as beep8.Utilities.assert() but only asserts if beep8.CONFIG.DEBUG is true.
Parameters
- cond (boolean): The condition that you fervently hope will be true.
- msg (string): The error message to show if the condition is false.
Returns
- (boolean): The ‘cond’ parameter.
beep8.Utilities.assertEquals(expected, actual, what)
Asserts that two values are equal.
Parameters
- expected (any): What you expect the value to be.
- actual (any): What the value actually is.
- what (string): A description of what the value is.
Returns
- (any): The ‘actual’ parameter.
beep8.Utilities.checkArray(varName, varValue)
Checks that a variable is an array.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (Array): The ‘varValue’ parameter.
beep8.Utilities.checkBoolean(varName, varValue)
Checks that a variable is a boolean.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (boolean): The ‘varValue’ parameter.
beep8.Utilities.checkFunction(varName, varValue)
Checks that a variable is a function.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (Function): The ‘varValue’ parameter.
beep8.Utilities.checkInstanceOf(varName, varValue, expectedClass)
Checks that a variable is an instance of a given class.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
- expectedClass (Function): The expected class.
Returns
- (any): The ‘varValue’ parameter.
beep8.Utilities.checkInt(varName, varValue)
Checks that a variable is an integer.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (number): The ‘varValue’ parameter.
beep8.Utilities.checkIsSet(varName, varValue)
Checks that a variable is set (not undefined or null).
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (any): The ‘varValue’ parameter.
beep8.Utilities.checkNumber(varName, varValue [,optMin] [,optMax])
Checks that a variable is a number.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
- [optMin] (number): The minimum acceptable value for the variable. (default: undefined)
- [optMax] (number): The maximum acceptable value for the variable. (default: undefined)
Returns
- (number): The ‘varValue’ parameter.
beep8.Utilities.checkObject(varName, varValue)
Checks that a variable is an object.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (Object): The ‘varValue’ parameter.
beep8.Utilities.checkString(varName, varValue)
Checks that a variable is a string.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
Returns
- (string): The ‘varValue’ parameter.
beep8.Utilities.checkType(varName, varValue, varType)
Checks the type of a variable and throws an exception if it’s incorrect.
Parameters
- varName (string): The name of the variable.
- varValue (any): The value of the variable.
- varType (string): The expected type of the variable.
Returns
- (any): The ‘varValue’ parameter.
beep8.Utilities.clamp(x, lo, hi)
Clamps a number, ensuring it’s between a minimum and a maximum.
Parameters
- x (number): The number to clamp.
- lo (number): The minimum value.
- hi (number): The maximum value.
Returns
- (number): The clamped number.
beep8.Utilities.deepMerge(objects)
Performs a deep merge of objects and returns new object. Does not modify objects (immutable) and merges arrays via concatenation.
Parameters
- objects (…object): Objects to merge
Returns
- (object): New object with merged key/values
beep8.Utilities.dist2d(x0, y0, x1, y1)
Calculates a 2D distance between points (x0, y0) and (x1, y1).
Parameters
- x0 (number): The x-coordinate of the first point.
- y0 (number): The y-coordinate of the first point.
- x1 (number): The x-coordinate of the second point.
- y1 (number): The y-coordinate of the second point.
Returns
- (number): The distance between the two points.
beep8.Utilities.downloadFile(filename, src)
Downloads a file.
Parameters
- filename (string): The name of the file.
- src (string): The source URL of the file.
beep8.Utilities.error(msg)
Prints an error to the console.
Parameters
- msg (string): The message to print.
beep8.Utilities.event(eventName [,detail] [,target])
Generate a new custom event.
Parameters
- eventName (string): The name of the event.
- [detail] (Object): The event detail. (default: {})
- [target] (EventTarget): The target of the event. (default: document)
beep8.Utilities.fatal(error)
Shows a fatal error and throws an exception.
Parameters
- error (string): The error to show.
beep8.Utilities.intersectIntervals(as, ae, bs, be [,result])
Calculates the intersection between two integer number intervals.
Given 2 ranges it will see if these ranges overlap and if they do it will optionally return the intersection range.
For example if the first interval is [1, 5] and the second interval is [3, 7] the intersection is [3, 5].
Parameters
- as (number): The start of the first interval.
- ae (number): The end of the first interval.
- bs (number): The start of the second interval.
- be (number): The end of the second interval.
- [result] (Object): If provided, used to return the intersection. (default: null)
Returns
- (boolean): True if there is an intersection, false otherwise.
beep8.Utilities.intersectRects(r1, r2 [,dx1] [,dy1] [,dx2] [,dy2] [,result])
Calculates the intersection of two rectangles.
Parameters
- r1 (Object): The first rectangle.
- r2 (Object): The second rectangle.
- [dx1] (number): The delta X to add to the first rectangle. (default: 0)
- [dy1] (number): The delta Y to add to the first rectangle. (default: 0)
- [dx2] (number): The delta X to add to the second rectangle. (default: 0)
- [dy2] (number): The delta Y to add to the second rectangle. (default: 0)
- [result] (Object): If provided, used to return the intersection. (default: null)
Returns
- (boolean): True if there is a non-empty intersection, false otherwise.
beep8.Utilities.loadFileAsync(url)
Loads a file asynchronously.
Parameters
- url (string): The URL of the file.
Returns
- (*Promise
*): A promise that resolves to the file content.
beep8.Utilities.loadImageAsync(src) async
Loads an image asynchronously.
Parameters
- src (string): The source URL of the image.
Returns
- (*Promise
*): A promise that resolves to the loaded image.
beep8.Utilities.log(msg)
Prints a log to the console if beep8.CONFIG.DEBUG is true.
Parameters
- msg (string): The message to print.
beep8.Utilities.makeColorTransparent(img, color, range) async
Makes a color transparent in an image.
This function is asynchronous because it uses an HTMLImageElement.
Uses a range because I found that occassionally the RGB values of a saved png are not exactly as they were set in the image. Possibly due to compression.
Parameters
- img (HTMLImageElement): The image to process.
- color (array): The color to make transparent. By default this is pure magenta [255,0,255].
- range (number): The range of RGB values to consider as the target color.
beep8.Utilities.makeUrlPretty(str)
Converts a string to a pretty URL-friendly format.
Parameters
- str (string): The string to convert.
Returns
- (string): The pretty string.
beep8.Utilities.padWithZeros(number, length)
Pads a number with leading zeros to the specified length.
Does not support negative numbers.
Parameters
- number (number): The number to pad.
- length (number): The desired length of the output string.
Returns
- (string): - The padded number as a string.
beep8.Utilities.repeatArray(array, times)
Utility function to repeat an array a specified number of times.
Parameters
- array (Array): The array to repeat.
- times (number): The number of times to repeat the array.
Returns
- (Array): The repeated array.
beep8.Utilities.warn(msg)
Prints a warning to the console.
Parameters
- msg (string): The message to print.