如何使用画布动画绘制曲线?

时间:2013-01-23 16:37:00

标签: javascript html5 canvas html5-canvas

我有一堆我想慢慢画的点。我尝试了setTimeOut和这个tutorial的效果。但没有那么多成功。

函数看起来像这样

功能:

var myFunction = function(ctx, grid, points) {
                ctx.beginPath();
                ctx.moveTo(points[0].x, points[0].y);
                ctx.lineWidth = 2;
                ctx.strokeStyle = '#2068A8';
                ctx.fillStyle = '#2068A8';
                var count = 1;
                for (count = 1; count < points.length; count++) {
                    ctx.lineTo(points[count].x , points[count].y);
                }
                ctx.stroke();
            }

在这个函数周围有许多其他绘图函数,但我只想动画一个。

如何用画布慢慢绘制一个函数?

2 个答案:

答案 0 :(得分:10)

有两种方法你可以做到这一点,我可以想到我的头脑。一个基本上是绘制一个点并在绘制另一个点之前暂停一段时间。这是我提供的第一个例子。第二种方法涉及将局部线条绘制到当前目标,这样可以提供更平滑的绘图外观。作为附注,我在两个示例中都使用requestAnimationFrame,这是建议使用任何类型的画布动画的方法。

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

canvas.width = 400;
canvas.height = 200;

var points = [],
    currentPoint = 1,
    nextTime = new Date().getTime()+500,
    pace = 200;

// make some points
for (var i = 0; i < 50; i++) {
    points.push({
        x: i * (canvas.width/50),
        y: 100+Math.sin(i) * 10
    });
}

function draw() {

    if(new Date().getTime() > nextTime){
        nextTime = new Date().getTime() + pace;

        currentPoint++;
        if(currentPoint > points.length){
            currentPoint = 0;
        }
    }
    ctx.clearRect(0,0,canvas.width, canvas.height);
    ctx.beginPath();
    ctx.moveTo(points[0].x, points[0].y);
    ctx.lineWidth = 2;
    ctx.strokeStyle = '#2068A8';
    ctx.fillStyle = '#2068A8';
    for (var p = 1, plen = currentPoint; p < plen; p++) {
        ctx.lineTo(points[p].x, points[p].y);
    }
    ctx.stroke();

    requestAnimFrame(draw);
}

draw();

<强> Live Demo

如果您发现有点不稳定,可以执行以下操作以获得更平滑的线条

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

canvas.width = 400;
canvas.height = 200;

var points = [],
    currentPoint = 1,
    speed = 2,
    targetX = 0,
    targetY = 0,
    x = 0,
    y = 0;

// make some points
for (var i = 0; i < 50; i++) {
    points.push({
        x: i * (canvas.width/50),
        y: 100+Math.sin(i) * 10
    });
}

// set the initial target and starting point
targetX = points[1].x;
targetY = points[1].y;
x = points[0].x;
y = points[0].y;

function draw() {
    var tx = targetX - x,
        ty = targetY - y,
        dist = Math.sqrt(tx*tx+ty*ty),
        velX = (tx/dist)*speed,
        velY = (ty/dist)*speed;

        x += velX
        y += velY;

    if(dist < 1){
        currentPoint++;

        if(currentPoint >= points.length){
            currentPoint = 1;
            x = points[0].x;
            y = points[0].y;
        }

        targetX = points[currentPoint].x;
        targetY = points[currentPoint].y;
    }

    ctx.clearRect(0,0,canvas.width, canvas.height);
    ctx.beginPath();
    ctx.moveTo(points[0].x, points[0].y);
    ctx.lineWidth = 2;
    ctx.strokeStyle = '#2068A8';
    ctx.fillStyle = '#2068A8';

    for (var p = 0, plen = currentPoint-1; p < plen; p++) {
        ctx.lineTo(points[p].x, points[p].y);
    }
    ctx.lineTo(x, y);    
    ctx.stroke();

    requestAnimFrame(draw);
}

draw();

<强> Live Demo

答案 1 :(得分:0)

感谢这些示例,Loktar!这应该是对Loktar发布的答案中上面第二个示例的评论,但是我无法发表评论,因为我刚刚注册并且没有足够的“信誉”。

我尝试了上面的第二个演示,如果放慢速度,您会注意到新线段从应该开始的点开始开始增长。这是我对其进行调整以使其正常工作的方式:

我更改了以下行:

ctx.lineTo(points[p].x, points[p].y); 
// => 
ctx.lineTo(points[p+1].x, points[p+1].y);

,现在结果看起来非常平滑。我之所以发表评论是因为Loktar的帖子近乎完美,我希望人们不要因为需要调整一些细节而错过他的帖子。再次感谢,Loktar!