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)

Methods

(static) createCanvasContext(width, heightopt, willReadFrequentlyopt) → {OffscreenCanvasRenderingContext2D}

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
Parameters:
NameTypeAttributesDefaultDescription
widthnumber

In pixels

heightnumber<optional>

In pixels, defaults to the width for a square

willReadFrequentlyboolean<optional>
false

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

Returns:
Type: 
OffscreenCanvasRenderingContext2D

(async, static) fetchJSON(url) → {Promise.<object>}

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

Parameters:
NameTypeDescription
urlstring

URL of JSON file

Returns:
Type: 
Promise.<object>

(static) formatTime(t) → {string}

Formats seconds to mm:ss style for display purposes

Parameters:
NameTypeDescription
tnumber

time in seconds

Returns:
Type: 
string

(static) noise1D(x) → {number}

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.

Parameters:
NameTypeDescription
xnumber
Returns:
Type: 
number

(static) noise2D(x, y) → {number}

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

Parameters:
NameTypeDescription
xnumber
ynumber
Returns:
Type: 
number

(static) readSaveData(saveName, defaultSaveDataopt) → {T}

Read save data from local storage

  • The result has the type of defaultSaveData, or any when there is none
Parameters:
NameTypeAttributesDescription
saveNamestring

unique name for the game/save

defaultSaveDataT<optional>

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

Returns:
Type: 
T

(static) saveCanvas(canvas, filenameopt, typeopt)

Save a canvas to disk

Parameters:
NameTypeAttributesDefaultDescription
canvasHTMLCanvasElement | OffscreenCanvas
filenamestring<optional>
screenshot
typestring<optional>
image/png

(static) saveDataURL(url, filenameopt, revokeTimeopt)

Save a data url to disk

Parameters:
NameTypeAttributesDefaultDescription
urlstring
filenamestring<optional>
download
revokeTimenumber<optional>

how long before revoking the url

(static) saveText(text, filenameopt, typeopt)

Save a text file to disk

Parameters:
NameTypeAttributesDefaultDescription
textstring
filenamestring<optional>
text
typestring<optional>
text/plain

(static) shareURL(title, url, callbackopt)

Share content using the native share dialog if available

Parameters:
NameTypeAttributesDescription
titlestring

title of the share

urlstring

url to share

callbackfunction<optional>

Called when share is complete

(static) writeSaveData(saveName, saveData)

Write save data to local storage

Parameters:
NameTypeDescription
saveNamestring

unique name for the game/save

saveDataobject

object containing data to be saved