1+ import p5 from '../../../src/app.js' ;
2+
3+ suite ( 'set() with p5.Graphics' , function ( ) {
4+ let myp5 ;
5+
6+ beforeEach ( function ( ) {
7+ myp5 = new p5 ( function ( p ) {
8+ p . setup = function ( ) {
9+ p . createCanvas ( 100 , 100 ) ;
10+ p . pixelDensity ( 1 ) ;
11+ } ;
12+ } ) ;
13+ } ) ;
14+
15+ afterEach ( function ( ) {
16+ myp5 . remove ( ) ;
17+ } ) ;
18+
19+ test ( 'set() works with p5.Graphics on main canvas' , function ( ) {
20+ myp5 . background ( 0 , 255 , 0 ) ;
21+ const gfx = myp5 . createGraphics ( 20 , 20 ) ;
22+ gfx . background ( 255 , 0 , 0 ) ;
23+ myp5 . set ( 10 , 10 , gfx ) ;
24+ myp5 . updatePixels ( ) ;
25+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 255 , 0 , 0 , 255 ] ) ;
26+ assert . deepEqual ( myp5 . get ( 5 , 5 ) , [ 0 , 255 , 0 , 255 ] ) ;
27+ } ) ;
28+
29+ test ( 'set() works with p5.Graphics on p5.Graphics' , function ( ) {
30+ const mainGfx = myp5 . createGraphics ( 100 , 100 ) ;
31+ mainGfx . background ( 255 ) ;
32+ const smallGfx = myp5 . createGraphics ( 20 , 20 ) ;
33+ smallGfx . background ( 0 , 0 , 255 ) ;
34+ mainGfx . set ( 10 , 10 , smallGfx ) ;
35+ mainGfx . updatePixels ( ) ;
36+ myp5 . image ( mainGfx , 0 , 0 ) ;
37+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 0 , 0 , 255 , 255 ] ) ;
38+ } ) ;
39+
40+ test ( 'set() clears area under p5.Graphics' , function ( ) {
41+ myp5 . background ( 255 ) ;
42+ myp5 . stroke ( 255 , 0 , 0 ) ;
43+ myp5 . strokeWeight ( 2 ) ;
44+ for ( let i = 0 ; i < 100 ; i += 10 ) {
45+ myp5 . line ( i , 0 , i , 100 ) ;
46+ }
47+ const gfx = myp5 . createGraphics ( 40 , 40 ) ;
48+ gfx . background ( 0 , 255 , 0 ) ;
49+ myp5 . set ( 30 , 30 , gfx ) ;
50+ myp5 . updatePixels ( ) ;
51+ assert . deepEqual ( myp5 . get ( 35 , 35 ) , [ 0 , 255 , 0 , 255 ] ) ;
52+ } ) ;
53+
54+ test ( 'set() works at edge (0,0)' , function ( ) {
55+ myp5 . background ( 255 ) ;
56+ const gfx = myp5 . createGraphics ( 30 , 30 ) ;
57+ gfx . background ( 0 , 0 , 255 ) ;
58+ myp5 . set ( 0 , 0 , gfx ) ;
59+ myp5 . updatePixels ( ) ;
60+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 0 , 0 , 255 , 255 ] ) ;
61+ assert . deepEqual ( myp5 . get ( 35 , 35 ) , [ 255 , 255 , 255 , 255 ] ) ;
62+ } ) ;
63+
64+ test ( 'set() behaves same for p5.Graphics and p5.Image' , function ( ) {
65+ const gfx = myp5 . createGraphics ( 20 , 20 ) ;
66+ gfx . background ( 255 , 0 , 0 ) ;
67+ const img = gfx . get ( ) ;
68+ myp5 . background ( 255 ) ;
69+ myp5 . set ( 10 , 10 , gfx ) ;
70+ myp5 . updatePixels ( ) ;
71+ const pixelFromGfx = myp5 . get ( 15 , 15 ) ;
72+ myp5 . background ( 255 ) ;
73+ myp5 . set ( 10 , 10 , img ) ;
74+ myp5 . updatePixels ( ) ;
75+ const pixelFromImg = myp5 . get ( 15 , 15 ) ;
76+ assert . deepEqual ( pixelFromGfx , pixelFromImg ) ;
77+ } ) ;
78+ } ) ;
0 commit comments