code: function preloadImages( images, callback ) {
  
      function _preload( asset ) {
          asset.img = new Image();
          asset.img.src = 'img/' + asset.id+'.png';
  
          asset.img.addEventListener("load", function() {
              _check();
          }, false);
  
          asset.img.addEventListener("error", function(err) {
              _check(err, asset.id);
          }, false);
      }
  
      var loadc = 0;
      function _check( err, id ) {
          if ( err ) {
              alert('Failed to load ' + id );
          }
          loadc++;
          if ( images.length == loadc )
              return callback()
      }
  
      images.forEach(function(asset) {
          _preload( asset );
      });
  }
docs: Images must be preloaded before they are used to draw into canvas