diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index 3d23cc4c0b..fd3ba0aa30 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -265,6 +265,11 @@ function loadingDisplaying(p5, fn){ if (typeof duration !== 'number') { throw TypeError('Duration parameter must be a number'); } + // a non-positive duration captures zero frames, which later crashes + // in palette generation (frames[0] is undefined). reject early. + if (duration <= 0) { + throw RangeError('Duration parameter must be greater than 0'); + } // extract variables for more comfortable use const delay = (options && options.delay) || 0; // in seconds diff --git a/test/unit/image/downloading.js b/test/unit/image/downloading.js index 3d37d979e3..edc025d1a9 100644 --- a/test/unit/image/downloading.js +++ b/test/unit/image/downloading.js @@ -177,6 +177,11 @@ suite('Downloading', () => { assert.typeOf(mockP5Prototype.saveGif, 'function'); }); + test('should reject a non-positive duration', async () => { + await expect(mockP5Prototype.saveGif('myGif', 0)).rejects.toThrow(); + await expect(mockP5Prototype.saveGif('myGif', -3)).rejects.toThrow(); + }); + // TODO: this implementation need refactoring test.todo('should not throw an error', async () => { await mockP5Prototype.saveGif('myGif', 3);