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,
|
|
|
|
width: undefined,
|
|
|
|
height: undefined
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-04-06 14:48:58 +08:00
|
|
|
this.shapeType = "Image";
|
2012-03-07 13:45:48 +08:00
|
|
|
config.drawFunc = function() {
|
2012-04-08 10:08:16 +08:00
|
|
|
if(this.image !== undefined) {
|
|
|
|
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
|
|
|
|
var height = this.attrs.height !== undefined ? this.attrs.height : this.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-10 13:31:55 +08:00
|
|
|
this.applyStyles();
|
2012-04-28 10:08:45 +08:00
|
|
|
|
|
|
|
// if cropping
|
2012-05-14 08:04:15 +08:00
|
|
|
if(cropWidth !== undefined && cropHeight !== undefined) {
|
2012-04-28 10:08:45 +08:00
|
|
|
context.drawImage(this.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height);
|
|
|
|
}
|
|
|
|
// no cropping
|
|
|
|
else {
|
|
|
|
context.drawImage(this.image, 0, 0, width, height);
|
|
|
|
}
|
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 image
|
|
|
|
* @param {ImageObject} image
|
|
|
|
*/
|
|
|
|
setImage: function(image) {
|
|
|
|
this.image = image;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get image
|
|
|
|
*/
|
2012-03-12 14:01:23 +08:00
|
|
|
getImage: function() {
|
2012-03-07 13:45:48 +08:00
|
|
|
return this.image;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set width
|
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
setWidth: function(width) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.width = width;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get width
|
|
|
|
*/
|
|
|
|
getWidth: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.width;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set height
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
setHeight: function(height) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.height = height;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get height
|
|
|
|
*/
|
|
|
|
getHeight: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.height;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set width and height
|
|
|
|
* @param {Number} width
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
setSize: function(width, height) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.width = width;
|
|
|
|
this.attrs.height = height;
|
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-04-23 01:56:50 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* return cropping
|
|
|
|
*/
|
2012-04-28 10:08:45 +08:00
|
|
|
getCrop: function() {
|
|
|
|
return this.attrs.crop;
|
2012-04-23 01:56:50 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set cropping
|
2012-04-28 10:08:45 +08:00
|
|
|
* @param {Object} crop
|
|
|
|
* @config {Number} [x] crop x
|
|
|
|
* @config {Number} [y] crop y
|
|
|
|
* @config {Number} [width] crop width
|
|
|
|
* @config {Number} [height] crop height
|
2012-04-23 01:56:50 +08:00
|
|
|
*/
|
2012-04-28 10:08:45 +08:00
|
|
|
setCrop: function(config) {
|
|
|
|
var c = {};
|
|
|
|
c.crop = config;
|
|
|
|
this.setAttrs(c);
|
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);
|