@@ -49,4 +49,77 @@ suite('p5.Image', function() {
49
49
assert . strictEqual ( img . height , 30 ) ;
50
50
} ) ;
51
51
} ) ;
52
+
53
+ suite ( 'p5.Image.prototype.mask' , function ( ) {
54
+ test ( 'it should mask the image' , function ( ) {
55
+ let img = myp5 . createImage ( 10 , 10 ) ;
56
+ img . loadPixels ( ) ;
57
+ for ( let i = 0 ; i < img . height ; i ++ ) {
58
+ for ( let j = 0 ; j < img . width ; j ++ ) {
59
+ let alpha = i < 5 ? 255 : 0 ;
60
+ img . set ( i , j , myp5 . color ( 0 , 0 , 0 , alpha ) ) ;
61
+ }
62
+ }
63
+ img . updatePixels ( ) ;
64
+
65
+ let mask = myp5 . createImage ( 10 , 10 ) ;
66
+ mask . loadPixels ( ) ;
67
+ for ( let i = 0 ; i < mask . width ; i ++ ) {
68
+ for ( let j = 0 ; j < mask . height ; j ++ ) {
69
+ let alpha = j < 5 ? 255 : 0 ;
70
+ mask . set ( i , j , myp5 . color ( 0 , 0 , 0 , alpha ) ) ;
71
+ }
72
+ }
73
+ mask . updatePixels ( ) ;
74
+
75
+ img . mask ( mask ) ;
76
+ img . loadPixels ( ) ;
77
+ for ( let i = 0 ; i < img . width ; i ++ ) {
78
+ for ( let j = 0 ; j < img . height ; j ++ ) {
79
+ let alpha = i < 5 && j < 5 ? 255 : 0 ;
80
+ assert . strictEqual ( img . get ( i , j ) [ 3 ] , alpha ) ;
81
+ }
82
+ }
83
+ } ) ;
84
+
85
+ test ( 'it should mask the animated gif image' , function ( ) {
86
+ const imagePath = 'unit/assets/nyan_cat.gif' ;
87
+ return new Promise ( function ( resolve , reject ) {
88
+ myp5 . loadImage ( imagePath , resolve , reject ) ;
89
+ } ) . then ( function ( img ) {
90
+ let mask = myp5 . createImage ( img . width , img . height ) ;
91
+ mask . loadPixels ( ) ;
92
+ for ( let i = 0 ; i < mask . width ; i ++ ) {
93
+ for ( let j = 0 ; j < mask . height ; j ++ ) {
94
+ const alpha = j < img . height / 2 ? 255 : 0 ;
95
+ mask . set ( i , j , myp5 . color ( 0 , 0 , 0 , alpha ) ) ;
96
+ }
97
+ }
98
+ mask . updatePixels ( ) ;
99
+
100
+ img . mask ( mask ) ;
101
+ img . loadPixels ( ) ;
102
+ for ( let i = 0 ; i < img . width ; i ++ ) {
103
+ for ( let j = 0 ; j < img . height ; j ++ ) {
104
+ const alpha = j < img . height / 2 ? 255 : 0 ;
105
+ assert . strictEqual ( img . get ( i , j ) [ 3 ] , alpha ) ;
106
+ }
107
+ }
108
+ for (
109
+ frameIndex = 0 ;
110
+ frameIndex < img . gifProperties . numFrames ;
111
+ frameIndex ++
112
+ ) {
113
+ const frameData = img . gifProperties . frames [ frameIndex ] . image . data ;
114
+ for ( let i = 0 ; i < img . width ; i ++ ) {
115
+ for ( let j = 0 ; j < img . height ; j ++ ) {
116
+ const index = 4 * ( i + j * img . width ) + 3 ;
117
+ const alpha = j < img . height / 2 ? 255 : 0 ;
118
+ assert . strictEqual ( frameData [ index ] , alpha ) ;
119
+ }
120
+ }
121
+ }
122
+ } ) ;
123
+ } ) ;
124
+ } ) ;
52
125
} ) ;
0 commit comments