On this page

Source
  - utils.js, line 1

## Methods

### (static) pause(msopt) → {Promise}

Return promise for pause of ms. Use: await pause(ms)

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`ms` | number | <optional> | 1000 | Number of ms to pause

Source
  - utils.js, line 87

Returns:

A promise to wait this number of ms

Type:
  Promise

### (static) timeoutLoop(fcn, stepsopt, msopt)

Use pause for an animation loop. Calls the fcn each step Stops after steps calls, negative means run forever

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`fcn` | function |  |  | The function to be called.
`steps` | number | <optional> | -1 | How many times, -1 means forever
`ms` | number | <optional> | 0 | Number of ms between calls.

Source
  - utils.js, line 102

### (static) randomInt(max) → {number}

Returns ramdp, int in [0, max), equal or grater than 0, less than max

Parameters:

Name | Type | Description
--- | --- | ---
`max` | number | The max integer to return

Source
  - utils.js, line 229

Returns:

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

Parameters:

Name | Type | Description
--- | --- | ---
`min` | number | The min integer to return
`max` | number | The max integer to return

Source
  - utils.js, line 240

Returns:

an integer in [min, max)

Type:
  number

### (static) randomFloat(max) → {number}

Returns a random float in [0, max)

Parameters:

Name | Type | Description
--- | --- | ---
`max` | number | The max float to return

Source
  - utils.js, line 250

Returns:

a float in [0, max)

Type:
  number

### (static) randomFloat2(min, max) → {number}

Returns a random float in [min, max)

Parameters:

Name | Type | Description
--- | --- | ---
`min` | number | The min float to return
`max` | number | The max float to return

Source
  - utils.js, line 261

Returns:

a float in [min, max)

Type:
  number

### (static) randomCentered(r) → {number}

Return a random float centered around r, in [-r/2, r/2)

Parameters:

Name | Type | Description
--- | --- | ---
`r` | number | The center float

Source
  - utils.js, line 270

Returns:

a float in [-r/2, r/2)

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.

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`seed` | number | <optional> | 123456 |

Source
  - utils.js, line 292

### (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

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`num` | number | Array |  |  | The number to convert/shorten
`digits` | number | <optional> | 4 | The number of decimal digits

Source
  - utils.js, line 310

Returns:

The resulting number

Type:
  number

### (static) mod(v, n) → {number}

A true modulus function, differing from the % remainder operation.

Parameters:

Name | Type | Description
--- | --- | ---
`v` | number | The value to calculate the modulus of
`n` | number | The number relative to which the modulus is calculated.

Source
  - utils.js, line 334

Returns:

The value of v mod n

Type:
  number

### (static) clamp(v, min, max) → {number}

Clamp a float to be between [min, max).

Parameters:

Name | Type | Description
--- | --- | ---
`v` | number | value to clamp between min & max
`min` | number | min value
`max` | number | max value

Source
  - utils.js, line 349

Returns:

a float between min/max

Type:
  number

### (static) degToRad(degrees) → {number}

Convert from degrees to radians

Parameters:

Name | Type | Description
--- | --- | ---
`degrees` | number | a value in degrees: in [0, 360)

Source
  - utils.js, line 385

Returns:

the value as radians: in [0, 2PI)

Type:
  number

### (static) radToDeg(radians) → {number}

Convert from radians to degrees

Parameters:

Name | Type | Description
--- | --- | ---
`radians` | number | a value in radians: in [0, 2PI)

Source
  - utils.js, line 394

Returns:

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

Parameters:

Name | Type | Description
--- | --- | ---
`radians` | number | a value in radians: in [0, 2PI)

Source
  - utils.js, line 430

Returns:

a value in degrees: in [0, 360)

Type:
  number

### (static) headingToRad(heading) → {number}

Convert from heading to radians

Parameters:

Name | Type | Description
--- | --- | ---
`heading` | number | a value in degrees: in [0, 360)

Source
  - utils.js, line 440

Returns:

a value in radians: in [0, 2PI)

Type:
  number

### (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

Parameters:

Name | Type | Description
--- | --- | ---
`head1` | number | The first heading in degrees
`head0` | number | The second heading in degrees

Source
  - utils.js, line 486

Returns:

The smallest andle from head0 to head1

Type:
  number

### (static) sampleModel(model) → {Object}

Return an object with samples of the models components. Useful for testing Models without needing a View, just data.

Parameters:

Name | Type | Description
--- | --- | ---
`model` | Model | A model to sample

Source
  - utils.js, line 622

Returns:

An object with all the samples

Type:
  Object

### (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

Parameters:

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()

Source
  - utils.js, line 698

Returns:

N results of calling f() n times

Type:
  Array

### (static) oneOf(An) → {any}

Return random item from an array

Parameters:

Name | Type | Description
--- | --- | ---
`An` | Array | array to choose from

Source
  - utils.js, line 756

Returns:

The chosen item

Type:
  any

### (static) imagePromise(url) → {Promise}

Return a Promise for getting an image. See https://deno.land/x/skia_canvas/mod.ts?s=Image for Image in deno

use: imagePromise('./path/to/img').then(img => imageFcn(img)) or: await imagePromise('./path/to/img')

Parameters:

Name | Type | Description
--- | --- | ---
`url` | string | URL for path to image

Source
  - utils.js, line 1083

Returns:

A promise resolving to the image

Type:
  Promise

### (static) createCanvas(width, height, preferDOMopt) → {Canvas}

Create a blank 2D canvas of a given width/height.

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`width` | number |  |  | The canvas height in pixels
`height` | number |  |  | The canvas width in pixels
`preferDOM` | boolean | <optional> | false | If false, return "Offscreen" canvas

Source
  - utils.js, line 1141

Returns:

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.

Parameters:

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

Source
  - utils.js, line 1163

Returns:

The resulting Canvas's 2D context

Type:
  Context2D

### (static) setCtxImage(ctx, img)

Fill this context with the given image, resizing it to img size if needed.

Parameters:

Name | Type | Description
--- | --- | ---
`ctx` | Context2D | a canvas 2D context
`img` | Image | the Image to install in this ctx

Source
  - utils.js, line 1330

### (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.

Parameters:

Name | Type | Description
--- | --- | ---
`obj` | Object | Object who's key/val pairs will be installed in window.

Source
  - utils.js, line 1344

Type anything to view search result