LittleJS 1.31.0

Utilities

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
  • Value noise (1D and 2D)

Functions

Functions

createCanvasContext(width, height, willReadFrequently) → OffscreenCanvasRenderingContext2DengineUtilities.js:189

Create an offscreen canvas to draw into, and return its 2D context

  • The canvas is context.canvas, which is what TextureInfo and the like take
  • A browser with no OffscreenCanvas, Safari before 16.4, gets a canvas element that is not on the page
width number

In pixels

height number optional

In pixels, defaults to the width for a square

willReadFrequently boolean optional, default false

Keep it in software, faster when getImageData is called on it often

Returns

An HTMLCanvasElement's context where there is no OffscreenCanvas, as in Safari before 16.4, which has the same drawing calls

fetchJSON(url) → Promise<any>asyncengineUtilities.js:134

Fetches a JSON file from a URL and returns the parsed JSON object. Must be used with await!

url string

URL of JSON file

Returns
  • The parsed JSON, any shape, as response.json() gives it
formatTime(t) → stringengineUtilities.js:122

Formats seconds to mm:ss style for display purposes

t number

time in seconds

noise1D(x) → numberengineUtilities.js:441

1D value 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.

x number
noise2D(x, y) → numberengineUtilities.js:452

2D value noise — returns a smooth value in [0, 1] for any real (x, y)

x number
y number
readSaveData(saveName, defaultSaveData) → TengineUtilities.js:275

Read save data from local storage

  • The result has the type of defaultSaveData, or any when there is none
  • A top level value whose default is a vec2, a vec3 or a Color, a value with a copy method and only numbers, comes back as a new one, where JSON keeps only its numbers, a number not saved or not finite the default's; with nothing saved any value with a copy method is a copy of the default, so changing it leaves the default
  • A value whose default is a number, at the top level or inside an array or object default, comes back as the default when what was saved is not a number, as for Infinity, which JSON writes as null
  • An array or object default not saved comes back as a copy, a vec2 or a Color in it too, so changing it leaves the default; a key a nested object default has that the save has not, a setting added since, is the default's, and a saved array is the array that was saved, shorter or longer than its default
  • Inside an array or object a vec2 or a Color comes back as one too, from its saved numbers, and an object or array default saved as anything that is not one, an older save's number or a null, comes back as the default, so a value that may be none is best a default of null, but a vec2 or a Color, which over a null default comes back as plain numbers with no methods, is best its own default with a flag beside it; a value of a class with no copy method is the default's own when not saved, and as JSON gave it once saved
saveName string

unique name for the game/save

defaultSaveData T optional

default values, result is {...default, ...loaded} so this must be an object

saveCanvas(canvas, filename, type)engineUtilities.js:207

Save a canvas to disk

canvas HTMLCanvasElement|OffscreenCanvas
filename string optional, default screenshot
type string optional, default image/png
saveDataURL(url, filename, revokeTime)engineUtilities.js:227

Save a data url to disk

url string
filename string optional, default download
revokeTime number optional

how long before revoking the url

saveText(text, filename, type)engineUtilities.js:177

Save a text file to disk

text string
filename string optional, default text
type string optional, default text/plain
shareURL(title, url, callback)engineUtilities.js:246

Share content using the native share dialog if available

title string

title of the share

url string

url to share

callback function optional

Called when share is complete

writeSaveData(saveName, saveData) → booleanengineUtilities.js:395

Write save data to local storage, an object as readSaveData gives it back

saveName string

unique name for the game/save

saveData object

object containing data to be saved

Returns
  • Whether it was written, false when storage is unavailable or full