diff --git a/build.js b/build.js index ae515fd1..a55e4755 100644 --- a/build.js +++ b/build.js @@ -176,7 +176,7 @@ var filesToInclude = [ ifSpecifiedInclude('object_straightening', 'src/mixins/object_straightening.mixin.js'), - ifSpecifiedInclude('image_filters', 'src/filters/filter.js'), + ifSpecifiedInclude('image_filters', 'src/filters/base_filter.class.js'), ifSpecifiedInclude('image_filters', 'src/filters/brightness_filter.class.js'), ifSpecifiedInclude('image_filters', 'src/filters/convolute_filter.class.js'), ifSpecifiedInclude('image_filters', 'src/filters/gradienttransparency_filter.class.js'), diff --git a/src/filters/base_filter.class.js b/src/filters/base_filter.class.js new file mode 100644 index 00000000..1462e40e --- /dev/null +++ b/src/filters/base_filter.class.js @@ -0,0 +1,38 @@ +/** + * @namespace fabric.Image.filters + * @memberOf fabric.Image + */ +fabric.Image.filters = fabric.Image.filters || { }; + + +/** + * Root filter class from which all filter classes inherit from + * @class fabric.Image.filters.BaseFilter + * @memberOf fabric.Image.filters + */ +fabric.Image.filters.BaseFilter = fabric.util.createClass(/** @lends fabric.Image.filters.BaseFilter.prototype */ { + + /** + * Filter type + * @param {String} type + * @default + */ + type: 'BaseFilter', + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return { type: this.type }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); + } +}); diff --git a/src/filters/brightness_filter.class.js b/src/filters/brightness_filter.class.js index 70a21c65..f447bd41 100644 --- a/src/filters/brightness_filter.class.js +++ b/src/filters/brightness_filter.class.js @@ -1,73 +1,73 @@ -/** - * Brightness filter class - * @class fabric.Image.filters.Brightness - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Brightness = fabric.util.createClass(/** @lends fabric.Image.filters.Brightness.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Brightness filter class + * @class fabric.Image.filters.Brightness + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Brightness', + fabric.Image.filters.Brightness = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Brightness.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.Brightness.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.brightness = options.brightness || 100; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Brightness', - /** - * Applies filter to canvas element - * @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, - brightness = this.brightness; + /** + * Constructor + * @memberOf fabric.Image.filters.Brightness.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.brightness = options.brightness || 100; + }, - for (var i = 0, len = data.length; i < len; i += 4) { - data[i] += brightness; - data[i + 1] += brightness; - data[i + 2] += brightness; + /** + * Applies filter to canvas element + * @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, + brightness = this.brightness; + + for (var i = 0, len = data.length; i < len; i += 4) { + data[i] += brightness; + data[i + 1] += brightness; + data[i + 2] += brightness; + } + + context.putImageData(imageData, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + brightness: this.brightness + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.Brightness} Instance of fabric.Image.filters.Brightness */ - toObject: function() { - return { - type: this.type, - brightness: this.brightness - }; - }, + fabric.Image.filters.Brightness.fromObject = function(object) { + return new fabric.Image.filters.Brightness(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.Brightness} Instance of fabric.Image.filters.Brightness - */ -fabric.Image.filters.Brightness.fromObject = function(object) { - return new fabric.Image.filters.Brightness(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/convolute_filter.class.js b/src/filters/convolute_filter.class.js index f8de639e..23de87b1 100644 --- a/src/filters/convolute_filter.class.js +++ b/src/filters/convolute_filter.class.js @@ -1,125 +1,125 @@ -/** - * Adapted from html5rocks article - * @class fabric.Image.filters.Convolute - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Convolute = fabric.util.createClass(/** @lends fabric.Image.filters.Convolute.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Adapted from html5rocks article + * @class fabric.Image.filters.Convolute + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Convolute', + fabric.Image.filters.Convolute = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Convolute.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.Convolute.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Convolute', - this.opaque = options.opaque; - this.matrix = options.matrix || [ 0, 0, 0, - 0, 1, 0, - 0, 0, 0 ]; + /** + * Constructor + * @memberOf fabric.Image.filters.Convolute.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; - var canvasEl = fabric.util.createCanvasElement(); - this.tmpCtx = canvasEl.getContext('2d'); - }, + this.opaque = options.opaque; + this.matrix = options.matrix || [ 0, 0, 0, + 0, 1, 0, + 0, 0, 0 ]; - /** - * @private - */ - _createImageData: function(w, h) { - return this.tmpCtx.createImageData(w, h); - }, + var canvasEl = fabric.util.createCanvasElement(); + this.tmpCtx = canvasEl.getContext('2d'); + }, - /** - * Applies filter to canvas element - * @param {Object} canvasEl Canvas element to apply filter to - */ - applyTo: function(canvasEl) { - var weights = this.matrix; - var context = canvasEl.getContext('2d'); - var pixels = context.getImageData(0, 0, canvasEl.width, canvasEl.height); + /** + * @private + */ + _createImageData: function(w, h) { + return this.tmpCtx.createImageData(w, h); + }, - var side = Math.round(Math.sqrt(weights.length)); - var halfSide = Math.floor(side/2); - var src = pixels.data; - var sw = pixels.width; - var sh = pixels.height; + /** + * Applies filter to canvas element + * @param {Object} canvasEl Canvas element to apply filter to + */ + applyTo: function(canvasEl) { + var weights = this.matrix; + var context = canvasEl.getContext('2d'); + var pixels = context.getImageData(0, 0, canvasEl.width, canvasEl.height); - // pad output by the convolution matrix - var w = sw; - var h = sh; - var output = this._createImageData(w, h); + var side = Math.round(Math.sqrt(weights.length)); + var halfSide = Math.floor(side/2); + var src = pixels.data; + var sw = pixels.width; + var sh = pixels.height; - var dst = output.data; + // pad output by the convolution matrix + var w = sw; + var h = sh; + var output = this._createImageData(w, h); - // go through the destination image pixels - var alphaFac = this.opaque ? 1 : 0; - for (var y=0; y= 0 && scy < sh && scx >= 0 && scx < sw) { - var srcOff = (scy*sw+scx)*4; - var wt = weights[cy*side+cx]; - r += src[srcOff] * wt; - g += src[srcOff+1] * wt; - b += src[srcOff+2] * wt; - a += src[srcOff+3] * wt; + var dst = output.data; + + // go through the destination image pixels + var alphaFac = this.opaque ? 1 : 0; + for (var y=0; y= 0 && scy < sh && scx >= 0 && scx < sw) { + var srcOff = (scy*sw+scx)*4; + var wt = weights[cy*side+cx]; + r += src[srcOff] * wt; + g += src[srcOff+1] * wt; + b += src[srcOff+2] * wt; + a += src[srcOff+3] * wt; + } } } + dst[dstOff] = r; + dst[dstOff+1] = g; + dst[dstOff+2] = b; + dst[dstOff+3] = a + alphaFac*(255-a); } - dst[dstOff] = r; - dst[dstOff+1] = g; - dst[dstOff+2] = b; - dst[dstOff+3] = a + alphaFac*(255-a); } + + context.putImageData(output, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + opaque: this.opaque, + matrix: this.matrix + }); } - - context.putImageData(output, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.Convolute} Instance of fabric.Image.filters.Convolute */ - toObject: function() { - return { - type: this.type, - opaque: this.opaque, - matrix: this.matrix - }; - }, + fabric.Image.filters.Convolute.fromObject = function(object) { + return new fabric.Image.filters.Convolute(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.Convolute} Instance of fabric.Image.filters.Convolute - */ -fabric.Image.filters.Convolute.fromObject = function(object) { - return new fabric.Image.filters.Convolute(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/filter.js b/src/filters/filter.js deleted file mode 100644 index 7a66584a..00000000 --- a/src/filters/filter.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * @namespace fabric.Image.filters - * @memberOf fabric.Image - */ -fabric.Image.filters = fabric.Image.filters || { }; diff --git a/src/filters/gradienttransparency_filter.class.js b/src/filters/gradienttransparency_filter.class.js index bdf625c9..67ef41e5 100644 --- a/src/filters/gradienttransparency_filter.class.js +++ b/src/filters/gradienttransparency_filter.class.js @@ -1,72 +1,72 @@ -/** - * GradientTransparency filter class - * @class fabric.Image.filters.GradientTransparency - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.GradientTransparency = fabric.util.createClass(/** @lends fabric.Image.filters.GradientTransparency.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * GradientTransparency filter class + * @class fabric.Image.filters.GradientTransparency + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'GradientTransparency', + fabric.Image.filters.GradientTransparency = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.GradientTransparency.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.GradientTransparency - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.threshold = options.threshold || 100; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'GradientTransparency', - /** - * Applies filter to canvas element - * @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, - threshold = this.threshold, - total = data.length; + /** + * Constructor + * @memberOf fabric.Image.filters.GradientTransparency + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.threshold = options.threshold || 100; + }, - for (var i = 0, len = data.length; i < len; i += 4) { - data[i + 3] = threshold + 255 * (total - i) / total; + /** + * Applies filter to canvas element + * @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, + threshold = this.threshold, + total = data.length; + + for (var i = 0, len = data.length; i < len; i += 4) { + data[i + 3] = threshold + 255 * (total - i) / total; + } + + context.putImageData(imageData, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + threshold: this.threshold + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.GradientTransparency} Instance of fabric.Image.filters.GradientTransparency */ - toObject: function() { - return { - type: this.type, - threshold: this.threshold - }; - }, + fabric.Image.filters.GradientTransparency.fromObject = function(object) { + return new fabric.Image.filters.GradientTransparency(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.GradientTransparency} Instance of fabric.Image.filters.GradientTransparency - */ -fabric.Image.filters.GradientTransparency.fromObject = function(object) { - return new fabric.Image.filters.GradientTransparency(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/grayscale_filter.class.js b/src/filters/grayscale_filter.class.js index 4850b827..7cc41552 100644 --- a/src/filters/grayscale_filter.class.js +++ b/src/filters/grayscale_filter.class.js @@ -1,64 +1,56 @@ -/** - * Grayscale image filter class - * @class fabric.Image.filters.Grayscale - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Grayscale = fabric.util.createClass(/** @lends fabric.Image.filters.Grayscale.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }); /** - * Filter type - * @param {String} type - * @default + * Grayscale image filter class + * @class fabric.Image.filters.Grayscale + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Grayscale', + fabric.Image.filters.Grayscale = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Grayscale.prototype */ { - /** - * Applies filter to canvas element - * @memberOf fabric.Image.filters.Grayscale.prototype - * @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, - len = imageData.width * imageData.height * 4, - index = 0, - average; + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Grayscale', - while (index < len) { - average = (data[index] + data[index + 1] + data[index + 2]) / 3; - data[index] = average; - data[index + 1] = average; - data[index + 2] = average; - index += 4; + /** + * Applies filter to canvas element + * @memberOf fabric.Image.filters.Grayscale.prototype + * @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, + len = imageData.width * imageData.height * 4, + index = 0, + average; + + while (index < len) { + average = (data[index] + data[index + 1] + data[index + 2]) / 3; + data[index] = average; + data[index + 1] = average; + data[index + 2] = average; + index += 4; + } + + context.putImageData(imageData, 0, 0); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @return {fabric.Image.filters.Grayscale} Instance of fabric.Image.filters.Grayscale */ - toObject: function() { - return { type: this.type }; - }, + fabric.Image.filters.Grayscale.fromObject = function() { + return new fabric.Image.filters.Grayscale(); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @return {fabric.Image.filters.Grayscale} Instance of fabric.Image.filters.Grayscale - */ -fabric.Image.filters.Grayscale.fromObject = function() { - return new fabric.Image.filters.Grayscale(); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/invert_filter.class.js b/src/filters/invert_filter.class.js index dfc30f8f..2524a0a4 100644 --- a/src/filters/invert_filter.class.js +++ b/src/filters/invert_filter.class.js @@ -1,60 +1,52 @@ -/** - * Invert filter class - * @class fabric.Image.filters.Invert - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Invert = fabric.util.createClass(/** @lends fabric.Image.filters.Invert.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }); /** - * Filter type - * @param {String} type - * @default + * Invert filter class + * @class fabric.Image.filters.Invert + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Invert', + fabric.Image.filters.Invert = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Invert.prototype */ { - /** - * Applies filter to canvas element - * @memberOf fabric.Image.filters.Invert.prototype - * @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; + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Invert', - for (i = 0; i < iLen; i+=4) { - data[i] = 255 - data[i]; - data[i + 1] = 255 - data[i + 1]; - data[i + 2] = 255 - data[i + 2]; + /** + * Applies filter to canvas element + * @memberOf fabric.Image.filters.Invert.prototype + * @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; + + for (i = 0; i < iLen; i+=4) { + data[i] = 255 - data[i]; + data[i + 1] = 255 - data[i + 1]; + data[i + 2] = 255 - data[i + 2]; + } + + context.putImageData(imageData, 0, 0); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @return {fabric.Image.filters.Invert} Instance of fabric.Image.filters.Invert */ - toObject: function() { - return { type: this.type }; - }, + fabric.Image.filters.Invert.fromObject = function() { + return new fabric.Image.filters.Invert(); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @return {fabric.Image.filters.Invert} Instance of fabric.Image.filters.Invert - */ -fabric.Image.filters.Invert.fromObject = function() { - return new fabric.Image.filters.Invert(); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/noise_filter.class.js b/src/filters/noise_filter.class.js index ef7dc0af..3139fc49 100644 --- a/src/filters/noise_filter.class.js +++ b/src/filters/noise_filter.class.js @@ -1,76 +1,76 @@ -/** - * Noise filter class - * @class fabric.Image.filters.Noise - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Noise = fabric.util.createClass(/** @lends fabric.Image.filters.Noise.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Noise filter class + * @class fabric.Image.filters.Noise + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Noise', + fabric.Image.filters.Noise = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Noise.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.Noise.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.noise = options.noise || 100; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Noise', - /** - * Applies filter to canvas element - * @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, - noise = this.noise, rand; + /** + * Constructor + * @memberOf fabric.Image.filters.Noise.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.noise = options.noise || 100; + }, - for (var i = 0, len = data.length; i < len; i += 4) { + /** + * Applies filter to canvas element + * @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, + noise = this.noise, rand; - rand = (0.5 - Math.random()) * noise; + for (var i = 0, len = data.length; i < len; i += 4) { - data[i] += rand; - data[i + 1] += rand; - data[i + 2] += rand; + rand = (0.5 - Math.random()) * noise; + + data[i] += rand; + data[i + 1] += rand; + data[i + 2] += rand; + } + + context.putImageData(imageData, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + noise: this.noise + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.Noise} Instance of fabric.Image.filters.Noise */ - toObject: function() { - return { - type: this.type, - noise: this.noise - }; - }, + fabric.Image.filters.Noise.fromObject = function(object) { + return new fabric.Image.filters.Noise(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.Noise} Instance of fabric.Image.filters.Noise - */ -fabric.Image.filters.Noise.fromObject = function(object) { - return new fabric.Image.filters.Noise(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/pixelate_filter.class.js b/src/filters/pixelate_filter.class.js index b282d56d..3290d9ce 100644 --- a/src/filters/pixelate_filter.class.js +++ b/src/filters/pixelate_filter.class.js @@ -1,101 +1,101 @@ -/** - * Pixelate filter class - * @class fabric.Image.filters.Pixelate - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Pixelate = fabric.util.createClass(/** @lends fabric.Image.filters.Pixelate.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Pixelate filter class + * @class fabric.Image.filters.Pixelate + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Pixelate', + fabric.Image.filters.Pixelate = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Pixelate.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.Pixelate.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.blocksize = options.blocksize || 4; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Pixelate', - /** - * Applies filter to canvas element - * @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 = imageData.height, - jLen = imageData.width, - index, i, j, r, g, b, a; + /** + * Constructor + * @memberOf fabric.Image.filters.Pixelate.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.blocksize = options.blocksize || 4; + }, - for (i = 0; i < iLen; i += this.blocksize) { - for (j = 0; j < jLen; j += this.blocksize) { + /** + * Applies filter to canvas element + * @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 = imageData.height, + jLen = imageData.width, + index, i, j, r, g, b, a; - index = (i * 4) * jLen + (j * 4); + for (i = 0; i < iLen; i += this.blocksize) { + for (j = 0; j < jLen; j += this.blocksize) { - r = data[index]; - g = data[index+1]; - b = data[index+2]; - a = data[index+3]; + index = (i * 4) * jLen + (j * 4); - /* - blocksize: 4 + r = data[index]; + g = data[index+1]; + b = data[index+2]; + a = data[index+3]; - [1,x,x,x,1] - [x,x,x,x,1] - [x,x,x,x,1] - [x,x,x,x,1] - [1,1,1,1,1] - */ + /* + blocksize: 4 - for (var _i = i, _ilen = i + this.blocksize; _i < _ilen; _i++) { - for (var _j = j, _jlen = j + this.blocksize; _j < _jlen; _j++) { - index = (_i * 4) * jLen + (_j * 4); - data[index] = r; - data[index + 1] = g; - data[index + 2] = b; - data[index + 3] = a; + [1,x,x,x,1] + [x,x,x,x,1] + [x,x,x,x,1] + [x,x,x,x,1] + [1,1,1,1,1] + */ + + for (var _i = i, _ilen = i + this.blocksize; _i < _ilen; _i++) { + for (var _j = j, _jlen = j + this.blocksize; _j < _jlen; _j++) { + index = (_i * 4) * jLen + (_j * 4); + data[index] = r; + data[index + 1] = g; + data[index + 2] = b; + data[index + 3] = a; + } } } } + + context.putImageData(imageData, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + blocksize: this.blocksize + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.Pixelate} Instance of fabric.Image.filters.Pixelate */ - toObject: function() { - return { - type: this.type, - blocksize: this.blocksize - }; - }, + fabric.Image.filters.Pixelate.fromObject = function(object) { + return new fabric.Image.filters.Pixelate(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.Pixelate} Instance of fabric.Image.filters.Pixelate - */ -fabric.Image.filters.Pixelate.fromObject = function(object) { - return new fabric.Image.filters.Pixelate(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/removewhite_filter.class.js b/src/filters/removewhite_filter.class.js index 43f090e1..db48b8b8 100644 --- a/src/filters/removewhite_filter.class.js +++ b/src/filters/removewhite_filter.class.js @@ -1,89 +1,89 @@ -/** - * Remove white filter class - * @class fabric.Image.filters.RemoveWhite - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.RemoveWhite = fabric.util.createClass(/** @lends fabric.Image.filters.RemoveWhite.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Remove white filter class + * @class fabric.Image.filters.RemoveWhite + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'RemoveWhite', + fabric.Image.filters.RemoveWhite = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.RemoveWhite.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.RemoveWhite.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.threshold = options.threshold || 30; - this.distance = options.distance || 20; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'RemoveWhite', - /** - * Applies filter to canvas element - * @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, - threshold = this.threshold, - distance = this.distance, - limit = 255 - threshold, - abs = Math.abs, - r, g, b; + /** + * Constructor + * @memberOf fabric.Image.filters.RemoveWhite.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.threshold = options.threshold || 30; + this.distance = options.distance || 20; + }, - for (var i = 0, len = data.length; i < len; i += 4) { - r = data[i]; - g = data[i+1]; - b = data[i+2]; + /** + * Applies filter to canvas element + * @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, + threshold = this.threshold, + distance = this.distance, + limit = 255 - threshold, + abs = Math.abs, + r, g, b; - if (r > limit && - g > limit && - b > limit && - abs(r-g) < distance && - abs(r-b) < distance && - abs(g-b) < distance - ) { - data[i+3] = 1; + for (var i = 0, len = data.length; i < len; i += 4) { + r = data[i]; + g = data[i+1]; + b = data[i+2]; + + if (r > limit && + g > limit && + b > limit && + abs(r-g) < distance && + abs(r-b) < distance && + abs(g-b) < distance + ) { + data[i+3] = 1; + } } + + context.putImageData(imageData, 0, 0); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + threshold: this.threshold, + distance: this.distance + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.RemoveWhite} Instance of fabric.Image.filters.RemoveWhite */ - toObject: function() { - return { - type: this.type, - threshold: this.threshold, - distance: this.distance - }; - }, + fabric.Image.filters.RemoveWhite.fromObject = function(object) { + return new fabric.Image.filters.RemoveWhite(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.RemoveWhite} Instance of fabric.Image.filters.RemoveWhite - */ -fabric.Image.filters.RemoveWhite.fromObject = function(object) { - return new fabric.Image.filters.RemoveWhite(object); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/sepia2_filter.class.js b/src/filters/sepia2_filter.class.js index 03eca1b5..0dbe45fb 100644 --- a/src/filters/sepia2_filter.class.js +++ b/src/filters/sepia2_filter.class.js @@ -1,64 +1,56 @@ -/** - * Sepia2 filter class - * @class fabric.Image.filters.Sepia2 - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Sepia2 = fabric.util.createClass(/** @lends fabric.Image.filters.Sepia2.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }); /** - * Filter type - * @param {String} type - * @default + * Sepia2 filter class + * @class fabric.Image.filters.Sepia2 + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Sepia2', + fabric.Image.filters.Sepia2 = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Sepia2.prototype */ { - /** - * Applies filter to canvas element - * @memberOf fabric.Image.filters.Sepia.prototype - * @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; + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Sepia2', - for (i = 0; i < iLen; i+=4) { - r = data[i]; - g = data[i + 1]; - b = data[i + 2]; + /** + * Applies filter to canvas element + * @memberOf fabric.Image.filters.Sepia.prototype + * @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; - data[i] = (r * 0.393 + g * 0.769 + b * 0.189 ) / 1.351; - data[i + 1] = (r * 0.349 + g * 0.686 + b * 0.168 ) / 1.203; - data[i + 2] = (r * 0.272 + g * 0.534 + b * 0.131 ) / 2.140; + for (i = 0; i < iLen; i+=4) { + r = data[i]; + g = data[i + 1]; + b = data[i + 2]; + + data[i] = (r * 0.393 + g * 0.769 + b * 0.189 ) / 1.351; + data[i + 1] = (r * 0.349 + g * 0.686 + b * 0.168 ) / 1.203; + data[i + 2] = (r * 0.272 + g * 0.534 + b * 0.131 ) / 2.140; + } + + context.putImageData(imageData, 0, 0); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @return {fabric.Image.filters.Sepia2} Instance of fabric.Image.filters.Sepia2 */ - toObject: function() { - return { type: this.type }; - }, + fabric.Image.filters.Sepia2.fromObject = function() { + return new fabric.Image.filters.Sepia2(); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @return {fabric.Image.filters.Sepia2} Instance of fabric.Image.filters.Sepia2 - */ -fabric.Image.filters.Sepia2.fromObject = function() { - return new fabric.Image.filters.Sepia2(); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/sepia_filter.class.js b/src/filters/sepia_filter.class.js index ff182e52..70f493e9 100644 --- a/src/filters/sepia_filter.class.js +++ b/src/filters/sepia_filter.class.js @@ -1,61 +1,53 @@ -/** - * Sepia filter class - * @class fabric.Image.filters.Sepia - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Sepia = fabric.util.createClass(/** @lends fabric.Image.filters.Sepia.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }); /** - * Filter type - * @param {String} type - * @default + * Sepia filter class + * @class fabric.Image.filters.Sepia + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Sepia', + fabric.Image.filters.Sepia = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Sepia.prototype */ { - /** - * Applies filter to canvas element - * @memberOf fabric.Image.filters.Sepia.prototype - * @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, avg; + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Sepia', - for (i = 0; i < iLen; i+=4) { - avg = 0.3 * data[i] + 0.59 * data[i + 1] + 0.11 * data[i + 2]; - data[i] = avg + 100; - data[i + 1] = avg + 50; - data[i + 2] = avg + 255; + /** + * Applies filter to canvas element + * @memberOf fabric.Image.filters.Sepia.prototype + * @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, avg; + + for (i = 0; i < iLen; i+=4) { + avg = 0.3 * data[i] + 0.59 * data[i + 1] + 0.11 * data[i + 2]; + data[i] = avg + 100; + data[i + 1] = avg + 50; + data[i + 2] = avg + 255; + } + + context.putImageData(imageData, 0, 0); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @return {fabric.Image.filters.Sepia} Instance of fabric.Image.filters.Sepia */ - toObject: function() { - return { type: this.type }; - }, + fabric.Image.filters.Sepia.fromObject = function() { + return new fabric.Image.filters.Sepia(); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @return {fabric.Image.filters.Sepia} Instance of fabric.Image.filters.Sepia - */ -fabric.Image.filters.Sepia.fromObject = function() { - return new fabric.Image.filters.Sepia(); -}; +})(typeof exports !== 'undefined' ? exports : this); diff --git a/src/filters/tint_filter.class.js b/src/filters/tint_filter.class.js index c231b455..dec010d4 100644 --- a/src/filters/tint_filter.class.js +++ b/src/filters/tint_filter.class.js @@ -1,83 +1,83 @@ -/** - * Tint filter class - * @class fabric.Image.filters.Tint - * @memberOf fabric.Image.filters - */ -fabric.Image.filters.Tint = fabric.util.createClass(/** @lends fabric.Image.filters.Tint.prototype */ { +(function(global) { + + "use strict"; + + var fabric = global.fabric || (global.fabric = { }), + extend = fabric.util.object.extend; /** - * Filter type - * @param {String} type - * @default + * Tint filter class + * @class fabric.Image.filters.Tint + * @memberOf fabric.Image.filters + * @extends fabric.Image.filters.BaseFilter */ - type: 'Tint', + fabric.Image.filters.Tint = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.Tint.prototype */ { - /** - * Constructor - * @memberOf fabric.Image.filters.Tint.prototype - * @param {Object} [options] Options object - */ - initialize: function(options) { - options = options || { }; - this.color = options.color || 0; - }, + /** + * Filter type + * @param {String} type + * @default + */ + type: 'Tint', - /** - * Applies filter to canvas element - * @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, a; + /** + * Constructor + * @memberOf fabric.Image.filters.Tint.prototype + * @param {Object} [options] Options object + */ + initialize: function(options) { + options = options || { }; + this.color = options.color || 0; + }, - var rgb = parseInt(this.color, 10).toString(16); + /** + * Applies filter to canvas element + * @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, a; - var cr = parseInt('0x' + rgb.substr(0, 2), 16); - var cg = parseInt('0x' + rgb.substr(2, 2), 16); - var cb = parseInt('0x' + rgb.substr(4, 2), 16); + var rgb = parseInt(this.color, 10).toString(16); - for (i = 0; i < iLen; i+=4) { - a = data[i+3]; + var cr = parseInt('0x' + rgb.substr(0, 2), 16); + var cg = parseInt('0x' + rgb.substr(2, 2), 16); + var cb = parseInt('0x' + rgb.substr(4, 2), 16); - if (a > 0){ - data[i] = cr; - data[i+1] = cg; - data[i+2] = cb; + 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); + }, + + /** + * Returns object representation of an instance + * @return {Object} Object representation of an instance + */ + toObject: function() { + return extend(this.callSuper('toObject'), { + color: this.color + }); } - - context.putImageData(imageData, 0, 0); - }, + }); /** - * Returns object representation of an instance - * @return {Object} Object representation of an instance + * Returns filter instance from an object representation + * @static + * @param {Object} object Object to create an instance from + * @return {fabric.Image.filters.Tint} Instance of fabric.Image.filters.Tint */ - toObject: function() { - return { - type: this.type, - color: this.color - }; - }, + fabric.Image.filters.Tint.fromObject = function(object) { + return new fabric.Image.filters.Tint(object); + }; - /** - * Returns a JSON representation of an instance - * @return {Object} JSON - */ - toJSON: function() { - // delegate, not alias - return this.toObject(); - } -}); - -/** - * Returns filter instance from an object representation - * @static - * @param {Object} object Object to create an instance from - * @return {fabric.Image.filters.Tint} Instance of fabric.Image.filters.Tint - */ -fabric.Image.filters.Tint.fromObject = function(object) { - return new fabric.Image.filters.Tint(object); -}; +})(typeof exports !== 'undefined' ? exports : this);