# 定义 schema 参考 每个 DiceBear 头像样式都是一个遵循 [DiceBear Definition Schema](https://github.com/dicebear/schema) 的 JSON 文件。此页面 记录了样式定义的完整结构。 ## 概览 一个样式定义描述了生成头像所需的一切:canvas 尺寸、要渲染的 SVG 元素、可随机化的组件,以及可用的 颜色调色板。该定义完全是声明式的:没有代码,没有 函数。渲染逻辑位于 DiceBear Core 的实现中。 ## 顶层结构 ```json { "$schema": "https://...", "$id": "https://...", "$comment": "可选注释", "meta": { ... }, "canvas": { ... }, "components": { ... }, "colors": { ... }, "attributes": { ... } } ``` | 属性 | 必需 | 描述 | | ------------ | ---- | ------------------------------------------------------------------------------------ | | `$schema` | 否 | 用于编辑器验证的定义 schema URL | | `$id` | 否 | 此定义的规范标识符(通常为其托管地址的 URL,最多 256 个字符) | | `$comment` | 否 | 自由文本注释,例如 "Generated by Figma"(最多 4096 个字符) | | `meta` | 否 | 许可证、创建者和来源元数据 | | `canvas` | **是** | Canvas 尺寸和根元素树 | | `components` | 否 | 命名的、可随机化的 SVG 组件(最多 512 项) | | `colors` | 否 | 用于动态着色的命名颜色调色板(最多 512 项) | | `attributes` | 否 | 根 `` 元素的全局 SVG 属性 | ## `meta` 关于样式的元数据,用于许可证注释、CLI 横幅和 文档。 ```json { "meta": { "license": { "name": "CC0 1.0", "url": "https://creativecommons.org/publicdomain/zero/1.0/", "text": "完整许可证文本..." }, "creator": { "name": "DiceBear", "url": "https://www.dicebear.com" }, "source": { "name": "Initials", "url": "https://github.com/dicebear/dicebear" } } } ``` ## `canvas` 定义 SVG 视口和根元素树。`width` 和 `height` 决定生成的 SVG 的 `viewBox`。 ```json { "canvas": { "width": 100, "height": 100, "elements": [ { "type": "component", "name": "background" }, { "type": "component", "name": "face" } ] } } ``` | Property | Type | Required | Description | | ---------- | ------ | -------- | ------------------------------------------------ | | `width` | number | **Yes** | 画布宽度(像素,>= 1) | | `height` | number | **Yes** | 画布高度(像素,>= 1) | | `elements` | array | **Yes** | 根元素树(最多 1024 个顶层条目) | ## 元素 Elements 是 SVG 的构建块。支持三种类型: ### `element`: SVG 标签 渲染一个 SVG 元素,例如 ``、``、`` 等。 ```json { "type": "element", "name": "circle", "attributes": { "cx": "50", "cy": "50", "r": "40", "fill": { "type": "color", "name": "skin" } }, "children": [] } ``` 只允许 [白名单中的 SVG 元素](https://github.com/dicebear/schema/blob/main/src/definition.json) (例如 `circle`、`path`、`g`、`rect`、`text`、`defs`、`filter`、 `linearGradient`、`radialGradient` 等)。像 `script`、 `foreignObject` 和 `a` 这样的元素会因安全原因被阻止。 一个节点最多可以有 1024 个子节点。`name: "defs"` 的元素具有 特殊语义(见下文的[可重用 `` 条目](#reusable-defs-entries))。 ### `text`: 文本内容 在 SVG 元素内渲染原始文本。支持变量引用。 ```json { "type": "text", "value": "Hello" } ``` 或者使用变量: ```json { "type": "text", "value": { "type": "variable", "name": "initials" } } ``` 在文本 `value` 中只接受 `initial` 和 `initials`。其他变量 (`fontFamily`、`fontWeight`)只在其专用属性中有效(见 [变量引用](#variable-references))。 ### `component`: 组件引用 引用在 `components` 部分定义的命名组件。DiceBear Core 会根据种子和选项选择一个变体。 ```json { "type": "component", "name": "eyes" } ``` 组件引用可以携带自己的 `attributes` 映射。它们会原样写入 生成的 `` 元素,这就是你将组件实例放置到画布上的方式: ```json { "type": "component", "name": "eyes", "attributes": { "transform": "translate(10 20)" } } ``` 用户提供的 `transform` 会被追加在渲染器为每个组件选择的 rotate/translate/scale 之前,因此它充当外层 (放置)变换。 ### `