Virtual whiteboard for sketching hand-drawn like diagrams
fix(editor): arrow label rendering fixes and perf improvements (#11637)
David Luzar063e0256f5f8
diff --git a/packages/element/src/renderElement.ts b/packages/element/src/renderElement.tsindex 44b5e941af..22c3ed494c 100644--- a/packages/element/src/renderElement.ts+++ b/packages/element/src/renderElement.ts@@ -137,14 +137,11 @@ export interface ExcalidrawElementWithCanvas {canvas: HTMLCanvasElement;theme: AppState["theme"];scale: number;- angle: number;zoomValue: AppState["zoom"]["value"];canvasOffsetX: number;canvasOffsetY: number;- boundTextElementVersion: number | null;imageCrop: ExcalidrawImageElement["crop"] | null;containingFrameOpacity: number;- boundTextCanvas: HTMLCanvasElement;}const cappedElementCanvasSize = (@@ -258,69 +255,6 @@ const generateElementCanvas = (context.restore();- const boundTextElement = getBoundTextElement(element, elementsMap);- const boundTextCanvas = document.createElement("canvas");- const boundTextCanvasContext = boundTextCanvas.getContext("2d")!;-- if (isArrowElement(element) && boundTextElement) {- const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);- // Take max dimensions of arrow canvas so that when canvas is rotated- // the arrow doesn't get clipped- const maxDim = Math.max(distance(x1, x2), distance(y1, y2));- boundTextCanvas.width =- maxDim * window.devicePixelRatio * scale + padding * scale * 10;- boundTextCanvas.height =- maxDim * window.devicePixelRatio * scale + padding * scale * 10;- boundTextCanvasContext.translate(- boundTextCanvas.width / 2,- boundTextCanvas.height / 2,- );- boundTextCanvasContext.rotate(element.angle);- boundTextCanvasContext.drawImage(- canvas!,- -canvas.width / 2,- -canvas.height / 2,- canvas.width,- canvas.height,- );-- const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords(- boundTextElement,- elementsMap,- );-- boundTextCanvasContext.rotate(-element.angle);- const offsetX = (boundTextCanvas.width - canvas!.width) / 2;- const offsetY = (boundTextCanvas.height - canvas!.height) / 2;- const shiftX =- boundTextCanvas.width / 2 -- (boundTextCx - x1) * window.devicePixelRatio * scale -- offsetX -- padding * scale;-- const shiftY =- boundTextCanvas.height / 2 -- (boundTextCy - y1) * window.devicePixelRatio * scale -- offsetY -- padding * scale;- boundTextCanvasContext.translate(-shiftX, -shiftY);- // Clear the bound text area- boundTextCanvasContext.clearRect(- -(boundTextElement.width / 2 + BOUND_TEXT_PADDING) *- window.devicePixelRatio *- scale,- -(boundTextElement.height / 2 + BOUND_TEXT_PADDING) *- window.devicePixelRatio *- scale,- (boundTextElement.width + BOUND_TEXT_PADDING * 2) *- window.devicePixelRatio *- scale,- (boundTextElement.height + BOUND_TEXT_PADDING * 2) *- window.devicePixelRatio *- scale,- );- }-return {element,canvas,@@ -329,12 +263,8 @@ const generateElementCanvas = (zoomValue: zoom.value,canvasOffsetX,canvasOffsetY,- boundTextElementVersion:- getBoundTextElement(element, elementsMap)?.version || null,containingFrameOpacity:getContainingFrame(element, elementsMap)?.opacity || 100,- boundTextCanvas,- angle: element.angle,imageCrop: isImageElement(element) ? element.crop : null,};};@@ -622,8 +552,6 @@ const generateElementWithCanvas = (prevElementWithCanvas &&prevElementWithCanvas.zoomValue !== zoom.value &&!appState?.shouldCacheIgnoreZoom;- const boundTextElement = getBoundTextElement(element, elementsMap);- const boundTextElementVersion = boundTextElement?.version || null;const imageCrop = isImageElement(element) ? element.crop : null;const containingFrameOpacity =@@ -633,16 +561,8 @@ const generateElementWithCanvas = (!prevElementWithCanvas ||shouldRegenerateBecauseZoom ||prevElementWithCanvas.theme !== appState.theme ||- prevElementWithCanvas.boundTextElementVersion !== boundTextElementVersion ||prevElementWithCanvas.imageCrop !== imageCrop ||- prevElementWithCanvas.containingFrameOpacity !== containingFrameOpacity ||- // since we rotate the canvas when copying from cached canvas, we don't- // regenerate the cached canvas. But we need to in case of labels which are- // cached alongside the arrow, and we want the labels to remain unrotated- // with respect to the arrow.- (isArrowElement(element) &&- boundTextElement &&- element.angle !== prevElementWithCanvas.angle)+ prevElementWithCanvas.containingFrameOpacity !== containingFrameOpacity) {const elementWithCanvas = generateElementCanvas(element,@@ -672,7 +592,6 @@ const drawElementFromCanvas = () => {const element = elementWithCanvas.element;const padding = getCanvasPadding(element);- const zoom = elementWithCanvas.scale;const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, allElementsMap);const cx = ((x1 + x2) / 2 + appState.scrollX) * window.devicePixelRatio;const cy = ((y1 + y2) / 2 + appState.scrollY) * window.devicePixelRatio;@@ -683,71 +602,86 @@ const drawElementFromCanvas = (const boundTextElement = getBoundTextElement(element, allElementsMap);if (isArrowElement(element) && boundTextElement) {- const offsetX =- (elementWithCanvas.boundTextCanvas.width -- elementWithCanvas.canvas!.width) /- 2;- const offsetY =- (elementWithCanvas.boundTextCanvas.height -- elementWithCanvas.canvas!.height) /- 2;- context.translate(cx, cy);- context.drawImage(- elementWithCanvas.boundTextCanvas,- (-(x2 - x1) / 2) * window.devicePixelRatio - offsetX / zoom - padding,- (-(y2 - y1) / 2) * window.devicePixelRatio - offsetY / zoom - padding,- elementWithCanvas.boundTextCanvas.width / zoom,- elementWithCanvas.boundTextCanvas.height / zoom,+ // punch the label "hole" by clipping the arrow's blit out of the label+ // rect (even-odd) instead of baking a rotated arrow copy with a cleared+ // hole into a separate (`maxDim`-squared!) canvas. The hole stays+ // axis-aligned in scene space while the blit below rotates.+ const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords(+ boundTextElement,+ allElementsMap,);- } else {- // we translate context to element center so that rotation and scale- // originates from the element center- context.translate(cx, cy);-- context.rotate(element.angle);-- if (- "scale" in elementWithCanvas.element &&- !isPendingImageElement(element, renderConfig)- ) {- context.scale(- elementWithCanvas.element.scale[0],- elementWithCanvas.element.scale[1],- );- }+ // generously covers the arrow's blit at any rotation+ const outerHalf =+ Math.max(distance(x1, x2), distance(y1, y2)) * window.devicePixelRatio ++ padding * 10;+ context.beginPath();+ context.rect(cx - outerHalf, cy - outerHalf, outerHalf * 2, outerHalf * 2);+ context.rect(+ (boundTextCx -+ boundTextElement.width / 2 -+ BOUND_TEXT_PADDING ++ appState.scrollX) *+ window.devicePixelRatio,+ (boundTextCy -+ boundTextElement.height / 2 -+ BOUND_TEXT_PADDING ++ appState.scrollY) *+ window.devicePixelRatio,+ (boundTextElement.width + BOUND_TEXT_PADDING * 2) *+ window.devicePixelRatio,+ (boundTextElement.height + BOUND_TEXT_PADDING * 2) *+ window.devicePixelRatio,+ );+ context.clip("evenodd");+ }++ // we translate context to element center so that rotation and scale+ // originates from the element center+ context.translate(cx, cy);- // revert afterwards we don't have account for it during drawing- context.translate(-cx, -cy);-- context.drawImage(- elementWithCanvas.canvas!,- (x1 + appState.scrollX) * window.devicePixelRatio -- (padding * elementWithCanvas.scale) / elementWithCanvas.scale,- (y1 + appState.scrollY) * window.devicePixelRatio -- (padding * elementWithCanvas.scale) / elementWithCanvas.scale,- elementWithCanvas.canvas!.width / elementWithCanvas.scale,- elementWithCanvas.canvas!.height / elementWithCanvas.scale,+ context.rotate(element.angle);++ if (+ "scale" in elementWithCanvas.element &&+ !isPendingImageElement(element, renderConfig)+ ) {+ context.scale(+ elementWithCanvas.element.scale[0],+ elementWithCanvas.element.scale[1],);+ }- if (- import.meta.env.VITE_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX ===- "true" &&- hasBoundTextElement(element)- ) {- const textElement = getBoundTextElement(- element,- allElementsMap,- ) as ExcalidrawTextElementWithContainer;- const coords = getContainerCoords(element);- context.strokeStyle = "#c92a2a";- context.lineWidth = 3;- context.strokeRect(- (coords.x + appState.scrollX) * window.devicePixelRatio,- (coords.y + appState.scrollY) * window.devicePixelRatio,- getBoundTextMaxWidth(element, textElement) * window.devicePixelRatio,- getBoundTextMaxHeight(element, textElement) * window.devicePixelRatio,- );- }+ // revert afterwards we don't have account for it during drawing+ context.translate(-cx, -cy);++ context.drawImage(+ elementWithCanvas.canvas!,+ (x1 + appState.scrollX) * window.devicePixelRatio -+ (padding * elementWithCanvas.scale) / elementWithCanvas.scale,+ (y1 + appState.scrollY) * window.devicePixelRatio -+ (padding * elementWithCanvas.scale) / elementWithCanvas.scale,+ elementWithCanvas.canvas!.width / elementWithCanvas.scale,+ elementWithCanvas.canvas!.height / elementWithCanvas.scale,+ );++ if (+ import.meta.env.VITE_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX ===+ "true" &&+ hasBoundTextElement(element)+ ) {+ const textElement = getBoundTextElement(+ element,+ allElementsMap,+ ) as ExcalidrawTextElementWithContainer;+ const coords = getContainerCoords(element);+ context.strokeStyle = "#c92a2a";+ context.lineWidth = 3;+ context.strokeRect(+ (coords.x + appState.scrollX) * window.devicePixelRatio,+ (coords.y + appState.scrollY) * window.devicePixelRatio,+ getBoundTextMaxWidth(element, textElement) * window.devicePixelRatio,+ getBoundTextMaxHeight(element, textElement) * window.devicePixelRatio,+ );}context.restore();@@ -915,17 +849,14 @@ export const renderElement = (const boundTextElement = getBoundTextElement(element, elementsMap);if (isArrowElement(element) && boundTextElement) {- // Draw arrow directly as vector and clear label hole separately.- // This avoids temp-canvas bitmap blit which introduces resampling blur.+ // Draw arrow directly as vector (no temp-canvas bitmap blit which+ // introduces resampling blur). The label "hole" is cut by clipping+ // the arrow's own strokes out of the label rect (even-odd clip)+ // so that elements rendered beneath the arrow keep showing through+ // the gap.shiftX = element.width / 2 - (element.x - x1);shiftY = element.height / 2 - (element.y - y1);- context.save();- context.rotate(element.angle);- context.translate(-shiftX, -shiftY);- drawElementOnCanvas(element, rc, context, renderConfig);- context.restore();-const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords(boundTextElement,elementsMap,@@ -943,21 +874,21 @@ export const renderElement = (const holeWidth = boundTextElement.width + BOUND_TEXT_PADDING * 2;const holeHeight = boundTextElement.height + BOUND_TEXT_PADDING * 2;- const isTransparentHole =- "viewBackgroundColor" in appState &&- (appState.viewBackgroundColor === "transparent" ||- !appState.viewBackgroundColor);- if (!isTransparentHole) {- context.save();- context.fillStyle = applyDarkModeFilter(- renderConfig.canvasBackgroundColor,- renderConfig.theme === THEME.DARK,- );- context.fillRect(holeX, holeY, holeWidth, holeHeight);- context.restore();- } else {- context.clearRect(holeX, holeY, holeWidth, holeHeight);- }+ // generously covers the arrow's painted extent at any rotation+ // (the hole rect stays axis-aligned in scene space)+ const outerHalf =+ Math.max(distance(x1, x2), distance(y1, y2)) ++ getCanvasPadding(element) * 10;++ context.save();+ context.beginPath();+ context.rect(-outerHalf, -outerHalf, outerHalf * 2, outerHalf * 2);+ context.rect(holeX, holeY, holeWidth, holeHeight);+ context.clip("evenodd");+ context.rotate(element.angle);+ context.translate(-shiftX, -shiftY);+ drawElementOnCanvas(element, rc, context, renderConfig);+ context.restore();} else {context.rotate(element.angle);diff --git a/packages/excalidraw/tests/scene/export.test.ts b/packages/excalidraw/tests/scene/export.test.tsindex 3771d52229..4da68316d5 100644--- a/packages/excalidraw/tests/scene/export.test.ts+++ b/packages/excalidraw/tests/scene/export.test.ts@@ -2,6 +2,7 @@ import { exportToCanvas, exportToSvg } from "@excalidraw/utils";import {applyDarkModeFilter,+ BOUND_TEXT_PADDING,FONT_FAMILY,FRAME_STYLE,} from "@excalidraw/common";@@ -238,6 +239,72 @@ describe("exportToSvg", () => {});});+describe("exportToCanvas", () => {+ it("does not paint over elements beneath a labeled arrow's label (#11591)", async () => {+ const rectangle = API.createElement({+ type: "rectangle",+ id: "rect-11591",+ x: 0,+ y: -50,+ width: 200,+ height: 100,+ backgroundColor: "#ffc9c9",+ fillStyle: "solid",+ });+ const arrow = API.createElement({+ type: "arrow",+ id: "arrow-11591",+ x: -50,+ y: 0,+ width: 300,+ height: 0,+ points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(300, 0)],+ boundElements: [{ type: "text", id: "label-11591" }],+ });+ const label = API.createElement({+ type: "text",+ id: "label-11591",+ text: "label",+ x: 75,+ y: -10,+ width: 50,+ height: 20,+ containerId: "arrow-11591",+ });++ const canvas = await exportToCanvas({+ elements: [rectangle, arrow, label],+ files: null,+ appState: { exportBackground: true, viewBackgroundColor: "#ffffff" },+ });++ const context = canvas.getContext("2d") as any;+ const events = context.__getEvents();++ // the label "hole" must be cut out of the arrow's own strokes via an+ // even-odd clip — erasing (clearRect) or painting the background color+ // (fillRect) over the hole rect would destroy elements rendered beneath+ // the arrow+ expect(+ events.some(+ (event: any) =>+ event.type === "clip" && event.props.fillRule === "evenodd",+ ),+ ).toBe(true);++ const holeWidth = label.width + BOUND_TEXT_PADDING * 2;+ const holeHeight = label.height + BOUND_TEXT_PADDING * 2;+ expect(+ events.filter(+ (event: any) =>+ (event.type === "fillRect" || event.type === "clearRect") &&+ event.props.width === holeWidth &&+ event.props.height === holeHeight,+ ),+ ).toEqual([]);+ });+});+describe("exporting frames", () => {const getFrameNameHeight = (exportType: "canvas" | "svg") => {const height =