简体   繁体   中英

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:

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:

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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