我正在使用html2canvas将具有自定义功能的Google地图JavaScript API转换为画布,然后转换为图像。在除IE 11以外的所有浏览器上都可以正常工作,但是在IE 11上生成的图像右侧会有额外的空白空间,等于(浏览器窗口宽度-地图宽度)。因此,我的窗口越宽,右侧的空间就越多,反之亦然。如何在实际图像边缘(768px宽)精确地切割此图像(或HTMLcanvas)?
我在这里找到了这段代码,但不知道如何修改它以适应此任务:
var image = new Image();
image.onload = cutImageUp;
image.src = 'myimage.png';
function cutImageUp() {
var imagePieces = [];
for(var x = 0; x < numColsToCut; ++x) {
for(var y = 0; y < numRowsToCut; ++y) {
var canvas = document.createElement('canvas');
canvas.width = widthOfOnePiece;
canvas.height = heightOfOnePiece;
var context = canvas.getContext('2d');
context.drawImage(image, x * widthOfOnePiece, y * heightOfOnePiece, widthOfOnePiece, heightOfOnePiece, 0, 0, canvas.width, canvas.height);
imagePieces.push(canvas.toDataURL());
}
}
// imagePieces now contains data urls of all the pieces of the image
// load one piece onto the page
var anImageElement = document.getElementById('myImageElementInTheDom');
anImageElement.src = imagePieces[0];
}
return image;
吗?这个语句具体是做什么的?我还在尝试理解立即调用的匿名函数内返回值的工作原理。 - TetraDev