Skip to main content
added 35 characters in body
Source Link
Larry Battle
  • 2.2k
  • 11
  • 19
Loop.prototype.start = function () {
    // Timeout is used to specify a framerate
            var _this = this;
    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);
};
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);
};
Loop.prototype.start = function () {
    // Timeout is used to specify a framerate
            var _this = this;
    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);
};
added 156 characters in body
Source Link
Larry Battle
  • 2.2k
  • 11
  • 19

#5) Rename String.prototype.charAt to String.prototype.replaceByIndex

#6) this.fps = fps ? fps : 60; is the same as this.fps = fps || 60;

#5) Rename String.prototype.charAt to String.prototype.replaceByIndex

#6) this.fps = fps ? fps : 60; is the same as this.fps = fps || 60;

Source Link
Larry Battle
  • 2.2k
  • 11
  • 19

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);
};