konva/src/shapes/Image.js

125 lines
2.8 KiB
JavaScript
Raw Normal View History

///////////////////////////////////////////////////////////////////////
// Image
///////////////////////////////////////////////////////////////////////
/**
* Image constructor
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
Kinetic.Image = function(config) {
this.setDefaultAttrs({
crop: {
x: 0,
y: 0,
}
});
this.shapeType = "Image";
config.drawFunc = function() {
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;
var cropX = this.attrs.crop.x;
var cropY = this.attrs.crop.y;
var cropWidth = this.attrs.crop.width;
var cropHeight = this.attrs.crop.height;
var canvas = this.getCanvas();
var context = this.getContext();
context.beginPath();
context.rect(0, 0, width, height);
context.closePath();
this.fill();
this.stroke();
// if cropping
if(!!cropWidth && !!cropHeight) {
this.drawImage(this.attrs.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height);
}
// no cropping
else {
this.drawImage(this.attrs.image, 0, 0, width, height);
}
}
};
// call super constructor
Kinetic.Shape.apply(this, [config]);
};
/*
* Image methods
*/
Kinetic.Image.prototype = {
/**
* set width and height
*/
setSize: function() {
var size = Kinetic.GlobalObject._getSize(Array.prototype.slice.call(arguments));
this.setAttrs(size);
},
/**
* return image size
*/
getSize: function() {
return {
width: this.attrs.width,
height: this.attrs.height
};
}
};
// extend Shape
Kinetic.GlobalObject.extend(Kinetic.Image, Kinetic.Shape);
// add setters and getters
Kinetic.GlobalObject.addSettersGetters(Kinetic.Image, ['height', 'width', 'image', 'crop']);
/**
* set width
2012-06-14 17:19:51 +08:00
* @name setWidth
* @methodOf Kinetic.Image.prototype
* @param {Number} width
*/
/**
* set height
2012-06-14 17:19:51 +08:00
* @name setHeight
* @methodOf Kinetic.Image.prototype
* @param {Number} height
*/
/**
* set image
2012-06-14 17:19:51 +08:00
* @name setImage
* @methodOf Kinetic.Image.prototype
* @param {ImageObject} image
*/
/**
* set crop
* @name setCrop
* @methodOf Kinetic.Image.prototype
* @param {Object} config
*/
/**
* get crop
2012-06-14 17:19:51 +08:00
* @name getCrop
* @methodOf Kinetic.Image.prototype
*/
/**
* get width
2012-06-14 17:19:51 +08:00
* @name getWidth
* @methodOf Kinetic.Image.prototype
*/
/**
* get height
2012-06-14 17:19:51 +08:00
* @name getHeight
* @methodOf Kinetic.Image.prototype
*/
/**
* get image
2012-06-14 17:19:51 +08:00
* @name getImage
* @methodOf Kinetic.Image.prototype
*/