Added error handler for Image.fromURL

This commit is contained in:
Artyom Khudyakov 2021-12-14 19:17:11 +03:00
parent 80802f59f1
commit 8ffac91f05
2 changed files with 13 additions and 1 deletions

View File

@ -122,6 +122,7 @@ export class Image extends Shape<ImageConfig> {
* @memberof Konva.Image * @memberof Konva.Image
* @param {String} url image source * @param {String} url image source
* @param {Function} callback with Konva.Image instance as first argument * @param {Function} callback with Konva.Image instance as first argument
* @param {Function} onError optional error handler
* @example * @example
* Konva.Image.fromURL(imageURL, function(image){ * Konva.Image.fromURL(imageURL, function(image){
* // image is Konva.Image instance * // image is Konva.Image instance
@ -129,7 +130,7 @@ export class Image extends Shape<ImageConfig> {
* layer.draw(); * layer.draw();
* }); * });
*/ */
static fromURL(url, callback) { static fromURL(url, callback, onError = null) {
var img = Util.createImageElement(); var img = Util.createImageElement();
img.onload = function () { img.onload = function () {
var image = new Image({ var image = new Image({
@ -137,6 +138,7 @@ export class Image extends Shape<ImageConfig> {
}); });
callback(image); callback(image);
}; };
img.onerror = onError;
img.crossOrigin = 'Anonymous'; img.crossOrigin = 'Anonymous';
img.src = url; img.src = url;
} }

View File

@ -356,6 +356,16 @@ describe('Image', function () {
}); });
}); });
it('check loading failure', function (done) {
var stage = addStage();
var layer = new Konva.Layer();
stage.add(layer);
var src = 'non-existent.jpg';
Konva.Image.fromURL(src, null, function (e) {
done();
});
});
it('check zero values', function (done) { it('check zero values', function (done) {
loadImage('darth-vader.jpg', (imageObj) => { loadImage('darth-vader.jpg', (imageObj) => {
var stage = addStage(); var stage = addStage();