• <small id='W7BqY'></small><noframes id='W7BqY'>

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

        html5 canvas toDataURL 返回空白图片

        时间:2023-06-20
          <bdo id='jXKED'></bdo><ul id='jXKED'></ul>
          <i id='jXKED'><tr id='jXKED'><dt id='jXKED'><q id='jXKED'><span id='jXKED'><b id='jXKED'><form id='jXKED'><ins id='jXKED'></ins><ul id='jXKED'></ul><sub id='jXKED'></sub></form><legend id='jXKED'></legend><bdo id='jXKED'><pre id='jXKED'><center id='jXKED'></center></pre></bdo></b><th id='jXKED'></th></span></q></dt></tr></i><div id='jXKED'><tfoot id='jXKED'></tfoot><dl id='jXKED'><fieldset id='jXKED'></fieldset></dl></div>
              <tbody id='jXKED'></tbody>

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

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

                • <legend id='jXKED'><style id='jXKED'><dir id='jXKED'><q id='jXKED'></q></dir></style></legend>

                  本文介绍了html5 canvas toDataURL 返回空白图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用以下代码绘制画布并使用 toDataUrl 将其保存为 png 图像.

                  i am using following code to draw canvas and save it as png image using toDataUrl.

                  <!DOCTYPE HTML>
                  <html>
                    <head>
                      <style>
                        body {
                          margin: 0px;
                          padding: 0px;
                        }
                        #myCanvas {
                          border: 1px solid #9C9898;
                        }
                      </style>
                      <script>
                        function loadImages(sources, callback) {
                          var images = {};
                          var loadedImages = 0;
                          var numImages = 0;
                          // get num of sources
                          for(var src in sources) {
                            numImages++;
                          }
                          for(var src in sources) {
                            images[src] = new Image();
                            images[src].onload = function() {
                              if(++loadedImages >= numImages) {
                                callback(images);
                              }
                            };
                            images[src].src = sources[src];
                          }
                        }
                  
                        window.onload = function(images) {
                          var canvas = document.getElementById("myCanvas");
                          var context = canvas.getContext("2d");
                  
                          var sources = {
                            darthVader: "http://a1.sphotos.ak.fbcdn.net/hphotos-ak-snc6/228564_449431448422343_1996991887_n.jpg",
                            yoda: "http://a4.sphotos.ak.fbcdn.net/hphotos-ak-snc7/427489_449423165089838_503188418_n.jpg"
                          };
                  
                          loadImages(sources, function(images) {
                            context.drawImage(images.darthVader, 250, 30, 250, 250);
                            context.drawImage(images.yoda, 530, 30, 250, 250);
                          });
                  
                  
                  // save canvas image as data url (png format by default)
                          var dataURL = canvas.toDataURL();
                  
                          // set canvasImg image src to dataURL
                          // so it can be saved as an image
                          document.getElementById("canvasImg").src = dataURL;
                  };
                  
                  
                      </script>
                    </head>
                    <body>
                      <canvas id="myCanvas" width="850" height="315"></canvas>
                  <img id="canvasImg" alt="Right click to save me!">
                    </body>
                  </html>
                  

                  但我的 png 图像显示为空白图像.我只看到一个空白的白色图像.我搜索了这个,但什么也没找到.我正在使用 php 和 chrome 浏览器.这里有什么问题?

                  But my png image is shown as blank image. i see only a blank white image. i searched for this but found nothing on this. i am using php and chrome browser. what is wrong here?

                  推荐答案

                  你首先调用 loadImages,然后在 loadImages 调用之后获取数据 URL.但是,正如您的 loadImages 实现所示,调用它只会启动加载过程;当所有图像已加载时调用回调,这是将来的某个时间.

                  You are first calling loadImages, then getting the data URL right after that loadImages call. However, as your loadImages implementation shows, calling it merely starts the loading process; the callback is called when all images have been loaded, which is some time in the future.

                  目前你得到一张空白图片,因为图片还没有加载,所以你的回调还没有被调用,结果你的画布仍然是空的.

                  Currently you're getting a blank image because the images have not been loaded yet, so your callback is not called yet, and as a result your canvas is still empty.

                  简而言之,依赖于已加载图像的所有内容(例如,您预期的 toDataURL 结果)都应在实际加载这些图像时执行 - 因此在回调中.

                  In short, everything that relies on the images having being loaded (e.g. your expected toDataURL result) should be executed when those images are actually loaded - thus in the callback.

                  loadImages(sources, function(images) {
                    context.drawImage(images.darthVader, 250, 30, 250, 250);
                    context.drawImage(images.yoda, 530, 30, 250, 250);
                  
                    var dataURL = canvas.toDataURL();
                    document.getElementById("canvasImg").src = dataURL;
                  });
                  

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

                  上一篇:使用新数据更新每个 AJAX 请求的 HTML 画布标签 下一篇:如何使用 HTML5 和画布或 SVG 绘制网格

                  相关文章

                  • <bdo id='aOzkY'></bdo><ul id='aOzkY'></ul>
                  <tfoot id='aOzkY'></tfoot>
                    <legend id='aOzkY'><style id='aOzkY'><dir id='aOzkY'><q id='aOzkY'></q></dir></style></legend>

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

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