I'm sure this has probably been asked before (although I couldn't find a question akin to this one).
I have a canvas "app" that is refreshing persistently, but performance is awful on mobile devices. I just wondered if there was a nice alternative to requestAnimFrame()
or the like.
Here is my "app": http://www.barriereader.co.uk/Principality/
Here is the main rendering methods (I've removed the actual rendering - you can view that here http://www.barriereader.co.uk/Principality/System/Engine.js)
var fps = 0;
var oldtime = +new Date();
var time;
var Engine = {
/** FOR DEBUGGING **/
FPS: true,
/*******************/
Canvas : null,
CTX : null,
GameLooper : null,
NodeSize : 10,
MapSize : 32,
Init : function (w, h) {
Engine.Canvas = document.createElement('canvas');
document.body.appendChild(Engine.Canvas);
Engine.CTX = Engine.Canvas.getContext('2d');
Engine.Canvas.width = w;
Engine.Canvas.height = h;
Engine.GameLoop();
},
Update : function (time) {
fps = 1000/(time-oldtime)
oldtime = time;
Engine.Draw();
},
Draw : function () {
Engine.CTX.save();
Engine.CTX.setTransform(1, 0, 0, 1, 0, 0);
Engine.CTX.clearRect(0, 0, Engine.Canvas.width, Engine.Canvas.height);
Engine.CTX.restore();
/**************DRAW_SCENE_HERE*****************/
//A BUNCH OF RENDERING GOES HERE...
//Performance is bad on mobiles :(
/**********************************************/
Engine.GameLoop();
},
GameLoop : function () {
Engine.GameLooper = setTimeout(function () {
requestAnimFrame(Engine.Update, Engine.Canvas);
}, 10);
},
};
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
window.setTimeout(callback(+new Date), 1000 / 60);
};
}());
window.onload = function () {
Engine.Init(320, 320);
};