2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Image
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2012-07-09 12:56:52 +08:00
|
|
|
/**
|
|
|
|
* Image constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
2012-07-17 15:32:26 +08:00
|
|
|
* @param {ImageObject} config.image
|
2012-07-15 07:25:56 +08:00
|
|
|
* @param {Number} [config.width]
|
|
|
|
* @param {Number} [config.height]
|
|
|
|
* @param {Object} [config.crop]
|
2012-07-09 12:56:52 +08:00
|
|
|
*/
|
2012-07-04 03:07:27 +08:00
|
|
|
Kinetic.Image = Kinetic.Shape.extend({
|
|
|
|
init: function(config) {
|
|
|
|
this.shapeType = "Image";
|
2012-07-29 01:46:16 +08:00
|
|
|
config.drawFunc = this.drawFunc;
|
|
|
|
// call super constructor
|
|
|
|
this._super(config);
|
|
|
|
},
|
|
|
|
drawFunc: function(context) {
|
2012-08-15 14:52:53 +08:00
|
|
|
var width = this.getWidth();
|
|
|
|
var height = this.getHeight();
|
2012-06-25 03:44:08 +08:00
|
|
|
|
2012-08-15 14:52:53 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, width, height);
|
|
|
|
context.closePath();
|
|
|
|
this.fill(context);
|
|
|
|
this.stroke(context);
|
|
|
|
|
|
|
|
if(this.attrs.image) {
|
|
|
|
context.beginPath();
|
2012-07-29 01:46:16 +08:00
|
|
|
// if cropping
|
|
|
|
if(this.attrs.crop && this.attrs.crop.width && this.attrs.crop.height) {
|
|
|
|
var cropX = this.attrs.crop.x ? this.attrs.crop.x : 0;
|
|
|
|
var cropY = this.attrs.crop.y ? this.attrs.crop.y : 0;
|
|
|
|
var cropWidth = this.attrs.crop.width;
|
|
|
|
var cropHeight = this.attrs.crop.height;
|
|
|
|
this.drawImage(context, this.attrs.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height);
|
2012-04-28 10:08:45 +08:00
|
|
|
}
|
2012-07-29 01:46:16 +08:00
|
|
|
// no cropping
|
|
|
|
else {
|
|
|
|
this.drawImage(context, this.attrs.image, 0, 0, width, height);
|
|
|
|
}
|
|
|
|
}
|
2012-07-04 03:07:27 +08:00
|
|
|
},
|
2012-07-02 07:48:04 +08:00
|
|
|
/**
|
|
|
|
* set width and height
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name setSize
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-07-02 07:48:04 +08:00
|
|
|
*/
|
|
|
|
setSize: function() {
|
2012-07-23 08:05:45 +08:00
|
|
|
var size = Kinetic.Type._getSize(Array.prototype.slice.call(arguments));
|
2012-07-02 07:48:04 +08:00
|
|
|
this.setAttrs(size);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* return image size
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getSize
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-07-02 07:48:04 +08:00
|
|
|
*/
|
|
|
|
getSize: function() {
|
|
|
|
return {
|
|
|
|
width: this.attrs.width,
|
|
|
|
height: this.attrs.height
|
|
|
|
};
|
2012-07-17 15:32:26 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get width
|
|
|
|
* @name getWidth
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
getWidth: function() {
|
|
|
|
if(this.attrs.width) {
|
|
|
|
return this.attrs.width;
|
|
|
|
}
|
|
|
|
if(this.attrs.image) {
|
|
|
|
return this.attrs.image.width;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get height
|
|
|
|
* @name getHeight
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
getHeight: function() {
|
|
|
|
if(this.attrs.height) {
|
|
|
|
return this.attrs.height;
|
|
|
|
}
|
|
|
|
if(this.attrs.image) {
|
|
|
|
return this.attrs.image.height;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* apply filter
|
|
|
|
* @name applyFilter
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {Function} config.filter filter function
|
|
|
|
* @param {Function} [config.callback] callback function to be called once
|
|
|
|
* filter has been applied
|
|
|
|
*/
|
|
|
|
applyFilter: function(config) {
|
|
|
|
try {
|
2012-07-20 14:30:59 +08:00
|
|
|
var trans = this._clearTransform();
|
|
|
|
this.saveImageData(this.getWidth(), this.getHeight());
|
|
|
|
this._setTransform(trans);
|
2012-07-17 15:32:26 +08:00
|
|
|
|
|
|
|
config.filter.call(this, config);
|
|
|
|
var that = this;
|
|
|
|
Kinetic.Type._getImage(this.getImageData(), function(imageObj) {
|
|
|
|
that.setImage(imageObj);
|
|
|
|
|
|
|
|
if(config.callback) {
|
|
|
|
config.callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Kinetic.Global.warn('Unable to apply filter.');
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
2012-07-04 03:07:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// add getters setters
|
2012-07-17 15:32:26 +08:00
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Image, ['image', 'crop', 'filter']);
|
|
|
|
Kinetic.Node.addSetters(Kinetic.Image, ['width', 'height']);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-07-02 07:48:04 +08:00
|
|
|
/**
|
|
|
|
* set width
|
|
|
|
* @name setWidth
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Number} width
|
|
|
|
*/
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-07-02 07:48:04 +08:00
|
|
|
/**
|
|
|
|
* set height
|
|
|
|
* @name setHeight
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
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
|
|
|
/**
|
2012-07-17 15:32:26 +08:00
|
|
|
* set filter
|
|
|
|
* @name setFilter
|
2012-06-14 17:19:51 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-07-17 15:32:26 +08:00
|
|
|
* @param {Object} config
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
2012-07-02 07:48:04 +08:00
|
|
|
/**
|
2012-07-17 15:32:26 +08:00
|
|
|
* get crop
|
|
|
|
* @name getCrop
|
2012-07-02 07:48:04 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-07-17 15:32:26 +08:00
|
|
|
* get image
|
|
|
|
* @name getImage
|
2012-07-02 07:48:04 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
|
2012-06-11 04:07:09 +08:00
|
|
|
/**
|
2012-07-17 15:32:26 +08:00
|
|
|
* get filter
|
|
|
|
* @name getFilter
|
2012-06-14 17:19:51 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|