Methods
(static) clamp(v, min, max) → {number}
Clamp a float to be between [min, max).
Name | Type | Description |
---|---|---|
v | number | value to clamp between min & max |
min | number | min value |
max | number | max value |
a float between min/max
- Type:
- number
(static) createCanvas(width, height, offscreenopt) → {Canvas}
Create a blank 2D canvas of a given width/height.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
width | number | The canvas height in pixels | ||
height | number | The canvas width in pixels | ||
offscreen | boolean | <optional> | offscreenOK() | If true, return "Offscreen" canvas |
The resulting Canvas object
- Type:
- Canvas
(static) createCtx(width, height, offscreenopt) → {Context2D}
As above, but returing the 2D context object instead of the canvas. Note ctx.canvas is the canvas for the ctx, and can be use as an image.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
width | number | The canvas height in pixels | ||
height | number | The canvas width in pixels | ||
offscreen | boolean | <optional> | offscreenOK() | If true, return "Offscreen" canvas |
The resulting Canvas's 2D context
- Type:
- Context2D
(static) degToRad(degrees) → {number}
Convert from degrees to radians
Name | Type | Description |
---|---|---|
degrees | number | a value in degrees: in [0, 360) |
the value as radians: in [0, 2PI)
- Type:
- number
(static) headingToRad(heading) → {number}
Convert from heading to radians
Name | Type | Description |
---|---|---|
heading | number | a value in degrees: in [0, 360) |
a value in radians: in [0, 2PI)
- Type:
- number
(static) imagePromise(url) → {Promise}
Return a Promise for getting an image.
use: imagePromise('./path/to/img').then(img => imageFcn(img)) or: await imagePromise('./path/to/img')
Name | Type | Description |
---|---|---|
url | string | URL for path to image |
A promise resolving to the image
- Type:
- Promise
(static) mod(v, n) → {number}
A true modulus function, differing from the % remainder operation.
Name | Type | Description |
---|---|---|
v | number | The value to calculate the modulus of |
n | number | The number relative to which the modulus is calculated. |
The value of v mod n
- Type:
- number
(static) oneOf(An) → {any}
Return random item from an array
Name | Type | Description |
---|---|---|
An | Array | array to choose from |
The chosen item
- Type:
- any
(static) pause(msopt) → {Promise}
Return promise for pause of ms. Use: await pause(ms)
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
ms | number | <optional> | 1000 | Number of ms to pause |
A promise to wait this number of ms
- Type:
- Promise
(static) precision(num, digitsopt) → {number}
Round a number to be of a given decimal precision. If the number is an array, round each item in the array
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
num | number | | The number to convert/shorten | ||
digits | number | <optional> | 4 | The number of decimal digits |
The resulting number
- Type:
- number
(static) radToDeg(radians) → {number}
Convert from radians to degrees
Name | Type | Description |
---|---|---|
radians | number | a value in radians: in [0, 2PI) |
the value as degrees: in [0, 360)
- Type:
- number
(static) radToHeading(radians) → {number}
Convert from radians to heading
Heading is 0-up (y-axis), clockwise angle measured in degrees. Radians is euclidean: 0-right (x-axis), counterclockwise in radians
Name | Type | Description |
---|---|---|
radians | number | a value in radians: in [0, 2PI) |
a value in degrees: in [0, 360)
- Type:
- number
(static) randomCentered(r) → {number}
Return a random float centered around r, in [-r/2, r/2)
Name | Type | Description |
---|---|---|
r | number | The center float |
a float in [-r/2, r/2)
- Type:
- number
(static) randomFloat(max) → {number}
Returns a random float in [0, max)
Name | Type | Description |
---|---|---|
max | number | The max float to return |
a float in [0, max)
- Type:
- number
(static) randomFloat2(min, max) → {number}
Returns a random float in [min, max)
Name | Type | Description |
---|---|---|
min | number | The min float to return |
max | number | The max float to return |
a float in [min, max)
- Type:
- number
(static) randomInt(max) → {number}
Returns an int in [0, max), equal or grater than 0, less than max
Name | Type | Description |
---|---|---|
max | number | The max integer to return |
an integer in [0, max)
- Type:
- number
(static) randomInt2(min, max) → {number}
Returns an int in [min, max), equal or grater than min, less than max
Name | Type | Description |
---|---|---|
min | number | The min integer to return |
max | number | The max integer to return |
an integer in [min, max)
- Type:
- number
(static) randomSeed(seedopt)
Install a seeded random generator as Math.random Uses an optimized version of the Park-Miller PRNG.
Math.random will return a sequence of "random" numbers in a known sequence. Useful for testing to see if the same results occur in multiple runs of a model with the same parameters.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
seed | number | <optional> | 123456 |
(static) repeat(n, f, aopt) → {Array}
Repeats the function f(i, a) i in 0, n-1. a is an optional array, default a new empty Array returns a, only needed if f() places data in a
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
n | number | An integer number of times to run f() | ||
f | function | The function called. | ||
a | Array | <optional> | [] | An optional array for use by f() |
The result of calling f() n times
- Type:
- Array
(static) sampleModel(model) → {Object}
Return an object with samples of the models components. Useful for testing Models without needing a View, just data.
Name | Type | Description |
---|---|---|
model | Model | A model to sample |
An object with all the samples
- Type:
- Object
(static) setCtxImage(ctx, img)
Fill this context with the given image, resizing it to img size if needed.
Name | Type | Description |
---|---|---|
ctx | Context2D | a canvas 2D context |
img | Image | the Image to install in this ctx |
(static) subtractHeadings(head1, head0) → {number}
Subtract two headings, returning the smaller difference.
Computes the difference between the given headings, that is, the number of degrees in the smallest angle by which heading2 could be rotated to produce heading1 See NetLogo's subtract-headings for explanation
Name | Type | Description |
---|---|---|
head1 | number | The first heading in degrees |
head0 | number | The second heading in degrees |
The smallest andle from head0 to head1
- Type:
- number
(static) timeoutLoop(fcn, stepsopt, msopt)
Use pause for an animation loop. Calls the fcn each step Stops after steps calls, negative means run forever
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
fcn | function | The function to be called. | ||
steps | number | <optional> | -1 | How many times. |
ms | number | <optional> | 0 | Number of ms between calls. |
(static) toWindow(obj)
Merge a module's obj key/val pairs into to the global/window namespace. Primary use is to make console logging easier when debugging modules.
Name | Type | Description |
---|---|---|
obj | Object | Object who's key/val pairs will be installed in window. |
(static) xhrPromise(url, typeopt, methodopt) → {any}
Return Promise for ajax/xhr data.
type: 'arraybuffer', 'blob', 'document', 'json', 'text'. method: 'GET', 'POST' use: xhrPromise('./path/to/data').then(data => dataFcn(data))
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
url | UTL | A URL path to the data to be retrieved | ||
type | string | <optional> | 'text' | The type of the data |
method | string | <optional> | 'GET' | The retrieval method |
The resulting data of the given type
- Type:
- any