mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
Added error handler for Image.fromURL
This commit is contained in:
parent
80802f59f1
commit
8ffac91f05
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user