CanvasContext.createPattern(string image, string repetition)

基础库 1.9.90 开始支持,低版本需做兼容处理

对指定的图像创建模式的方法,可在指定的方向上重复元图像

参数

string image

重复的图像源,支持代码包路径和本地临时路径 (本地路径)

string repetition

如何重复图像

repetition 的合法值

说明最低版本
repeat水平竖直方向都重复
repeat-x水平方向重复
repeat-y竖直方向重复
no-repeat不重复

示例代码

  1. const ctx = wx.createCanvasContext('myCanvas')
  2. const pattern = ctx.createPattern('/path/to/image', 'repeat-x')
  3. ctx.fillStyle = pattern
  4. ctx.fillRect(0, 0, 300, 150)
  5. ctx.draw()