【问题标题】:Javascript Canvas clear/ redrawJavascript Canvas 清除/重绘
【发布时间】:2015-06-09 17:18:56
【问题描述】:

我试过用谷歌搜索这个问题的答案,但我只是在兜圈子...... 如果我清除矩形(使用 clearRect),则图像不会在之后重绘。 但是,如果我不清除图像只是堆叠。 我希望它清除当前图像,然后用新图像绘制。 我错过了什么? 它与图像 Load 有关系吗? 抱歉,如果这是一个问题重复,我找不到确切的答案 - 我尝试了其他人的建议,但结果很差。

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);
}

【问题讨论】:

  • 我刚刚尝试使用 canvas.width = canvas.width; - 有什么理由我不应该使用它吗?
  • 只需在填充函数的底部添加两行:链接小提琴的ctx.globalCompositeOperation = "source-over";ctx.globalAlpha = 1;,它应该开始工作了。

标签: javascript canvas html5-canvas


【解决方案1】:

画布工作流程如下:

  1. 在画布上画一些东西。
  2. 计算这些东西的位置变化。
  3. 清除画布。
  4. 在新位置重新绘制所有事物。

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>

【讨论】:

  • 但这似乎是我正在做的事情的顺序:drawImage,fill,clear,drawImage,fill
  • 好吧,我试图启动你的代码,但是你遗漏了太多东西,以至于我什至无法让它开始运行:-(话虽如此,当你设置 globalCompositeOperation='source-atop'之后的所有绘图只会在现有的非透明像素上绘制。必须关闭合成,否则它的效果将永远持续下去。也许这就是您的代码出错的地方。为了进一步帮助,我们需要查看 mcve:@987654321 @
【解决方案2】:

您需要在其中添加beginPath()rect() 将累积矩形到路径,clearRect() 不会清除那些。同时重置补偿。 mode 和 alpha 因为它们是粘性的。

如果您使用fillRect() 而不是rect() + fill()(下面添加示例),则可以避免使用beginPath(),因为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;    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2019-03-30
    • 2011-11-15
    • 1970-01-01
    • 2019-03-05
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多