Skip to content

Commit

Permalink
Added more error flag passing throughout functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TanukiSharp committed Mar 29, 2020
1 parent 854e68d commit 105c4b0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/pattern.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@
// img src string
var _this = this;
this.source = fabric.util.createImage();
fabric.util.loadImage(options.source, function(img) {
fabric.util.loadImage(options.source, function(img, isError) {
_this.source = img;
callback && callback(_this);
callback && callback(_this, isError);
}, null, this.crossOrigin);
}
},
Expand Down
18 changes: 9 additions & 9 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@
* @chainable
*/
setSrc: function(src, callback, options) {
fabric.util.loadImage(src, function(img) {
fabric.util.loadImage(src, function(img, isError) {
this.setElement(img, options);
this._setWidthHeight();
callback && callback(this);
callback && callback(this, isError);
}, this, options && options.crossOrigin);
return this;
},
Expand Down Expand Up @@ -687,9 +687,9 @@
*/
fabric.Image.fromObject = function(_object, callback) {
var object = fabric.util.object.clone(_object);
fabric.util.loadImage(object.src, function(img, error) {
if (error) {
callback && callback(null, error);
fabric.util.loadImage(object.src, function(img, isError) {
if (isError) {
callback && callback(null, true);
return;
}
fabric.Image.prototype._initFilters.call(object, object.filters, function(filters) {
Expand All @@ -699,7 +699,7 @@
fabric.util.enlivenObjects([object.clipPath], function(enlivedProps) {
object.clipPath = enlivedProps[0];
var image = new fabric.Image(img, object);
callback(image);
callback(image, false);
});
});
});
Expand All @@ -710,12 +710,12 @@
* Creates an instance of fabric.Image from an URL string
* @static
* @param {String} url URL to create an image from
* @param {Function} [callback] Callback to invoke when image is created (newly created image is passed as a first argument)
* @param {Function} [callback] Callback to invoke when image is created (newly created image is passed as a first argument). Second argument is a boolean indicating if an error occured or not.
* @param {Object} [imgOptions] Options object
*/
fabric.Image.fromURL = function(url, callback, imgOptions) {
fabric.util.loadImage(url, function(img) {
callback && callback(new fabric.Image(img, imgOptions));
fabric.util.loadImage(url, function(img, isError) {
callback && callback(new fabric.Image(img, imgOptions), isError);
}, null, imgOptions && imgOptions.crossOrigin);
};

Expand Down
12 changes: 6 additions & 6 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
* originY: 'top'
* });
* @example <caption>Stretched overlayImage #1 - width/height correspond to canvas width/height</caption>
* fabric.Image.fromURL('http://fabricjs.com/assets/jail_cell_bars.png', function(img) {
* fabric.Image.fromURL('http://fabricjs.com/assets/jail_cell_bars.png', function(img, isError) {
* img.set({width: canvas.width, height: canvas.height, originX: 'left', originY: 'top'});
* canvas.setOverlayImage(img, canvas.renderAll.bind(canvas));
* });
Expand Down Expand Up @@ -347,7 +347,7 @@
* originY: 'top'
* });
* @example <caption>Stretched backgroundImage #1 - width/height correspond to canvas width/height</caption>
* fabric.Image.fromURL('http://fabricjs.com/assets/honey_im_subtle.png', function(img) {
* fabric.Image.fromURL('http://fabricjs.com/assets/honey_im_subtle.png', function(img, isError) {
* img.set({width: canvas.width, height: canvas.height, originX: 'left', originY: 'top'});
* canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas));
* });
Expand Down Expand Up @@ -442,25 +442,25 @@
* @param {String} property Property to set ({@link fabric.StaticCanvas#backgroundImage|backgroundImage}
* or {@link fabric.StaticCanvas#overlayImage|overlayImage})
* @param {(fabric.Image|String|null)} image fabric.Image instance, URL of an image or null to set background or overlay to
* @param {Function} callback Callback to invoke when image is loaded and set as background or overlay
* @param {Function} callback Callback to invoke when image is loaded and set as background or overlay. The first argument is the created image, the second argument is a flag indicating whether an error occured or not.
* @param {Object} [options] Optional options to set for the {@link fabric.Image|image}.
*/
__setBgOverlayImage: function(property, image, callback, options) {
if (typeof image === 'string') {
fabric.util.loadImage(image, function(img) {
fabric.util.loadImage(image, function(img, isError) {
if (img) {
var instance = new fabric.Image(img, options);
this[property] = instance;
instance.canvas = this;
}
callback && callback(img);
callback && callback(img, isError);
}, this, options && options.crossOrigin);
}
else {
options && image.setOptions(options);
this[property] = image;
image && (image.canvas = this);
callback && callback(image);
callback && callback(image, false);
}

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@

/** @ignore */
var onLoadCallback = function () {
callback && callback.call(context, img);
callback && callback.call(context, img, false);
img = img.onload = img.onerror = null;
};

Expand Down
4 changes: 2 additions & 2 deletions test/lib/visualTestLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@
var img = fabric.document.createElement('img');
img.onload = function() {
img.onload = null;
callback(img);
callback(img, false);
};
img.onerror = function(err) {
img.onerror = null;
callback(img);
callback(img, true);
console.log('Image loading errored', err);
};
img.src = filename;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
IMG_WIDTH = 276,
IMG_HEIGHT = 110;

var IMG_URL_NON_EXISTING = 'http://www.google.com/non-existing';

var REFERENCE_IMG_OBJECT = {
version: fabric.version,
type: 'image',
Expand Down Expand Up @@ -478,6 +480,16 @@
});
});

QUnit.test('fromURL error', function(assert) {
var done = assert.async();
assert.ok(typeof fabric.Image.fromURL === 'function');
fabric.Image.fromURL(IMG_URL_NON_EXISTING, function(instance, isError) {
assert.ok(instance instanceof fabric.Image);
assert.equal(isError, true);
done();
});
});

QUnit.test('fromElement', function(assert) {
var done = assert.async();

Expand Down
6 changes: 4 additions & 2 deletions test/unit/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,11 @@
return;
}

fabric.util.loadImage(IMG_URL, function(obj) {
fabric.util.loadImage(IMG_URL, function(obj, isError) {
if (obj) {
var oImg = new fabric.Image(obj);
assert.ok(/fixtures\/very_large_image\.jpg$/.test(oImg.getSrc()), 'image should have correct src');
assert.ok(!isError);
}
done();
});
Expand Down Expand Up @@ -473,9 +474,10 @@
return;
}
try {
fabric.util.loadImage(IMG_URL, function(img) {
fabric.util.loadImage(IMG_URL, function(img, isError) {
assert.equal(img.src, IMG_URL, 'src is set');
assert.equal(img.crossOrigin, 'anonymous', 'crossOrigin is set');
assert.ok(!isError);
done();
}, null, 'anonymous');
}
Expand Down

0 comments on commit 105c4b0

Please sign in to comment.