, , JavaScript . , , . , , , requestAnimation Frame. , , . , .
var engine = {
start : function(interval_) {
var accumulated_time = interval_;
var current_time = undefined;
var elapsed_time = undefined;
var handle = this;
var last_time = Date.now();
(function logic() {
handle.timeout = window.setTimeout(logic, interval_);
current_time = Date.now();
elapsed_time = current_time - last_time;
last_time = current_time;
accumulated_time += elapsed_time;
while (accumulated_time >= interval_) {
red_square.update();
accumulated_time -= interval_;
}
})();
(function render(time_stamp_) {
handle.animation_frame = window.requestAnimationFrame(render);
if (accumulated_time < interval_) {
buffer.clearRect(0, 0, buffer.canvas.width, buffer.canvas.height);
red_square.draw(accumulated_time / interval_);
html.output.innerHTML = "Number of warps: " + red_square.number_of_warps;
display.clearRect(0, 0, display.canvas.width, display.canvas.height);
display.drawImage(buffer.canvas, 0, 0, buffer.canvas.width, buffer.canvas.height, 0, 0, display.canvas.width, display.canvas.height);
}
})();
},
stop : function() {
window.cancelAnimationFrame(this.animation_frame);
window.clearTimeout(this.timeout);
this.animation_frame = this.timeout = undefined;
},
animation_frame : undefined,
timeout : undefined
};
, , . red_square - . , github! userpoth.imtqy.com , , - , . - , , , , , , .