【问题标题】:Canvas image leaves weird trail when moving left向左移动时画布图像留下奇怪的痕迹
【发布时间】:2018-05-13 06:10:58
【问题描述】:

我有一个HTML5 Canvas,我需要将主要对象的 2 个副本左右移动。右侧似乎工作正常,但左侧开始留下奇怪的绿色痕迹。 jSfiddle link here

这里是代码。作业要求我使用各种画布形状写字,并将其框成 3 个不同的立方体。我想我错过了一些广告无法真正弄清楚的东西。任何帮助表示赞赏

var c = document.getElementById("cId");
    var ctx = c.getContext("2d");

    var cWidth = c.width;
    var cHeight = c.height;

    var xOff = 1;

    var direction = 1;

    function playAnimation() {
        ctx.clearRect(0, 0, cWidth, cHeight);

        ctx.save();
        ctx.translate(xOff, 0);
        drawName();
        ctx.restore();

        ctx.save();

        ctx.translate(-1*xOff, 0);
        drawName();
        ctx.restore();

        ctx.save();
        drawName();
        ctx.restore();

        xOff++;

        window.requestAnimationFrame(playAnimation);
    }

    function fDrawRect() {
        ctx.beginPath();
        ctx.fillStyle = "red";
        ctx.rect(5, 5, 80, 60);
        ctx.fillRect(5, 5, 80, 60);
        ctx.stroke();
        ctx.beginPath();
        ctx.fillStyle = "blue";
        ctx.rect(85, 5, 80, 60);
        ctx.fillRect(85, 5, 80, 60);
        ctx.stroke();
        ctx.beginPath();
        ctx.fillStyle = "yellow";
        ctx.rect(165, 5, 80, 60);
        ctx.fillRect(165, 5, 80, 60);
        ctx.stroke();
    }

    function draw(x, y, xTo, yTo, color) {
        ctx.beginPath();
        ctx.lineWidth= 2;
        ctx.strokeStyle=color;
        ctx.moveTo(x,y);
        ctx.lineTo(xTo,yTo);
        ctx.stroke();

    }

    function drawName() {
        //Draw rect around
        fDrawRect();
        //L
        draw(10, 10, 10, 60, "black");
        draw(10, 60, 30, 60, "black");
        //A
        ctx.beginPath();
        ctx.arc(60, 25, 15, 1*Math.PI, 0);
        ctx.stroke();
        draw(45, 25, 45, 60, "black");
        draw(75, 25, 75, 60, "black");
        draw(45, 35, 75, 35, "black");
        //U
        ctx.beginPath();
        ctx.moveTo(90, 45);
        ctx.quadraticCurveTo(105, 75, 125, 45);
        ctx.stroke();
        draw(90, 45, 90, 10, "black");
        draw(125, 45, 125, 10, "black");
        //R
        draw(140, 10, 140, 60, "black");
        ctx.beginPath();
        ctx.arc(140, 25, 15, 1.5*Math.PI, 0.5*Math.PI);
        ctx.stroke();
        draw(140, 40, 155, 60, "black");
        //I
        draw(170, 10, 170, 60, "black");
        //S
        ctx.beginPath();
        ctx.moveTo(210, 10);
        ctx.bezierCurveTo(170, 10, 250, 60, 190, 60);
        ctx.stroke();

    }
    ctx.translate(450, 150);
    //drawName();
    playAnimation();

【问题讨论】:

    标签: javascript jquery html css html5-canvas


    【解决方案1】:

    您需要在fDrawRect 函数中清除画布:

        function fDrawRect() {
            ctx.clearRect(0, 0, c.width, c.height); //Clear canvas
            ctx.beginPath();
            ctx.fillStyle = "red";
            ctx.rect(5, 5, 80, 60);
            ctx.fillRect(5, 5, 80, 60);
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = "blue";
            ctx.rect(85, 5, 80, 60);
            ctx.fillRect(85, 5, 80, 60);
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = "yellow";
            ctx.rect(165, 5, 80, 60);
            ctx.fillRect(165, 5, 80, 60);
            ctx.stroke();
        }
    

    【讨论】:

    • 为什么在每个playAnimation()的开头清除一个rect整个画布大小还不够?
    • 嘿,谢谢,它修复了尾随,但现在只有对象向左移动,右侧不起作用。 jsfiddle.net/av7tazmy/1
    • @ArneHugo 因为playAnimation只被调用了一次,画布每次重新绘制都需要清除
    • @DNKROZ 在原始帖子中向两侧移动(ctx.translate(xOff, 0) - 向右移动 1,ctx.translate(-1*xOff, ) - 向左移动相同数量的-1)。在我的 fDrawRect 函数中添加 clearRect 后,它停止向右移动,仅向左移动。还是我错过了什么?
    • @Kirdeika 对不起,你是对的。我在一个细长的显示器上打开小提琴,却没有意识到一半的动画被切断了!哈哈
    【解决方案2】:

    问题

    当您尝试清除 playAnimation 开头的画布(使用 ctx.clearRect(0, 0, cWidth, cHeight))时,您不会清除画布中可见的整个区域。

    证明:如果您绘制一个矩形来覆盖您在playAnimation 中清除的区域,您会发现它并没有像应有的那样从左上角开始 (fiddle)。

    原因它不起作用是这一行,在你开始渲染之前翻译一次上下文:

    ctx.translate(450, 150);
    

    当您删除它时,您会看到整个可见区域都被清除了 (fiddle)。但是,现在您不会在画布中间看到名称。

    解决方案

    解决方法是在每个playAnimation()的开头进行翻译,在函数的末尾撤消翻译(fiddle)。

    【讨论】:

    • 感谢您的解释!真是愚蠢的错误
    猜你喜欢
    • 2019-06-09
    • 2023-04-05
    • 2011-12-30
    • 2021-07-07
    • 1970-01-01
    • 2013-12-14
    • 2019-04-16
    • 2012-05-11
    • 1970-01-01
    相关资源
    最近更新 更多