aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/renderer/helpers.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/helpers.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/renderer/helpers.ts')
-rw-r--r--packages/excalidraw/renderer/helpers.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/excalidraw/renderer/helpers.ts b/packages/excalidraw/renderer/helpers.ts
new file mode 100644
index 0000000..90f4009
--- /dev/null
+++ b/packages/excalidraw/renderer/helpers.ts
@@ -0,0 +1,75 @@
+import type { StaticCanvasAppState, AppState } from "../types";
+
+import type { StaticCanvasRenderConfig } from "../scene/types";
+
+import { THEME, THEME_FILTER } from "../constants";
+
+export const fillCircle = (
+ context: CanvasRenderingContext2D,
+ cx: number,
+ cy: number,
+ radius: number,
+ stroke = true,
+) => {
+ context.beginPath();
+ context.arc(cx, cy, radius, 0, Math.PI * 2);
+ context.fill();
+ if (stroke) {
+ context.stroke();
+ }
+};
+
+export const getNormalizedCanvasDimensions = (
+ canvas: HTMLCanvasElement,
+ scale: number,
+): [number, number] => {
+ // When doing calculations based on canvas width we should used normalized one
+ return [canvas.width / scale, canvas.height / scale];
+};
+
+export const bootstrapCanvas = ({
+ canvas,
+ scale,
+ normalizedWidth,
+ normalizedHeight,
+ theme,
+ isExporting,
+ viewBackgroundColor,
+}: {
+ canvas: HTMLCanvasElement;
+ scale: number;
+ normalizedWidth: number;
+ normalizedHeight: number;
+ theme?: AppState["theme"];
+ isExporting?: StaticCanvasRenderConfig["isExporting"];
+ viewBackgroundColor?: StaticCanvasAppState["viewBackgroundColor"];
+}): CanvasRenderingContext2D => {
+ const context = canvas.getContext("2d")!;
+
+ context.setTransform(1, 0, 0, 1, 0, 0);
+ context.scale(scale, scale);
+
+ if (isExporting && theme === THEME.DARK) {
+ context.filter = THEME_FILTER;
+ }
+
+ // Paint background
+ if (typeof viewBackgroundColor === "string") {
+ const hasTransparence =
+ viewBackgroundColor === "transparent" ||
+ viewBackgroundColor.length === 5 || // #RGBA
+ viewBackgroundColor.length === 9 || // #RRGGBBA
+ /(hsla|rgba)\(/.test(viewBackgroundColor);
+ if (hasTransparence) {
+ context.clearRect(0, 0, normalizedWidth, normalizedHeight);
+ }
+ context.save();
+ context.fillStyle = viewBackgroundColor;
+ context.fillRect(0, 0, normalizedWidth, normalizedHeight);
+ context.restore();
+ } else {
+ context.clearRect(0, 0, normalizedWidth, normalizedHeight);
+ }
+
+ return context;
+};