zoukankan      html  css  js  c++  java
  • 解决CHROME中画布中无法显示图片的方法

    最终效果图如下

    我按照W3SCHOOL里面的方法,代码如下

    <!DOCTYPE html>
    <html>
    <body>
    
    <script type="text/javascript">
    
    
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    var img=document.getElementById("tulip");
    ctx.drawImage(img,10,10);
    
    //tulip.src="eg_tulip.jpg";
    
    
    </script>
    <p>要使用的图像:</p>
    <img id="tulip" src="eg_tulip.jpg" alt="The Tulip" />
    
    <p>画布:</p>
    <canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;background:#ffffff;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>
    
    
    
    
    
    </body>
    </html>
    

      但是在谷歌浏览器显示如下:

    图片无法显示

    解决方法:

    在body的onload方法里加载script段里的代码就Ok了,代码如下

    <!DOCTYPE html>
    <html>
    
    <!--加一个方法ONLOAD时候用上-->
    <body onLoad="abc()">
    
    <script type="text/javascript">
    
    /*这里做成一个方法,方便ONLOAD时调用*/
    function abc()
    {
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    var img=document.getElementById("tulip");
    ctx.drawImage(img,10,10);
    }
    //tulip.src="eg_tulip.jpg";
    
    
    </script>
    <p>要使用的图像:</p>
    <img id="tulip" src="eg_tulip.jpg" alt="The Tulip" />
    
    <p>画布:</p>
    <canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;background:#ffffff;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>
    
    
    
    
    
    </body>
    </html>
    

      

  • 相关阅读:
    Xdebug
    单点登录
    一个Https网站发送Http的 ajax请求的解决方法
    js关闭微信浏览器页面
    标准的身份证验证(第18位校验码)
    Redis 更新(set) key值 会重置过期时间问题
    PHP 报错:Deprecated: Methods with the same name as their class will not be constructor...
    php防sql注入
    web开发原则
    fopen()函数
  • 原文地址:https://www.cnblogs.com/kmsfan/p/3484763.html
Copyright © 2011-2022 走看看