简体   繁体   English

在Firefox中调整base64图像的大小

[英]Resize base64 image in firefox

I'm still new in coding and HTML5. 我仍然是编码和HTML5的新手。 I have a HTML5 stage with multiple canvas. 我有一个包含多个画布的HTML5阶段。 I need to get the base64 image from that stage and resize it, then get the base64 image of the resized canvas. 我需要从该阶段获取base64图像并调整其大小,然后获取调整后的画布的base64图像。 This is the code I use: 这是我使用的代码:

stage.toDataURL({
    callback: function(dataUrl) {
        var tmp_img = new Image();
        tmp_img.src = dataUrl;

        //resize image to 45x75
        var canvas = document.getElementById('hidden_canvas');
        canvas.width = 45;
        canvas.height = 75;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(tmp_img, 0, 0, canvas.width, canvas.height);
        dataUrl = canvas.toDataURL();
)};

The dataUrl is correct in Chrome but when I run the code in Firefox, it seems that the Firefox didn't generate the correct base64 code. dataUrl在Chrome中是正确的,但是当我在Firefox中运行代码时,似乎Firefox没有生成正确的base64代码。

I would really appreciate any help 我真的很感谢任何帮助

You've stumbled upon a common problem. 您偶然发现了一个常见问题。

tmp_img.src loads the dataUrl into your new image--but that takes time. tmp_img.srctmp_img.src加载到新映像中-但这需要时间。 So javascript continues with your code below that even before the image is loaded. 因此,即使在图片加载之前,javascript仍会在其下方继续执行您的代码。 The result is that you are sometimes trying to ctx.drawImage before tmp_img has been fully loaded and is ready to use. 结果是您有时会在tmp_img完全加载并可以使用之前尝试ctx.drawImage

The fix is to refactor your code to use tmp_img.onload . 解决方法是将代码重构为使用tmp_img.onload The .onload triggers a callback function when tmp_img is finally ready to use. 当最终可以使用tmp_img时,.onload会触发一个回调函数。 The refactoring might look like this: 重构可能看起来像这样:

var tmp_img = new Image();

tmp_img.onload=function(){

    //resize image to 45x75
    var canvas = document.getElementById('hidden_canvas');
    canvas.width = 45;
    canvas.height = 75;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(tmp_img, 0, 0, canvas.width, canvas.height);
    dataUrl = canvas.toDataURL();

}

tmp_img.src = dataUrl;

// more javascript

JavaScript will execute the above code in this order: JavaScript将按以下顺序执行上述代码:

  1. Create a new Image object 创建一个新的Image对象
  2. See tmp_img.onload and make a not that it should do that code when the image is fully loaded 请参阅tmp_img.onload并不要在图像完全加载后执行该代码
  3. Set tmp_img.src and start loading the image. 设置tmp_img.src并开始加载图像。
  4. Continue with "more javascript" 继续“更多javascript”
  5. When the image is fully loaded, execute everything in the .onload callback function. 完全加载映像后,执行.onload回调函数中的所有内容。

It looks like you're using KineticJS--are you? 看来您正在使用KineticJS,是吗?

If so, then instead of stage.toDataURL you could use stage.toImage . 如果是这样,那么代替stage.toDataURL你可以使用stage.toImage That way you already have the image loaded when the stage.toImage callback is executed (you don't have to worry about waiting for the image to load). 这样,当执行stage.toImage回调时,您已经加载了图像(您不必担心等待图像加载)。 Then just .drawImage the image that KineticJS provides in the callback. 然后,只需.drawImage KineticJS在回调中提供的图像即可。

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

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