您可以做的是制作一个临时画布,并将具有所需尺寸的图像复制到其中,然后使用该临时画布作为图案而不是图像本身。
现场演示 http://jsfiddle.net/loktar/cLpybwbb/
首先创建画布
var tempCanvas = document.createElement("canvas"),
tCtx = tempCanvas.getContext("2d");
tempCanvas.width = side*2;
tempCanvas.height = side*2;
现在将图像绘制到其上,调整为您需要的缩放尺寸
tCtx.drawImage(image,0,0,image.width,image.height,0,0,side*2,side*2);
现在使用您刚刚创建的画布作为图案
ctx.fillStyle = ctx.createPattern(tempCanvas, "repeat");
完整代码
edit创建了一个更通用的可重用示例
function drawPattern(img, size) {
var canvas = document.getElementById('canvas');
canvas.height = 500;
canvas.width = 500;
var tempCanvas = document.createElement("canvas"),
tCtx = tempCanvas.getContext("2d");
tempCanvas.width = size;
tempCanvas.height = size;
tCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, size, size);
// use getContext to use the canvas for drawing
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = ctx.createPattern(tempCanvas, 'repeat');
ctx.beginPath();
ctx.rect(0,0,canvas.width,canvas.height);
ctx.fill();
}
var img = new Image();
img.src = "http://static6.depositphotos.com/1070439/567/v/450/dep_5679549-Moon-Surface-seamless.jpg";
img.onload = function(){
drawPattern(this, 100);
}