Here are some tips.
#1)
Avoid using java keywords. In Math.rand, there is a variable called float.
Try calling it toFloat.
#2)
It's best not to browser sniff. Detect for features instead.
Old Code:
function createRequestObject() {
var request, browser = navigator.appName;
if (browser == "Microsoft Internet Explorer")
{ request = new ActiveXObject("Microsoft.XMLHTTP"); }
else
{ request = new XMLHttpRequest(); }
return request;
}
New Code:
function createRequestObject() {
var request;
if (ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
} else {
request = new XMLHttpRequest();
}
return request;
}
#3)
Try not to repeat yourself.
Old Code:
CanvasRenderingContext2D.prototype.strokeRect = function(x, y, w, h, r1, r2, r3, r4) {
if (r1 == null) {
this.beginPath();
//...
this.closePath();
this.stroke();
} else {
//...
this.beginPath();
//...
this.closePath();
this.stroke();
}
};
New Code:
CanvasRenderingContext2D.prototype.strokeRect = function(x, y, w, h, r1, r2, r3, r4) {
this.beginPath();
if (r1 == null) {
this.moveTo(x, y);
//...
} else {
//...
}
this.closePath();
this.stroke();
};
#4)
Make functions as small as possible. Try adding functions to the prototype instead of creating them inside the constructor.
Old Code:
var Loop = function(callback, fps) {
// ...
this.start = function() {
// Timeout is used to specify a framerate
this.request = setTimeout(function() {
requestAnimFrame(function() {
_this.start();
_this.delta = (+new Date - _this.lastTime) / (1000 / _this.fps);
_this.real_fps = Math.round((1 + (1-_this.delta))*_this.fps);
_this.callback();
_this.lastTime = +new Date;
});
}, 1000 / this.fps);
};
//...
};
New Code:
Loop.prototype.start = function () {
// Timeout is used to specify a framerate
this.request = setTimeout(function () {
requestAnimFrame(function () {
_this.start();
_this.delta = (+new Date() - _this.lastTime) / (1000 / _this.fps);
_this.real_fps = Math.round((1 + (1 - _this.delta)) * _this.fps);
_this.callback();
_this.lastTime = +new Date();
});
}, 1000 / this.fps);
};