キャンバスはダブル バッファリングされます。setTimeout()
キャンバスに変更を描画する機会を与えるために、アニメーションの各ステップを延期する必要があります。更新:requestAnimationFrame
の代わりに を参照してくださいsetTimeout()
。
コンテキスト メソッドを呼び出して一時停止することにより、四角形の各セグメントを描画する 1 つの例を作成しました。私はあなたが探している特定のアニメーションを知っていると思いますが、これはそうではありませんが、うまくいけば良いスタートを切れます.
以下のコードとデモはこちら: http://jsfiddle.net/q8GcR/
function animateRoundRect(ctx, x, y, width, height, radius, delay) {
commands = [
['moveTo', x,y+radius],
['lineTo', x,y+height-radius],
['quadraticCurveTo', x,y+height,x+radius,y+height],
['lineTo', x+width-radius,y+height],
['quadraticCurveTo', x+width,y+height,x+width,y+height-radius],
['lineTo', x+width,y+radius],
['quadraticCurveTo', x+width,y,x+width-radius,y],
['lineTo', x+radius,y],
['quadraticCurveTo', x,y,x,y+radius]
];
function draw() {
var args = commands.shift();
var method = args.shift();
ctx[method].apply(ctx, args);
ctx.stroke();
if (commands.length) {
setTimeout(draw, delay);
}
}
ctx.beginPath();
draw();
}
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.lineWidth = 3;
ctx.strokeStyle = '#f00';
animateRoundRect(ctx, 20, 20, 250, 100, 10, 500);