Error.fromURL callback

I am trying to upload an image using fromURL. The problem is that I would like it to be able to load the default icon if it cannot get to the Image server to load the image. Looking at the docs, I did not see the error callback for the function fromURL. How should we catch that the challenge was unsuccessful and therefore did the right thing? It seems that the callback was not called at all when the image loading was unsuccessful.

+4
source share
2 answers

You can use the method fabric.util.loadImage()instead fabric.Image.fromURL().

If you look at the implementation of the method fromURL(), it is used inside it loadImage().

:

fabric.util.loadImage('https://s3-eu-west-1.amazonaws.com/kienzle.dev.cors/img/image2.png', function(img) {
    if(img == null) {
        alert("Error!");
    }else {
        var image = new fabric.Image(img);
        canvas.add(image).setActiveObject(image);
        canvas.renderAll();
    }
}, { crossOrigin: 'anonymous' });

: http://jsfiddle.net/k7moorthi/30kmn5kL/

+3

getElement(), .

fabric.Image.fromURL('/foo.jpg', (img) => {
  if (img.getElement() === undefined) {
    console.log('Failed to load image!');
    return;
  }
  // do something on success
}
+1

Source: https://habr.com/ru/post/1539975/


All Articles