Added clipping to Image

This commit is contained in:
Lewis Peckover 2012-04-22 18:56:50 +01:00
parent d3eac4d5f9
commit dedf276c74

View File

@ -12,6 +12,8 @@ Kinetic.Image = function(config) {
if(this.attrs === undefined) { if(this.attrs === undefined) {
this.attrs = {}; this.attrs = {};
} }
this.attrs.cropX = 0;
this.attrs.cropY = 0;
// special // special
this.image = config.image; this.image = config.image;
@ -21,6 +23,8 @@ Kinetic.Image = function(config) {
if(this.image !== undefined) { if(this.image !== undefined) {
var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width; var width = this.attrs.width !== undefined ? this.attrs.width : this.image.width;
var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height; var height = this.attrs.height !== undefined ? this.attrs.height : this.image.height;
var cropWidth = this.attrs.cropWidth !== undefined ? this.attrs.cropWidth : this.image.width;
var cropHeight = this.attrs.cropHeight !== undefined ? this.attrs.cropHeight : this.image.height;
var canvas = this.getCanvas(); var canvas = this.getCanvas();
var context = this.getContext(); var context = this.getContext();
context.beginPath(); context.beginPath();
@ -28,7 +32,7 @@ Kinetic.Image = function(config) {
context.rect(0, 0, width, height); context.rect(0, 0, width, height);
context.closePath(); context.closePath();
this.fillStroke(); this.fillStroke();
context.drawImage(this.image, 0, 0, width, height); context.drawImage(this.image,this.attrs.cropX, this.attrs.cropY, cropWidth, cropHeight, 0, 0, width, height);
} }
}; };
// call super constructor // call super constructor
@ -94,6 +98,30 @@ Kinetic.Image.prototype = {
width: this.attrs.width, width: this.attrs.width,
height: this.attrs.height height: this.attrs.height
}; };
},
/**
* return cropping
*/
getCropping: function() {
return {
cropX: this.attrs.cropX,
cropY: this.attrs.cropY,
cropWidth: this.attrs.cropWidth,
cropHeight: this.attrs.cropHeight
};
},
/**
* set cropping
* @param {Number} cropX
* @param {Number} cropY
* @param {Number} cropWidth
* @param {Number} cropHeight
*/
setCropping: function() {
this.attrs.cropX = cropX;
this.attrs.cropY = cropY;
this.attrs.cropWidth = cropWidth;
this.attrs.cropHeight = cropHeight;
} }
}; };
// extend Shape // extend Shape