@@ -84,6 +84,99 @@ export function getBorder(
8484 return undefined ;
8585}
8686
87+ export function roundRect (
88+ this : CanvasRenderingContext2D | Path2D ,
89+ x : number ,
90+ y : number ,
91+ width : number ,
92+ height : number ,
93+ radius : number | DOMPointInit | ( number | DOMPointInit ) [ ] ,
94+ ) {
95+ const context = Object . getPrototypeOf ( this ) as Path2D ;
96+ if ( ! context . roundRect ) {
97+ const fixOverlappingCorners = ( radii : {
98+ topLeft : number ;
99+ topRight : number ;
100+ bottomRight : number ;
101+ bottomLeft : number ;
102+ } ) => {
103+ const maxRadius = Math . min ( width / 2 , height / 2 ) ;
104+ const totalHorizontal =
105+ radii . topLeft + radii . topRight + radii . bottomRight + radii . bottomLeft ;
106+
107+ if ( totalHorizontal > width || totalHorizontal > height ) {
108+ const scale =
109+ maxRadius /
110+ Math . max (
111+ radii . topLeft ,
112+ radii . topRight ,
113+ radii . bottomRight ,
114+ radii . bottomLeft ,
115+ ) ;
116+ radii . topLeft *= scale ;
117+ radii . topRight *= scale ;
118+ radii . bottomRight *= scale ;
119+ radii . bottomLeft *= scale ;
120+ }
121+ } ;
122+ const radii =
123+ typeof radius === 'number'
124+ ? {
125+ topLeft : radius ,
126+ topRight : radius ,
127+ bottomRight : radius ,
128+ bottomLeft : radius ,
129+ }
130+ : { topLeft : 0 , topRight : 0 , bottomRight : 0 , bottomLeft : 0 , ...radius } ;
131+
132+ fixOverlappingCorners ( radii ) ;
133+
134+ this . moveTo ( x + radii . topLeft , y ) ;
135+ this . lineTo ( x + width - radii . topRight , y ) ;
136+ this . ellipse (
137+ x + width - radii . topRight ,
138+ y + radii . topRight ,
139+ radii . topRight ,
140+ radii . topRight ,
141+ 0 ,
142+ 1.5 * Math . PI ,
143+ 2 * Math . PI ,
144+ ) ;
145+ this . lineTo ( x + width , y + height - radii . bottomRight ) ;
146+ this . ellipse (
147+ x + width - radii . bottomRight ,
148+ y + height - radii . bottomRight ,
149+ radii . bottomRight ,
150+ radii . bottomRight ,
151+ 0 ,
152+ 0 ,
153+ 0.5 * Math . PI ,
154+ ) ;
155+ this . lineTo ( x + radii . bottomLeft , y + height ) ;
156+ this . ellipse (
157+ x + radii . bottomLeft ,
158+ y + height - radii . bottomLeft ,
159+ radii . bottomLeft ,
160+ radii . bottomLeft ,
161+ 0 ,
162+ 0.5 * Math . PI ,
163+ Math . PI ,
164+ ) ;
165+ this . lineTo ( x , y + radii . topLeft ) ;
166+ this . ellipse (
167+ x + radii . topLeft ,
168+ y + radii . topLeft ,
169+ radii . topLeft ,
170+ radii . topLeft ,
171+ 0 ,
172+ Math . PI ,
173+ 1.5 * Math . PI ,
174+ ) ;
175+ } else {
176+ this . roundRect ( x , y , width , height , radius ) ;
177+ }
178+ }
179+
87180export function strokeLine (
88181 ctx : CanvasRenderingContext2D ,
89182 x : number ,
0 commit comments