2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Image
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Image constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Image = function(config) {
|
2012-04-29 03:55:18 +08:00
|
|
|
this.setDefaultAttrs({
|
|
|
|
crop: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-04-06 14:48:58 +08:00
|
|
|
this.shapeType = "Image";
|
2012-03-07 13:45:48 +08:00
|
|
|
config.drawFunc = function() {
|
2012-06-23 04:15:29 +08:00
|
|
|
if(!!this.attrs.image) {
|
|
|
|
var width = !!this.attrs.width ? this.attrs.width : this.attrs.image.width;
|
|
|
|
var height = !!this.attrs.height ? this.attrs.height : this.attrs.image.height;
|
2012-04-28 10:08:45 +08:00
|
|
|
var cropX = this.attrs.crop.x;
|
|
|
|
var cropY = this.attrs.crop.y;
|
|
|
|
var cropWidth = this.attrs.crop.width;
|
|
|
|
var cropHeight = this.attrs.crop.height;
|
2012-04-08 10:08:16 +08:00
|
|
|
var canvas = this.getCanvas();
|
|
|
|
var context = this.getContext();
|
2012-04-28 10:08:45 +08:00
|
|
|
|
2012-04-08 10:08:16 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, width, height);
|
|
|
|
context.closePath();
|
2012-05-27 11:34:36 +08:00
|
|
|
this.fill();
|
|
|
|
this.stroke();
|
2012-04-28 10:08:45 +08:00
|
|
|
|
|
|
|
// if cropping
|
2012-06-23 04:15:29 +08:00
|
|
|
if(!!cropWidth && !!cropHeight) {
|
2012-05-27 11:34:36 +08:00
|
|
|
this.drawImage(this.attrs.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height);
|
2012-04-28 10:08:45 +08:00
|
|
|
}
|
|
|
|
// no cropping
|
|
|
|
else {
|
2012-05-27 11:34:36 +08:00
|
|
|
this.drawImage(this.attrs.image, 0, 0, width, height);
|
2012-04-28 10:08:45 +08:00
|
|
|
}
|
2012-04-08 10:08:16 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
};
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.apply(this, [config]);
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Image methods
|
|
|
|
*/
|
|
|
|
Kinetic.Image.prototype = {
|
|
|
|
/**
|
|
|
|
* set width and height
|
|
|
|
*/
|
2012-05-27 07:37:37 +08:00
|
|
|
setSize: function() {
|
2012-06-21 03:55:34 +08:00
|
|
|
var size = Kinetic.GlobalObject._getSize(Array.prototype.slice.call(arguments));
|
2012-05-29 14:46:40 +08:00
|
|
|
this.setAttrs(size);
|
2012-03-31 15:08:50 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* return image size
|
|
|
|
*/
|
|
|
|
getSize: function() {
|
|
|
|
return {
|
2012-04-06 14:48:58 +08:00
|
|
|
width: this.attrs.width,
|
|
|
|
height: this.attrs.height
|
2012-03-31 15:08:50 +08:00
|
|
|
};
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// extend Shape
|
2012-03-12 14:01:23 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Image, Kinetic.Shape);
|
2012-06-11 04:07:09 +08:00
|
|
|
// add setters and getters
|
2012-06-24 09:09:10 +08:00
|
|
|
Kinetic.GlobalObject.addSettersGetters(Kinetic.Image, ['height', 'width', 'image', 'crop']);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setWidth
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set height
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setHeight
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set image
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setImage
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {ImageObject} image
|
|
|
|
*/
|
|
|
|
|
2012-06-24 09:09:10 +08:00
|
|
|
/**
|
|
|
|
* set crop
|
|
|
|
* @name setCrop
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
|
2012-06-11 04:07:09 +08:00
|
|
|
/**
|
|
|
|
* get crop
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getCrop
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getWidth
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get height
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getHeight
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get image
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getImage
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|