Home Reference Source

Function

Static Public Summary
public

calcStepSize(value: number, stepAmount: number): number

Finds the best fitting scale for a given range.

public

clamp(val: number, min: number, max: number): number

Constrain a value within a range of values.

public

cos(val: number, type: number): number

Cosine function that can accept angles in radians or degrees.

public

distSquared(x0: number, y0: number, x1: number, y1: number): number

Given a set of two points the squared distance is calculated.

public

Makes sure that a HTML id allways has a # at the beginning.

public

formatValue(val: number | string, units: string, decPlaces: number, fixPlaces: boolean): string

Format a number to have a certain number of decimal places and/or fixed places and add a unit label.

public

Generate a random number following a gaussian distribuition.

public

Obtains the pixel ratio of the device.

public

isCoordInside(x: number, y: number, bx: number, by: number, bw: number, bh: number): boolean

Determines if a point is inside of a box.

public

Test if an object is a function.

public

Test if an object is an object.

public

Test if an object is a string.

public

loadOptions(obj: object, args: object)

Assign the matching properties from the args object to the obj object.

public

rad(angle: number, type: number): number

Given an angle makes sure that it is in radians.

public

rgbToRgba(color: string, alpha: number): string

Adds an alpha channel to an rgb color.

public

round(value: number, precision: number): number

Round a number to a given amount of decimal places.

public

sin(val: number, type: number): number

Sine function that can accept angles in radians or degrees.

Static Public

public calcStepSize(value: number, stepAmount: number): number source

import {calcStepSize} from 'newton-lib/src/utils/Utils.js'

Finds the best fitting scale for a given range. Used for simulations where the scale of the data changes dramatically.

Params:

NameTypeAttributeDescription
value number

Corresponds to the range between the minimum required value to be displayed and the maximum required value to be displayed.

stepAmount number

Desired amount of steps within the range.

Return:

number

Magnitude of the step size.

public clamp(val: number, min: number, max: number): number source

import {clamp} from 'newton-lib/src/utils/Utils.js'

Constrain a value within a range of values. If value > max then value = max; If value < min then value = min;

Params:

NameTypeAttributeDescription
val number

Value to clamp within range.

min number

Minimum acceptable value.

max number

Maximum acceptable value.

Return:

number

Constrained value to the given range.

public cos(val: number, type: number): number source

import {cos} from 'newton-lib/src/utils/Utils.js'

Cosine function that can accept angles in radians or degrees.

Params:

NameTypeAttributeDescription
val number

Angle for calculating the cosine.

type number

Determines if the angle provided is in radians or in degrees. See ANGLE_STYLE.

Return:

number

Cosine of the angle.

public distSquared(x0: number, y0: number, x1: number, y1: number): number source

import {distSquared} from 'newton-lib/src/utils/Utils.js'

Given a set of two points the squared distance is calculated. This is faster than calculating the Euclidean distance between them, since no square root is calculated.

Params:

NameTypeAttributeDescription
x0 number

Initial -x coordinate.

y0 number

Initial -y coordinate.

x1 number

Final -x coordinate.

y1 number

Final -y coordinate.

Return:

number

The squared distance between points.

public fixId(id: string): string source

import {fixId} from 'newton-lib/src/utils/Utils.js'

Makes sure that a HTML id allways has a # at the beginning. If the provided id already has a # then it returns the same id, otherwise it is added.

Params:

NameTypeAttributeDescription
id string

Id of an HTML object.

Return:

string

Id with a # prepended.

public formatValue(val: number | string, units: string, decPlaces: number, fixPlaces: boolean): string source

import {formatValue} from 'newton-lib/src/utils/Utils.js'

Format a number to have a certain number of decimal places and/or fixed places and add a unit label. Used to format the strings of labels and sliders. If the input value is a SYMBOL then return the same symbol.

Params:

NameTypeAttributeDescription
val number | string

Input number. Can also be a string or a symbol such as in SYMBOL.

units string

Units of the input number.

decPlaces number

Amount of decimal places to round.

fixPlaces boolean

Fix the amount of decimal places. If the number doesn't have enough, zeros will be added.

Return:

string

Formated number.

public gaussian(n: number): number source

import {gaussian} from 'newton-lib/src/utils/Utils.js'

Generate a random number following a gaussian distribuition.

Params:

NameTypeAttributeDescription
n number

Amount of iterations.

Return:

number

Random number that follows a gaussian distribuition.

public getPixelRatio(): number source

import {getPixelRatio} from 'newton-lib/src/utils/Utils.js'

Obtains the pixel ratio of the device. Used to scale properly the canvas for high resolution devices.

Return:

number

The pixel ratio of the device.

public isCoordInside(x: number, y: number, bx: number, by: number, bw: number, bh: number): boolean source

import {isCoordInside} from 'newton-lib/src/utils/Utils.js'

Determines if a point is inside of a box. Often used to test is the mouse is over an element.

Params:

NameTypeAttributeDescription
x number

x coordinate to test.

y number

y coordinate to test.

bx number

x center coordinate of the box.

by number

y center coordinate of the box.

bw number

Half of the box's width.

bh number

Half of the box's height.

Return:

boolean

True if point is within the box, false otherwise.

public isFunction(f: object): boolean source

import {isFunction} from 'newton-lib/src/utils/Utils.js'

Test if an object is a function.

Params:

NameTypeAttributeDescription
f object

Object to test.

Return:

boolean

True if the object is a function, false otherwise.

public isObject(o: object): boolean source

import {isObject} from 'newton-lib/src/utils/Utils.js'

Test if an object is an object.

Params:

NameTypeAttributeDescription
o object

Object to test.

Return:

boolean

True if the object is an object, false otherwise.

public isString(s: object): boolean source

import {isString} from 'newton-lib/src/utils/Utils.js'

Test if an object is a string.

Params:

NameTypeAttributeDescription
s object

Object to test.

Return:

boolean

True if the object is a string, false otherwise.

public loadOptions(obj: object, args: object) source

import {loadOptions} from 'newton-lib/src/utils/Utils.js'

Assign the matching properties from the args object to the obj object. This allows for settings to be passed in single line and set on the receiving object. Almost all World Element objects accept such settings on their constructors.

Params:

NameTypeAttributeDescription
obj object

Object where the settings will be loaded.

args object

Object with matching properties from obj.

Example:

loadOptions(font, { face: "Helvetica", size: 12 });

public rad(angle: number, type: number): number source

import {rad} from 'newton-lib/src/utils/Utils.js'

Given an angle makes sure that it is in radians.

Params:

NameTypeAttributeDescription
angle number

Angle to convert to radians.

type number

Determines if the angle provided is in radians or in degrees. See ANGLE_STYLE.

Return:

number

Angle in radians.

public rgbToRgba(color: string, alpha: number): string source

import {rgbToRgba} from 'newton-lib/src/utils/Utils.js'

Adds an alpha channel to an rgb color. Must have the pattern #AABBCC.

Params:

NameTypeAttributeDescription
color string

Color to add alpha.

alpha number

Alpha channel added to the color.

Return:

string

public round(value: number, precision: number): number source

import {round} from 'newton-lib/src/utils/Utils.js'

Round a number to a given amount of decimal places.

Params:

NameTypeAttributeDescription
value number

Value to round.

precision number

Amount of decimal places required.

Return:

number

Rounded number.

public sin(val: number, type: number): number source

import {sin} from 'newton-lib/src/utils/Utils.js'

Sine function that can accept angles in radians or degrees.

Params:

NameTypeAttributeDescription
val number

Angle for calculating the sine.

type number

Determines if the angle provided is in radians or in degrees. See ANGLE_STYLE.

Return:

number

Sine of the angle.