2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Image constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {ImageObject} config.image
|
|
|
|
* @param {Object} [config.crop]
|
2013-01-27 12:42:19 +08:00
|
|
|
* {{ShapeParams}}
|
|
|
|
* {{NodeParams}}
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
Kinetic.Image = function(config) {
|
|
|
|
this._initImage(config);
|
|
|
|
};
|
2012-11-29 12:50:33 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Image.prototype = {
|
|
|
|
_initImage: function(config) {
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
2013-01-01 16:41:13 +08:00
|
|
|
this.shapeType = 'Image';
|
2012-12-02 04:04:10 +08:00
|
|
|
this._setDrawFuncs();
|
2012-11-29 12:50:33 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
var that = this;
|
|
|
|
this.on('imageChange', function(evt) {
|
|
|
|
that._syncSize();
|
|
|
|
});
|
2012-11-13 11:59:19 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
this._syncSize();
|
|
|
|
},
|
2012-12-10 01:52:33 +08:00
|
|
|
drawFunc: function(canvas) {
|
|
|
|
var width = this.getWidth(), height = this.getHeight(), params, that = this, context = canvas.getContext();
|
2012-11-29 12:50:33 +08:00
|
|
|
|
2012-11-24 15:55:20 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, width, height);
|
|
|
|
context.closePath();
|
2012-12-10 01:52:33 +08:00
|
|
|
canvas.fillStroke(this);
|
2012-07-17 15:32:26 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
if(this.attrs.image) {
|
|
|
|
// if cropping
|
|
|
|
if(this.attrs.crop && this.attrs.crop.width && this.attrs.crop.height) {
|
|
|
|
var cropX = this.attrs.crop.x || 0;
|
|
|
|
var cropY = this.attrs.crop.y || 0;
|
|
|
|
var cropWidth = this.attrs.crop.width;
|
|
|
|
var cropHeight = this.attrs.crop.height;
|
2012-12-10 01:52:33 +08:00
|
|
|
params = [this.attrs.image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height];
|
2012-07-17 15:32:26 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// no cropping
|
|
|
|
else {
|
2012-12-10 01:52:33 +08:00
|
|
|
params = [this.attrs.image, 0, 0, width, height];
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
|
2012-12-31 16:45:38 +08:00
|
|
|
if(this.hasShadow()) {
|
2012-12-10 01:52:33 +08:00
|
|
|
canvas.applyShadow(this, function() {
|
|
|
|
that._drawImage(context, params);
|
2012-12-02 04:04:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2012-12-10 01:52:33 +08:00
|
|
|
this._drawImage(context, params);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2012-12-10 01:52:33 +08:00
|
|
|
drawHitFunc: function(canvas) {
|
|
|
|
var width = this.getWidth(), height = this.getHeight(), imageHitRegion = this.imageHitRegion, appliedShadow = false, context = canvas.getContext();
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
if(imageHitRegion) {
|
2012-12-10 01:52:33 +08:00
|
|
|
context.drawImage(imageHitRegion, 0, 0, width, height);
|
2012-12-02 04:04:10 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, width, height);
|
|
|
|
context.closePath();
|
2012-12-10 01:52:33 +08:00
|
|
|
canvas.stroke(this);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, width, height);
|
|
|
|
context.closePath();
|
2012-12-10 01:52:33 +08:00
|
|
|
canvas.fillStroke(this);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* apply filter
|
|
|
|
* @name applyFilter
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {Function} filter filter function
|
|
|
|
* @param {Object} [config] optional config object used to configure filter
|
|
|
|
* @param {Function} [callback] callback function to be called once
|
|
|
|
* filter has been applied
|
|
|
|
*/
|
|
|
|
applyFilter: function(filter, config, callback) {
|
|
|
|
var canvas = new Kinetic.Canvas(this.attrs.image.width, this.attrs.image.height);
|
|
|
|
var context = canvas.getContext();
|
|
|
|
context.drawImage(this.attrs.image, 0, 0);
|
|
|
|
try {
|
|
|
|
var imageData = context.getImageData(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
|
|
filter(imageData, config);
|
|
|
|
var that = this;
|
|
|
|
Kinetic.Type._getImage(imageData, function(imageObj) {
|
|
|
|
that.setImage(imageObj);
|
|
|
|
|
|
|
|
if(callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Kinetic.Global.warn('Unable to apply filter. ' + e.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set crop
|
|
|
|
* @name setCrop
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Object|Array} config
|
|
|
|
* @param {Number} config.x
|
|
|
|
* @param {Number} config.y
|
|
|
|
* @param {Number} config.width
|
|
|
|
* @param {Number} config.height
|
|
|
|
*/
|
|
|
|
setCrop: function() {
|
|
|
|
var config = [].slice.call(arguments);
|
|
|
|
var pos = Kinetic.Type._getXY(config);
|
|
|
|
var size = Kinetic.Type._getSize(config);
|
|
|
|
var both = Kinetic.Type._merge(pos, size);
|
|
|
|
this.setAttr('crop', Kinetic.Type._merge(both, this.getCrop()));
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* create image hit region which enables more accurate hit detection mapping of the image
|
|
|
|
* by avoiding event detections for transparent pixels
|
|
|
|
* @name createImageHitRegion
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
* @param {Function} [callback] callback function to be called once
|
|
|
|
* the image hit region has been created
|
|
|
|
*/
|
|
|
|
createImageHitRegion: function(callback) {
|
|
|
|
var canvas = new Kinetic.Canvas(this.attrs.width, this.attrs.height);
|
|
|
|
var context = canvas.getContext();
|
|
|
|
context.drawImage(this.attrs.image, 0, 0);
|
|
|
|
try {
|
|
|
|
var imageData = context.getImageData(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
|
|
var data = imageData.data;
|
|
|
|
var rgbColorKey = Kinetic.Type._hexToRgb(this.colorKey);
|
|
|
|
// replace non transparent pixels with color key
|
|
|
|
for(var i = 0, n = data.length; i < n; i += 4) {
|
|
|
|
data[i] = rgbColorKey.r;
|
|
|
|
data[i + 1] = rgbColorKey.g;
|
|
|
|
data[i + 2] = rgbColorKey.b;
|
|
|
|
// i+3 is alpha (the fourth element)
|
|
|
|
}
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
Kinetic.Type._getImage(imageData, function(imageObj) {
|
|
|
|
that.imageHitRegion = imageObj;
|
|
|
|
if(callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Kinetic.Global.warn('Unable to create image hit region. ' + e.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* clear image hit region
|
|
|
|
* @name clearImageHitRegion
|
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
clearImageHitRegion: function() {
|
|
|
|
delete this.imageHitRegion;
|
|
|
|
},
|
|
|
|
_syncSize: function() {
|
|
|
|
if(this.attrs.image) {
|
|
|
|
if(!this.attrs.width) {
|
|
|
|
this.setWidth(this.attrs.image.width);
|
|
|
|
}
|
|
|
|
if(!this.attrs.height) {
|
|
|
|
this.setHeight(this.attrs.image.height);
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 01:52:33 +08:00
|
|
|
},
|
|
|
|
_drawImage: function(context, a) {
|
|
|
|
if(a.length === 5) {
|
|
|
|
context.drawImage(a[0], a[1], a[2], a[3], a[4]);
|
|
|
|
}
|
|
|
|
else if(a.length === 9) {
|
|
|
|
context.drawImage(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
|
|
|
|
}
|
2012-07-17 15:32:26 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
Kinetic.Global.extend(Kinetic.Image, Kinetic.Shape);
|
|
|
|
|
|
|
|
// add getters setters
|
2013-03-15 23:33:05 +08:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Image, 'image');
|
|
|
|
Kinetic.Node.addGetter(Kinetic.Image, 'crop');
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2012-09-25 11:34:23 +08:00
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* set image
|
|
|
|
* @name setImage
|
2012-09-25 11:34:23 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {ImageObject} image
|
2012-09-25 11:34:23 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2012-08-19 13:02:16 +08:00
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* get crop
|
|
|
|
* @name getCrop
|
2012-08-19 13:02:16 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* get image
|
|
|
|
* @name getImage
|
2012-08-19 13:02:16 +08:00
|
|
|
* @methodOf Kinetic.Image.prototype
|
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
})();
|