Skip to content

HTML5 Canvas, Web Storage, Geolocation API

HTML5 browser APIs — Canvas 2D drawing, Web Storage for client-side key/value data, and Geolocation.

3 classes · 13 methods

Canvas

5 methods

The Canvas 2D rendering context for drawing shapes, text, and images programmatically.

canvas.getContext('2d') -> CanvasRenderingContext2D | null

Returns the 2D rendering context used for drawing on the canvas.

Returns

CanvasRenderingContext2D | null

Example

html5
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 100, 50);
ctx.fillRect(x, y, width, height)

Paints a filled rectangle using the current fillStyle.

Parameters

NameTypeDescription
xnumberX coordinate of the top-left corner.
ynumberY coordinate of the top-left corner.
widthnumberRectangle width.
heightnumberRectangle height.

Returns

void

Example

html5
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 50, 50);
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise?)

Adds a circular arc to the current sub-path. Call fill() or stroke() afterward to render.

Parameters

NameTypeDescription
xnumberCenter X.
ynumberCenter Y.
radiusnumberArc radius.
startAnglenumberStart angle in radians.
endAnglenumberEnd angle in radians.

Returns

void

Example

html5
ctx.beginPath();
ctx.arc(75, 75, 50, 0, Math.PI * 2);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.fillText(text, x, y, maxWidth?)

Draws filled text at the given coordinates using the current font and fillStyle.

Parameters

NameTypeDescription
textstringText to draw.
xnumberX coordinate.
ynumberY coordinate (baseline).

Returns

void

Example

html5
ctx.font = '24px sans-serif';
ctx.fillText('Hello', 20, 40);
ctx.drawImage(image, dx, dy, dw?, dh?)

Draws an image, canvas, or video onto the canvas at the given position and size.

Parameters

NameTypeDescription
imageImage | Canvas | VideoSource image to draw.
dxnumberDestination X.
dynumberDestination Y.
dwnumberOptional destination width.
dhnumberOptional destination height.

Returns

void

Example

html5
const img = new Image();
img.onload = () => ctx.drawImage(img, 0, 0, 100, 100);
img.src = 'logo.png';

Storage

5 methods

Web Storage API (localStorage and sessionStorage) for synchronous key/value persistence scoped per origin.

storage.setItem(key, value)

Adds or updates a key/value pair. Both key and value must be strings.

Parameters

NameTypeDescription
keystringKey name.
valuestringValue to store.

Returns

void

Example

html5
localStorage.setItem('theme', 'dark');
sessionStorage.setItem('token', 'abc');
storage.getItem(key) -> string | null

Returns the value for the key, or null if the key does not exist.

Parameters

NameTypeDescription
keystringKey name.

Returns

string | null

Example

html5
const theme = localStorage.getItem('theme');
console.log(theme); // 'dark'
storage.removeItem(key)

Removes the key and its value from storage.

Parameters

NameTypeDescription
keystringKey name.

Returns

void

Example

html5
localStorage.removeItem('theme');
storage.clear()

Removes all key/value pairs from the storage area.

Returns

void

Example

html5
sessionStorage.clear();
storage.key(index) -> string | null

Returns the name of the key at the given index, useful for iterating storage.

Parameters

NameTypeDescription
indexnumberZero-based index.

Returns

string | null

Example

html5
for (let i = 0; i < localStorage.length; i++) {
  console.log(localStorage.key(i));
}

Geolocation

3 methods

The navigator.geolocation API for obtaining the user's geographic position.

navigator.geolocation.getCurrentPosition(success, error?, options?)

Gets the device's current position once. Requires a secure context (HTTPS) and user permission.

Parameters

NameTypeDescription
success(position: GeolocationPosition) => voidCallback receiving the position.
error(err: GeolocationPositionError) => voidOptional error callback.
optionsPositionOptionsOptional { enableHighAccuracy, timeout, maximumAge }.

Returns

void

Example

html5
navigator.geolocation.getCurrentPosition(
  pos => console.log(pos.coords.latitude, pos.coords.longitude),
  err => console.error(err.code, err.message)
);
navigator.geolocation.watchPosition(success, error?, options?) -> number

Registers a handler called automatically each time the position changes. Returns a watch ID.

Parameters

NameTypeDescription
success(position: GeolocationPosition) => voidCallback receiving each new position.
error(err: GeolocationPositionError) => voidOptional error callback.

Returns

number (watch ID)

Example

html5
const id = navigator.geolocation.watchPosition(
  pos => console.log(pos.coords)
);
navigator.geolocation.clearWatch(id)

Unregisters a position watcher previously installed with watchPosition.

Parameters

NameTypeDescription
idnumberWatch ID returned by watchPosition.

Returns

void

Example

html5
navigator.geolocation.clearWatch(id);