【问题标题】:copy only a part of a canvas using context.putImageData()使用 context.putImageData() 仅复制画布的一部分
【发布时间】:2015-07-20 11:11:59
【问题描述】:

我正在尝试逐个迭代地填充画布。此外,我希望用原始图像的正方形迭代地填充画布。

出于性能原因,我首先将原始图像渲染到屏幕外画布,然后使用 getImageData() 获取图像数据。

在屏幕画布中,然后我尝试使用以下代码迭代地绘制片段:

        var x = 0;
        var y = 0;
        for (var i = 0; i <= (nbPiecesHorizontal*nbPiecesVertical-1); i++) {

            onscreenContext.putImageData(
                offScreenData.imageData, 
                x*pieceWidth, 
                y*pieceHeight, 
                x*pieceWidth, 
                y*pieceHeight, 
                pieceWidth, 
                pieceHeight);


            // iter
            x = x + 1;
            if (x == (nbPiecesHorizontal)) {
                x = 0;
                y = y +1;
            }
        };

但是,我只知道画了几块,更具体地说,只有角落中的块。我想当然,所有的作品都被画出来。我该如何解决这个问题?

【问题讨论】:

    标签: javascript html canvas


    【解决方案1】:

    使用.getImageData 和.putImageData 是在画布上绘制缓慢且昂贵的方法,因为它们在像素级别操作画布。

    将图像增量绘制到画布的更好方法是使用context.drawImage 的剪辑版本。 drawImage 的裁剪版本将裁剪原始图像的指定部分并将其绘制到画布上。

    使用 drawImage 更快,因为像素只是从源图像复制到目标画布,而不是通过额外的“过滤器”像素操作。

    以下是 drawImage 的剪辑版本的工作原理:

    context.drawImage(
    
        sourceImage,  // the source image to clip from
    
        sX,           // the left X position to start clipping 
        sY,           // the top Y position to start clipping
        sW,           // clip this width of pixels from the source
        wH,           // clip this height of pixels from the source
    
        dX,           // the left X canvas position to start drawing the clipped sub-image
        dY,           // the top Y canvas position to start drawing the clipped sub-image
        dW,           // scale sW to dW and draw a dW wide sub-image on the canvas
        dH            // scale sH to dH and draw a dH high sub-image on the canvas
    
    }
    

    示例代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    
    var nextTime=0;
    var duration=500;
    var nextX=0;
    var nextY=0;
    var cols=5;
    var rows=3;
    var iw,ih,colWidth,rowHeight;
    
    var img=new Image();
    img.onload=start;
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/annotateMe.jpg";
    function start(){
      iw=canvas.width=img.width;
      ih=canvas.height=img.height;
      colWidth=iw/cols;
      rowHeight=ih/rows;
      requestAnimationFrame(animate);
    }
    
    
    function animate(time){
    
      // check if the elapsed time has reached nextTime
      // if not, just request another animation loop
      if(time<nextTime){requestAnimationFrame(animate); return;}
    
      // reset nextTime 
      nextTime=time+duration;
    
      // calc the x,y of the subimage to clip from the whole img
      var x=parseInt(nextX*colWidth);
      var y=parseInt(nextY*rowHeight);
    
      // clip the subimage from the source image
      // and draw the subimage onto the canvas
      ctx.drawImage(
        // use img as the source for clipping
        img,
        // clip the next cell from the img at x,y 
        // and clip out a subimage with size colWidth x rowHeight
        x,y,colWidth,rowHeight,
        // draw the clipped subimage to the canvas
        x,y,colWidth,rowHeight
      );
    
      // calc the next subimage x,y for the next loop
      var imageIsComplete=false;
      nextX++;
      if(nextX>cols-1){
        nextX=0;
        nextY++;
        if(nextY>rows-1){ imageIsComplete=true; }
      }
    
      // if the image is not complete, request another loop
      if(!imageIsComplete){ requestAnimationFrame(animate); }
    
      if(imageIsComplete){alert('Image is complete!');}
    
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    &lt;canvas id="canvas" width=300 height=300&gt;&lt;/canvas&gt;

    【讨论】:

    • 谢谢你,有道理。我正在尝试的是通过首先使用屏幕外画布来加速碎片的填充,从而渲染图像。任何机会如何用drawImage做到这一点?例如。使用屏幕外画布而不是原始图像作为输入是否有意义?
    • 绝对!您可以轻松(并且高效地)使用屏幕外画布而不是图像。图像或屏幕外(内存中)画布都可以用作 drawImage 的图像源:context.drawImage(myOffscreenCanvas...。无论哪种方式,性能仍然会很好。祝你的项目好运!
    猜你喜欢
    • 1970-01-01
    • 2013-01-22
    • 2017-12-27
    • 2012-12-10
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多