本頁由機器翻譯,可能有錯誤。 閱讀英文原文

API

Canvas

Canvas 使用 HTML Canvas 2D 風格的 context 進行繪圖。你的 draw 函式會記錄繪圖內容,平台再以螢幕解析度原生重播這些內容(iOS 上使用 Core Graphics,Android 上使用 android.graphics.Canvas)。繪圖中的數字與顏色可以是動畫值:當它們產生動畫時,平台會自行重繪,無須再次執行 draw。

mod-canvas.tsx
mod-canvas.tsxLive
import { Canvas, mount, Path2D, signal, Text, View } from "nsh";
import { createAnimatedValue, Easing } from "nsh";
import { button, buttonText, Panel, Split, tint, ui, wide } from "./palette";

const TAU = Math.PI * 2;
const heart = new Path2D("M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6c-2.5 4.5-9.5 9-9.5 9z");

function Chip(props: { label: string; onPress: () => void }) {
  return (
    <View onPress={props.onPress} {...button()}>
      <Text style={buttonText()}>{props.label}</Text>
    </View>
  );
}

function App() {
  const data = signal([12, 30, 22, 48, 36, 60, 52, 75, 64, 88]);
  const progress = createAnimatedValue(0.3);
  const target = signal(0.3);
  const go = () => {
    const to = Math.round(Math.random() * 100) / 100;
    target.set(to);
    progress.timing(to, { duration: 900, easing: Easing.inOut(Easing.cubic) });
  };

  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 16 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>Canvas</Text>
      <Split
        gap={16}
        width={560}
        inspector={
          wide() ? (
            <>
              <Panel title="draw reads a signal">
                <View style={{ gap: 10 }}>
                  <Text style={{ fontSize: 13, color: ui().text, fontVariant: ["tabular-nums"] }}>[{data().join(", ")}]</Text>
                  <Chip label="New point" onPress={() => data.update((v) => [...v.slice(1), Math.round(Math.random() * 90) + 5])} />
                </View>
              </Panel>
              <Panel title="An animated value in draw">
                <View style={{ gap: 10 }}>
                  <Text style={{ fontSize: 13, color: ui().text, fontVariant: ["tabular-nums"] }}>{Math.round(target() * 100)}%</Text>
                  <Chip label="Animate ring" onPress={go} />
                </View>
              </Panel>
            </>
          ) : (
            <View style={{ flexDirection: "row", gap: 10 }}>
              <Chip label="New point" onPress={() => data.update((v) => [...v.slice(1), Math.round(Math.random() * 90) + 5])} />
              <Chip label="Animate ring" onPress={go} />
            </View>
          )
        }
      >
      <Canvas
        style={{ height: 170, borderRadius: 12, ...ui().inset }}
        draw={(cx, { width, height }) => {
          const values = data();
          const pad = 16;
          const w = width - pad * 2, h = height - pad * 2;
          const x = (i: number) => pad + (i / (values.length - 1)) * w;
          const y = (v: number) => pad + h - (v / 100) * h;
          const area = cx.createLinearGradient(0, pad, 0, pad + h);
          area.addColorStop(0, tint(ui().accent, 0.28));
          area.addColorStop(1, tint(ui().accent, 0));
          cx.beginPath();
          cx.moveTo(x(0), pad + h);
          values.forEach((v, i) => cx.lineTo(x(i), y(v)));
          cx.lineTo(x(values.length - 1), pad + h);
          cx.closePath();
          cx.fillStyle = area;
          cx.fill();
          cx.beginPath();
          values.forEach((v, i) => (i ? cx.lineTo(x(i), y(v)) : cx.moveTo(x(i), y(v))));
          cx.strokeStyle = ui().accent;
          cx.lineWidth = 2.5;
          cx.lineJoin = "round";
          cx.stroke();
        }}
      />
      <View style={{ flexDirection: "row", alignItems: "center", gap: 16 }}>
        <Canvas
          style={{ width: 110, height: 110 }}
          draw={(cx, { width, height }) => {
            const centerX = width / 2, cy = height / 2, r = width / 2 - 10;
            cx.lineWidth = 12;
            cx.lineCap = "round";
            cx.strokeStyle = ui().inset.backgroundColor;
            cx.beginPath();
            cx.arc(centerX, cy, r, 0, TAU);
            cx.stroke();
            const g = cx.createConicGradient(-Math.PI / 2, centerX, cy);
            g.addColorStop(0, ui().info);
            g.addColorStop(1, ui().accent);
            cx.strokeStyle = g;
            cx.beginPath();
            cx.arc(centerX, cy, r, -Math.PI / 2, progress.mul(TAU).sub(Math.PI / 2));
            cx.stroke();
            cx.fillStyle = ui().text;
            cx.font = "600 18px system-ui";
            cx.textAlign = "center";
            cx.textBaseline = "middle";
            cx.fillText(`${Math.round(target() * 100)}%`, centerX, cy);
          }}
        />
        <Canvas
          style={{ width: 96, height: 96 }}
          draw={(cx) => {
            cx.save();
            cx.scale(4, 4);
            cx.fillStyle = ui().down;
            cx.fill(heart);
            cx.restore();
          }}
        />
      </View>
      </Split>
    </View>
  );
}

mount(App);
import { Canvas, CanvasGradient, CanvasRenderingContext2D, Path2D } from "nsh";

請參閱 Canvas 取得使用指南,並參閱 <Canvas> 了解其 props。

#Canvas

function Canvas(props: CanvasProps): JSX.Element;

interface CanvasProps extends ViewProps {
  draw: (cx: CanvasRenderingContext2D, size: CanvasSize) => void;
}

interface CanvasSize {
  width: number;   // points
  height: number;
}

以 2D context 繪製的 view。它接受所有 view props(樣式、class、觸控處理器、無障礙、ref),另外還有:

Prop型別說明
draw(cx, { width, height }) => void繪製 canvas。在尺寸確定後執行,當它所讀取的 signal 變更時會再次執行,canvas 調整大小時也會再次執行。以數字或顏色傳入的動畫值會產生動畫,而不會重新執行它。

請透過樣式(width / height、flex: 1、aspectRatio 等)為 canvas 指定尺寸。座標以 point 為單位,原點位於左上角。

import { Canvas, mount, signal, Text, View } from "nsh";

function App() {
  const values = signal([12, 30, 22, 48, 36, 60, 52, 75]);
  return (
    <View style={{ flex: 1, padding: 24 }}>
      <Canvas
        style={{ height: 160 }}
        draw={(cx, { width, height }) => {
          const v = values();
          const step = width / (v.length - 1);
          cx.strokeStyle = "#6366f1";
          cx.lineWidth = 3;
          cx.lineJoin = "round";
          cx.beginPath();
          v.forEach((n, i) => (i ? cx.lineTo(i * step, height - n * 2) : cx.moveTo(0, height - n * 2)));
          cx.stroke();
        }}
      />
      <Text onPress={() => values.update((v) => [...v.slice(1), Math.random() * 80])}>Next</Text>
    </View>
  );
}

mount(App);

#不重繪的動畫

任何型別為 CanvasNumber 的引數,以及任何 paint(fillStyle、strokeStyle)都接受 AnimatedNode。此時 canvas 會在繪製時讀取該節點,並在其動畫的每一幀以原生方式重繪:

import { createAnimatedValue, Easing } from "nsh";
import { Canvas } from "nsh";

const progress = createAnimatedValue(0);

<Canvas
  style={{ width: 120, height: 120 }}
  draw={(cx, { width }) => {
    const c = width / 2;
    cx.lineWidth = 12;
    cx.lineCap = "round";
    cx.strokeStyle = "#6366f1";
    cx.beginPath();
    // The end angle is an animated node: animating `progress` redraws natively.
    cx.arc(c, c, c - 10, -Math.PI / 2, progress.mul(Math.PI * 2).sub(Math.PI / 2));
    cx.stroke();
  }}
/>;

progress.timing(0.75, { duration: 900, easing: Easing.inOut(Easing.cubic) });
type CanvasNumber = number | AnimatedNode;
type CanvasPaint = string | CanvasGradient | AnimatedNode;

#CanvasRenderingContext2D

draw 所接收的 context。呼叫只會記錄操作,平台會在 draw 返回後才進行繪製。

#狀態與變換

readonly canvas: { readonly width: number; readonly height: number };
save(): void;
restore(): void;
translate(x: CanvasNumber, y: CanvasNumber): void;
scale(x: CanvasNumber, y: CanvasNumber): void;
rotate(angle: CanvasNumber): void;
transform(a: CanvasNumber, b: CanvasNumber, c: CanvasNumber, d: CanvasNumber, e: CanvasNumber, f: CanvasNumber): void;
setTransform(a: CanvasNumber, b: CanvasNumber, c: CanvasNumber, d: CanvasNumber, e: CanvasNumber, f: CanvasNumber): void;
resetTransform(): void;
成員說明
canvascanvas 的尺寸,以 point 為單位。
save() / restore()推入/彈出繪圖狀態(變換、裁切、樣式)。
translate(x, y)移動原點。
scale(x, y)縮放座標軸。
rotate(angle)旋轉座標軸;單位為弧度,順時針。
transform(a, b, c, d, e, f)將目前的變換乘上矩陣 [a c e; b d f]。
setTransform(a, b, c, d, e, f)取代目前的變換。
resetTransform()還原為單位矩陣。

#屬性

屬性型別說明
fillStyleCanvasPaint供 fill、fillRect、fillText 使用的顏色、漸層或原生顏色節點。
strokeStyleCanvasPaint同上,用於描邊。
lineWidthCanvasNumber描邊寬度,以 point 為單位。
lineCap"butt" | "round" | "square"描邊的端點樣式。
lineJoin"miter" | "round" | "bevel"描邊線段的接合方式。
miterLimitnumber超過此長度(以線寬計)的斜接(miter)接合會改為斜切(bevel)。
lineDashOffsetCanvasNumber虛線樣式的起始位置。
globalAlphaCanvasNumber接下來繪製內容的不透明度,範圍 0..1。
globalCompositeOperationstring接下來繪製的內容如何與 canvas 合成("source-over"、"multiply" 等)。
shadowColorstring陰影的顏色(透明:無陰影)。
shadowBlurnumber陰影模糊程度,以 point 為單位。
shadowOffsetX、shadowOffsetYnumber陰影偏移量,以 point 為單位。
fontstringCSS font 簡寫,例如 "600 14px Inter"(尺寸可使用 px、pt 或 rem)。
textAlign"start" | "end" | "left" | "right" | "center"文字相對於其 x 的水平位置。
textBaseline"alphabetic" | "top" | "hanging" | "middle" | "ideographic" | "bottom"文字相對於其 y 的垂直位置。

虛線:setLineDash(segments: number[]) 與 getLineDash(): number[]。

#路徑

context 具有一個目前路徑,其建立方式與 Path2D 使用相同的方法:

beginPath(): void;
moveTo(x, y): void;
lineTo(x, y): void;
quadraticCurveTo(cpx, cpy, x, y): void;
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y): void;
arc(x, y, radius, startAngle, endAngle, counterclockwise?: boolean): void;
arcTo(x1, y1, x2, y2, radius): void;
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise?: boolean): void;
rect(x, y, w, h): void;
roundRect(x, y, w, h, radii?: CanvasNumber | CanvasNumber[]): void;
closePath(): void;

所有座標皆為 CanvasNumber。角度以弧度為單位,從 x 軸起順時針計算。roundRect 的 radii 可以是單一半徑,也可以如同 CSS border-radius 一樣給 2–4 個值(左上角在前)。

#繪製

fill(path?: Path2D | "nonzero" | "evenodd", rule?: "nonzero" | "evenodd"): void;
stroke(path?: Path2D): void;
clip(path?: Path2D | "nonzero" | "evenodd", rule?: "nonzero" | "evenodd"): void;
fillRect(x, y, w, h): void;
strokeRect(x, y, w, h): void;
clearRect(x, y, w, h): void;
方法說明
fill(path?, rule?)以 fillStyle 填滿目前路徑(或 path),採用非零環繞(預設)或奇偶規則。
stroke(path?)以 strokeStyle 描繪目前路徑(或 path)的邊線。
clip(path?, rule?)將接下來繪製的內容限制在該路徑內。
fillRect、strokeRect繪製矩形,不會影響目前路徑。
clearRect將矩形區域清為透明。

#文字

fillText(text: string, x: CanvasNumber, y: CanvasNumber, maxWidth?: number): void;
strokeText(text: string, x: CanvasNumber, y: CanvasNumber, maxWidth?: number): void;
measureText(text: string): TextMetrics; // { width: number }

measureText 是同步的:它使用平台的文字引擎與目前的 font 進行測量。只會回報 width。

cx.font = "11px system-ui";
const label = "100";
cx.fillText(label, 40 - cx.measureText(label).width, 20);

#漸層

createLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradient;
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradient;
createConicGradient(startAngle: number, x: number, y: number): CanvasGradient;

class CanvasGradient {
  addColorStop(offset: number, color: string): void; // offset 0..1, any CSS color
}
const g = cx.createLinearGradient(0, 0, 0, height);
g.addColorStop(0, "rgba(99, 102, 241, 0.4)");
g.addColorStop(1, "rgba(99, 102, 241, 0)");
cx.fillStyle = g;
cx.fillRect(0, 0, width, height);

#圖片

drawImage(image: string | ImageAsset, ...args: CanvasNumber[]): void;

繪製以 URL 或匯入資源(import logo from "./logo.png")提供的圖片。圖片會以原生方式載入,載入完成後 canvas 會再次繪製。匯入的資源會繪製匯入的檔案本身;請匯入 @3x 檔案,以便在所有螢幕上都能清晰繪製。與 HTML 相同,共有三種形式:

  • drawImage(image, dx, dy):以原始尺寸繪製。
  • drawImage(image, dx, dy, dw, dh):縮放至指定矩形內。
  • drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh):只繪製其中一部分(來源以圖片像素為單位)。

#Path2D

class Path2D {
  constructor(path?: Path2D | string);
  addPath(path: Path2D): void;
  // and every path method: moveTo, lineTo, quadraticCurveTo, bezierCurveTo,
  // arc, arcTo, ellipse, rect, roundRect, closePath
}

可重複使用的路徑,可用於填滿、描邊或裁切。建構函式會複製另一個 Path2D,或以原生方式解析 SVG 路徑資料:

const heart = new Path2D("M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6c-2.5 4.5-9.5 9-9.5 9z");
cx.save();
cx.scale(4, 4);
cx.fillStyle = "#ef4444";
cx.fill(heart);
cx.restore();

#不支援的功能

像素存取(getImageData、putImageData)、filter、isPointInPath 與 SVG 圖片皆不可用。與 HTML Canvas 相同,無效的顏色或字型會使該項操作被忽略。