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

    <tfoot id='qCN2E'></tfoot>

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

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

        • <bdo id='qCN2E'></bdo><ul id='qCN2E'></ul>

        我无法在 html5 中的画布中获取图像,该怎么办?

        时间:2023-06-19

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

            1. <legend id='7H7hD'><style id='7H7hD'><dir id='7H7hD'><q id='7H7hD'></q></dir></style></legend>

                1. <small id='7H7hD'></small><noframes id='7H7hD'>

                  <tfoot id='7H7hD'></tfoot>

                2. 本文介绍了我无法在 html5 中的画布中获取图像,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经从我的 w3schools 检查了这段代码,它运行良好.

                  I have checked this code from my w3schools where its working well.

                  但是当我在浏览器中运行这段代码时,它不起作用.

                  But when I run this code in my browser, it's not working.

                  图像未显示在画布中.此外,我在其他地方的 w3schools 浏览器中尝试了相同的代码,但它仍然无法在该浏览器中运行.

                  The image does not get displayed in the canvas. Also, I tried the same code in w3schools browser somewhere else, but still it's not working in that browser either.

                  <!DOCTYPE html>
                  <html>
                      <body>
                          <p>Image to use:</p>
                          <img id="scream" src="flower.jpg" alt="The Scream" width="220" height="277"><p>Canvas:</p>
                          <canvas id="myCanvas" width="250" height="300" style="border:1px solid #d3d3d3;">
                  
                          <script>
                              var c=document.getElementById("myCanvas");
                              var ctx=c.getContext("2d");
                              var img=document.getElementById("scream");
                              ctx.drawImage(img,10,10);
                          </script>
                      </body>
                  </html>
                  

                  推荐答案

                  问题是图片的加载是异步的,所以你的Javascript代码可能在图片加载完成之前就已经运行了.因为当 ctx.drawImage(img, 10, 10) 被称为 img 时没有完整的数据并且在画布上什么也没有.

                  The problem is that load of an image is asynchronous, so your Javascript code might run before the image finish his loading. Because of that when ctx.drawImage(img, 10, 10) is called img have no complete data and draws nothing on canvas.

                  要解决您需要等待图像完全加载的问题.Javascript 让您能够设置一个函数以在图像完全加载时运行.

                  To solve that you need to wait the image to completely load. Javascript give you the ability to setup a function to run when an image is completely loaded.

                  所有依赖于图像数据的代码都应该放在 onload 回调中.

                  All code that depends of the image data should be put inside the onload callback.

                  img.onload = function(){ 
                      ctx.drawImage(img,10,10);
                  }
                  

                  带有修改部分的脚本:

                  <script>
                      var c=document.getElementById("myCanvas");
                      var ctx=c.getContext("2d");
                      var img=document.getElementById("scream");
                      img.onload = function(){ 
                          ctx.drawImage(img,10,10);
                      }
                  </script>
                  

                  如果 src 正确,无论图像是从 html 标记加载还是使用 new Image() 在 Javascript 中动态创建,onload 方法适用于两者.

                  If the src is correct doesn't matter if the image is loaded from a html tag or created dynamically in Javascript with new Image(), the onload method works for both.

                  这篇关于我无法在 html5 中的画布中获取图像,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:JavaScript HTML5-Canvas - 使用 SetInterval 绘制弧会导致锯齿状边缘 下一篇:如何使用 Javascript 根据 HTML 中的输入文本字段更改文本元素?

                  相关文章

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

                    <small id='1HVNI'></small><noframes id='1HVNI'>

                  1. <legend id='1HVNI'><style id='1HVNI'><dir id='1HVNI'><q id='1HVNI'></q></dir></style></legend>

                        <bdo id='1HVNI'></bdo><ul id='1HVNI'></ul>
                      <tfoot id='1HVNI'></tfoot>