Javascript Canvas清除/重绘

时间:2015-06-09 17:18:57

标签: javascript canvas html5-canvas

我已经尝试使用谷歌搜索答案,但我只是绕圈子走了...... 如果我清除rect(使用clearRect),则图像不会重绘。 但是,如果我不清除图像只是堆叠。 我希望它清除当前图像然后用新图像绘制。 我错过了什么? 它与图像负载有关吗? 对不起,如果这是一个重复的问题,我无法找到确切的答案 - 我尝试了其他人的建议,但结果很差。

http://jsfiddle.net/bxeuhh4h/

function clear() {
    var canvasTemp = document.getElementById(imgSection);
    var ctxTemp = canvasTemp.getContext("2d");
    ctxTemp.clearRect(0, 0, 500, 500);

}

function fillColorOrPattern(imgSection,currentcolor){
if ((oldcolor !== currentcolor) || (oldxImgToBeFilled !== xImgToBeFilled)){
    clear();
}
imgFill.onload = function () {
imgToBeFilled.onload = function () {
        if ((oldcolor !== currentcolor) || (oldxImgToBeFilled !== xImgToBeFilled)){

        fill(imgSection,currentcolor)

        }
  };
imgToBeFilled.src = xImgToBeFilled;
}
imgFill.src = xImgFill;
}


function fill(imgSection,currentcolor){
canvas = document.getElementById(imgSection);
ctx = canvas.getContext("2d");
ctx.drawImage(imgToBeFilled, 0, 0);
ctx.globalCompositeOperation = "source-atop";
console.log(isItColorOrPattern);
        if (isItColorOrPattern ==   "color"){
            ctx.rect(0, 0, canvas.width, canvas.height);
            console.log("currentcolor: " + currentcolor);
            ctx.fillStyle = getColor(currentcolor);
            console.log(getColor(currentcolor));
            ctx.fill();
        }else{
            var pattern = ctx.createPattern(imgFill, 'repeat');
            console.log("canvas.width: " + canvas.width);
            console.log("xImgFill: " + xImgFill);
            console.log(canvas.getContext);
            ctx.rect(0, 0, canvas.width, canvas.height);
            ctx.fillStyle = pattern;
            ctx.fill();
        }

        ctx.globalAlpha = .10;
        ctx.drawImage(imgToBeFilled, 0, 0);
        ctx.drawImage(imgToBeFilled, 0, 0);
        ctx.drawImage(imgToBeFilled, 0, 0);


    oldcolor = currentcolor;
    oldxImgToBeFilled = xImgToBeFilled;

}

$(window).load(function(){
imgToBeFilled = new Image();
imgFill = new Image();  
fillColorOrPattern(imgSection,currentcolor);
}

2 个答案:

答案 0 :(得分:4)

Canvas工作流程如下:

  1. 在画布上画一些东西。
  2. 计算这些事物的位置变化。
  3. 清除画布。
  4. 重新绘制所有新职位。
  5. Canvas不记得"它吸引你的东西,所以你不能直接命令你的东西移动。

    但是你可以在javascript对象中保存你的东西的定义:

    var myCircle={
        centerX:50,
        centerY:50,
        radius:25,
        fill:'blue'
    }
    

    然后你可以"移动"使用javascript对象的东西:

    myCircle.centerX += 5;
    

    然后重新绘制新位置的东西。将重绘代码放在函数中会使重绘更容易:

    function redraw(){
    
        // clear the canvas
        ctx.clearRect(0,0,canvas.width,canvas.height);
    
        // redraw one or more things based on their javascript objects
        ctx.beginPath();
        ctx.arc( myCircle.centerX, myCircle.centerY, myCircle.radius, 0, Math.PI*2 );
        ctx.closePath();
        ctx.fillStyle=myCircle.fill;
        ctx.fill();
    }
    

    全部放在一起:

    
    
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    
    var myCircle={
      centerX:50,
      centerY:50,
      radius:25,
      fill:'blue'
    }
    
    redraw();
    
    document.getElementById('move').addEventListener('click',function(){
      myCircle.centerX+=5;
      redraw();
    });
    
    function redraw(){
      ctx.clearRect(0,0,canvas.width,canvas.height);
      ctx.beginPath();
      ctx.arc( myCircle.centerX, myCircle.centerY, myCircle.radius, 0, Math.PI*2 );
      ctx.closePath();
      ctx.fillStyle=myCircle.fill;
      ctx.fill();
    }
    
    body{ background-color: ivory; }
    #canvas{border:1px solid red; margin:0 auto; }
    
    <button id=move>Move</button>
    <br>
    <canvas id="canvas" width=300 height=300></canvas>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:3)

您需要在其中添加beginPath()rect()会将矩形累积到路径中,clearRect()将无法清除这些矩形。还重置comp。模式和alpha,因为它们很粘。

如果您使用beginPath()代替fillRect() + rect()(下面添加了示例),则可以避免fill(),因为fillRect()未添加到路径中。

function fill(imgSection,currentcolor){

    // these should really be initialized outside the loop    
    canvas = document.getElementById(imgSection);
    ctx = canvas.getContext("2d");

    // clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // clear path
    ctx.beginPath();

    // use default comp. mode
    ctx.globalCompositeOperation = "source-over";

    // reset alpha
    ctx.globalAlpha = 1;

    ctx.drawImage(imgToBeFilled, 0, 0);
    ctx.globalCompositeOperation = "source-atop";

    if (isItColorOrPattern === "color"){

        // rect() accumulates on path
        ctx.rect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = getColor(currentcolor);
        ctx.fill();

       // instead of rect() + fill() you could have used:
       // fillRect() does not accumulate on path
       // fillRect(0, 0, canvas.width, canvas.height);
    }
    else {
        var pattern = ctx.createPattern(imgFill, 'repeat');
        ctx.rect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = pattern;
        ctx.fill();
    }

    ctx.globalAlpha = .1;
    ctx.drawImage(imgToBeFilled, 0, 0);
    ctx.drawImage(imgToBeFilled, 0, 0);
    ctx.drawImage(imgToBeFilled, 0, 0);

    oldcolor = currentcolor;
    oldxImgToBeFilled = xImgToBeFilled;    
}