文档

画布

画布是一个用 HTML Canvas 2D API 的操作绘制的视图:图表、进度环、仪表盘、签名、自定义图标,凡是画出来比用视图拼出来更容易的东西都适合。在 Rust 中,canvas(..) 显示一个 Painter 录制的内容;在 TSX 中,nsh 的 Canvas 接收一个 draw 函数,一次性录制整幅绘制。平台以原生方式回放(iOS 上是 Core Graphics,Android 上是 android.graphics.Canvas),按屏幕分辨率绘制,所以在任何像素密度下都很清晰。

guide-canvas.tsx
guide-canvas.tsx运行中
import { Canvas, type GestureResponderEvent, mount, Path2D, signal, Text, View } from "nsh";
import { createAnimatedValue, Easing } from "nsh";
import { button, buttonText, Inspector, Panel, tint, ui, wide } from "./palette";

const TAU = Math.PI * 2;
const heart = new Path2D("M12 21s-7-4.35-9.5-8.5C.5 8.5 3 4 7 4c2 0 3.5 1 5 3 1.5-2 3-3 5-3 4 0 6.5 4.5 4.5 8.5C19 16.65 12 21 12 21z");

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

function App() {
  // The ring's sweep is an animated value: animating it redraws without running `draw`.
  const progress = createAnimatedValue(0.35);
  const target = signal(0.35);
  const go = () => {
    const to = Math.round(Math.random() * 100) / 100;
    target.set(to);
    progress.timing(to, { duration: 800, easing: Easing.inOut(Easing.cubic) });
  };

  // The chart reads a signal: changing it runs `draw` again.
  const data = signal([12, 30, 22, 48, 36, 60, 52, 75]);

  // Finger drawing through the responder system.
  const strokes = signal<[number, number][][]>([]);
  const point = (e: GestureResponderEvent): [number, number] => [e.locationX, e.locationY];

  const ring = () => (
    <Canvas
      style={{ width: 110, height: 110 }}
      draw={(cx, { width, height }) => {
        const centerX = width / 2;
        const cy = height / 2;
        const r = width / 2 - 8;
        cx.lineWidth = 10;
        cx.lineCap = "round";
        cx.strokeStyle = ui().border;
        cx.beginPath();
        cx.arc(centerX, cy, r, 0, TAU);
        cx.stroke();
        const g = cx.createConicGradient(-Math.PI / 2, centerX, cy);
        g.addColorStop(0, ui().up);
        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);
      }}
    />
  );
  const heartCanvas = () => (
    <Canvas
      style={{ width: 48, height: 48 }}
      draw={(cx) => {
        cx.scale(2, 2);
        cx.shadowColor = tint(ui().down, 0.45);
        cx.shadowBlur = 4;
        cx.fillStyle = ui().down;
        cx.fill(heart);
      }}
    />
  );
  const chart = (height: number) => (
    <Canvas
      style={{ ...ui().inset, height, borderRadius: 12 }}
      draw={(cx, { width, height }) => {
        const values = data();
        const pad = 16;
        const x = (i: number) => pad + (i / (values.length - 1)) * (width - 2 * pad);
        const y = (v: number) => height - pad - (v / 100) * (height - 2 * pad);
        const area = cx.createLinearGradient(0, pad, 0, height - pad);
        area.addColorStop(0, tint(ui().accent, 0.28));
        area.addColorStop(1, tint(ui().accent, 0));
        cx.beginPath();
        values.forEach((v, i) => (i === 0 ? cx.moveTo(x(i), y(v)) : cx.lineTo(x(i), y(v))));
        cx.lineTo(x(values.length - 1), height - pad);
        cx.lineTo(x(0), height - pad);
        cx.closePath();
        cx.fillStyle = area;
        cx.fill();
        cx.beginPath();
        values.forEach((v, i) => (i === 0 ? cx.moveTo(x(i), y(v)) : cx.lineTo(x(i), y(v))));
        cx.strokeStyle = ui().accent;
        cx.lineWidth = 2.5;
        cx.lineJoin = "round";
        cx.stroke();
      }}
    />
  );
  const newData = () => <Button label="New data" onPress={() => data.set(data().map(() => Math.round(10 + Math.random() * 85)))} />;
  const pad = () => (
    <Canvas
      style={{ ...ui().inset, flex: 1, borderRadius: 12 }}
      onStartShouldSetResponder={() => true}
      onResponderGrant={(e) => {
        strokes.set([...strokes(), [point(e)]]);
        return true;
      }}
      onResponderMove={(e) => {
        const all = strokes();
        strokes.set([...all.slice(0, -1), [...all[all.length - 1], point(e)]]);
      }}
      draw={(cx, { width }) => {
        cx.fillStyle = ui().faint;
        cx.font = "13px system-ui";
        cx.textAlign = "center";
        cx.fillText("Draw here", width / 2, 20);
        cx.strokeStyle = ui().text;
        cx.lineWidth = 3;
        cx.lineCap = "round";
        cx.lineJoin = "round";
        for (const s of strokes()) {
          cx.beginPath();
          s.forEach(([px, py], i) => (i === 0 ? cx.moveTo(px, py) : cx.lineTo(px, py)));
          cx.stroke();
        }
      }}
    />
  );

  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 16, paddingTop: 24, gap: 14 }}>
      {() =>
        wide() ? (
          <View style={{ flex: 1, flexDirection: "row", gap: 16 }}>
            <View style={{ flex: 1, minWidth: 0, gap: 14 }}>
              {chart(200)}
              {pad()}
            </View>
            <Inspector>
              <Panel title="An animated value in draw">
                <View style={{ gap: 10 }}>
                  <View style={{ flexDirection: "row", alignItems: "center", gap: 16 }}>
                    {ring()}
                    {heartCanvas()}
                  </View>
                  <Button label="Animate the ring" onPress={go} />
                </View>
              </Panel>
              <Panel title="A signal in draw">
                <View style={{ gap: 10 }}>
                  <Text style={{ fontSize: 13, color: ui().text, fontVariant: ["tabular-nums"] }}>[{data().join(", ")}]</Text>
                  {newData()}
                </View>
              </Panel>
              <Panel title="Responder events">
                <View style={{ gap: 10 }}>
                  <Text style={{ fontSize: 13, color: ui().text, fontVariant: ["tabular-nums"] }}>
                    {strokes().length} strokes · {strokes().reduce((n, s) => n + s.length, 0)} points
                  </Text>
                  <Button label="Clear" onPress={() => strokes.set([])} />
                </View>
              </Panel>
            </Inspector>
          </View>
        ) : (
          <>
            <View style={{ flexDirection: "row", alignItems: "center", gap: 16 }}>
              {ring()}
              <View style={{ gap: 10 }}>
                <Button label="Animate the ring" onPress={go} />
                {heartCanvas()}
              </View>
            </View>
            {chart(150)}
            {newData()}
            {pad()}
          </>
        )
      }
    </View>
  );
}

mount(App);
import { Canvas, mount, View } from "nsh";
import { Window } from "nsh/desktop";

function App() {
  return (
    <View style={{ flex: 1, padding: 24, paddingTop: 60 }}>
      <Canvas
        style={{ height: 200, borderRadius: 12, backgroundColor: "#ffffff" }}
        draw={(cx, { width, height }) => {
          cx.fillStyle = "#0969da";
          cx.beginPath();
          cx.arc(width / 2, height / 2, 60, 0, Math.PI * 2);
          cx.fill();
          cx.strokeStyle = "#1f2328";
          cx.lineWidth = 4;
          cx.strokeRect(20, 20, width - 40, height - 40);
        }}
      />
    </View>
  );
}

mount(() => (
  <Window id="main">
    <App />
  </Window>
));

画布接受视图的一切(样式、类、触摸事件、无障碍、ref),再加上它的绘制。它需要从样式或布局中得到尺寸;它的内容就是绘制结果。坐标以点为单位,原点在左上角。

#绘制何时生成

在 Rust 中,canvas(move || ..) 在绑定中录制:闭包运行一次,它读取过的 signal 改变时再运行。依赖画布尺寸的绘制把 .on_layout(..) 得到的尺寸存在 signal 中并读取它,如上例所示。在 TSX 中,draw(cx, { width, height }) 会在以下时机运行:

  • 画布尺寸确定后(第一次布局之后);
  • 画布尺寸改变时;
  • 它读取过的 signal 改变时。

每次运行都会替换整幅绘制。在绘制时读取 signal,画布就能跟随你的数据:

const values = signal([12, 30, 22, 48, 36, 60]);

<Canvas
  style={{ height: 160 }}
  draw={(cx, { width, height }) => {
    const v = values();
    const x = (i: number) => (i / (v.length - 1)) * width;
    const y = (n: number) => height - (n / 100) * height;
    cx.beginPath();
    v.forEach((n, i) => (i === 0 ? cx.moveTo(x(i), y(n)) : cx.lineTo(x(i), y(n))));
    cx.strokeStyle = "#3b82f6";
    cx.lineWidth = 2.5;
    cx.lineJoin = "round";
    cx.stroke();
  }}
/>

绘制调用只是录制:录制完成后由平台进行绘制,所以不会读回像素。录制结果作为一个操作发送给平台。

#2D 操作

这些操作是 HTML Canvas 2D API 的一个子集:TSX 中是 CanvasRenderingContext2D,Rust 中是 Painter 的方法,使用 Rust 风格的名称(begin_path、fill_rect、line_width、set_transform、draw_image 等;颜色通过 fill_color / stroke_color,渐变通过 fill_style / stroke_style)。支持的内容:

状态

成员 Rust 说明
save()、restore() .save()、.restore() 压入和弹出绘制状态(样式、变换、裁剪)。
fillStyle、strokeStyle .fill_color(..)、.fill_style(..)、.stroke_color(..)、.stroke_style(..) CSS 颜色、渐变,或插值为颜色的动画值。
lineWidth、lineCap、lineJoin、miterLimit .line_width(..)、.line_cap(..)、.line_join(..)、.miter_limit(..) 描边形状。
setLineDash(segments)、getLineDash()、lineDashOffset .line_dash(..)、.line_dash_offset(..) 虚线。
globalAlpha .global_alpha(..) 后续绘制的不透明度,0..1。
globalCompositeOperation .composite(Composite::DestinationOut) 合成方式:"source-over"、"destination-out"、"multiply"、"screen" 等。
shadowColor、shadowBlur、shadowOffsetX、shadowOffsetY .shadow(color, blur, offset) 阴影。
font .font(..) CSS font 简写:"600 14px Inter"(尺寸可用 px、pt 或 rem);small-caps 和 tabular-nums、oldstyle-nums 等数字形式可以写在尺寸之前。
fontVariantNumeric .font_variant_numeric(..) "tabular-nums" 让所有数字等宽,变化的数字(计数器、价格、坐标轴标签)不会跳动;还有 "proportional-nums"、"oldstyle-nums"、"lining-nums"、"normal"。这是 NSH 新增的(即 CSS 的 font-variant-numeric);设置 font 时会保留它。浏览器会忽略它。
textAlign、textBaseline .text_align(..)、.text_baseline(..) 文字位置。

变换:translate(x, y)、scale(x, y)、rotate(angle)(弧度,顺时针)、transform(a, b, c, d, e, f)、setTransform(a, b, c, d, e, f)、resetTransform()。

路径:beginPath()、moveTo、lineTo、quadraticCurveTo、bezierCurveTo、arc、arcTo、ellipse、rect、roundRect(x, y, w, h, radii)、closePath();然后是 fill(rule?)、stroke() 和 clip(rule?),它们也都接受一条路径(Rust 中的 fill_path、stroke_path、clip_path)。填充规则为 "nonzero" 和 "evenodd"。

矩形:fillRect、strokeRect、clearRect。

文字:fillText(text, x, y, maxWidth?)、strokeText(...)、measureText(text)(由平台的文字引擎按当前 font 同步测量:width,以及这一行字体在基线上方、下方的高度 fontBoundingBoxAscent、fontBoundingBoxDescent)。在 Rust 中,measure_text(font, text) 给出宽度,text_metrics(font, text) 给出全部。

命中测试:isPointInPath(path?, x, y, rule?) 和 isPointInStroke(path?, x, y)(Painter 上的 path_contains、stroke_contains、is_point_in_path 和 is_point_in_stroke)同步给出结果,依据录制的几何信息(当前变换、路径、lineWidth 和 lineCap)计算。保留绘制和画过的路径,就能找出点击位置下的形状:

let cx: CanvasRenderingContext2D | undefined;
const slices = [new Path2D("M50 50 L100 50 A50 50 0 0 1 50 100 Z"), /* ... */];

<Canvas
  style={{ width: 100, height: 100 }}
  onStartShouldSetResponder={() => true}
  onResponderRelease={(e) => {
    const hit = slices.findIndex((p) => cx?.isPointInPath(p, e.locationX, e.locationY));
    if (hit >= 0) select(hit);
  }}
  draw={(c) => {
    cx = c;
    slices.forEach((p) => c.fill(p));
  }}
/>

描边按圆角连接、不带虚线进行测试;以动画值给出的坐标按 0 计算。

渐变:createLinearGradient、createRadialGradient、createConicGradient,都通过 addColorStop(offset, color) 添加色标(Rust 中的 CanvasGradient::linear、radial、conic 和 add_color_stop)。

图片:drawImage(image, dx, dy)、drawImage(image, dx, dy, dw, dh)、drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)(Rust 中的 draw_image(url, dest) 和 draw_image_part(..))。

在 TSX 中,cx.canvas.width 和 cx.canvas.height 给出画布以点为单位的尺寸。

支持的范围:绘制操作。像素由平台写入,因此 getImageData / putImageData 等像素访问不在其中,filter 也不在其中。与 HTML Canvas 一样,无效的颜色或字体只会让那一个操作被丢弃。

#路径与 SVG 路径数据

构建一次的路径可以多次填充、描边或裁剪:Rust 中是 svg_path(d),TSX 中是 Path2D。两者都接受 SVG 路径数据,在原生端解析,用来画图标很方便:

import { Canvas, Path2D } from "nsh";

const heart = new Path2D("M12 21s-7-4.35-9.5-8.5C.5 8.5 3 4 7 4c2 0 3.5 1 5 3 1.5-2 3-3 5-3 4 0 6.5 4.5 4.5 8.5C19 16.65 12 21 12 21z");

function Heart(props: { size: number; color: string }) {
  return (
    <Canvas
      style={{ width: props.size, height: props.size }}
      draw={(cx) => {
        cx.scale(props.size / 24, props.size / 24); // 路径画在 24 点的网格上
        cx.fillStyle = props.color;
        cx.fill(heart);
      }}
    />
  );
}

Path2D 拥有与上下文相同的路径方法(moveTo、arc、roundRect 等)以及 addPath(other)。new Path2D(other) 复制一条路径。

#渐变

<Canvas
  style={{ height: 120 }}
  draw={(cx, { width, height }) => {
    const linear = cx.createLinearGradient(0, 0, width, 0);
    linear.addColorStop(0, "#22d3ee");
    linear.addColorStop(1, "#6366f1");
    cx.fillStyle = linear;
    cx.fillRect(0, 0, width, height / 2);

    const radial = cx.createRadialGradient(width / 2, height * 0.75, 4, width / 2, height * 0.75, 40);
    radial.addColorStop(0, "#fde68a");
    radial.addColorStop(1, "#f97316");
    cx.fillStyle = radial;
    cx.beginPath();
    cx.arc(width / 2, height * 0.75, 28, 0, Math.PI * 2);
    cx.fill();
  }}
/>

锥形渐变(createConicGradient(startAngle, x, y)、CanvasGradient::conic(start_angle, center))让颜色绕一个点旋转过渡;起始角以弧度表示,从 x 轴顺时针计算。把锥形渐变用在描边的圆弧上,就是渐变色的进度环。

#文字

cx.font = "600 20px system-ui";
cx.fillStyle = "#1f2328";
cx.textAlign = "center";
cx.textBaseline = "middle";
cx.fillText("72%", width / 2, height / 2);

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

字体族与样式中的相同:系统字体族以及应用发布的字体名称(见字体)。

#图片

在 Rust 中,绘制图片接受 URL(资源的 URL 是 LOGO.url);在 TSX 中接受 URL 或导入的资源(import logo from "./logo.png")。图片在原生端加载,加载完成后画布会重新绘制,你不需要自己跟踪加载状态。远程 URL 受应用的网络能力声明约束。

iOS、macOS、Android、Windows、Linux 和 GPUI 上的画布既绘制位图,也绘制 SVG:SVG 由 nsh_core::svg 栅格化为位图后交给画布,栅格尺寸由加载器按 SVG 的固有尺寸(点)分档决定。请把它绘制到明确的目标矩形中,不要依赖栅格位图的像素尺寸。把它放大很多倍绘制会放大位图而显得模糊;要得到清晰的边缘和文字,请按固有尺寸或更小绘制。在 Web 上,画布绘制浏览器能绘制的内容,包括 SVG。

import { Canvas } from "nsh";
import logo from "./logo.png";

<Canvas
  style={{ width: 120, height: 120 }}
  draw={(cx) => {
    cx.save();
    cx.beginPath();
    cx.arc(60, 60, 56, 0, Math.PI * 2);
    cx.clip(); // 圆形头像
    cx.drawImage(logo, 4, 4, 112, 112);
    cx.restore();
  }}
/>

九个参数的形式(draw_image_part)中,源矩形以图片的像素为单位。

#用动画值驱动绘制

绘制调用中的任何数字参数,以及填充和描边的颜料,都可以是动画值或由它派生的节点。这类参数在平台绘制时才被读取;当动画值在动画中时,平台会自行逐帧重绘,无需重新录制绘制,也不运行你的任何代码。

import { Canvas, mount, signal, Text, View } from "nsh";
import { createAnimatedValue, Easing } from "nsh";
import { Window } from "nsh/desktop";

function Ring() {
  const progress = createAnimatedValue(0.3);
  const label = signal(30);
  const go = (to: number) => {
    label.set(Math.round(to * 100));
    progress.timing(to, { duration: 900, easing: Easing.inOut(Easing.cubic) });
  };
  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center", gap: 16 }}>
      <Canvas
        style={{ width: 140, height: 140 }}
        draw={(cx, { width, height }) => {
          const centerX = width / 2, cy = height / 2, r = width / 2 - 10;
          cx.lineWidth = 12;
          cx.lineCap = "round";
          cx.strokeStyle = "#e5e7eb";
          cx.beginPath();
          cx.arc(centerX, cy, r, 0, Math.PI * 2);
          cx.stroke();
          cx.strokeStyle = "#6366f1";
          cx.beginPath();
          // 终止角在每一帧读取动画值。
          cx.arc(centerX, cy, r, -Math.PI / 2, progress.mul(Math.PI * 2).sub(Math.PI / 2));
          cx.stroke();
          cx.fillStyle = "#1f2328";
          cx.font = "600 22px system-ui";
          cx.textAlign = "center";
          cx.textBaseline = "middle";
          cx.fillText(`${label()}%`, centerX, cy);
        }}
      />
      <View onPress={() => go(Math.random())}>
        <Text style={{ color: "#0969da" }}>Animate</Text>
      </View>
    </View>
  );
}

mount(() => (
  <Window id="main">
    <Ring />
  </Window>
));

圆弧的终止角是 progress.mul(2π).sub(π/2):它是一个节点,所以进度环会在 900 毫秒内以原生方式扫过,而绘制只录制了一次(标签读取了 signal,因此再录制一次)。时钟的秒针也是同样的做法:rotate(seconds.mul(π / 30)),其中 seconds 做线性动画。

颜色也可以这样动起来:Rust 中为 .fill_style(&t.interpolate_colors(&[0.0, 1.0], &[rgb(0x54aeff), rgb(0xcf222e)])),TSX 中为 cx.fillStyle = t.interpolate({ inputRange: [0, 1], outputRange: ["#54aeff", "#cf222e"] })。

#画布上的触摸

画布是一个视图,因此接受按压事件和手势响应者系统。手写绘图就是把点记录到 signal 中再画出来:

const strokes = signal<[number, number][][]>([]);

<Canvas
  style={{ height: 240 }}
  onStartShouldSetResponder={() => true}
  onResponderGrant={(e) => {
    strokes.set([...strokes(), [[e.locationX, e.locationY]]]);
    return true; // 外层的滚动视图不能接管触摸
  }}
  onResponderMove={(e) => {
    const all = strokes();
    strokes.set([...all.slice(0, -1), [...all[all.length - 1], [e.locationX, e.locationY]]]);
  }}
  draw={(cx) => {
    cx.lineWidth = 3;
    cx.lineCap = "round";
    cx.lineJoin = "round";
    for (const s of strokes()) {
      cx.beginPath();
      s.forEach(([x, y], i) => (i === 0 ? cx.moveTo(x, y) : cx.lineTo(x, y)));
      cx.stroke();
    }
  }}
/>

#性能

  • 绘制在 Rust 中录制(在 TSX 中是 JS 调用上下文时),并作为一个操作发送给平台;不存在每次调用都跨越一次桥的开销。
  • 每次绘制都重新录制全部内容。让工作量与显示的内容相称,把每帧都变化的数据放在动画值而不是 signal 中。
  • 如果一幅绘制既有静态部分又有频繁变化的部分,把它拆成上下叠放的两个画布,这样只有一个需要重绘。
  • 在可缩放的滚动视图中,缩放结束后画布会按缩放后的分辨率重新绘制。