1. <tfoot id='XUDPO'></tfoot>

    <i id='XUDPO'><tr id='XUDPO'><dt id='XUDPO'><q id='XUDPO'><span id='XUDPO'><b id='XUDPO'><form id='XUDPO'><ins id='XUDPO'></ins><ul id='XUDPO'></ul><sub id='XUDPO'></sub></form><legend id='XUDPO'></legend><bdo id='XUDPO'><pre id='XUDPO'><center id='XUDPO'></center></pre></bdo></b><th id='XUDPO'></th></span></q></dt></tr></i><div id='XUDPO'><tfoot id='XUDPO'></tfoot><dl id='XUDPO'><fieldset id='XUDPO'></fieldset></dl></div>
      <bdo id='XUDPO'></bdo><ul id='XUDPO'></ul>

    <small id='XUDPO'></small><noframes id='XUDPO'>

  2. <legend id='XUDPO'><style id='XUDPO'><dir id='XUDPO'><q id='XUDPO'></q></dir></style></legend>

    1. HTML5 Canvas toDataURL 返回空白

      HTML5 Canvas toDataURL returns blank(HTML5 Canvas toDataURL 返回空白)
      <tfoot id='tV2ax'></tfoot>

        <small id='tV2ax'></small><noframes id='tV2ax'>

            <i id='tV2ax'><tr id='tV2ax'><dt id='tV2ax'><q id='tV2ax'><span id='tV2ax'><b id='tV2ax'><form id='tV2ax'><ins id='tV2ax'></ins><ul id='tV2ax'></ul><sub id='tV2ax'></sub></form><legend id='tV2ax'></legend><bdo id='tV2ax'><pre id='tV2ax'><center id='tV2ax'></center></pre></bdo></b><th id='tV2ax'></th></span></q></dt></tr></i><div id='tV2ax'><tfoot id='tV2ax'></tfoot><dl id='tV2ax'><fieldset id='tV2ax'></fieldset></dl></div>

                <bdo id='tV2ax'></bdo><ul id='tV2ax'></ul>

                <legend id='tV2ax'><style id='tV2ax'><dir id='tV2ax'><q id='tV2ax'></q></dir></style></legend>

                  <tbody id='tV2ax'></tbody>
                本文介绍了HTML5 Canvas toDataURL 返回空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想上传图像文件,将它们绘制到画布中,进行更改并将其保存在数据库中.我试图测试画布图像 (Pic) 返回的 base64 值,它是空白的.但是,当我将画布 (Pic) 附加到文档时,我看到了结果.我在这里做错了什么?

                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. However, I see the result when I append the canvas (Pic) to the document. 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);
                  }
                }
                

                推荐答案

                我的直觉是 var imageData = ... 中的所有内容都应该进入 img.onload 函数.

                My intuition says that everything from var imageData = … should go into the img.onload function.

                这意味着,在相关部分,代码变为:

                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;
                

                原因是行

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

                以及所有后续行.

                需要加载图像才能在画布上绘制它.画布需要包含加载的图像才能调用 getImageData.

                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.

                这篇关于HTML5 Canvas toDataURL 返回空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

                • <i id='bYvH2'><tr id='bYvH2'><dt id='bYvH2'><q id='bYvH2'><span id='bYvH2'><b id='bYvH2'><form id='bYvH2'><ins id='bYvH2'></ins><ul id='bYvH2'></ul><sub id='bYvH2'></sub></form><legend id='bYvH2'></legend><bdo id='bYvH2'><pre id='bYvH2'><center id='bYvH2'></center></pre></bdo></b><th id='bYvH2'></th></span></q></dt></tr></i><div id='bYvH2'><tfoot id='bYvH2'></tfoot><dl id='bYvH2'><fieldset id='bYvH2'></fieldset></dl></div>
                  <legend id='bYvH2'><style id='bYvH2'><dir id='bYvH2'><q id='bYvH2'></q></dir></style></legend>
                    <bdo id='bYvH2'></bdo><ul id='bYvH2'></ul>

                    <small id='bYvH2'></small><noframes id='bYvH2'>

                            <tbody id='bYvH2'></tbody>

                          <tfoot id='bYvH2'></tfoot>