define('Canvas',

['util', 'vent'],

function( util, vent ){

    'use strict';

    function Canvas( targetId, opts ){
        this.parent = util.getById(targetId);
        if ( this.parent === null ) {
            throw new Error('Need a valid canvas container target');
        }
        this.elem = document.createElement('canvas');
        this.elem.id = 'game-canvas';
        this.parent.appendChild(this.elem);
        this.ctx = this.elem.getContext('2d');

        this.offscreen = document.createElement('canvas');
        this.offscreenCtx = this.offscreen.getContext('2d');

        if ( opts !== undefined ) {
            this.height = this.elem.height = opts.height;
            this.width = this.elem.width = opts.width;
            this.offscreen.height = opts.height;
            this.offscreen.width = opts.width;
            this.bgColor = opts.bgColor;
        }

        this.entityCache = [];
        this.laserCache = [];
        this.hudCache = [];
        this.bgCache = [];
        this.bindEvents();
    }

    var proto = Canvas.prototype;


    proto.get = function( attr ){
        return this.elem.getAttribute(attr);
    };


    proto.bindEvents = function(){
        vent.on('render', this.render.bind(this));
        vent.on('bg-item-added', this.addBGItem.bind(this));
        vent.on('entity-added', this.addSprite.bind(this));
        vent.on('entity-destroyed', this.destroySprite.bind(this));
        vent.on('laser-added', this.addLaser.bind(this));
        vent.on('laser-destroyed', this.destroyLaser.bind(this));
        vent.on('hud-item-added', this.addHUDItem.bind(this));
    };


    proto.render = function( ){
        this.ctx.clearRect(0, 0, this.width, this.height);
        // this.ctx.fillStyle = this.bgColor;
        // this.ctx.fillRect(0, 0, this.width, this.height);

        this.bgCache.forEach(function( sprite ){
            sprite.render();
        });
        this.laserCache.forEach(function( sprite ){
            sprite.render();
        });
        this.entityCache.forEach(function( sprite ){
            sprite.render();
        });
    };


    proto.addSprite = function( sprite ){
        this.entityCache.push(sprite);
    };


    proto.addLaser = function( sprite ){
        this.laserCache.push(sprite);
        vent.emit('laser-cache-updated', this.laserCache);
    };


    proto.destroyLaser = function( sprite ) {
        var target;
        this.laserCache.forEach(function( obj, key ){
            if ( obj.id === sprite.id ) {
                target = key;
            }
        });
        this.laserCache.splice(target, 1);
    };


    proto.destroySprite = function( sprite ) {
        var target;
        this.entityCache.forEach(function( obj, key ){
            if ( obj.id === sprite.id ) {
                target = key;
            }
        });
        this.entityCache.splice(target, 1);
    };


    proto.addHUDItem = function(){

    };


    proto.addBGItem = function( sprite ){
        this.bgCache.push(sprite);
    };


    return Canvas;

});

@@ -1667,69 +1667,23 @@
     
-this.ctx.clearRect(0, 0, this.width, this.height)
+var i
 ;%0A
+%0A
        
- //
  thi
@@ -1692,163 +1692,130 @@
 ctx.
-fillStyle = this.bgColor;%0A        // this.ctx.fillRect(0, 0, this.width, this.height);%0A%0A        this.bgCache.forEach(function( sprite )%7B%0A            sprite
+clearRect(0, 0, this.width, this.height);%0A%0A        for (i = 0; i %3C this.bgCache.length; i++) %7B%0A            this.bgCache%5Bi%5D
 .ren
@@ -1822,34 +1822,32 @@
 der();%0A        %7D
-);
 %0A        this.la
@@ -1843,70 +1843,85 @@
     
-this.laserCache.forEach(function( sprite )%7B%0A            sprite
+for (i = 0; i %3C this.laserCache.length; i++) %7B%0A            this.laserCache%5Bi%5D
 .ren
@@ -1932,34 +1932,32 @@
 );%0A        %7D
-);
 %0A       
  this.entity
@@ -1936,32 +1936,48 @@
        %7D%0A       
+ for (i = 0; i %3C
  this.entityCach
@@ -1982,54 +1982,54 @@
 che.
-forEach(function( sprite )%7B%0A            sprite
+length; i++) %7B%0A            this.entityCache%5Bi%5D
 .ren
@@ -2040,26 +2040,24 @@
 );%0A        %7D
-);
 %0A    %7D;%0A%0A%0A  

use for loops instead of forEach (anonymous functions == bad for GC perf)