CanvasContext.draw
更新时间:2022-02-22 10:50:48

注意:该接口已停止维护,建议使用 Canvas 组件 V2

基础库 1.0.0 开始支持本方法,这是一个同步方法。

将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。

语法

CanvasContext.draw(reserve, callback)

参数说明

reserve

类型默认值必填说明最低支持版本
booleanfalse本次绘制是否接着上一次绘制。若 reserve 参数为 false,则在本次调用绘制之前会先清空画布再继续绘制;若 reserve 参数为 true,则保留当前画布上的内容,本次调用 draw 绘制的内容继续覆盖在上面1.0.0

callback

类型默认值必填说明最低支持版本
function绘制完成后执行的回调函数1.0.0

返回值

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

【示例 1】简单绘制:

<!-- index.ttml --> <button type="primary" size="default" bindtap="draw">开始绘制</button> <canvas canvas-id="myCanvas"></canvas>
复制代码
Page({ draw() { const canvasCtx = tt.createCanvasContext("myCanvas"); canvasCtx.setFillStyle("red"); canvasCtx.fillRect(10, 10, 100, 100); canvasCtx.draw(); }, });
复制代码

【示例 2】保留上次绘制结果:

<!-- index.ttml --> <button type="primary" size="default" bindtap="draw">继续绘制</button> <canvas canvas-id="myCanvas"></canvas>
复制代码
Page({ onReady() { const canvasCtx = tt.createCanvasContext("myCanvas"); canvasCtx.setFillStyle("red"); canvasCtx.fillRect(10, 10, 100, 100); canvasCtx.draw(); this.canvasCtx = canvasCtx; }, draw() { this.canvasCtx.setFillStyle("black"); this.canvasCtx.fillRect(50, 50, 100, 100); this.canvasCtx.draw(true); }, });
复制代码

Bug & Tip

点击纠错
社区相关主题
查看更多相关主题
还有疑问?发起在线咨询
该文档是否对你的开发有所帮助?
有帮助
没帮助
该文档是否对你的开发有所帮助?
有帮助
没帮助
https://developer.open-douyin.com/forum/activity/post/64586644efa15e3e15723269