Skip to content

Instantly share code, notes, and snippets.

@codemasta92
Created January 3, 2012 15:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save codemasta92/1555328 to your computer and use it in GitHub Desktop.
Save codemasta92/1555328 to your computer and use it in GitHub Desktop.
Button class with EaselJS
(function(k) {
var Button = function(a, x, y) {
this.initialize(a,x,y)
},p = Button.prototype = new DisplayObject;
p.left = 0;
p.top = 0;
p.ypos = 0;
p.image = null;
p.snapToPixel = true;
p.DisplayObject_initialize = p.initialize;
p.initialize = function(a,x,y) {
this.DisplayObject_initialize();
if (typeof a == "string") {
this.image = new Image();
this.image.src = a;
} else {
this.image = a;
}
this.top = y;
this.left = x;
};
p.isVisible = function() {
return this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0 && this.image && (this.image.complete || this.image.getContext || this.image.readyState == 2)
};
p.DisplayObject_draw = p.draw;
p.draw = function(ctx, ignoreCache) {
if (this.DisplayObject_draw(ctx, ignoreCache))return true;
ctx.drawImage(this.image, 0,this.ypos, this.image.width, this.image.width, this.left, this.top, this.image.width, this.image.width);
return true
};
p.clone = function() {
var a = new Button(this.image, 0, 0);
this.cloneProps(a);
return a
};
p.toString = function() {
return"[Button (name=" + this.name + ")]"
};
p.onMouseOver = function(){
this.ypos = Math.floor(this.image.height/3);
}
p.onMouseOut = function() {
this.ypos = 0;
}
p.onClick = function(){
console.log("mousedown")
this.ypos = Math.floor(this.image.height/3*2);
}
k.Button = Button
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment