canvas.draw()
一次向宿主绘图面提交一批有序 2D 指令。它是即时绘制接口,不保存场景树,也不返回像素。
Syntax
const result = await host.canvas.draw(canvasId, operations);
Parameters
| 参数 | 类型 | 说明 |
|---|---|---|
canvasId |
string |
目标绘图面 |
operations |
DrawOperation[] |
按数组顺序执行的有界指令批次 |
v0.1 指令使用带 op 的封闭联合类型。未知 op、NaN/Infinity、过深状态栈和未声明资源引用必须拒绝。图片用包内 assetId,字体用类型化 font handle;不接受 URL。
Return value
{ accepted: u32, dropped: u32 }
accepted 是已接受指令数。只有规范允许在预算压力下丢弃的非关键指令才计入 dropped;宿主不能返回成功后悄悄丢掉状态栈操作。
Examples
await host.canvas.draw('main', [
{ op: 'clear', color: '#101418' },
{ op: 'fillRect', x: 20, y: 20, width: 160, height: 80, color: '#238636' },
{ op: 'fillText', x: 36, y: 66, text: 'Hello', color: '#ffffff' },
]);
批量提交优于每个矩形调用一次 Host API:
const operations = bars.map((bar) => ({
op: 'fillRect', x: bar.x, y: bar.y, width: bar.width, height: bar.height, color: bar.color,
}));
await host.canvas.draw('chart', operations);
Errors
not-found、invalid-operation、resource-denied、limit-exceeded。整批结构非法时必须整体失败;是否允许对预算丢弃做部分接受由具体指令 schema 明确规定。
Requirements
- 能力:
host:canvas.draw - 用户激活:不需要
- 信任档位:green