• 简体中文
  • AgentPortrait

    放进列表里直接使用的头像。

    Ada
    Grace
    Alan
    +1Platform
    import { CharacterStageProvider, AgentPortrait } from '@aora-bot/react';
    
    const people = [
      { id: 'ada@example.com', name: 'Ada' },
      { id: 'grace@example.com', name: 'Grace' },
      { id: 'alan@example.com', name: 'Alan' },
      { id: 'team', name: 'Platform', members: [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }] },
    ];
    
    export default function Demo() {
      return (
        <CharacterStageProvider>
          <div className="site-demo">
            {people.map((agent) => (
              <div className="site-demo__cell" key={agent.id}>
                <AgentPortrait agent={agent} sizePx={52} />
                <span className="site-demo__label">{agent.name}</span>
              </div>
            ))}
          </div>
        </CharacterStageProvider>
      );
    }

    头像的选择顺序

    按以下顺序选择:

    1. 照片:设置了 pictureUrl 时
    2. 群组头像:members 非空时
    3. 角色:由 id 推导

    因此应用可以把手头有的数据直接传进来,得到合理的结果,不必在每个调用处写分支。

    无需存储的身份

    未显式指定时,形状和墨色由 id 哈希得出,所以每个用户都对应一个固定的角色,无需存储任何数据。不同的客户端、不同的会话以及服务端渲染,得到的结果都一致。

    import { AgentPortrait } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo">
          {['u-1', 'u-2', 'u-3', 'u-4', 'u-5', 'u-6', 'u-7', 'u-8'].map((id) => (
            <AgentPortrait key={id} agent={{ id }} sizePx={44} still />
          ))}
        </div>
      );
    }

    在 agent 上传入 shape 或 color 可以单独覆盖形状或颜色。

    脸型

    在 agent 上加 face: 'line' 即可使用线条脸。鼻子和形状、墨色一样从 id 哈希而来,所以同样稳定;设置 nose 可以指定鼻子,设为 null 则不画。

    import { AgentPortrait } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo">
          {['u-1', 'u-2', 'u-3', 'u-4', 'u-5', 'u-6', 'u-7', 'u-8'].map((id) => (
            <AgentPortrait key={id} agent={{ id, face: 'line' }} sizePx={44} still />
          ))}
        </div>
      );
    }

    阵容 preset 也可以带脸型和鼻子;agent 上的显式值优先于预设,预设优先于哈希。

    尺寸

    xs, 16px
    sm, 24px
    md, 32px
    lg, 40px
    xl, 80px
    import { AgentPortrait, AVATAR_SIZES } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo" style={{ alignItems: 'flex-end' }}>
          {Object.entries(AVATAR_SIZES).map(([size, px]) => (
            <div className="site-demo__cell" key={size}>
              <AgentPortrait agent={{ id: 'nova' }} size={size as never} />
              <span className="site-demo__label">
                {size}, {px}px
              </span>
            </div>
          ))}
        </div>
      );
    }

    静止与光晕

    still 渲染静态图片而不是动画,适合长列表。halo 会加一层柔和的底色,颜色由角色自身的色相推导而来。

    import { AgentPortrait } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo">
          <AgentPortrait agent={{ id: 'halo-1' }} sizePx={64} halo still />
          <AgentPortrait agent={{ id: 'halo-2' }} sizePx={64} halo still />
          <AgentPortrait agent={{ id: 'halo-3' }} sizePx={64} halo still />
        </div>
      );
    }

    填充

    fillPx 设定圆内角色的大小。圆本身的尺寸不变,变的只是里面绘制的内容:比圆小会留出边距,比圆大则会被裁掉。默认正好填满圆。它通过自定义属性设置,而不是设置子元素的宽度,因为三种渲染方式中有两种的子元素是 img,样式表已经为它们设定了尺寸。

    fillPx 44
    fillPx 64
    fillPx 80
    import { AgentPortrait } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo">
          {[44, 64, 80].map((fill) => (
            <div className="site-demo__cell" key={fill}>
              <AgentPortrait agent={{ id: 'fill', shape: 'hex', color: 'blue' }} sizePx={64} fillPx={fill} halo />
              <span className="site-demo__label">fillPx {fill}</span>
            </div>
          ))}
        </div>
      );
    }

    形状正在改变时

    isMorphingShape 表示形状此刻正在被替换,例如编辑器打开着,用户正在网格里逐个点选。为 true 时,头像在落定窗口(SETTLE_MS,通过 useMarkSettle)内保持上一次落定的状态,这样一次变形只算一次变化,而不是两次。如果角色在同一帧里同时改变形状和表情,看起来不像同一个角色换了轮廓,而像换成了另一个角色。

    无障碍

    头像默认是 aria-hidden,只有带名称的照片除外。如果头像单独出现、旁边没有文字,请自行为它设置名称,详见无障碍。

    API

    属性类型默认值说明
    agent*AgentLike——
    classNamestring——
    emotionIdstring—Protocol id, for hosts that speak the wire format. Ignored when `state` is given.
    face"symbol" | "capsule" | "cute" | "eyes" | "line" | "brow" | "glossy" | "sparkle" | "button" | "shades" | "droopy" | "bigmouth" | "pie" | "screen" | "teardrop" | "specs"—Face style, overriding whatever the agent identity resolves to.
    fillPxnumber—Size of the character inside the circle. Smaller than the circle leaves a margin; larger crops it. Defaults to filling the circle exactly.
    halobooleanfalseSoft colour wash behind the character.
    isMorphingShapebooleanfalseThe shape is being changed under the character right now. Holds the last settled state for the settle window, so a morph is one change rather than two. A body that changes shape and expression in the same frame does not read as the same character having a new outline; it reads as a different character appearing.
    lineWeightnumber—Stroke width multiplier. Not part of an identity, so always explicit.
    nose"slant" | "angular" | "hook" | "long" | "curve" | "bow" | null—Nose, overriding the identity. `null` draws no nose.
    size"xs" | "sm" | "md" | "lg" | "xl"'md'—
    sizePxnumber—Exact pixel size, overriding `size`.
    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"——
    stillbooleanfalseRender a still image instead of an animation.
    styleCSSProperties——

    接下来