v0.1首个正式支持的版本化规范查看版本范围

canvas.draw()

一次向宿主绘图面提交一批有序 2D 指令。它是即时绘制接口,不保存场景树,也不返回像素。

Syntax

await host.canvas.draw(canvasId, operations);

Parameters

参数 类型 说明
canvasId string 目标绘图面
operations DrawOperation[] 按数组顺序执行的有界指令批次

DrawOperation 是模块页定义的封闭联合类型。未知 op、NaN/Infinity、过深状态栈和无效字体句柄必须拒绝。v0.1 不接受图片、URL、路径或文件 token。

目标绘图面在 manifest 中必须是 mode: "draw"。状态栈、transform/clip、路径合法性和固定 source-over 合成语义见模块页;有限但极大的坐标由安全裁剪处理,不能触发实现私有阈值。

Return value

无。整批要么按顺序全部执行,要么以 HostError 整体失败;成功返回时不得丢弃任何指令。

Examples

await host.canvas.draw('main', [
  { op: 'clear', color: { r: 16, g: 20, b: 24, a: 255 } },
  { op: 'fillRect', rect: { x: 20, y: 20, width: 160, height: 80 }, color: { r: 35, g: 134, b: 54, a: 255 } },
  { op: 'fillText', x: 36, y: 66, text: 'Hello', style: { size: 16, align: 'start', direction: 'ltr' }, color: { r: 255, g: 255, b: 255, a: 255 } },
]);

批量提交优于每个矩形调用一次 Host API:

const operations = bars.map((bar) => ({
  op: 'fillRect', rect: { x: bar.x, y: bar.y, width: bar.width, height: bar.height }, color: bar.color,
}));
await host.canvas.draw('chart', operations);

Errors

特有错误:deniednot-foundinvalid-operationresource-deniedlimit-exceeded。mode 不匹配属于 invalid-operation;结构非法或超预算必须整体失败。

Requirements

  • 能力:host:canvas.draw
  • 用户激活:不需要
  • 信任档位:green

See also

canvas.measureText()canvas.frame

Host API 版本与分层概览Host API v0.1版本非正式提议提议canvas API呈现canvas.getInfo()方法canvas.draw()方法canvas.present()方法canvas.measureText()方法canvas.frame event事件canvas.resize event事件asset API呈现asset.read()方法asset.loadFont()方法playback API呈现playback.open()方法playback.write()方法playback.stop()方法playback.ready event事件playback.ended event事件input API输入input.focus()方法input.pointer event事件input.key event事件input.wheel event事件input.focusChange event事件ime API输入ime.openSession()方法ime.updateState()方法ime.updateGeometry()方法ime.closeSession()方法ime.textUpdate event事件ime.composition event事件ime.formatUpdate event事件ime.characterBoundsRequest event事件ime.ended event事件capture API输入capture.requestCamera()方法capture.requestMic()方法capture.frame()方法capture.readAudio()方法capture.stop()方法capture.ended event事件store API数据store.get()方法store.put()方法store.delete()方法store.list()方法store.deletePrefix()方法clipboard API数据clipboard.readText()方法clipboard.writeText()方法env API平台集成env.snapshot()方法env.change event事件开始调用 Host API指南构建交互式绘图应用指南构建自绘文本编辑器指南管理媒体采集与播放指南组织本地数据与同步指南