test rect performance
HTML
<canvas id="canvas" width=400 height=400></canvas>
JavaScript
// requestAnimationFrame polyfill
(function () {
var lastTime = 0;
var vendors = ['webkit', 'moz'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
},
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}());
(function () {
var canvas = document.getElementById("canvas");
var canvasContext = canvas.getContext("2d");
var cw = canvas.width;
var ch = canvas.height;
var fps = 0,
timeBetweenFrames = 0,
lastTime = 0;
function drawText() {
canvasContext.fillStyle = "#FCEB77";
canvasContext.fillText(' Score: ' + 66 + ' Lives: ' + 66 + ' other info: ' + 66, 10, 45);
}
function drawRects() {
//canvasContext.beginPath();
canvasContext.rect(2, 1, 210, 30);
canvasContext.rect(2, 1, 80, 30);
canvasContext.rect(80, 1, 70, 30);
canvasContext.strokeStyle = "#FCEB77";
canvasContext.stroke();
//canvasContext.closePath();
}
function renderLoop(timeElapsed) {
timeBetweenFrames = timeElapsed - lastTime;
fps = Math.round(1000 / timeBetweenFrames);
lastTime = timeElapsed;
canvasContext.clearRect(0, 0, cw - 200, ch);
drawText();
drawRects();
...