我尝试了很多不同的方法,但是在Firefox中,当我第一次调整大小时,它会返回一个空白图像:
function imageToDataUri(img, width, height) {
var canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
setTimeout(function () {
cursorimg = canvas.toDataURL();
}, 500);
}
这是我的功能,我称之为:
cursorImage.onload = function(){
imageToDataUri(cursorImage, width, height);
}
答案 0 :(得分:0)
我和你有完全相同的问题,当我在“onload”回调之外创建canva时问题就消失了,如下所示:
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
cursorImage.onload = function(){
ctx.drawImage(img, 0, 0, width, height);
cursorimg = canvas.toDataURL();
}
我仍然是JavaScript的初学者,我无法解释它为何有效......