@@ -208,4 +208,81 @@ suite('Rendering', function() {
208208 expect ( myp5 . splineProperties ( ) ) . toMatchObject ( { tightness : 2 } ) ;
209209 } ) ;
210210 } ) ;
211+
212+ suite ( 'set() with p5.Graphics' , function ( ) {
213+ let myp5 ;
214+
215+ beforeEach ( function ( ) {
216+ myp5 = new p5 ( function ( p ) {
217+ p . setup = function ( ) {
218+ p . createCanvas ( 100 , 100 ) ;
219+ p . pixelDensity ( 1 ) ;
220+ } ;
221+ } ) ;
222+ } ) ;
223+
224+ afterEach ( function ( ) {
225+ myp5 . remove ( ) ;
226+ } ) ;
227+
228+ test ( 'set() works with p5.Graphics on main canvas' , function ( ) {
229+ myp5 . background ( 0 , 255 , 0 ) ;
230+ const gfx = myp5 . createGraphics ( 20 , 20 ) ;
231+ gfx . background ( 255 , 0 , 0 ) ;
232+ myp5 . set ( 10 , 10 , gfx ) ;
233+ myp5 . updatePixels ( ) ;
234+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 255 , 0 , 0 , 255 ] ) ;
235+ assert . deepEqual ( myp5 . get ( 5 , 5 ) , [ 0 , 255 , 0 , 255 ] ) ;
236+ } ) ;
237+
238+ test ( 'set() works with p5.Graphics on p5.Graphics' , function ( ) {
239+ const mainGfx = myp5 . createGraphics ( 100 , 100 ) ;
240+ mainGfx . background ( 255 ) ;
241+ const smallGfx = myp5 . createGraphics ( 20 , 20 ) ;
242+ smallGfx . background ( 0 , 0 , 255 ) ;
243+ mainGfx . set ( 10 , 10 , smallGfx ) ;
244+ mainGfx . updatePixels ( ) ;
245+ myp5 . image ( mainGfx , 0 , 0 ) ;
246+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 0 , 0 , 255 , 255 ] ) ;
247+ } ) ;
248+
249+ test ( 'set() clears area under p5.Graphics' , function ( ) {
250+ myp5 . background ( 255 ) ;
251+ myp5 . stroke ( 255 , 0 , 0 ) ;
252+ myp5 . strokeWeight ( 2 ) ;
253+ for ( let i = 0 ; i < 100 ; i += 10 ) {
254+ myp5 . line ( i , 0 , i , 100 ) ;
255+ }
256+ const gfx = myp5 . createGraphics ( 40 , 40 ) ;
257+ gfx . background ( 0 , 255 , 0 ) ;
258+ myp5 . set ( 30 , 30 , gfx ) ;
259+ myp5 . updatePixels ( ) ;
260+ assert . deepEqual ( myp5 . get ( 35 , 35 ) , [ 0 , 255 , 0 , 255 ] ) ;
261+ } ) ;
262+
263+ test ( 'set() works at edge (0,0)' , function ( ) {
264+ myp5 . background ( 255 ) ;
265+ const gfx = myp5 . createGraphics ( 30 , 30 ) ;
266+ gfx . background ( 0 , 0 , 255 ) ;
267+ myp5 . set ( 0 , 0 , gfx ) ;
268+ myp5 . updatePixels ( ) ;
269+ assert . deepEqual ( myp5 . get ( 15 , 15 ) , [ 0 , 0 , 255 , 255 ] ) ;
270+ assert . deepEqual ( myp5 . get ( 35 , 35 ) , [ 255 , 255 , 255 , 255 ] ) ;
271+ } ) ;
272+
273+ test ( 'set() behaves same for p5.Graphics and p5.Image' , function ( ) {
274+ const gfx = myp5 . createGraphics ( 20 , 20 ) ;
275+ gfx . background ( 255 , 0 , 0 ) ;
276+ const img = gfx . get ( ) ;
277+ myp5 . background ( 255 ) ;
278+ myp5 . set ( 10 , 10 , gfx ) ;
279+ myp5 . updatePixels ( ) ;
280+ const pixelFromGfx = myp5 . get ( 15 , 15 ) ;
281+ myp5 . background ( 255 ) ;
282+ myp5 . set ( 10 , 10 , img ) ;
283+ myp5 . updatePixels ( ) ;
284+ const pixelFromImg = myp5 . get ( 15 , 15 ) ;
285+ assert . deepEqual ( pixelFromGfx , pixelFromImg ) ;
286+ } ) ;
287+ } ) ;
211288} ) ;
0 commit comments