简体   繁体   English

Html5画布,延迟绘制2行

[英]Html5 canvas, delay drawing 2 lines

I wanna draw a 2 lines. 我想画一条2行。 First should be start a 3 sec after open the page, and this is no problem, second line (and later another) should start drawing after when first will finish (or maybe later will be 3sec when finish first, or click on the button). 首先应该在打开页面后开始3秒,这没有问题,第二行(以及后面的另一行)应该在第一行完成后开始绘制(或者可能在第一次完成后将是3秒,或者单击按钮) 。

There is a code of this lines, but i don't know how to do that, i can only make a 2 lines in this same time. 有这样的代码,但我不知道怎么做,我只能在同一时间做2行。

var amount = 0;
var amountt=1;
var startX = 0;
var startY = 0;
var endX = 500;
var endY = 300;
var startXx = 0;
var startYy = 0;
var endXx = 500;
var endYy = -300;    

setTimeout(function() {
    var interval = setInterval(function() {
        amount += 0.01; // change to alter duration
        if (amount > 1) {
            amount = 1;
            clearInterval(interval);
        }

        ctx.strokeStyle = "black";
        ctx.lineWidth=1;
        ctx.strokeStyle="#707070";
        ctx.moveTo(startX, startY);
        // lerp : a  + (b - a) * f
        ctx.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
        ctx.stroke();                   

        ctx.strokeStyle = "black";
        ctx.lineWidth=1;
        ctx.strokeStyle="#707070";
        ctx.moveTo(startX, startY);
        // lerp : a  + (b - a) * f
        ctx.lineTo(startXx + (endXx - startXx) * amount, startYy + (endYy - startYy) * amount);
        ctx.stroke();            

    }, 0);

}, 3000);

I'm not sure if this is exactly what you're after, but I took some of what you wrote and interpreted it. 我不确定这是否正是你所追求的,但我接受了你写的一些内容并对其进行了解释。

Here is a jsFiddle with the results http://jsfiddle.net/GZSJp/ 这是一个jsFiddle与结果http://jsfiddle.net/GZSJp/

Essentially you have one interval that is calling to animate a line every 3 seconds. 基本上你有一个间隔,每隔3秒调用一行线。 Then you have an inner interval that animates the lines drawing. 然后你有一个内部间隔,动画线条绘制。

var idx = -1;
var startx = [0, 500, 100];
var starty = [0, 0, 300];
var endx = [500, 0, 400];
var endy = [300, 500, 300];

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

var drawLinesInterval = setInterval(function() {
    if(idx > startx.length)
        clearInterval(drawLinesInterval);

    var linepercentage = 0;
    idx++; //move onto the next line
    var animateInterval = setInterval(function() {
        linepercentage += 0.01;
        if(linepercentage > 1)
        {
            clearInterval(animateInterval);
        }

        ctx.strokeStyle = "black";
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#707070";
        ctx.moveTo(startx[idx], starty[idx]);
        var tempxend = 0;
        var tempyend = 0;
        if(startx[idx] > endx[idx])
            tempxend = startx[idx] - ((startx[idx]-endx[idx])*linepercentage);
        else
            tempxend = startx[idx] + ((endx[idx]-startx[idx])*linepercentage);
        if(starty[idx] > endy[idx])
            tempyend = starty[idx] - ((starty[idx]-endy[idx])*linepercentage);
        else
            tempyend = starty[idx] + ((endy[idx]-starty[idx])*linepercentage);

        ctx.lineTo(tempxend, tempyend);
        ctx.stroke();
    }, 10);
}, 3000);

Let me know if that doesn't answer your question. 如果这不能回答你的问题,请告诉我。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM