简体   繁体   English

HTML5 Canvas toDataURL 返回空白

[英]HTML5 Canvas toDataURL returns blank

I wanted to upload the image files, draw them into canvas, make changes and save it in the database.我想上传图像文件,将它们绘制到画布中,进行更改并将其保存在数据库中。 I tried to test the base64 value that the canvas image ( Pic ) returned, and it is blank.我尝试测试画布图像 ( Pic ) 返回的 base64 值,它是空白的。 However, I see the result when I append the canvas ( Pic ) to the document.但是,当我将画布 ( Pic ) 附加到文档时,我看到了结果。 What am I doing wrong here?我在这里做错了什么?

function handleFileSelect(evt) {
  var files = evt.target.files; // FileList object                
  for (var i = 0, f; f = files[i]; i++) {

    if (!f.type.match('image.*')) {
      continue;
    }
    // read contents of files asynchronously
    var reader = new FileReader();

    // Closure to capture the file information.
    reader.onload = (function(theFile) {
      return function(e) {

        var canvas = document.createElement("canvas");

        var datauri = event.target.result,
          ctx = canvas.getContext("2d"),
          img = new Image();

        img.onload = function() {

          canvas.width = width;
          canvas.height = height;
          ctx.drawImage(img, 0, 0, width, height);
        };
        img.src = datauri;
        var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

        document.body.appendChild(canvas); //picture gets uploaded                    

        // Generate the image data

        var Pic = canvas.toDataURL("image/png");

        console.log(Pic); // => returns base64 value which when tested equivalent to blank                              
        Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")

        // Sending image to Server
        $.ajax({
          // …
        });

      };
    })(f);
    reader.readAsDataURL(f);
  }
}

My intuition says that everything from var imageData = … should go into the img.onload function.我的直觉是var imageData = …中的所有内容都应该进入img.onload函数。

That means, at the relevant part the code becomes:这意味着,在相关部分,代码变为:

img.onload = function() {
  canvas.width = width;
  canvas.height = height;
  ctx.drawImage(img, 0, 0, width, height);

  var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

  document.body.appendChild(canvas); //picture gets uploaded                    

  // Generate the image data

  var Pic = canvas.toDataURL("image/png");

  console.log(Pic); // => returns base64 value which when tested equivalent to blank                              
  Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")

  // Sending image to Server
  $.ajax({
    // …
  });
};
img.src = datauri;

The reason is that the line原因是这条线

ctx.drawImage(img, 0, 0, width, height);

correctly executes after the image has been loaded.加载图像后正确执行。 But unfortunately, you don't wait for loading when this line gets executed:但不幸的是,当这一行被执行时,你不会等待加载:

var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

and all subsequent lines.以及所有后续行。

The image needs to be loaded in order to draw it on the canvas.需要加载图像才能在画布上绘制它。 The canvas needs to contain the loaded image in order to call getImageData .画布需要包含加载的图像才能调用getImageData

I found a better solution to get the base64 code我找到了一个更好的解决方案来获取base64代码

Instead of this line:而不是这一行:

var Pic = canvas.toDataURL("image/png");

use this:用这个:

var Pic = canvas.toDataURL("image/png").split(',')[1];

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

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