LittleJS Utility Classes and Functions
- Timer - tracks time automatically with support for pause and real-time modes
- Time formatting helper
- JSON file fetching
- File saving (text, canvas, data URLs)
- Native share dialog support
- Local storage save data management
- Gradient noise (1D and 2D)
- Source
Methods
(async, static) fetchJSON(url) → {Promise.<object>}
Fetches a JSON file from a URL and returns the parsed JSON object. Must be used with await!
| Name | Type | Description |
|---|---|---|
url | string | URL of JSON file |
- Source
- Type:
- Promise.<object>
(static) formatTime(t) → {string}
Formats seconds to mm:ss style for display purposes
| Name | Type | Description |
|---|---|---|
t | number | time in seconds |
- Source
- Type:
- string
(static) noise1D(x) → {number}
1D gradient noise — returns a smooth value in [0, 1] for any real x. Integer inputs land on deterministic lattice values; non-integer inputs are interpolated with smoothStep for C1 continuity.
| Name | Type | Description |
|---|---|---|
x | number |
- Source
- Type:
- number
(static) noise2D(x, y) → {number}
2D gradient noise — returns a smooth value in [0, 1] for any real (x, y).
| Name | Type | Description |
|---|---|---|
x | number | |
y | number |
- Source
- Type:
- number
(static) readSaveData(saveName, defaultSaveDataopt) → {Object}
Read save data from local storage
| Name | Type | Attributes | Description |
|---|---|---|---|
saveName | string | unique name for the game/save | |
defaultSaveData | Object | <optional> | default values for save |
- Source
- Type:
- Object
(static) saveCanvas(canvas, filenameopt, typeopt)
Save a canvas to disk
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
canvas | HTMLCanvasElement | | |||
filename | string | <optional> | screenshot | |
type | string | <optional> | image/png |
- Source
(static) saveDataURL(url, filenameopt, revokeTimeopt)
Save a data url to disk
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
url | string | |||
filename | string | <optional> | download | |
revokeTime | number | <optional> | how long before revoking the url |
- Source
(static) saveText(text, filenameopt, typeopt)
Save a text file to disk
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
text | string | |||
filename | string | <optional> | text | |
type | string | <optional> | text/plain |
- Source
(static) shareURL(title, url, callbackopt)
Share content using the native share dialog if available
| Name | Type | Attributes | Description |
|---|---|---|---|
title | string | title of the share | |
url | string | url to share | |
callback | function | <optional> | Called when share is complete |
- Source
(static) writeSaveData(saveName, saveData)
Write save data to local storage
| Name | Type | Description |
|---|---|---|
saveName | string | unique name for the game/save |
saveData | Object | object containing data to be saved |
- Source