LittleJS 1.28.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:177

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

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

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:112

Formats seconds to mm:ss style for display purposes

t number

time in seconds

noise1D(x) → numberengineUtilities.js:328

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:339

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

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

Read save data from local storage

  • The result has the type of defaultSaveData, or any when there is none
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:195

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:215

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:166

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:234

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:282

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