首页 > 代码库 > Canvas 图片平铺设置

Canvas 图片平铺设置

/**
 * 图片平铺
 */
function initDemo7(){
    var canvas = document.getElementById("demo7");
    if (!canvas) return;
    var context = canvas.getContext("2d");
    var type = [
        "no-repeat", // 不平铺
        "repeat-x", // 横向平铺
        "repeat-y", // 纵向平铺
        "repeat" // 全画布平铺
    ];
    var index = 3;
    var img = new Image();
    img.src = "http://www.mamicode.com/images/timg3.jpg";
    img.onload = function () {
        // 平铺方式
        context.fillStyle = context.createPattern(img, type[index]);
        context.fillRect(0, 0, canvas.width, canvas.height);
    }
}

 

Canvas 图片平铺设置