• 简体中文
  • CharacterStageProvider

    让外观相同的角色共享同一份动画。

    import { CharacterStageProvider, StagedMark, COLOR_IDS } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <CharacterStageProvider>
          <div className="site-demo">
            {Array.from({ length: 44 }, (_, i) => (
              <StagedMark
                key={i}
                shape="pebble"
                state={i % 3 === 0 ? 'happy' : i % 3 === 1 ? 'thinking' : 'idle'}
                color={COLOR_IDS[i % COLOR_IDS.length]}
                sizePx={34}
              />
            ))}
          </div>
        </CharacterStageProvider>
      );
    }

    上面有四十四个角色,只运行三个引擎。

    它怎么工作

    Provider 为每一种外观保留一个隐藏角色,每个可见实例都是指向它的 <use href="#…">。浏览器会直接复用源在当前帧渲染的内容,所以一个镜像只占一个元素,不需要任何动画计算。

    外观是形状、状态、脸型和笔

    key 由形状、状态、脸型、鼻子、线宽、强调和暂停组成,墨色和尺寸被有意排除在外。带 hud 的角色会完全离开舞台,自己运行一个引擎:表盘是实时读数,如果按读数的值生成 key,每一帧都会产生一个新源。

    脸型和鼻子计入 key,是因为镜像显示的就是源画出来的内容:线条脸的角色不能借用另一种脸的源。两个鼻子不同的线条脸 blob 是两个源。默认脸上的鼻子没有意义,所以不计入 key,也不会拆分舞台。

    墨色是 CSS 自定义属性,而可继承的自定义属性在使用处解析,不在定义处解析,所以每个镜像都可以有不同的墨色,同时共享同一份动画。尺寸设在外层元素上,因此也可以任意变化。

    这是整个设计的核心,也很容易搞错。按智能体身份给舞台建索引看似自然,因为应用手里拿的正是智能体列表。但这样什么也共享不了:每个智能体按定义都是唯一的,五十个智能体就要五十个引擎。按外观建索引,五十个智能体都是 idle 状态的 blob,也只需要一个引擎。

    上限

    maxSources 限制同时运行的引擎数,默认为 36。超出上限后,认领仍会被记录,但不再托管新的源。页面因此是让一部分角色静止,而不是让所有角色都变慢。

    认领会合并

    滚动时,IntersectionObserver 可能在一帧内产生几十次认领。如果逐次提交,Provider 会在主线程最忙的时候重新渲染几十次,所以认领会合并,每帧只更新一次。

    源的尺寸

    隐藏的源按 96 像素渲染,而不是缩到零。测量自身元素的代码读到的是源的盒子,不是镜像的盒子,所以如果把源缩到几个像素,每个镜像都会表现得像自己很小。

    在 Provider 外面

    StagedMark 会回退为自己的 CharacterMark,所以使用它的组件可以单独工作,例如在测试或文档示例里,组件树上不需要有 Provider。

    style 与 ref

    style 最后应用,所以在其中设置的 --aora-fg 会覆盖由 color 推出的墨色。要给角色传一个可动画的自定义属性,而不是固定墨色,只能这样做,群组活动的墨色过渡就依赖它。

    ref 暴露的 CharacterHandle 与 CharacterMark 相同,但只有在角色自己拥有引擎时才有效,对 StagedMark 来说就是在 provider 之外。在 provider 之内,每个角色都是舞台隐藏源的镜像,与所有外观相同的角色共用一份动画,无法单独动作。这时句柄不会挂载,useTapCycle 会返回没有可戳的角色。

    import { useRef, type CSSProperties } from 'react';
    import { CharacterStageProvider, StagedMark, type CharacterHandle } from '@aora-bot/react';
    
    export default function Demo() {
      const own = useRef<CharacterHandle>(null);
      const mirror = useRef<CharacterHandle>(null);
      return (
        <div className="site-demo site-demo--center">
          <CharacterStageProvider>
            <StagedMark
              shape="hex"
              color="cyan"
              state="happy"
              sizePx={72}
              style={{ '--aora-fg': '#0ea5e9' } as CSSProperties}
            />
            <StagedMark ref={mirror} shape="hex" color="cyan" state="happy" sizePx={72} />
          </CharacterStageProvider>
          <StagedMark ref={own} shape="hex" color="cyan" state="happy" sizePx={72} />
          <button className="site-btn" onClick={() => (mirror.current ?? own.current)?.bounce()}>
            bounce
          </button>
        </div>
      );
    }

    第一个角色通过 style 改了墨色。第二个是镜像,句柄保持 null,所以按钮改用第三个角色,它在 provider 之外,运行自己的引擎。如果要让一整组角色一起动,请使用共享的角色指令,而不是逐个使用 ref。

    API

    属性类型默认值说明
    children*string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<AwaitedReactNode> | null——
    maxSourcesnumber36Cap on simultaneously running engines. Claims past the cap fall back to a static render rather than degrading every animation on the page.

    StagedMark

    属性类型默认值说明
    classNamestring——
    colorColorId——
    emotionIdstring—Protocol id, for hosts that speak the wire format. Ignored when `state` is given.
    emphasisbooleanfalseEnlarge and converge the eyes. Part of the shared appearance.
    eyeColorstring——
    face"symbol" | "capsule" | "cute" | "eyes" | "line" | "brow" | "glossy" | "sparkle" | "button" | "shades" | "droopy" | "bigmouth" | "pie" | "screen" | "teardrop" | "specs"—Face style. Part of the shared appearance, so line-face marks share a line-face source.
    hudHudSpec | nullnullA dial in the ring outside the body. `null` removes it. A HUD spec is a live readout whose value moves every tick, so unlike the other geometry props it cannot be folded into the appearance key — each new value would mint a source and orphan the last one. A mark with a HUD therefore leaves the stage and runs its own engine, which is what a per-instance readout needs anyway.
    lineWeightnumber—Multiplier on the shared line weight. Part of the shared appearance, so a heavier-pen mark gets its own source rather than mirroring a light one.
    nose"slant" | "angular" | "hook" | "long" | "curve" | "bow" | null—Nose for the line face. Unlike `CharacterMark`, the default here is `null` rather than seed-derived: mirrors of one source must agree on a nose, and "the seed's nose" differs per mark.
    pausedbooleanfalseHold the current frame. Part of the shared appearance.
    presetCastId—A named character from the cast. Supplies `shape`, `color`, `face` and `nose` when they are not given.
    refRef<CharacterHandle>—Flourishes, when this mark owns an engine. A mirror shares its animation with every other mark of the same appearance and so cannot move alone; the handle is simply never attached in that case, and `useTapCycle` reports back that there was nothing to poke.
    shapeShapeId——
    sizePxnumber32—
    state"idle" | "sleeping" | "drowsy" | "bored" | "waking" | "powering-down" | "listening" | "thinking" | "searching" | "reading" | "writing" | "working" | "loading" | "progress" | "radar" | "orbit" | "dictating" | "uploading" | "sending" | "receiving" | "messaging" | "waiting" | "spawning" | "composing" | "dragging" | "happy" | "excited" | "laughing" | "playful" | "proud" | "celebrate" | "bouncing" | "shy" | "sad" | "angry" | "confused" | "suspicious" | "surprised" | "scared" | "curious" | "humming" | "alerting" | "notifying" | "refusing"——
    styleCSSProperties—Applied last, so `--aora-fg` here overrides the ink derived from `color`. That is the only way to hand a mark an animatable custom property instead of a fixed colour, which is what the group activity ink transition needs.
    styleIdStyleId—Style series. Part of the shared appearance, so a pixel mark gets a pixel source. Named `styleId` because `style` is the inline CSS. Falls back to the surrounding `AppearanceProvider`.
    styleOptionsRecord<string, unknown>—Parameters of the series. Part of the shared appearance, compared by value after the engine's own clamping, so `{}` and the defaults share a source.
    variantStyleVariant—Paint variant within the series. Part of the shared appearance.

    接下来