##在canvas中插入图片(需要image对象)
1.canvas操作图片时,必须要等图片加载完才能操作
2.drawImage(image, x, y, width, height)
其中 image 是 image 或者 canvas 对象,x 和 y 是其在目标 canvas 里的起始坐标。
这个方法多了2个参数:width 和 height,这两个参数用来控制 当像canvas画入时应该缩放的大小

 

        window.onload=function(){
        var canvas=document.querySelector("#test");         
          if(canvas.getContext){
                   var ctx=canvas.getContext("2d");
                    var img= new Image();
                        img.src="img/tg.png";
                        img.onload=function(){
                        draw();
                }
                    function draw(){        
                        ctx.drawImage(img,0,0,img.width,img.height);
                    }
       }
       
       
    }
        window.onload=function(){
        var canvas=document.querySelector("#test");         
          if(canvas.getContext){
                   var ctx=canvas.getContext("2d");
                    var img= new Image();
                        img.src="img/tg.png";
                        img.onload=function(){
                        draw();
                }
                    function draw(){        
                        ctx.drawImage(img,0,0,img.width,img.height);
       }
       
      }       
    }    
drawImage

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-09-02
猜你喜欢
  • 2022-12-23
  • 2021-10-23
  • 2022-01-02
  • 2021-03-30
  • 2021-12-21
  • 2022-12-23
  • 2021-06-25
相关资源
相似解决方案