おそらくあなたが望むのは、ページ上で発生するすべてを一元化し、requestAnimationFrameを使用してすべての描画を行うことです。したがって、基本的には、次のような関数/クラスがあります(Mootoolsクラスに慣れているいくつかのスタイル/構文エラーを許す必要があります。これをアウトラインとして使用してください)
var Main = function(){
this.queue = [];
this.actions = {};
requestAnimationFrame(this.loop)
}
Main.prototype.loop = function(){
while (this.queue.length){
var action = this.queue.pop();
this.executeAction(e);
}
//do you rendering here
requestAnimationFrame(this.loop);
}
Main.prototype.addToQueue = function(e){
this.queue.push(e);
}
Main.prototype.addAction = function(target, event, callback){
if (this.actions[target] === void 0) this.actions[target] = {};
if (this.actions[target][event] === void 0) this.actions[target][event] = [];
this.actions[target][event].push(callback);
}
Main.prototype.executeAction = function(e){
if (this.actions[e.target]!==void 0 && this.actions[e.target][e.type]!==void 0){
for (var i=0; i<this.actions[e.target][e.type].length; i++){
this.actions[e.target][e.type](e);
}
}
}
したがって、基本的には、このクラスを使用して、ページで発生するすべてを処理します。すべてのイベントハンドラーはonclick='Main.addToQueue(event)'
、ページにイベントを追加するか、または追加したい場合は、イベントをキューに追加するように指示し、Main.addActionを使用して、それらのイベントを実行したいものに向けます。このようにして、キャンバスの再描画が完了するとすぐに、また再描画される前に、すべてのユーザーアクションが実行されます。キャンバスが適切なフレームレートでレンダリングされる限り、アプリは応答性を維持する必要があります。
編集:requestAnimationFrame(this.loop)の「this」を忘れた