HTML5_canvas_圖片加載_雙緩衝_跳幀閃爍問題

canvas 圖片加載javascript

pen.drawImage(ele, showX, showY, imgWidth, imgHeight);        css

ele    將 img 元素 加載到畫布上html

 

  • 步驟

1. 建立一個 <img> 對象java

var imgNode = new Image();canvas

imgNode.src = "./img/bird.png";瀏覽器

 

2. 等待圖片加載完成,再進行下一步操做app

imgNode.onload = function(){flex

3. 圖片顯示到畫布上spa

pen.drawImage(imgNode, 0, 0, 100, 100);3d

};

 

跳幀閃爍問題

  • 閃爍的緣由:
    • 清空了畫布,而後加載圖片,再等圖片加載完,最後畫下一幀。
    • 這個空白延遲,主要是由於等待圖片加載完成時間過久
  • 解法1: 
    • 在加載圖片以前,不清空上一幀圖像
    • 加載完成後,再清空畫布,最後畫下一幀。
    • <!DOCTYPE html>
      <html>
          <head>
              <meta charset="UTF-8">
              <title>幀閃爍解決</title>
              
              <style type="text/css">
                  * {
                      margin: 0;
                      padding: 0;
                  }
                  
                  body{
                      text-align: center;
                  }
                  
                  #myCanvas{
                      border: 1px solid ;
                  }
              </style>
          </head>
          
          <body>
              <canvas id="myCanvas" width="800" height="400"></canvas>
              
              
              
              <!-- javascript 代碼 -->
              <script type="text/javascript">
                  window.onload = function () {
                      var myCanvas = document.getElementById('myCanvas');
                      
                      var painting = myCanvas.getContext('2d');
                      
                      var num = 0;
                      var speed = 0;
                      setInterval(function(){
                          speed += 20;
                          if(speed > myCanvas.width){
                              speed = -150
                              //                speed = 0
                          };
                          
                          num++;
                          if(num > 8){
                              num = 1;
                          };
                          
                          painting.beginPath();
                          
                          //1.img對象
                          var imgNode = new Image();
                          
                          //2.img對象 設置 src
                          imgNode.src = 'img/q_r' + num + '.jpg';
                          
                          //3.等圖片加載完成後再去設置圖片顯示
                          imgNode.onload = function () {
                              //4.圖片顯示在畫布上
                              painting.clearRect(0, 0, myCanvas.width, myCanvas.height)
                              painting.drawImage(imgNode, speed, 150, 150, 90);
                          };
                      }, 340);
                  };
              </script>
          </body>
      </html>

 

飛鳥 案例(雙緩衝,解決跳幀閃爍問題

 

  • <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>canvas 雙緩衝案例</title>
    
            <style type="text/css">
                body {
                    width: 100%;
                    color: #000;
                    background: #96b377;
                    font: 14px Helvetica, Arial, sans-serif;
                }
                
                .wrap {
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    align-items: center;
                }
            </style>
        </head>
        
        <body>
            
            
            <div id="box" class="wrap">
            </div>
            
            
            
            
            <!-- javascript 代碼 -->
            <script type="text/javascript">
                //                 建立 畫布的width  畫布的height  背景顏色 父元素
                function createCanvasTo(canvasWidth, canvasHeight, bgColor, parentObj){
                    var myCanvas = document.createElement("canvas");
                    myCanvas.width = canvasWidth;
                    myCanvas.height = canvasHeight;
                    myCanvas.innerText = " 您的瀏覽器不支持 canvas,建議更新或者更換瀏覽器。";
                    if(bgColor){
                        myCanvas.style.backgroundColor = bgColor;
                    };
                    if(parentObj){
                        parentObj.appendChild(myCanvas);
                    };
                    
                    return myCanvas;
                };
                
                var box = document.getElementById("box");
                
                var topCanvas = createCanvasTo(600, 83, "#fff", box);
                movingPic(topCanvas, "./img/q_r", "jpg", -150, 0, 150, 83);
                
                var bottomCanvas = createCanvasTo(600, 300, "#fff", box);
                movingPic(bottomCanvas, "./img/walking", "png", -130, 27, 130, 246);
                
                
                //                 畫布對象 圖片路徑 圖片類型 起始x 起始y 圖片width 圖片height
                function movingPic(theCanvas, imgPath, imgType, posX, posY, imgWidth, imgHeight){
                    var cacheCanvas = document.createElement("canvas");
                    cacheCanvas.width = theCanvas.width;
                    cacheCanvas.height = theCanvas.height;
                    var cachePen = cacheCanvas.getContext("2d");
                    
                    var num = 1;
                    var pos = 0;
                    window.setInterval(function(){
                        pen = theCanvas.getContext("2d");    // 坑1: 必定要放在循環裏面
                        pen.clearRect(0, 0, theCanvas.width, theCanvas.height);
                        
                        // 圖形位移變換
                        num++;
                        if(num > 8){
                            num = 1;
                        };
                        
                        pos += 15;
                        if(posX+pos > theCanvas.width){
                            pos = -130;
                        };
                        
                        // 雙緩衝繪製    先畫到臨時 canvas
                        cachePen.save();
                        cachePen.beginPath();
                        var imgObj = new Image();
                        imgObj.src = imgPath+num+"."+imgType;
                        imgObj.onload = function(){
                            cachePen.drawImage(imgObj, posX+pos, posY, imgWidth,imgHeight);
                        };
                        cachePen.restore();
                        
                        // 再轉到正式 canvas
                        pen.save();
                        pen.drawImage(cacheCanvas, 0, 0, cacheCanvas.width, cacheCanvas.height);
                        pen.restore();
                        
                        // 坑2: 必定要在 取走緩衝內容後 再清除緩衝
                        cachePen.clearRect(0, 0, cacheCanvas.width, cacheCanvas.height);
                    }, 100);
                };
            </script>
        </body>
    </html>
相關文章
相關標籤/搜索