File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -287,13 +287,13 @@ function image(p5, fn){
287
287
const framebuffer = args [ 0 ] ;
288
288
temporaryGraphics = this . createGraphics ( framebuffer . width ,
289
289
framebuffer . height ) ;
290
- temporaryGraphics . pixelDensity ( pixelDensity ( ) ) ;
290
+ temporaryGraphics . pixelDensity ( framebuffer . pixelDensity ( ) ) ;
291
291
framebuffer . loadPixels ( ) ;
292
292
temporaryGraphics . loadPixels ( ) ;
293
293
temporaryGraphics . pixels . set ( framebuffer . pixels ) ;
294
294
temporaryGraphics . updatePixels ( ) ;
295
295
296
- htmlCanvas = temporaryGraphics . elt ;
296
+ htmlCanvas = temporaryGraphics . _renderer . canvas ;
297
297
args . shift ( ) ;
298
298
} else {
299
299
htmlCanvas = this . _curElement && this . _curElement . elt ;
Original file line number Diff line number Diff line change 1
1
import p5 from '../../../src/app.js' ;
2
2
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
+ } ) ;
3
21
4
22
suite ( 'p5.Framebuffer' , function ( ) {
5
23
let myp5 ;
@@ -640,4 +658,22 @@ suite('p5.Framebuffer', function() {
640
658
) ;
641
659
} ) ;
642
660
} ) ;
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
+ } )
643
679
} ) ;
You can’t perform that action at this time.
0 commit comments