mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-28 01:04:43 +00:00
New Tint filter
Added new Tint filter that will recolor a semi transparent image
This commit is contained in:
parent
d9a64e4529
commit
89402af132
1 changed files with 67 additions and 0 deletions
|
|
@ -430,4 +430,71 @@ fabric.Image.filters.GradientTransparency = fabric.util.createClass( /** @scope
|
|||
|
||||
fabric.Image.filters.GradientTransparency.fromObject = function(object) {
|
||||
return new fabric.Image.filters.GradientTransparency(object);
|
||||
};
|
||||
|
||||
/**
|
||||
* @class fabric.Image.filters.Tint
|
||||
* @memberOf fabric.Image.filters
|
||||
*/
|
||||
fabric.Image.filters.Tint = fabric.util.createClass( /** @scope fabric.Image.filters.Tint.prototype */ {
|
||||
|
||||
/**
|
||||
* @param {String} type
|
||||
*/
|
||||
type: "Tint",
|
||||
|
||||
/**
|
||||
* @memberOf fabric.Image.filters.RemoveWhite.prototype
|
||||
* @param {Object} [options] Options object
|
||||
*/
|
||||
initialize: function(options) {
|
||||
options || (options = { });
|
||||
this.color = options.color || 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method applyTo
|
||||
* @param {Object} canvasEl Canvas element to apply filter to
|
||||
*/
|
||||
applyTo: function(canvasEl) {
|
||||
|
||||
var context = canvasEl.getContext('2d'),
|
||||
imageData = context.getImageData(0, 0, canvasEl.width, canvasEl.height),
|
||||
data = imageData.data,
|
||||
iLen = data.length, i,
|
||||
r, g, b, a;
|
||||
|
||||
var rgb = parseInt(this.color).toString(16);
|
||||
var cr = parseInt('0x'+rgb.substr(0, 2));
|
||||
var cg = parseInt('0x'+rgb.substr(2, 2));
|
||||
var cb = parseInt('0x'+rgb.substr(4, 2));
|
||||
|
||||
for (i = 0; i < iLen; i+=4) {
|
||||
|
||||
a = data[i+3];
|
||||
|
||||
if (a > 0){
|
||||
data[i] = cr;
|
||||
data[i+1] = cg;
|
||||
data[i+2] = cb;
|
||||
}
|
||||
}
|
||||
|
||||
context.putImageData(imageData, 0, 0);
|
||||
},
|
||||
|
||||
/**
|
||||
* @method toJSON
|
||||
* @return {String} json representation of filter
|
||||
*/
|
||||
toJSON: function() {
|
||||
return {
|
||||
type: this.type,
|
||||
color: this.color
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
fabric.Image.filters.Tint.fromObject = function(object) {
|
||||
return new fabric.Image.filters.Tint(object);
|
||||
};
|
||||
Loading…
Reference in a new issue