@@ -9,6 +9,71 @@ import { snapshotImage } from './image-snapshot'
99
1010const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
1111
12+ // https://github.com/Brooooooklyn/canvas/issues/1204
13+ test ( 'putImageData should modify the canvas' , ( t ) => {
14+ const canvas = createCanvas ( 100 , 100 )
15+ const ctx = canvas . getContext ( '2d' )
16+
17+ // Fill canvas with red
18+ ctx . fillStyle = 'red'
19+ ctx . fillRect ( 0 , 0 , 100 , 100 )
20+
21+ // Get image data and modify to green
22+ const imageData = ctx . getImageData ( 0 , 0 , 10 , 10 )
23+ for ( let i = 0 ; i < imageData . data . length ; i += 4 ) {
24+ imageData . data [ i ] = 0 // R
25+ imageData . data [ i + 1 ] = 255 // G
26+ imageData . data [ i + 2 ] = 0 // B
27+ imageData . data [ i + 3 ] = 255 // A
28+ }
29+
30+ // putImageData should replace pixels at (0,0)
31+ ctx . putImageData ( imageData , 0 , 0 )
32+
33+ // Read back and verify the pixels were actually written
34+ const result = ctx . getImageData ( 0 , 0 , 1 , 1 )
35+ t . is ( result . data [ 0 ] , 0 , 'R should be 0 (green)' )
36+ t . is ( result . data [ 1 ] , 255 , 'G should be 255 (green)' )
37+ t . is ( result . data [ 2 ] , 0 , 'B should be 0 (green)' )
38+ t . is ( result . data [ 3 ] , 255 , 'A should be 255' )
39+ } )
40+
41+ // https://github.com/Brooooooklyn/canvas/issues/1204
42+ test ( 'putImageData with dirty rect should modify the canvas' , ( t ) => {
43+ const canvas = createCanvas ( 100 , 100 )
44+ const ctx = canvas . getContext ( '2d' )
45+
46+ // Fill canvas with blue
47+ ctx . fillStyle = 'blue'
48+ ctx . fillRect ( 0 , 0 , 100 , 100 )
49+
50+ // Create green image data
51+ const imageData = ctx . getImageData ( 0 , 0 , 20 , 20 )
52+ for ( let i = 0 ; i < imageData . data . length ; i += 4 ) {
53+ imageData . data [ i ] = 0 // R
54+ imageData . data [ i + 1 ] = 255 // G
55+ imageData . data [ i + 2 ] = 0 // B
56+ imageData . data [ i + 3 ] = 255 // A
57+ }
58+
59+ // putImageData with dirty rect
60+ ctx . putImageData ( imageData , 10 , 10 , 0 , 0 , 10 , 10 )
61+
62+ // Pixel at (15, 15) should be green (inside dirty rect)
63+ const inside = ctx . getImageData ( 15 , 15 , 1 , 1 )
64+ t . is ( inside . data [ 0 ] , 0 , 'R should be 0 (green) inside dirty rect' )
65+ t . is ( inside . data [ 1 ] , 255 , 'G should be 255 (green) inside dirty rect' )
66+ t . is ( inside . data [ 2 ] , 0 , 'B should be 0 (green) inside dirty rect' )
67+ t . is ( inside . data [ 3 ] , 255 , 'A should be 255 inside dirty rect' )
68+
69+ // Pixel at (25, 25) should still be blue (outside dirty rect)
70+ const outside = ctx . getImageData ( 25 , 25 , 1 , 1 )
71+ t . is ( outside . data [ 0 ] , 0 , 'R should be 0 (blue) outside dirty rect' )
72+ t . is ( outside . data [ 1 ] , 0 , 'G should be 0 (blue) outside dirty rect' )
73+ t . is ( outside . data [ 2 ] , 255 , 'B should be 255 (blue) outside dirty rect' )
74+ t . is ( outside . data [ 3 ] , 255 , 'A should be 255 outside dirty rect' )
75+ } )
76+
1277test ( 'transform-with-state' , async ( t ) => {
1378 const canvas = createCanvas ( 256 , 256 )
1479 const ctx = canvas . getContext ( '2d' )
0 commit comments