For AI agents: the complete documentation index is available at /vue-vrm/zh/llms.txt, the full documentation bundle is available at /vue-vrm/zh/llms-full.txt, and this page is available as Markdown at /vue-vrm/zh/guide/web-components.md.
  • 简体中文
  • Web Component

    同一组件也以原生自定义元素的形式提供,它通过 Vue 的 defineCustomElement 包装。Vue 自定义元素的行为请参阅 Vue 文档中的 Vue 与 Web Components。

    共有两个入口:

    入口注册的标签HTMLElementTagNameMap 的类型定义
    vue-vrm/wc-auto<vrm-canvas>(自动)包含
    vue-vrm/wc仅注册传给 register() 的标签不包含

    自动注册

    导入 vue-vrm/wc-auto 会注册 <vrm-canvas>:

    import 'vue-vrm/wc-auto';
    <vrm-canvas show-grid></vrm-canvas>

    使用自定义标签名注册

    导入 vue-vrm/wc,并把想要的标签名传给 register():

    import { register } from 'vue-vrm/wc';
    
    register('my-avatar');

    也可以使用默认导出自行定义:

    import VrmCanvasElement from 'vue-vrm/wc';
    
    customElements.define('my-avatar', VrmCanvasElement);
    每个页面只注册一个标签名

    同一个构造函数在一个自定义元素注册表中只能定义一次。请只注册一个标签名,并且不要在同一页面中同时使用 vue-vrm/wc 和 vue-vrm/wc-auto;定义第二个标签名会抛出 NotSupportedError。

    类型定义

    vue-vrm/wc 不会向 HTMLElementTagNameMap 添加任何内容,请自行声明标签的类型:

    import type VrmCanvasElement from 'vue-vrm/wc';
    
    declare global {
      interface HTMLElementTagNameMap {
        'my-avatar': InstanceType<typeof VrmCanvasElement>;
      }
    }

    注意事项

    • 在 Vue 模板中使用该元素时,请通过 compilerOptions.isCustomElement 将其标记为自定义元素(例如 tag => tag === 'vrm-canvas')。
    • 画布渲染在一个开放的 shadow root 中。
    • 以下示例使用 <vrm-canvas>;使用 vue-vrm/wc 时请替换为你自己的标签名。

    设置 props

    • 基本类型的 props(string、boolean、number)可以通过属性或 DOM 属性设置。属性名使用 kebab-case(show-grid、aspect-ratio)。
    • ArrayBuffer、对象和数组类型的 props(modelData、animationData、cameraInteraction、directionalLight 等)只能通过 DOM 属性设置。
    const element = document.createElement('vrm-canvas');
    element.setAttribute('width', '640');
    element.setAttribute('show-grid', '');
    element.cameraInteraction = { enabled: true };
    document.body.append(element);
    
    const response = await fetch('/api/vrm/my-avatar');
    element.modelData = await response.arrayBuffer();
    Info

    props 的默认值在元素连接到文档时才会应用。在此之前读取 element.width 等属性会返回 undefined。

    事件

    Vue 的 emit 会以同名(model:loaded、animation:end 等)的原生 CustomEvent 派发。emit 的参数以数组形式放在 event.detail 中。

    element.addEventListener('model:loaded', event => {
      const [vrm] = (event as CustomEvent).detail;
      console.log(vrm.meta);
    });
    
    element.addEventListener('error', event => {
      const [error] = (event as CustomEvent<[Error]>).detail;
      console.error(error);
    });

    实例 API

    实例 API 直接暴露在元素上:

    import type { VrmCanvasExposed } from 'vue-vrm/wc';
    
    const avatar = element as unknown as VrmCanvasExposed;
    avatar.resetCamera();
    const image = avatar.captureScreenshot();