(function() {

  $sprites = {};

  this.def_tag('CANVAS_TAG',"NODE_TAG",function(__scope,__class){

    if(!this.__ctor__){this.__ctor__= function CANVAS_TAG(){__class.__super__.__ctor__.apply(this, arguments)}; this.__ctor__.prototype = this.prototype; }

    this.prototype["width="] = function(width) {
      return this["@node"].width = width;
    }; __method_added(this,"width=");

    this.prototype["height="] = function(height) {
      return this["@node"].height = height;
    }; __method_added(this,"height=");

    this.prototype.draw_line = function(x1, y1, x2, y2, options) {
      var c;
      c = this.ctx();
      if (options.style) c.strokeStyle = options.style;
      if (options.width) c.lineWidth = options.width;
      c.beginPath();
      c.moveTo(x1, y1);
      c.lineTo(x2, y2);
      return c.stroke();
    }; __method_added(this,"draw_line");

    this.prototype.draw_image = function(image, x, y, w, h) {
      var _this = this;
      if (typeof image === 'string') {
        image = this.get_image(image, function(img) {
          return _this.ctx().drawImage(img, x, y, w, h);
        });
      }
      if (image) return this.ctx().drawImage(image, x, y, w, h);
    }; __method_added(this,"draw_image");

    this.prototype.fill_rect = function(color, x, y, w, h) {
      if (color) this.ctx().fillStyle = color;
      this.ctx().fillRect(x || 0, y || 0, w || this.width(), h || this.height());
      return this;
    }; __method_added(this,"fill_rect");

    this.prototype.ctx = function() {
      return this["@ctx"] || (this["@ctx"] = this["@node"].getContext('2d'));
    }; __method_added(this,"ctx");

    this.prototype.get_image = function(path, action) {
      var img;
      var _this = this;
      if (img = $sprites[path]) {
        if (img.complete) return img;
        img["@queue"].push(action);
        return nil;
      }
      img = $sprites[path] = new (__ctor(__scope.Image))();
      img["@queue"] = [action];
      img.onload = function() {
        img["@queue"].each(function(cb) {
          return cb(img);
        });
        return img["@queue"] = [];
      };
      img.src = path;
      return nil;
    }; __method_added(this,"get_image");

  });

}).call($$base);

