Skip to content

Commit

Permalink
bring callback toapplyfilters (#3210)
Browse files Browse the repository at this point in the history
* bring callback toapplyfilters
  • Loading branch information
asturur authored Aug 28, 2016
1 parent 148b5c1 commit bba8b3a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 202 deletions.
3 changes: 3 additions & 0 deletions src/filters/resize_filter.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
* @param {Number} scaleY
*/
applyTo: function(canvasEl, scaleX, scaleY) {
if (scaleX === 1 && scaleY === 1) {
return;
}

this.rcpScaleX = 1 / scaleX;
this.rcpScaleY = 1 / scaleY;
Expand Down
18 changes: 4 additions & 14 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,10 @@
});
};

fabric.Image.fromObject = function(object, callback) {
fabric.util.loadImage(object.src, function(img) {
var oImg = new fabric.Image(img);

oImg._initConfig(object);
oImg._initFilters(object.filters, function(filters) {
oImg.filters = filters || [ ];
oImg._initFilters(object.resizeFilters, function(resizeFilters) {
oImg.resizeFilters = resizeFilters || [ ];
callback && callback(oImg);
});
});
});
};
// fabric.util.createCanvasElement = function(_, width, height) {
// return new Canvas(width, height);
// }

/**
* Only available when running fabric on node.js
* @param {Number} width Canvas width
Expand Down
96 changes: 66 additions & 30 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,28 @@
*/
_lastScaleY: 1,

/**
* minimum scale factor under which any resizeFilter is triggered to resize the image
* 0 will disable the automatic resize. 1 will trigger automatically always.
* number bigger than 1 can be used in case we want to scale with some filter above
* the natural image dimensions
* @type Number
*/
minimumScaleTrigger: 0.5,

/**
* Constructor
* @param {HTMLImageElement | String} element Image element
* @param {Object} [options] Options object
* @param {function} [callback] callback function to call after eventual filters applied.
* @return {fabric.Image} thisArg
*/
initialize: function(element, options) {
initialize: function(element, options, callback) {
options || (options = { });
this.filters = [ ];
this.resizeFilters = [ ];
this.callSuper('initialize', options);
this._initElement(element, options);
this._initElement(element, options, callback);
},

/**
Expand All @@ -121,15 +131,28 @@
* @chainable
*/
setElement: function(element, callback, options) {

var _callback, _this;

this._element = element;
this._originalElement = element;
this._initConfig(options);

if (this.resizeFilters.length === 0) {
_callback = callback;
}
else {
_this = this;
_callback = function() {
_this.applyFilters(callback, _this.resizeFilters, _this._filteredEl || _this._originalElement, true);
};
}

if (this.filters.length !== 0) {
this.applyFilters(callback);
this.applyFilters(_callback);
}
else if (callback) {
callback();
else if (_callback) {
_callback(this);
}

return this;
Expand Down Expand Up @@ -296,7 +319,7 @@
* @return {String} Source of an image
*/
getSrc: function() {
var element = this.getElement();
var element = this._originalElement;
if (element) {
return fabric.isLikelyNode ? element._src : element.src;
}
Expand Down Expand Up @@ -340,10 +363,10 @@
* Applies filters assigned to this image (from "filters" array)
* @method applyFilters
* @param {Function} callback Callback is invoked when all filters have been applied and new image is generated
* @param {Array} filters to be initialized
* @param {fabric.Image} imgElement
* @param {Array} filters to be applied
* @param {fabric.Image} imgElement image to filter ( default to this._element )
* @param {Boolean} forResizing
* @return {fabric.Image} thisArg
* @return {CanvasElement} canvasEl to be drawn immediately
* @chainable
*/
applyFilters: function(callback, filters, imgElement, forResizing) {
Expand All @@ -355,22 +378,38 @@
return;
}

var imgEl = imgElement,
canvasEl = fabric.util.createCanvasElement(),
replacement = fabric.util.createImage(),
_this = this;

canvasEl.width = imgEl.width;
canvasEl.height = imgEl.height;
canvasEl.getContext('2d').drawImage(imgEl, 0, 0, imgEl.width, imgEl.height);
var replacement = fabric.util.createImage(),
retinaScaling = this.canvas ? this.canvas.getRetinaScaling() : fabric.devicePixelRatio,
minimumScale = this.minimumScaleTrigger / retinaScaling,
_this = this, scaleX, scaleY;

if (filters.length === 0) {
this._element = imgElement;
callback && callback();
return canvasEl;
callback && callback(this);
return imgElement;
}

var canvasEl = fabric.util.createCanvasElement();
canvasEl.width = imgElement.width;
canvasEl.height = imgElement.height;
canvasEl.getContext('2d').drawImage(imgElement, 0, 0, imgElement.width, imgElement.height);

filters.forEach(function(filter) {
filter && filter.applyTo(canvasEl, filter.scaleX || _this.scaleX, filter.scaleY || _this.scaleY);
if (forResizing) {
scaleX = _this.scaleX < minimumScale ? _this.scaleX : 1;
scaleY = _this.scaleY < minimumScale ? _this.scaleY : 1;
if (scaleX * retinaScaling < 1) {
scaleX *= retinaScaling;
}
if (scaleY * retinaScaling < 1) {
scaleY *= retinaScaling;
}
}
else {
scaleX = filter.scaleX;
scaleY = filter.scaleY;
}
filter && filter.applyTo(canvasEl, scaleX, scaleY);
if (!forResizing && filter && filter.type === 'Resize') {
_this.width *= filter.scaleX;
_this.height *= filter.scaleY;
Expand All @@ -380,20 +419,18 @@
/** @ignore */
replacement.width = canvasEl.width;
replacement.height = canvasEl.height;

if (fabric.isLikelyNode) {
replacement.src = canvasEl.toBuffer(undefined, fabric.Image.pngCompression);
// onload doesn't fire in some node versions, so we invoke callback manually
_this._element = replacement;
!forResizing && (_this._filteredEl = replacement);
callback && callback();
_this._element = replacement; // !forResizing && (_this._filteredEl = replacement);
callback && callback(_this);
}
else {
replacement.onload = function() {
_this._element = replacement;
!forResizing && (_this._filteredEl = replacement);
callback && callback();
replacement.onload = canvasEl = imgEl = null;
callback && callback(_this);
replacement.onload = canvasEl = null;
};
replacement.src = canvasEl.toDataURL('image/png');
}
Expand Down Expand Up @@ -494,8 +531,8 @@
* @param {HTMLImageElement|String} element The element representing the image
* @param {Object} [options] Options object
*/
_initElement: function(element, options) {
this.setElement(fabric.util.getById(element), null, options);
_initElement: function(element, options, callback) {
this.setElement(fabric.util.getById(element), callback, options);
fabric.util.addClass(this.getElement(), fabric.Image.CSS_CANVAS);
},

Expand Down Expand Up @@ -581,8 +618,7 @@
object.filters = filters || [ ];
fabric.Image.prototype._initFilters.call(object, object.resizeFilters, function(resizeFilters) {
object.resizeFilters = resizeFilters || [ ];
var instance = new fabric.Image(img, object);
callback && callback(instance);
return new fabric.Image(img, object, callback);
});
});
}, null, object.crossOrigin);
Expand Down
8 changes: 8 additions & 0 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@
return (fabric.devicePixelRatio !== 1 && this.enableRetinaScaling);
},

/**
* @private
* @return {Number} retinaScaling if applied, otherwise 1;
*/
getRetinaScaling: function() {
return this._isRetinaScaling() ? fabric.devicePixelRatio : 1;
},

/**
* @private
*/
Expand Down
Loading

0 comments on commit bba8b3a

Please sign in to comment.