CanvasContext.beginPath

CanvasContext.beginPath()

CanvasContext 是旧版的接口,新版 Canvas 2D 接口与 Web 一致

从基础库 2.9.0 开始,本接口停止维护,请使用 RenderingContext 代替

小程序插件:支持

相关文档: 旧版画布迁移指南canvas 组件介绍

功能描述

开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边

  • 在最开始的时候相当于调用了一次 beginPath
  • 同一个路径内的多次 setFillStylesetStrokeStylesetLineWidth等设置,以最后一次设置为准。

示例代码

const ctx = wx.createCanvasContext('myCanvas')
// begin path
ctx.rect(10, 10, 100, 30)
ctx.setFillStyle('yellow')
ctx.fill()

// begin another path
ctx.beginPath()
ctx.rect(10, 40, 100, 30)

// only fill this rect, not in current path
ctx.setFillStyle('blue')
ctx.fillRect(10, 70, 100, 30)

ctx.rect(10, 100, 100, 30)

// it will fill current path
ctx.setFillStyle('red')
ctx.fill()
ctx.draw()

img

posted on 2025-01-05 11:24  AtlasLapetos  阅读(20)  评论(0)    收藏  举报