【问题标题】:Image isn't drawn after canvas is cleared (html5 canvas)清除画布后不绘制图像(html5画布)
【发布时间】:2016-08-22 04:16:39
【问题描述】:

我正在尝试在 javascript 中制作一个 Boomshine 游戏的变体,当我使用 arc 函数绘制圆形时,一切正常。但是,当我尝试用 drawImage 函数替换 arc 函数以使用硬币图像而不是圆形时,当我清除画布以删除先前绘制的圆形时,我开始遇到问题。如果我在渲染图像之前不清除画布,则图像将绘制在画布上,但旧图像仍在画布上。但是当我在再次渲染图像之前清除画布时,画布上没有绘制任何内容。

我已经附上了截图,链接在下面。

这就是我清除画布的方式:

var ctx = game.context;
ctx.fillStyle = "darkgray";
ctx.fillRect(0, 0, game.canvas.width, game.canvas.height);

这就是我绘制图像的方式:

function drawImageBall(x,y,radius,startAngle,color)
{
var img = document.createElement('img');
img.src = 'img/coin-icon.png';

var tmpCtx= game.context;
var ax = x-radius;
var ay = y-radius;

img.onload = function() {
    tmpCtx.save();
    tmpCtx.beginPath();
    tmpCtx.arc(x, y, radius, 0, Math.PI * 2, true);
    tmpCtx.closePath();
    tmpCtx.clip();

    tmpCtx.drawImage(img, ax, ay, img.width, img.height);

    tmpCtx.beginPath();
    tmpCtx.arc(0, 0, radius, 0, Math.PI * 2, true);
    tmpCtx.clip();
    tmpCtx.closePath();
    tmpCtx.restore();
};
}

Clearing canvas (screenshot) Without clearing canvas (screenshot)

【问题讨论】:

  • 我不知道我们是否有足够的资源来弄清楚。我实际上写了一个fiddle 只是作为概念证明;我唯一调整的是模拟弧线绘制的起始角度,因此您发布的代码基本上没有任何问题。为闪烁道歉,我只是想把东西放在那里。
  • 您仅在加载图像时绘制图像,而不是每次调用drawImageBall

标签: javascript canvas


【解决方案1】:

使用您的代码,我做了一些更改,我删除了 tmpTcx.clip(),看看 fidlle。提示:对于性能问题,您不需要在每次编写画布时都加载图像。

可怜的例子:https://jsfiddle.net/wf4z0d2h/1/

function clearCanvas(){
        var ctx = game.context;
        ctx.fillStyle = "darkgray";
        ctx.fillRect(0, 0, game.canvas.width, game.canvas.height);
    }

    function drawImageBall(x,y,radius,startAngle,color)
    {
        if(x == undefined){x = 100;}
        if(y == undefined){y = 100;}
        if(radius == undefined){radius = 40;}

        //var img = document.createElement('img');
        //img.src = 'img/coin-icon.png';
        //img.src = "http://ps2.lansa.com/images/icons/normal/256/coin_256.png";

        var tmpCtx= game.context;
        var ax = x-radius;
        var ay = y-radius;


        //img.onload = function() {
            tmpCtx.save();
            tmpCtx.beginPath();
            tmpCtx.arc(x, y, radius, 0, Math.PI * 2, true);
            tmpCtx.stroke(); // Draw it
            tmpCtx.closePath();
            //tmpCtx.clip();


            tmpCtx.drawImage(img, ax, ay, img.width, img.height);

            //tmpCtx.beginPath();
            //tmpCtx.arc(0, 0, radius, 0, Math.PI * 2, true);
            ////tmpCtx.clip();
            //tmpCtx.stroke(); // Draw it
            //tmpCtx.closePath();
            //tmpCtx.restore();
        //};
    }
    var img = document.createElement('img');
        //img.src = 'img/coin-icon.png';
        img.src = "http://ps2.lansa.com/images/icons/normal/256/coin_256.png";

    //drawImageBall();
    img.onload = function(){
    x = 0;
    y = 0;
    setInterval(function(){
        x = x+10;
        y = y+10;
        clearCanvas();
        drawImageBall(x,y);

    },300);
    }

【讨论】:

    【解决方案2】:

    请记住,下载img 需要一些时间。

    在下载期间,Javascript 不会停止(!)。相反,JS 将继续执行任何以下代码。这导致了您意想不到的问题。

    所以在您的应用开始时只下载一次 img。这样您的drawImage 将按照您期望的顺序完成,因为在您的图片下载过程中不会有任何延迟。

    【讨论】:

    • 我将图像添加到我的全局游戏对象中,现在它可以工作了!
    • @FerreTahon。很高兴你把事情解决了。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2020-04-14
    • 2015-11-01
    相关资源
    最近更新 更多