sbiermanlytle / iioEngine

iio Engine: A JavaScript game engine for HTML5 Canvas
http://iioengine.com
455 stars 81 forks source link

iioengine 1.2: Unreachable code #31

Closed lcnvdl closed 10 years ago

lcnvdl commented 10 years ago

Using a minifier, I've detected the next problem:

iio.Text.prototype.right = function ()
    {
        this.ctx.save();
        this.ctx.font = this.font;
        if (this.textAlign == 'center') 
            return this.pos.x + this.ctx.measureText(this.text).width / 2;
        else if (this.textAlign == 'right' || this.textAlign == 'end') 
            return this.pos.x;
        else 
            return this.pos.x + this.ctx.measureText(this.text).width;
        this.ctx.restore(); // HERE: This will never be executed
    }
    iio.Text.prototype.left = function ()
    {
        this.ctx.save();
        this.ctx.font = this.font;
        if (this.textAlign == 'center') 
            return this.pos.x - this.ctx.measureText(this.text).width / 2;
        else if (this.textAlign == 'right' || this.textAlign == 'end') 
            return this.pos.x - this.ctx.measureText(this.text).width;
        else 
            return this.pos.x;
        this.ctx.restore(); // HERE: This will never be executed
    }

Solution:

iio.Text.prototype.right = function ()
    {
        var result;

        this.ctx.save();
        this.ctx.font = this.font;

        if (this.textAlign == 'center') 
            result = this.pos.x + this.ctx.measureText(this.text).width / 2;
        else if (this.textAlign == 'right' || this.textAlign == 'end') 
            result = this.pos.x;
        else 
            result = this.pos.x + this.ctx.measureText(this.text).width;

        this.ctx.restore();

        return result;
    }
    iio.Text.prototype.left = function ()
    {
        var result;

        this.ctx.save();
        this.ctx.font = this.font;

        if (this.textAlign == 'center') 
            result = this.pos.x - this.ctx.measureText(this.text).width / 2;
        else if (this.textAlign == 'right' || this.textAlign == 'end') 
            result = this.pos.x - this.ctx.measureText(this.text).width;
        else 
            result = this.pos.x;

        this.ctx.restore();

        return result;
    }
sbiermanlytle commented 10 years ago

Thanks for catching this, your solution has been integrated into 1.2