diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index c0ce679117..1f6486624b 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -393,6 +393,9 @@ p5.prototype.saveGif = async function( pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4); } + // store current looping status + const wasLooping = this.isLooping(); + // stop the loop since we are going to manually redraw this.noLoop(); @@ -452,7 +455,10 @@ p5.prototype.saveGif = async function( } if (!silent) p.html('Frames processed, generating color palette...'); - this.loop(); + if (wasLooping) { + this.loop(); + } + this.pixelDensity(lastPixelDensity); // create the gif encoder and the colorspace format @@ -552,7 +558,10 @@ p5.prototype.saveGif = async function( frames = []; this._recording = false; - this.loop(); + + if (wasLooping) { + this.loop(); + } if (!silent){ p.html('Done. Downloading your gif!🌸'); diff --git a/test/unit/image/downloading.js b/test/unit/image/downloading.js index fa3df09dd1..1cba2c9981 100644 --- a/test/unit/image/downloading.js +++ b/test/unit/image/downloading.js @@ -370,6 +370,22 @@ suite('p5.prototype.saveGif', function() { }); }); + test('should continue looping sketch after saveGif', function(done) { + myp5.saveGif('mySketch', 2).then(() => { + assert.equal(myp5.isLooping(), true, 'Should still be looping'); + done(); + }); + }); + + test('should not continue looping paused sketch after saveGif', + function(done) { + myp5.noLoop(); + myp5.saveGif('mySketch', 2).then(() => { + assert.equal(myp5.isLooping(), false, 'Should not be looping'); + done(); + }); + }); + testWithDownload('should download a GIF', async function(blobContainer) { myp5.saveGif(myGif, 3, 2); await waitForBlob(blobContainer);