Skip to content

Commit e9dbaa5

Browse files
authored
Merge pull request #7576 from processing/fix/fbo-saveCanvas
Fix saveCanvas for framebuffers
2 parents 11196fd + 8207570 commit e9dbaa5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/image/image.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ function image(p5, fn){
287287
const framebuffer = args[0];
288288
temporaryGraphics = this.createGraphics(framebuffer.width,
289289
framebuffer.height);
290-
temporaryGraphics.pixelDensity(pixelDensity());
290+
temporaryGraphics.pixelDensity(framebuffer.pixelDensity());
291291
framebuffer.loadPixels();
292292
temporaryGraphics.loadPixels();
293293
temporaryGraphics.pixels.set(framebuffer.pixels);
294294
temporaryGraphics.updatePixels();
295295

296-
htmlCanvas = temporaryGraphics.elt;
296+
htmlCanvas = temporaryGraphics._renderer.canvas;
297297
args.shift();
298298
} else {
299299
htmlCanvas = this._curElement && this._curElement.elt;

test/unit/webgl/p5.Framebuffer.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import p5 from '../../../src/app.js';
22
import { vi } from 'vitest';
3+
import * as fileSaver from 'file-saver';
4+
vi.mock('file-saver');
5+
6+
expect.extend({
7+
tobePng: (received) => {
8+
if (received.type === 'image/png') {
9+
return {
10+
message: 'expect blob to have type image/png',
11+
pass: true
12+
}
13+
} else {
14+
return {
15+
message: 'expect blob to have type image/png',
16+
pass: false
17+
}
18+
}
19+
}
20+
});
321

422
suite('p5.Framebuffer', function() {
523
let myp5;
@@ -640,4 +658,22 @@ suite('p5.Framebuffer', function() {
640658
);
641659
});
642660
});
661+
662+
suite('saveCanvas', function() {
663+
test('should download a png file', async () => {
664+
myp5.createCanvas(100, 100, myp5.WEBGL);
665+
const fbo = myp5.createFramebuffer();
666+
fbo.draw(() => myp5.background('red'));
667+
myp5.saveCanvas(fbo);
668+
669+
await new Promise(res => setTimeout(res, 100));
670+
671+
expect(fileSaver.saveAs).toHaveBeenCalledTimes(1);
672+
expect(fileSaver.saveAs)
673+
.toHaveBeenCalledWith(
674+
expect.tobePng(),
675+
'untitled.png'
676+
);
677+
})
678+
})
643679
});

0 commit comments

Comments
 (0)