createPattern
可以創建一個畫刷模式,進而可以設置到fillStyle
裏,進行畫刷的填充。
- 函數原型:
ctx.createPattern(image, type)
- type取值:
- ‘no-repeat’:不平鋪
- ‘repeat’:橫方向平鋪
- ‘repeat-x’:縱方向平鋪
- ‘repeat-y’:全方向平鋪
const canvas=document.getElementById("canvas");
const ctx = canvas.getContext('2d');
let img = new Image();
img.src = './logo.png';
img.onload = function(){
let pattern = ctx.createPattern(img, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}