2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Image
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Image constructor
|
|
|
|
* @constructor
|
2012-06-25 03:44:08 +08:00
|
|
|
* @augments Kinetic.Rect
|
2012-03-07 13:45:48 +08:00
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Image = function(config) {
|
2012-06-25 03:44:08 +08:00
|
|
|
this.shapeType = "Image";
|
|
|
|
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Rect.apply(this, [config]);
|
|
|
|
|
|
|
|
// update attrs when one of the following changes
|
2012-06-25 04:39:36 +08:00
|
|
|
this.on('widthChange', this._setAttrs);
|
|
|
|
this.on('heightChange', this._setAttrs);
|
|
|
|
this.on('imageChange', this._setAttrs);
|
|
|
|
this.on('cropChange', this._setAttrs);
|
2012-04-29 03:55:18 +08:00
|
|
|
|
2012-06-25 03:44:08 +08:00
|
|
|
this._setAttrs();
|
|
|
|
};
|
|
|
|
|
|
|
|
Kinetic.Image.prototype = {
|
|
|
|
_setAttrs: function() {
|
|
|
|
var a = this.attrs;
|
|
|
|
if(a.image) {
|
|
|
|
if(!a.width) {
|
|
|
|
a.width = a.image.width;
|
|
|
|
}
|
|
|
|
if(!a.height) {
|
|
|
|
a.height = a.image.height;
|
|
|
|
}
|
2012-04-28 10:08:45 +08:00
|
|
|
|
2012-06-25 03:44:08 +08:00
|
|
|
var scale;
|
|
|
|
var offset;
|
2012-04-28 10:08:45 +08:00
|
|
|
|
2012-06-25 03:44:08 +08:00
|
|
|
if(a.crop) {
|
|
|
|
scale = [a.width / a.crop.width, a.height / a.crop.height];
|
2012-06-28 10:50:32 +08:00
|
|
|
offset = [-1 * a.crop.x, -1 * a.crop.y];
|
2012-04-28 10:08:45 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-06-25 03:44:08 +08:00
|
|
|
scale = [a.width / a.image.width, a.height / a.image.height];
|
2012-04-28 10:08:45 +08:00
|
|
|
}
|
2012-06-25 03:44:08 +08:00
|
|
|
|
|
|
|
this.setFill({
|
|
|
|
image: a.image,
|
|
|
|
repeat: 'no-repeat',
|
|
|
|
scale: scale,
|
|
|
|
offset: offset
|
|
|
|
});
|
2012-04-08 10:08:16 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
};
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-06-25 03:44:08 +08:00
|
|
|
// extend Rect
|
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Image, Kinetic.Rect);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-06-25 03:44:08 +08:00
|
|
|
// add setters and getters
|
|
|
|
Kinetic.GlobalObject.addSettersGetters(Kinetic.Image, ['image', 'crop']);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 image
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getImage
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|