html5 canvas,定期绘制垂直线

时间:2018-06-20 19:01:36

标签: javascript jquery html5 canvas

我正在尝试使用画布绘制N条垂直线。绘制完成后,我要制作“运行块”,该块将沿着线下降,并在N秒后重复。

代码工作正常,但仅在最后一行绘制

   function isCanvasSupported() {
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }

    function drawBackground(ctx, x, height) {
        ctx.beginPath();
        ctx.lineWidth = 3;
        ctx.moveTo(x, 0);
        ctx.lineTo(x, height);
        ctx.strokeStyle = "black";
        ctx.stroke();
    }
    function drawStepBackgroundCall(ctx, x, currentstep, step) {
        console.log("here"+ currentstep +" "+ step);
        ctx.save();
        ctx.beginPath();
        ctx.lineWidth = 3;
        ctx.moveTo(x, currentstep - step);
        ctx.lineTo(x, currentstep);
        ctx.strokeStyle = "red";
        ctx.stroke();

        ctx.closePath();
        ctx.restore();
        setTimeout(function() {
            ctx.save();
            ctx.beginPath();
            ctx.lineWidth = 3;
            ctx.moveTo(x, currentstep - 2 * step);
            ctx.lineTo(x, currentstep - step);
            ctx.strokeStyle = "black";
            ctx.stroke();

            ctx.closePath();
            ctx.restore();
        }, 0);

    }

    function drawStepBackground(ctx, x, height) {
        console.log('jo');
        var step = Math.round(height / 6);
        var currentstep = Math.round(height / 6);
        while (currentstep <= height) {
            setTimeout(function() { 
                drawStepBackgroundCall(ctx, x, currentstep, step);

            }, 2000);
            console.log("other here"+ currentstep +" "+ step);
            currentstep += step;
        }
    }

while (currentstep < width) {
                        drawBackground(ctx, currentstep, height);
                        setInterval(function() {
                            drawStepBackground(ctx, currentstep, height);
                        }, 500);
                        currentstep += step;
                    }

当我更改此内容时,我认为问题就在这里

 setTimeout(function() { 
            drawStepBackgroundCall(ctx, x, currentstep, step);

        }, 2000);

行到

       setTimeout(function() { return function() {
            drawStepBackgroundCall(ctx, x, currentstep, step);
            }
        }, 2000);

它停止绘制

0 个答案:

没有答案