主题与样式机制
ice-render 的样式机制是「透传 + 增强」,主题机制是「三层 design token + 命名主题」。本文说明两者的设计、用法与优先级链。
一、样式机制:透传底座
组件通过 style 对象声明外观,applyStyleToCtx() 会把 style 里的每个字段原样透传到 canvas 上下文(ctx[key] = style[key])。因此任何 canvas 属性(fillStyle / strokeStyle / lineWidth / font / fontSize / shadow* …)都能直接写进 style,引擎不需要逐个实现。
渲染链路:
props.style / state.style
↓ applyStyleToCtx()(for...in 透传 + shadow 简写展开)
ctx(canvas 上下文状态)
↓ doRender()
fill(path2D) / stroke(path2D) / fillText()
fill / stroke 是顶层 props(不在 style 里),控制是否填充/描边:
new ICERect({
fill: false, // 不填充
stroke: true, // 描边
style: { strokeStyle: '#185fa5', lineWidth: 2 },
});
二、样式能力清单
在透传底座之上,引擎提供了这些增强:
文字排版
textAlign(start / center / right)与 textBaseline(alphabetic / middle)真正生效,文字可精确居中:
new ICEText({
left: 0, top: 0, width: 200, height: 40, text: '