canvas rect使用图片填充的方法
canvas fabric.js rect用图片填充
用fabric开发的时候遇到这个问题,纠结了许久,大佬一句话点通了,看了文档后,用setPatternfiil()这个方法就顺利结局,顺便记录一下。
const rect = new fabric.Rect({
left: 0,
top: 0,
width: this.width,
height: this.height,
});
fabric.util.loadImage(‘图片地址’,(img)=>{
rect.setPatternFill({
source:img,
repeat:'repeat'
})
})
new fabric.Group([rect], {left: this.x, top: this.y});//group包裹