Properties
5 methodsCSS3 引入的布局和变换属性,用于弹性盒布局、网格布局和 2D/3D 变换。
canvas.getContext('2d') -> CanvasRenderingContext2D | null建立弹性格式化上下文;子元素成为沿主轴/交叉轴排列的弹性项。
Returns
CanvasRenderingContext2D | null
Example
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
| Name | Type | Description |
|---|---|---|
| x | number | X coordinate of the top-left corner. |
| y | number | Y coordinate of the top-left corner. |
| width | number | Rectangle width. |
| height | number | Rectangle height. |
Returns
void
Example
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
| Name | Type | Description |
|---|---|---|
| x | number | Center X. |
| y | number | Center Y. |
| radius | number | Arc radius. |
| startAngle | number | Start angle in radians. |
| endAngle | number | End angle in radians. |
Returns
void
Example
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
| Name | Type | Description |
|---|---|---|
| text | string | Text to draw. |
| x | number | X coordinate. |
| y | number | Y coordinate (baseline). |
Returns
void
Example
ctx.font = '24px sans-serif';
ctx.fillText('Hello', 20, 40);ctx.drawImage(image, dx, dy, dw?, dh?)弹性项上 flex-grow、flex-shrink 和 flex-basis 的简写。
Parameters
| Name | Type | Description |
|---|---|---|
| image | Image | Canvas | Video | Source image to draw. |
| dx | number | Destination X. |
| dy | number | Destination Y. |
| dw | number | Optional destination width. |
| dh | number | Optional destination height. |
Returns
void
Example
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
| Name | Type | Description |
|---|---|---|
| key | string | Key name. |
| value | string | Value to store. |
Returns
void
Example
localStorage.setItem('theme', 'dark');
sessionStorage.setItem('token', 'abc');storage.getItem(key) -> string | null替换 CSS 自定义属性(变量)的值,可选未定义时的回退值。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | Key name. |
Returns
string | null
Example
const theme = localStorage.getItem('theme');
console.log(theme); // 'dark'storage.removeItem(key)沿指定方向创建带颜色停止点的线性渐变图像。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | Key name. |
Returns
void
Example
localStorage.removeItem('theme');storage.clear()创建从中心点辐射的径向渐变。
Returns
void
Example
sessionStorage.clear();storage.key(index) -> string | nullReturns the name of the key at the given index, useful for iterating storage.
Parameters
| Name | Type | Description |
|---|---|---|
| index | number | Zero-based index. |
Returns
string | null
Example
for (let i = 0; i < localStorage.length; i++) {
console.log(localStorage.key(i));
}Geolocation
3 methodsThe 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
| Name | Type | Description |
|---|---|---|
| success | (position: GeolocationPosition) => void | Callback receiving the position. |
| error | (err: GeolocationPositionError) => void | Optional error callback. |
| options | PositionOptions | Optional { enableHighAccuracy, timeout, maximumAge }. |
Returns
void
Example
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?) -> numberRegisters a handler called automatically each time the position changes. Returns a watch ID.
Parameters
| Name | Type | Description |
|---|---|---|
| success | (position: GeolocationPosition) => void | Callback receiving each new position. |
| error | (err: GeolocationPositionError) => void | Optional error callback. |
Returns
number (watch ID)
Example
const id = navigator.geolocation.watchPosition(
pos => console.log(pos.coords)
);navigator.geolocation.clearWatch(id)Unregisters a position watcher previously installed with watchPosition.
Parameters
| Name | Type | Description |
|---|---|---|
| id | number | Watch ID returned by watchPosition. |
Returns
void
Example
navigator.geolocation.clearWatch(id);