Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made image loading failure in preload() not to stop whole process #5918

Merged
merged 2 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/image/loading_displaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) {
e => {
if (typeof failureCallback === 'function') {
failureCallback(e);
self._decrementPreload();
} else {
console.error(e);
}
Expand All @@ -130,6 +131,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) {
p5._friendlyFileLoadError(0, img.src);
if (typeof failureCallback === 'function') {
failureCallback(e);
self._decrementPreload();
} else {
console.error(e);
}
Expand All @@ -152,6 +154,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) {
p5._friendlyFileLoadError(0, path);
if (typeof failureCallback === 'function') {
failureCallback(e);
self._decrementPreload();
} else {
console.error(e);
}
Expand Down
43 changes: 43 additions & 0 deletions test/unit/image/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,49 @@ suite('loading images', function() {
};
};
new p5(mySketch, null, false);

// Test loading image failure in preload() without failure callback
mySketch = function(this_p5) {
this_p5.preload = function() {
this_p5.loadImage('', function() {
throw new Error('Should not be called');
});
};

this_p5.setup = function() {
throw new Error('Should not be called');
};
};
new p5(mySketch, null, false);

// Test loading image failure in preload() with failure callback
mySketch = function(this_p5) {
var myImage;
this_p5.preload = function() {
suite('Test loading image failure in preload() with failure callback', function() {
test('Load fail and use failure callback', function(done) {
myImage = this_p5.loadImage('', function() {
assert.fail();
done();
}, function() {
assert.ok(myImage);
done();
});
});
});
};

this_p5.setup = function() {
suite('setup() after preload() failure with failure callback', function() {
test('should be loaded now preload() finished', function(done) {
assert.isTrue(myImage instanceof p5.Image);
assert.isTrue(myImage.width === 1 && myImage.height === 1);
done();
});
});
};
};
new p5(mySketch, null, false);
});

suite('loading animated gif images', function() {
Expand Down