Skip to content

Commit a50013f

Browse files
authored
Merge pull request #9012 from harshiltewari2004/fix-saveframes-frame-count
Terminate saveFrames() capture by frame count instead of wall-clock d…
2 parents 7a6d471 + 79a05f0 commit a50013f

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎src/image/image.js‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -640,22 +640,22 @@ function image(p5, fn) {
640640
const makeFrame = fn._makeFrame;
641641
const cnv = this._curElement.elt;
642642
let frames = [];
643+
const totalFrames = (duration * fps) / 1000;
643644
const frameFactory = setInterval(() => {
644645
frames.push(makeFrame(fName + count, ext, cnv));
645646
count++;
646-
}, 1000 / fps);
647-
648-
setTimeout(() => {
649-
clearInterval(frameFactory);
650-
if (callback) {
651-
callback(frames);
652-
} else {
653-
for (const f of frames) {
654-
fn.downloadFile(f.imageData, f.filename, f.ext);
647+
if (count >= totalFrames) {
648+
clearInterval(frameFactory);
649+
if (callback) {
650+
callback(frames);
651+
} else {
652+
for (const f of frames) {
653+
fn.downloadFile(f.imageData, f.filename, f.ext);
654+
}
655655
}
656+
frames = [];
656657
}
657-
frames = []; // clear frames
658-
}, duration + 0.01);
658+
}, 1000 / fps);
659659
};
660660

661661
fn._makeFrame = function (filename, extension, _cnv) {

‎test/unit/image/downloading.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ suite('Downloading', () => {
160160
assert.typeOf(mockP5Prototype.saveFrames, 'function');
161161
});
162162

163+
test('should capture duration x frames', async () => {
164+
return new Promise(resolve => {
165+
// 0.4s x 7.93fps -> totalFrames = 3.172, so 4 frames are captured.
166+
// Ticks land every 1000/7.93 = 126ms, so the old wall-clock cutoff at
167+
// 400ms stopped after only 3.
168+
mockP5Prototype.saveFrames('aaa', 'png', 0.4, 7.93, function cb1(arr) {
169+
assert.typeOf(arr, 'array');
170+
assert.equal(arr.length, 4);
171+
resolve();
172+
});
173+
});
174+
});
175+
163176
test('should get frames in callback (png)', async () => {
164177
return new Promise(resolve => {
165178
mockP5Prototype.saveFrames('aaa', 'png', 0.5, 25, function cb1(arr) {

0 commit comments

Comments
 (0)