Skip to content

CSS3 Canvas, Web Storage, Geolocation API

现代 CSS 属性(flexbox、grid、transforms)和内置函数(calc、var、gradients)。

3 classes · 13 methods

Properties

5 methods

CSS3 引入的布局和变换属性,用于弹性盒布局、网格布局和 2D/3D 变换。

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

建立弹性格式化上下文;子元素成为沿主轴/交叉轴排列的弹性项。

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)

建立网格格式化上下文,行和列由 grid-template-* 定义。

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?)

定义网格的列;接受长度、fr 单位、repeat()、minmax() 和 auto-fit/auto-fill。

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?)

应用 2D 或 3D 变换:translate、rotate、scale、skew、matrix 及其 3D 变体。

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?)

弹性项上 flex-grow、flex-shrink 和 flex-basis 的简写。

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';

Functions

5 methods

内置 CSS 值函数:calc 用于数学运算、var 用于自定义属性、gradient 用于生成渐变。

storage.setItem(key, value)

对混合单位(长度、百分比、角度)执行 + - * / 运算。

Parameters

NameTypeDescription
keystringKey name.
valuestringValue to store.

Returns

void

Example

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

替换 CSS 自定义属性(变量)的值,可选未定义时的回退值。

Parameters

NameTypeDescription
keystringKey name.

Returns

string | null

Example

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

沿指定方向创建带颜色停止点的线性渐变图像。

Parameters

NameTypeDescription
keystringKey name.

Returns

void

Example

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

创建从中心点辐射的径向渐变。

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);