logo

G

  • 教程
  • API
  • 示例
  • 插件
  • 所有产品antv logo arrow
  • 6.1.26
  • 画布
    • 简介
    • 初始化参数
    • 场景图能力与生命周期
    • 内置对象
    • 坐标系
    • 画布事件
    • OffscreenCanvas 和服务端渲染
    • CustomElementRegistry
    • 常见问题
  • 渲染器
    • 简介
    • Canvas 渲染器
    • Canvaskit 渲染器
    • SVG 渲染器
    • WebGL 渲染器
    • WebGPU 渲染器
    • 自定义渲染器
  • 相机
    • 简介
    • 相机参数
    • 相机动作
    • 相机动画
  • 事件
    • 简介
    • 事件对象
    • 手势和拖放
    • 常见问题
  • 动画
    • Web Animations API
    • Lottie 动画
  • 基础图形
    • 基础概念
    • DisplayObject
    • Group 图形分组
    • Text 文本
    • Circle 圆形
    • Ellipse 椭圆
    • Rect 矩形
    • Image 图片
    • Line 直线
    • Polygon 多边形
    • Polyline 折线
    • Path 路径
    • HTML 内容
  • 样式系统
    • 简介
    • 继承机制
    • CSS Typed OM
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 三维世界
    • 材质
    • 几何
    • 光源
    • Mesh
    • 雾
    • 交互
  • 内置对象
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • 工具方法
  • GPGPU
    • 简介
    • 编程模型
    • Kernel API
    • 经典 GPGPU 的实现原理
    • webgpu-graph
  • 声明式用法
    • 使用 Web Components
  • 开发调试工具
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

继承机制

上一篇
简介
下一篇
CSS Typed OM

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

在 CSS 中,当没有为属性设置值时,可继承属性将从父元素上获取计算值。

https://developer.mozilla.org/zh-CN/docs/Web/CSS/inheritance

例如 CSS 的 color 属性是可继承的,因此对于未设置该属性的 <em> 元素,它将使用父元素的值 green:

p { color: green; }
<p>This paragraph has <em>emphasized text</em> in it.</p>

我们在 G 中实现了一套样式系统,同样支持继承。例如我们创建一个 Text,并未指定 fontSize 或者 fontFamily 这些属性,但它依然可以被渲染出来,因为它加入画布后继承了根节点上的默认样式:fontSize: '16px'; fontFamily: 'sans-serif':

const text = new Text({
style: {
x: 100,
y: 100,
text: 'hello',
},
});
canvas.appendChild(text);

在该示例中,修改根节点的字号同样会影响到子元素,配合 rem 这样的单位我们可以轻松实现“弹性布局”:

canvas.document.documentElement.style.fontSize = `32px`;

根节点默认样式

和浏览器一样,默认值(initial value)会在根节点上应用可继承属性。

例如浏览器默认的 fontSize 是 16px。我们在 G 中给设置了如下样式:

expect(documentElement.style.fill).to.equal('');
expect(documentElement.style.fillOpacity).to.equal('1');
expect(documentElement.style.fontFamily).to.equal('sans-serif');
expect(documentElement.style.fontSize).to.equal('16px');
expect(documentElement.style.fontStyle).to.equal('normal');
expect(documentElement.style.fontVariant).to.equal('normal');
expect(documentElement.style.fontWeight).to.equal('normal');
expect(documentElement.style.height).to.equal('');
expect(documentElement.style.lineCap).to.equal('butt');
expect(documentElement.style.lineDashOffset).to.equal('0');
expect(documentElement.style.lineJoin).to.equal('miter');
expect(documentElement.style.lineWidth).to.equal('1');
expect(documentElement.style.opacity).to.equal('');
expect(documentElement.style.stroke).to.equal('');
expect(documentElement.style.strokeOpacity).to.equal('1');
expect(documentElement.style.textTransform).to.equal('none');
expect(documentElement.style.textAlign).to.equal('start');
expect(documentElement.style.textBaseline).to.equal('alphabetic');
expect(documentElement.style.transformOrigin).to.equal('');
expect(documentElement.style.visibility).to.equal('visible');
expect(documentElement.style.pointerEvents).to.equal('auto');
expect(documentElement.style.width).to.equal('');
expect(documentElement.style.x).to.equal(0);
expect(documentElement.style.y).to.equal(0);
expect(documentElement.style.z).to.equal(0);
expect(documentElement.style.zIndex).to.equal(0);

支持继承的属性

目前我们支持的可继承属性如下:

属性名初始值适用元素是否可继承是否支持动画computed value
fillOpacity'1'所有是是<number>
strokeOpacity'1'所有是是<number>
lineWidth'1'所有是是<length> <percentage>
lineJoin'miter'所有是否<keywords>
lineCap'butt'所有是否<keywords>
lineDash无所有是是<array>
lineDashOffset'0'所有是是<length> <percentage>
visibility'visible'所有是否<keywords>
pointerEvents'auto'所有是否<keywords>
fontSize'16px'所有是是<length> <percentage>
fontFamily'sans-serif'所有是否<keywords>
fontStyle'normal'所有是否<keywords>
fontWeight'normal'所有是否<keywords>
fontVariant'normal'所有是否<keywords>
textBaseline'alphabetic'所有是否<keywords>
textAlign'start'所有是否<keywords>