aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/renderer/roundRect.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/excalidraw/renderer/roundRect.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/renderer/roundRect.ts')
-rw-r--r--packages/excalidraw/renderer/roundRect.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/excalidraw/renderer/roundRect.ts b/packages/excalidraw/renderer/roundRect.ts
new file mode 100644
index 0000000..bbb9830
--- /dev/null
+++ b/packages/excalidraw/renderer/roundRect.ts
@@ -0,0 +1,41 @@
+/**
+ * https://stackoverflow.com/a/3368118
+ * Draws a rounded rectangle using the current state of the canvas.
+ * @param {CanvasRenderingContext2D} context
+ * @param {Number} x The top left x coordinate
+ * @param {Number} y The top left y coordinate
+ * @param {Number} width The width of the rectangle
+ * @param {Number} height The height of the rectangle
+ * @param {Number} radius The corner radius
+ */
+export const roundRect = (
+ context: CanvasRenderingContext2D,
+ x: number,
+ y: number,
+ width: number,
+ height: number,
+ radius: number,
+ strokeColor?: string,
+) => {
+ context.beginPath();
+ context.moveTo(x + radius, y);
+ context.lineTo(x + width - radius, y);
+ context.quadraticCurveTo(x + width, y, x + width, y + radius);
+ context.lineTo(x + width, y + height - radius);
+ context.quadraticCurveTo(
+ x + width,
+ y + height,
+ x + width - radius,
+ y + height,
+ );
+ context.lineTo(x + radius, y + height);
+ context.quadraticCurveTo(x, y + height, x, y + height - radius);
+ context.lineTo(x, y + radius);
+ context.quadraticCurveTo(x, y, x + radius, y);
+ context.closePath();
+ context.fill();
+ if (strokeColor) {
+ context.strokeStyle = strokeColor;
+ }
+ context.stroke();
+};