简体   繁体   English

调整base64图像的大小在Firefox中不起作用

[英]Resizing base64 image does not work in Firefox

I tried many different ways, but in Firefox, when I resize the first time, it returns a blank image: 我尝试了许多不同的方法,但是在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);
}

This is my function and I call it like this: 这是我的功能,我这样称呼它:

cursorImage.onload = function(){
  imageToDataUri(cursorImage, width, height);
}

I had the exact same issue as you, and the issue disappeared when I created the canva outside the "onload" callback, like this: 我遇到了与您完全相同的问题,并且在“ onload”回调之外创建画布时,该问题消失了,如下所示:

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();
}

I'm still a beginner in JavaScript, I can't explain why it worked... 我仍然是JavaScript的初学者,我无法解释它为什么起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM