Canvas—圖形畫刷

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);
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章