2つの配列を取るコードを記述しました。どちらも4隅の形状(実際には開始フレームと終了フレーム)の座標、キャンバスID、時間値。次に、各コーナーのdXとdYを計算し、window.performance.now()
を使用してタイムスタンプを作成します。次に、requestAnimationFrame()
ごとに、dX、dY、古いタイムスタンプ、新しいタイムスタンプ、および関数呼び出しからの時間値を使用して、座標が何であるべきかを計算します。複数のアニメーションを一度に実行するときに問題が発生するHTML5キャンバス
function doAnim(cv, startFrame, endFrame, animTime)
{
this.canvas = document.getElementById(cv);
this.ctx = this.canvas.getContext('2d');
if(startFrame.length != endFrame.length)
{
return('Error: Keyframe arrays do not match in length');
};
this.animChange = new Array();
for(i=1;i<=startFrame.length;i++)
{
var a = startFrame[i];
var b = endFrame[i]
var c = b - a;
this.animChange[i] = c;
}
this.timerStart = window.performance.now();
function draw()
{
this.requestAnimationFrame(draw, cv);
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
this.currentFrame = new Array();
for(i=1;i<=startFrame.length;i++)
{
this.currentFrame[i] = startFrame[i]+(this.animChange[i]*((window.performance.now()-this.timerStart)/animTime));
}
if((window.performance.now()-this.timerStart)>=animTime)
{
this.ctx.beginPath()
this.ctx.moveTo(endFrame[1], endFrame[2]);
this.ctx.lineTo(endFrame[3], endFrame[4]);
this.ctx.lineTo(endFrame[5], endFrame[6]);
this.ctx.lineTo(endFrame[7], endFrame[8]);
this.ctx.fill();
return;
}
else
{
this.ctx.beginPath()
this.ctx.moveTo(this.currentFrame[1], this.currentFrame[2]);
this.ctx.lineTo(this.currentFrame[3], this.currentFrame[4]);
this.ctx.lineTo(this.currentFrame[5], this.currentFrame[6]);
this.ctx.lineTo(this.currentFrame[7], this.currentFrame[8]);
this.ctx.fill();
}
}
draw();
}
目的は、複数のオブジェクトのアニメーションを同時に発生させることです。地平線から来ているかのようにオブジェクトを表示し、偽の3Dパースペクティブエフェクトを作成する(すべてのオブジェクトの開始フレームはキャンバスの中心にある単一のポイントになる)ために、私は全体のコーディネートアプローチをとった。私はオブジェクトのテクスチャを歪ませたくありません。
1つのアニメーションにはうまくいきますが、最初のアニメーションが実行されている間に完全に異なるキャンバスで新しいアニメーションを開始しようとすると、最初のアニメーションはそのトラックで停止します。
this
の無償使用で私のJSから見ることができました(私は完全に理解していません。this
がどのように機能し、私が読んだすべての説明が私をさらに混乱させました)。それは働いていません。私はまた、すべての関数の独自の変数を1つのグローバル配列に格納する恐ろしいアプローチを試みました(関数が最初に実行され、すべての変数が1から30のエントリに入れられ、2回目は31から60などに格納されます)。 )。意外にも、それはどちらもうまくいかなかった。
Here is a JSFiddle so you can see this scenario for yourself and play with my code.私は公式にアイデアがありません。どんな助けでも大歓迎です。
この要素はどこですか?document.getElementById(cv); –
cvは、doAnim関数の最初の引数で定義されたキャンバスのIDです。私のテスト環境で実行する関数は、ID「canvas1」のキャンバスで1秒のアニメーションを行うためにdoAnim( 'canvas1'、anim_frame1、anim_frame2,1000)です。 – Clifforus
実行方法を示したこの[Q&A](http://stackoverflow.com/questions/27521142/web-browser-paint-rate-with-multiple-animations-on-the-page/27534151#27534151)をご覧ください。 1つのrequestAnimationFrameループの下に複数のアニメーションがあります。 – markE