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 = "images/timg3.jpg";
    img.onload = function () {
        // 平铺方式
        context.fillStyle = context.createPattern(img, type[index]);
        context.fillRect(0, 0, canvas.width, canvas.height);
    }
}

 

posted @ 2017-04-11 11:50  就只是小茗  阅读(1901)  评论(0编辑  收藏  举报