@@ -17,6 +17,12 @@ function shapeCorners(p5, shape, mode, x1, y1, x2, y2) {
1717 // Don't use abs(), so we get negative values as well
1818 let w = x2 - x1 ; // w
1919 let h = y2 - y1 ; // h
20+ // With mode CORNER, rects with negative widths/heights result in mirrored/flipped rendering
21+ // In this case, adjust position so the rect is in line with the other cases
22+ if ( shape === 'rect' ) {
23+ if ( w < 0 ) { x += ( - w ) ; } // Move right
24+ if ( h < 0 ) { y += ( - h ) ; } // Move down
25+ }
2026 x1 = x ; y1 = y ; x2 = w ; y2 = h ;
2127 } else if ( mode === p5 . CENTER ) {
2228 // Find center
@@ -56,14 +62,15 @@ function shapeCorners(p5, shape, mode, x1, y1, x2, y2) {
5662}
5763
5864
59- /*
60- Comprehensive test for rendering ellipse(), arc(), and rect()
61- with the different ellipseMode() / rectMode() values: CORNERS, CORNER, CENTER, RADIUS.
62- Each of the 3 shapes is tested with each of the 4 possible modes, resulting in 12 test.
63- Each test renders the shape in 16 different coordinate configurations,
64- testing combinations of positive and negative coordinate values.
65- */
6665visualSuite ( 'Shape Modes' , function ( ...args ) {
66+ /*
67+ Comprehensive test for rendering ellipse(), arc(), and rect()
68+ with the different ellipseMode() / rectMode() values: CORNERS, CORNER, CENTER, RADIUS.
69+ Each of the 3 shapes is tested with each of the 4 possible modes, resulting in 12 tests.
70+ Each test renders the shape in 16 different coordinate configurations,
71+ testing combinations of positive and negative coordinate values.
72+ */
73+
6774 // Shapes to test
6875 const SHAPES = [ 'ellipse' , 'arc' , 'rect' ] ;
6976
@@ -113,6 +120,60 @@ visualSuite('Shape Modes', function(...args) {
113120 } ) ; // End of: visualTest
114121 } // End of: MODES loop
115122
116- } ) ; // End of: Inner visualSuite
123+ } ) ; // End of: visualSuite
117124 } // End of: SHAPES loop
118- } ) ; // End of: Outer visualSuite
125+
126+
127+ /*
128+ An extra test suite specific to shape mode CORNER and negative dimensions.
129+ For rect, negative widths/heights result in flipped rendering (horizontally/vertically)
130+ For ellipse and arc, using negative widths/heights has no effect (the absolute value is used)
131+ */
132+ visualSuite ( 'Negative dimensions' , function ( ) {
133+ // Negative widths/height result in flipped rects.
134+ visualTest ( 'rect' , function ( p5 , screenshot ) {
135+ p5 . createCanvas ( 50 , 50 ) ;
136+ p5 . translate ( p5 . width / 2 , p5 . height / 2 ) ;
137+ p5 . rectMode ( p5 . CORNER ) ;
138+ p5 . rect ( 0 , 0 , 20 , 10 ) ;
139+ p5 . fill ( 'red' ) ;
140+ p5 . rect ( 0 , 0 , - 20 , 10 ) ;
141+ p5 . fill ( 'green' ) ;
142+ p5 . rect ( 0 , 0 , 20 , - 10 ) ;
143+ p5 . fill ( 'blue' ) ;
144+ p5 . rect ( 0 , 0 , - 20 , - 10 ) ;
145+ screenshot ( ) ;
146+ } ) ;
147+ // Since negative widths/heights are used with their absolute value,
148+ // ellipses are drawn on top of each other, blue one last
149+ visualTest ( 'ellipse' , function ( p5 , screenshot ) {
150+ p5 . createCanvas ( 50 , 50 ) ;
151+ p5 . translate ( p5 . width / 2 , p5 . height / 2 ) ;
152+ p5 . ellipseMode ( p5 . CORNER ) ;
153+ p5 . ellipse ( 0 , 0 , 20 , 10 ) ;
154+ p5 . fill ( 'red' ) ;
155+ p5 . ellipse ( 0 , 0 , - 20 , 10 ) ;
156+ p5 . fill ( 'green' ) ;
157+ p5 . ellipse ( 0 , 0 , 20 , - 10 ) ;
158+ p5 . fill ( 'blue' ) ;
159+ p5 . ellipse ( 0 , 0 , - 20 , - 10 ) ;
160+ screenshot ( ) ;
161+ } ) ;
162+ // Since negative widths/heights are used with their absolute value,
163+ // arcs are drawn on top of each other, blue one last.
164+ visualTest ( 'arc' , function ( p5 , screenshot ) {
165+ p5 . createCanvas ( 50 , 50 ) ;
166+ p5 . translate ( p5 . width / 2 , p5 . height / 2 ) ;
167+ p5 . ellipseMode ( p5 . CORNER ) ;
168+ p5 . arc ( 0 , 0 , 20 , 10 , 0 , p5 . PI + p5 . HALF_PI ) ;
169+ p5 . fill ( 'red' ) ;
170+ p5 . arc ( 0 , 0 , - 20 , 10 , 0 , p5 . PI + p5 . HALF_PI ) ;
171+ p5 . fill ( 'green' ) ;
172+ p5 . arc ( 0 , 0 , 20 , - 10 , 0 , p5 . PI + p5 . HALF_PI ) ;
173+ p5 . fill ( 'blue' ) ;
174+ p5 . arc ( 0 , 0 , - 20 , - 10 , 0 , p5 . PI + p5 . HALF_PI ) ;
175+ screenshot ( ) ;
176+ } ) ;
177+ } ) ;
178+
179+ } ) ;
0 commit comments