小程序畫布對指定的圖像創建模式的方法CanvasContext.createPattern
CanvasContext.createPattern(string image, string repetition)
對指定的圖像創建模式的方法,可在指定的方向上重復元圖像
基礎庫 1.9.90 開始支持,低版本需做兼容處理。
小程序插件:支持
參數
string image
重復的圖像源,支持代碼包路徑和本地臨時路徑 (本地路徑)
string repetition
如何重復圖像
repetition 的合法值
值 說明 最低版本
repeat 水平豎直方向都重復
repeat-x 水平方向重復
repeat-y 豎直方向重復
no-repeat 不重復
示例代碼
const ctx = wx.createCanvasContext('myCanvas')
const pattern = ctx.createPattern('/path/to/image', 'repeat-x')
ctx.fillStyle = pattern
ctx.fillRect(0, 0, 300, 150)
ctx.draw()