Skip to content

AI-native design editor. Open-source Figma alternative.

refactor(canvas): draw screen-sized chrome through one outline helper (#947)

Danila Poyarkov1c09e3074d1f
diff --git a/oxlint.json b/oxlint.json
index 9cfa6caac..fd433f305 100644
--- a/oxlint.json
+++ b/oxlint.json
@@ -189,6 +189,7 @@
"open-pencil/no-ts-suppression-comments": "error",
"open-pencil/no-reflect-delete-global-this-outside-tests": "error",
"open-pencil/no-core-browser-globals": "error",
+ "open-pencil/no-zoom-in-scene-drawing": "error",
"open-pencil/no-direct-open-pencil-window-internals": "error",
"open-pencil/no-direct-open-pencil-browser-store": "error",
"open-pencil/no-function-alias-imports": "error",
diff --git a/packages/core/AGENTS.md b/packages/core/AGENTS.md
index 7fff2a026..ab5249dc5 100644
--- a/packages/core/AGENTS.md
+++ b/packages/core/AGENTS.md
@@ -80,6 +80,7 @@ Canvas is CanvasKit (Skia WASM) on a WebGL surface, not DOM.
- Use `@open-pencil/core/geometry` for world/screen transforms, inverses, bounds, and handle placement instead of interpreting ancestor rotations or reflections independently.
- Label drawing and hit testing share `packages/core/src/canvas/labels/{layout,transform,style}.ts`, including paragraph measurements and unreflected label axes.
+- Editor chrome sized in screen pixels (outlines, borders, carets, highlights) draws in the overlay pass, never in the scene, which is cached and scaled while navigating. Draw outlines with `withScreenStroke` and `inNodeSpace` (`packages/core/src/canvas/overlays/outline.ts`). `open-pencil/no-zoom-in-scene-drawing` enforces it.
- Selection border width is constant regardless of zoom: divide by scale. Section and frame title text never scales: render at a fixed font size and ellipsize to fit.
- Rulers are rendered on the canvas with selection range badges that do not overlap tick numbers. Remote cursors are Figma-style colored arrows with a white border and name pill, rendered in screen space.
diff --git a/packages/core/src/canvas/issues/draw.ts b/packages/core/src/canvas/issues/draw.ts
index e0825738d..64371b53e 100644
--- a/packages/core/src/canvas/issues/draw.ts
+++ b/packages/core/src/canvas/issues/draw.ts
@@ -3,6 +3,7 @@ import type { Canvas } from 'canvaskit-wasm'
import type { SceneGraph } from '@open-pencil/scene-graph'
import type { Color } from '@open-pencil/scene-graph/primitives'
+import { inNodeSpace, withScreenStroke } from '#core/canvas/overlays/outline'
import type { SkiaRenderer } from '#core/canvas/renderer'
import {
ISSUE_EDGE_ARROW,
@@ -55,16 +56,11 @@ export function drawIssueHighlight(
if (!highlight || !node) return
const color = issueSeverityColor(highlight.severity)
- canvas.save()
- try {
- canvas.concat(createSceneGeometry(graph, preview).screenMatrix(node, r))
+ inNodeSpace(r, canvas, createSceneGeometry(graph, preview), node, () => {
r.auxFill.setColor(color4f(r, color, ISSUE_HIGHLIGHT_FILL_ALPHA))
canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), r.auxFill)
-
- r.auxStroke.setPathEffect(null)
- r.auxStroke.setColor(color4f(r, color))
- r.auxStroke.setStrokeWidth(ISSUE_HIGHLIGHT_STROKE_WIDTH / r.zoom)
- r.strokeNodeShape(canvas, node, r.auxStroke)
+ const stroke = { color: color4f(r, color), width: ISSUE_HIGHLIGHT_STROKE_WIDTH }
+ withScreenStroke(r, stroke, (paint) => r.strokeNodeShape(canvas, node, paint))
const minSize = highlight.minSize
if (minSize && (node.width < minSize.width || node.height < minSize.height)) {
@@ -72,16 +68,12 @@ export function drawIssueHighlight(
const height = Math.max(node.height, minSize.height)
const left = (node.width - width) / 2
const top = (node.height - height) / 2
- const dash = r.ck.PathEffect.MakeDash([TARGET_DASH / r.zoom, TARGET_DASH / r.zoom], 0)
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setPathEffect(dash)
- canvas.drawRect(r.ck.LTRBRect(left, top, left + width, top + height), r.auxStroke)
- r.auxStroke.setPathEffect(null)
- dash.delete()
+ const target = { color: color4f(r, color), dash: [TARGET_DASH, TARGET_DASH] as const }
+ withScreenStroke(r, target, (paint) =>
+ canvas.drawRect(r.ck.LTRBRect(left, top, left + width, top + height), paint)
+ )
}
- } finally {
- canvas.restore()
- }
+ })
}
/**
diff --git a/packages/core/src/canvas/overlays/component-sets.ts b/packages/core/src/canvas/overlays/component-sets.ts
index 938480b17..9829891e0 100644
--- a/packages/core/src/canvas/overlays/component-sets.ts
+++ b/packages/core/src/canvas/overlays/component-sets.ts
@@ -7,6 +7,8 @@ import type { SkiaRenderer } from '#core/canvas/renderer'
import { COMPONENT_SET_OUTLINE_RADIUS } from '#core/constants'
import { createSceneGeometry, type RotationPreview } from '#core/geometry'
+import { inNodeSpace, withScreenStroke } from './outline'
+
/**
* The dashed editing border of each component set without a stroke of its own. It is drawn
* every frame at the live zoom, so it stays one pixel wide on screen; drawn into the scene, it
@@ -25,26 +27,20 @@ export function drawComponentSetBorders(
if (sets.length === 0) return
const geometry = createSceneGeometry(graph, preview)
- const dash = r.ck.PathEffect.MakeDash(
- [r.COMPONENT_SET_DASH / r.zoom, r.COMPONENT_SET_DASH_GAP / r.zoom],
- 0
- )
- r.auxStroke.setStrokeWidth(r.COMPONENT_SET_BORDER_WIDTH / r.zoom)
- r.auxStroke.setColor(r.compColor())
- r.auxStroke.setPathEffect(dash)
- try {
+ const stroke = {
+ color: r.compColor(),
+ width: r.COMPONENT_SET_BORDER_WIDTH,
+ dash: [r.COMPONENT_SET_DASH, r.COMPONENT_SET_DASH_GAP] as const
+ }
+ withScreenStroke(r, stroke, (paint) => {
for (const { node } of sets) {
- canvas.save()
- canvas.concat(geometry.screenMatrix(node, r))
- const rect = r.ck.LTRBRect(0, 0, node.width, node.height)
- canvas.drawRRect(
- r.ck.RRectXY(rect, COMPONENT_SET_OUTLINE_RADIUS, COMPONENT_SET_OUTLINE_RADIUS),
- r.auxStroke
- )
- canvas.restore()
+ inNodeSpace(r, canvas, geometry, node, () => {
+ const rect = r.ck.LTRBRect(0, 0, node.width, node.height)
+ canvas.drawRRect(
+ r.ck.RRectXY(rect, COMPONENT_SET_OUTLINE_RADIUS, COMPONENT_SET_OUTLINE_RADIUS),
+ paint
+ )
+ })
}
- } finally {
- r.auxStroke.setPathEffect(null)
- dash.delete()
- }
+ })
}
diff --git a/packages/core/src/canvas/overlays/feedback.ts b/packages/core/src/canvas/overlays/feedback.ts
index ba2a302ce..1968a6ff2 100644
--- a/packages/core/src/canvas/overlays/feedback.ts
+++ b/packages/core/src/canvas/overlays/feedback.ts
@@ -7,6 +7,8 @@ import type { SnapGuide } from '@open-pencil/scene-graph/snap'
import { drawNodeHighlightRect } from '#core/canvas/highlight-rect'
import type { RenderOverlays, SkiaRenderer } from '#core/canvas/renderer'
import {
+ DROP_HIGHLIGHT_ALPHA,
+ DROP_HIGHLIGHT_STROKE,
FLASH_ATTACK_MS,
FLASH_COLOR,
FLASH_HOLD_MS,
@@ -15,6 +17,9 @@ import {
LAYOUT_INDICATOR_STROKE,
MARQUEE_FILL_ALPHA
} from '#core/constants'
+import { createSceneGeometry, type RotationPreview } from '#core/geometry'
+
+import { inNodeSpace, outlineNode } from './outline'
export function drawSnapGuides(r: SkiaRenderer, canvas: Canvas, guides?: SnapGuide[]): void {
if (!guides || guides.length === 0) return
@@ -108,3 +113,30 @@ export function drawLayoutInsertIndicator(
canvas.drawLine(x, y1, x, y2, r.auxStroke)
}
}
+
+/** The outline of the frame a dragged layer would drop into. */
+export function drawDropTarget(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ dropTargetId?: string | null,
+ preview?: RotationPreview | null
+): void {
+ const stroke = { color: r.selColor(DROP_HIGHLIGHT_ALPHA), width: DROP_HIGHLIGHT_STROKE }
+ outlineNode(r, canvas, graph, dropTargetId, stroke, preview)
+}
+
+/** The caret and text selection of the text layer being edited, over the layer. */
+export function drawEditingText(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ overlays: Pick<RenderOverlays, 'editingTextId' | 'textEditor' | 'rotationPreview'>
+): void {
+ const node = overlays.editingTextId ? graph.getNode(overlays.editingTextId) : undefined
+ const editor = overlays.textEditor
+ if (!node || !editor?.state?.paragraph) return
+ inNodeSpace(r, canvas, createSceneGeometry(graph, overlays.rotationPreview), node, () =>
+ r.drawTextEditOverlay(canvas, node, editor)
+ )
+}
diff --git a/packages/core/src/canvas/overlays/outline.ts b/packages/core/src/canvas/overlays/outline.ts
new file mode 100644
index 000000000..04147aa6a
--- /dev/null
+++ b/packages/core/src/canvas/overlays/outline.ts
@@ -0,0 +1,75 @@
+import type { Canvas, Color, Paint } from 'canvaskit-wasm'
+
+import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
+
+import type { SkiaRenderer } from '#core/canvas/renderer'
+import { createSceneGeometry, type RotationPreview } from '#core/geometry'
+
+/** An outline sized in screen pixels, so it looks the same at every zoom. */
+export interface ScreenStroke {
+ color: Color
+ /** Line width in screen pixels; one by default. */
+ width?: number
+ /** Dash and gap lengths in screen pixels; a solid line without. */
+ dash?: readonly [dash: number, gap: number]
+}
+
+/**
+ * Draw with the shared stroke paint set to `stroke` at the live zoom. Editor chrome draws this
+ * way, every frame, in the overlay pass: the scene is cached and scaled while navigating, so
+ * anything sized by the zoom must not be drawn there. The paint is reset and the dash freed
+ * afterwards, since the paint is shared and a dash is native memory.
+ */
+export function withScreenStroke(
+ r: SkiaRenderer,
+ stroke: ScreenStroke,
+ draw: (paint: Paint) => void
+): void {
+ const dash = stroke.dash
+ ? r.ck.PathEffect.MakeDash([stroke.dash[0] / r.zoom, stroke.dash[1] / r.zoom], 0)
+ : null
+ r.auxStroke.setStrokeWidth((stroke.width ?? 1) / r.zoom)
+ r.auxStroke.setColor(stroke.color)
+ r.auxStroke.setPathEffect(dash)
+ try {
+ draw(r.auxStroke)
+ } finally {
+ r.auxStroke.setPathEffect(null)
+ dash?.delete()
+ }
+}
+
+/** Draw in `node`'s own coordinates, placed where it is on screen. */
+export function inNodeSpace(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ geometry: ReturnType<typeof createSceneGeometry>,
+ node: SceneNode,
+ draw: () => void
+): void {
+ canvas.save()
+ try {
+ canvas.concat(geometry.screenMatrix(node, r))
+ draw()
+ } finally {
+ canvas.restore()
+ }
+}
+
+/** Outline the bounds of the layer `nodeId`, if it exists, with `stroke`. */
+export function outlineNode(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ nodeId: string | null | undefined,
+ stroke: ScreenStroke,
+ preview?: RotationPreview | null
+): void {
+ const node = nodeId ? graph.getNode(nodeId) : undefined
+ if (!node) return
+ inNodeSpace(r, canvas, createSceneGeometry(graph, preview), node, () =>
+ withScreenStroke(r, stroke, (paint) =>
+ canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), paint)
+ )
+ )
+}
diff --git a/packages/core/src/canvas/overlays/selection.ts b/packages/core/src/canvas/overlays/selection.ts
index 58874bd03..d9df86993 100644
--- a/packages/core/src/canvas/overlays/selection.ts
+++ b/packages/core/src/canvas/overlays/selection.ts
@@ -24,6 +24,11 @@ import {
} from '#core/geometry'
import { pathTextSelectionBand, pointAtArc } from '#core/text/path'
+import { inNodeSpace, outlineNode, withScreenStroke } from './outline'
+
+/** The dash of faint selection bounds, in screen pixels. */
+const SELECTION_DASH = [4, 4] as const
+
export function drawHoverHighlight(
r: SkiaRenderer,
canvas: Canvas,
@@ -59,14 +64,11 @@ export function drawCodeFocus(
r.auxFill.setColor(
component ? r.compColor(CODE_FOCUS_FILL_ALPHA) : r.selColor(CODE_FOCUS_FILL_ALPHA)
)
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(component ? r.compColor() : r.selColor())
- r.auxStroke.setPathEffect(null)
- canvas.save()
- canvas.concat(createSceneGeometry(graph, preview).screenMatrix(node, r))
- r.strokeNodeShape(canvas, node, r.auxFill)
- r.strokeNodeShape(canvas, node, r.auxStroke)
- canvas.restore()
+ const color = component ? r.compColor() : r.selColor()
+ inNodeSpace(r, canvas, createSceneGeometry(graph, preview), node, () => {
+ r.strokeNodeShape(canvas, node, r.auxFill)
+ withScreenStroke(r, { color }, (paint) => r.strokeNodeShape(canvas, node, paint))
+ })
}
export function drawEnteredContainer(
@@ -76,21 +78,8 @@ export function drawEnteredContainer(
enteredContainerId?: string | null,
preview?: RotationPreview | null
): void {
- const node = enteredContainerId ? graph.getNode(enteredContainerId) : undefined
- if (!node) return
- const dash = r.ck.PathEffect.MakeDash([4 / r.zoom, 4 / r.zoom], 0)
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(r.selColor(SELECTION_DASH_ALPHA))
- r.auxStroke.setPathEffect(dash)
- canvas.save()
- try {
- canvas.concat(createSceneGeometry(graph, preview).screenMatrix(node, r))
- canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), r.auxStroke)
- } finally {
- canvas.restore()
- r.auxStroke.setPathEffect(null)
- dash.delete()
- }
+ const stroke = { color: r.selColor(SELECTION_DASH_ALPHA), dash: SELECTION_DASH }
+ outlineNode(r, canvas, graph, enteredContainerId, stroke, preview)
}
/** Single-node selection overlay: path-text curve/band for imported TEXT_PATH,
@@ -238,26 +227,19 @@ function drawTextPathSelection(
const immutableBand = band.detachAndDelete()
r.auxFill.setColor(r.selColor(0.16))
canvas.drawPath(immutableBand, r.auxFill)
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(r.selColor())
- r.auxStroke.setPathEffect(null)
- canvas.drawPath(immutableBand, r.auxStroke)
+ withScreenStroke(r, { color: r.selColor() }, (paint) =>
+ canvas.drawPath(immutableBand, paint)
+ )
immutableBand.delete()
}
// Faint dashed bounds + resize/rotate handles from the fitted path box.
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(r.selColor(SELECTION_DASH_ALPHA))
- // MakeDash allocates a WASM PathEffect the JS GC won't reclaim; this runs
- // every repaint while a TEXT_PATH node is selected, so free it explicitly.
- const dash = r.ck.PathEffect.MakeDash([4 / r.zoom, 4 / r.zoom], 0)
- r.auxStroke.setPathEffect(dash)
- canvas.drawRect(
- r.ck.LTRBRect(box.x, box.y, box.x + box.width, box.y + box.height),
- r.auxStroke
+ withScreenStroke(
+ r,
+ { color: r.selColor(SELECTION_DASH_ALPHA), dash: SELECTION_DASH },
+ (paint) =>
+ canvas.drawRect(r.ck.LTRBRect(box.x, box.y, box.x + box.width, box.y + box.height), paint)
)
- r.auxStroke.setPathEffect(null) // auxStroke is shared — never leave a dash effect on it.
- dash.delete()
drawBoundsHandles(
r,
canvas,
diff --git a/packages/core/src/canvas/overlays/slots.ts b/packages/core/src/canvas/overlays/slots.ts
index 93185bff6..f890fa67d 100644
--- a/packages/core/src/canvas/overlays/slots.ts
+++ b/packages/core/src/canvas/overlays/slots.ts
@@ -7,6 +7,8 @@ import type { SkiaRenderer } from '#core/canvas/renderer'
import { SLOT_EMPTY_FILL_ALPHA } from '#core/constants'
import { createSceneGeometry, type RotationPreview } from '#core/geometry'
+import { inNodeSpace, withScreenStroke } from './outline'
+
const SLOT_DASH = 4
/**
@@ -49,22 +51,14 @@ export function drawSlotOutlines(
if (slots.size === 0) return
const geometry = createSceneGeometry(graph, preview)
- const dash = r.ck.PathEffect.MakeDash([SLOT_DASH / r.zoom, SLOT_DASH / r.zoom], 0)
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(r.slotColor())
- r.auxStroke.setPathEffect(dash)
r.auxFill.setColor(r.slotColor(SLOT_EMPTY_FILL_ALPHA))
- try {
+ withScreenStroke(r, { color: r.slotColor(), dash: [SLOT_DASH, SLOT_DASH] }, (paint) => {
for (const frame of slots.values()) {
- canvas.save()
- canvas.concat(geometry.screenMatrix(frame, r))
- const rect = r.ck.LTRBRect(0, 0, frame.width, frame.height)
- if (frame.childIds.length === 0) canvas.drawRect(rect, r.auxFill)
- canvas.drawRect(rect, r.auxStroke)
- canvas.restore()
+ inNodeSpace(r, canvas, geometry, frame, () => {
+ const rect = r.ck.LTRBRect(0, 0, frame.width, frame.height)
+ if (frame.childIds.length === 0) canvas.drawRect(rect, r.auxFill)
+ canvas.drawRect(rect, paint)
+ })
}
- } finally {
- r.auxStroke.setPathEffect(null)
- dash.delete()
- }
+ })
}
diff --git a/packages/core/src/canvas/overlays/text-edit.ts b/packages/core/src/canvas/overlays/text-edit.ts
index 8c968cc9c..d1692500f 100644
--- a/packages/core/src/canvas/overlays/text-edit.ts
+++ b/packages/core/src/canvas/overlays/text-edit.ts
@@ -6,6 +6,8 @@ import type { SkiaRenderer } from '#core/canvas/renderer'
import { TEXT_CARET_COLOR, TEXT_CARET_WIDTH, TEXT_SELECTION_COLOR } from '#core/constants'
import type { TextEditor } from '#core/text/editor'
+import { withScreenStroke } from './outline'
+
export function drawTextEditOverlay(
r: SkiaRenderer,
canvas: Canvas,
@@ -22,10 +24,9 @@ export function drawTextEditOverlay(
return
}
- r.auxStroke.setStrokeWidth(1 / r.zoom)
- r.auxStroke.setColor(r.selColor())
- r.auxStroke.setPathEffect(null)
- canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), r.auxStroke)
+ withScreenStroke(r, { color: r.selColor() }, (paint) =>
+ canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), paint)
+ )
const selRects = editor.getSelectionRects()
if (selRects.length > 0) {
diff --git a/packages/core/src/canvas/renderer/overlay-pass.ts b/packages/core/src/canvas/renderer/overlay-pass.ts
index b96b8d476..088b00aa8 100644
--- a/packages/core/src/canvas/renderer/overlay-pass.ts
+++ b/packages/core/src/canvas/renderer/overlay-pass.ts
@@ -6,6 +6,7 @@ import { drawGuides } from '#core/canvas/guides/draw'
import { drawIssueHighlight, drawIssueMarkers } from '#core/canvas/issues/draw'
import { layoutIssueMarkers } from '#core/canvas/issues/layout'
import { drawComponentSetBorders } from '#core/canvas/overlays/component-sets'
+import { drawDropTarget, drawEditingText } from '#core/canvas/overlays/feedback'
import { drawLayoutOutlines } from '#core/canvas/overlays/layout-outlines'
import { drawMeasurementSegment } from '#core/canvas/overlays/measurement'
import { drawCodeFocus } from '#core/canvas/overlays/selection'
@@ -105,6 +106,8 @@ export function drawOverlayPass(
r.drawHoverHighlight(canvas, graph, hoveredNodeId, overlays.rotationPreview)
drawIssueHighlight(r, canvas, graph, overlays.designIssues?.highlight, overlays.rotationPreview)
r.drawEnteredContainer(canvas, graph, overlays.enteredContainerId, overlays.rotationPreview)
+ drawDropTarget(r, canvas, graph, overlays.dropTargetId, overlays.rotationPreview)
+ drawEditingText(r, canvas, graph, overlays)
r.profiler.beginPhase('render:selection')
r.drawSelection(canvas, graph, selectedIds, overlays)
if (measuring) r.drawMeasurements(canvas, graph, selectedIds, overlays.hoveredNodeId)
diff --git a/packages/core/src/canvas/renderer/pipeline.ts b/packages/core/src/canvas/renderer/pipeline.ts
index 369621ae1..97ecd02d1 100644
--- a/packages/core/src/canvas/renderer/pipeline.ts
+++ b/packages/core/src/canvas/renderer/pipeline.ts
@@ -98,11 +98,7 @@ export function renderFromEditorState(
function sceneContentDependsOnOverlay(overlays: RenderOverlays): boolean {
return (
- overlays.dropTargetId != null ||
- overlays.rotationPreview != null ||
- overlays.editingTextId != null ||
- overlays.nodeEditState != null ||
- overlays.playing === true
+ overlays.rotationPreview != null || overlays.nodeEditState != null || overlays.playing === true
)
}
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index be6912e36..0935b736d 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -12,7 +12,6 @@ import { computeDescendantVisualBounds } from '@open-pencil/scene-graph/geometry
import Matrix from '@open-pencil/scene-graph/matrix'
import type { Color } from '@open-pencil/scene-graph/primitives'
-import { DROP_HIGHLIGHT_ALPHA, DROP_HIGHLIGHT_STROKE } from '#core/constants'
import { createSceneGeometry, nodeOrientationMatrix, projectedNode } from '#core/geometry'
import { transformTextCase } from '#core/text/case'
import { fontManager } from '#core/text/fonts'
@@ -120,29 +119,18 @@ function renderNodeContent(
canvas: Canvas,
graph: SceneGraph,
node: SceneNode,
- nodeId: string,
overlays: RenderOverlays
): void {
if (node.type === 'SECTION') {
r.renderSection(canvas, node, graph)
} else if (node.type === 'COMPONENT_SET' && !overlays.playing) {
- // A previewing canvas draws a set as a plain frame, without its dashed editing border.
+ // A previewing canvas draws a set as a plain frame.
r.renderComponentSet(canvas, node, graph)
} else if (node.type === 'BOOLEAN_OPERATION') {
renderBooleanOperation(r, canvas, node, graph)
} else {
r.renderShape(canvas, node, graph)
}
-
- if (overlays.editingTextId === nodeId && overlays.textEditor?.state?.paragraph) {
- r.drawTextEditOverlay(canvas, node, overlays.textEditor)
- }
-
- if (overlays.dropTargetId === nodeId) {
- r.auxStroke.setStrokeWidth(DROP_HIGHLIGHT_STROKE / r.zoom)
- r.auxStroke.setColor(r.selColor(DROP_HIGHLIGHT_ALPHA))
- canvas.drawRect(r.ck.LTRBRect(0, 0, node.width, node.height), r.auxStroke)
- }
}
function renderMaskNodeContent(
@@ -150,13 +138,12 @@ function renderMaskNodeContent(
canvas: Canvas,
graph: SceneGraph,
node: SceneNode,
- nodeId: string,
overlays: RenderOverlays
): void {
canvas.save()
canvas.translate(node.x, node.y)
applyNodeTransforms(canvas, node, overlays)
- renderNodeContent(r, canvas, graph, node, nodeId, {})
+ renderNodeContent(r, canvas, graph, node, {})
canvas.restore()
}
@@ -181,7 +168,7 @@ function renderChildIds(
(childId) => r.renderNode(canvas, graph, childId, overlays, absX, absY, hasTransformedAncestor),
(childId) => {
const child = graph.getNode(childId)
- if (child) renderMaskNodeContent(r, canvas, graph, child, childId, overlays)
+ if (child) renderMaskNodeContent(r, canvas, graph, child, overlays)
},
(childId) => {
const child = graph.getNode(childId)
@@ -241,7 +228,7 @@ export function renderNodeSelf(
canvas.save()
canvas.translate(node.x, node.y)
applyNodeTransforms(canvas, node, overlays)
- renderNodeContent(r, canvas, graph, node, nodeId, overlays)
+ renderNodeContent(r, canvas, graph, node, overlays)
drawLayoutGrids(r, canvas, node)
canvas.restore()
}
@@ -374,7 +361,7 @@ export function renderNode(
}
applyNodeTransforms(canvas, node, overlays)
- renderNodeContent(r, canvas, graph, node, nodeId, overlays)
+ renderNodeContent(r, canvas, graph, node, overlays)
drawLayoutGrids(r, canvas, node)
renderChildren(
r,
diff --git a/tests/engine/render/canvas/cache.test.ts b/tests/engine/render/canvas/cache.test.ts
index bea38117d..4200fa305 100644
--- a/tests/engine/render/canvas/cache.test.ts
+++ b/tests/engine/render/canvas/cache.test.ts
@@ -214,4 +214,24 @@ describe('render cache regressions', () => {
surface.delete()
}
})
+
+ test('drop targeting and text editing reuse the cached scene', () => {
+ const surface = expectDefined(ck.MakeSurface(900, 700), 'preview surface')
+ const renderer = new SkiaRenderer(ck, surface)
+ renderer.viewportWidth = 900
+ renderer.viewportHeight = 700
+ renderer.dpr = 1
+ renderer.zoom = 0.75
+ renderer.pageId = graph.getPages()[0].id
+
+ try {
+ renderPreview(renderer, 20)
+ for (const overlays of [{ dropTargetId: movingNodeId }, { editingTextId: movingNodeId }]) {
+ renderer.render(graph, new Set(), overlays, 20)
+ expect(renderer.profiler.stats.scenePictureMode).toBe('hit')
+ }
+ } finally {
+ surface.delete()
+ }
+ })
})
diff --git a/tools/checks/lint/src/plugin.ts b/tools/checks/lint/src/plugin.ts
index 3b6d15af7..962afb906 100644
--- a/tools/checks/lint/src/plugin.ts
+++ b/tools/checks/lint/src/plugin.ts
@@ -396,6 +396,7 @@ import {
noReflectDeleteGlobalThisOutsideTests,
noTsSuppressionComments,
noCoreBrowserGlobals,
+ noZoomInSceneDrawing,
noDirectGraphEmitterSubscriptions,
noOnUnmountedInCompositionRoots,
noComposableStateWrappers,
@@ -476,6 +477,7 @@ const plugin = {
'no-function-type': noFunctionType,
'no-reflect-delete-global-this-outside-tests': noReflectDeleteGlobalThisOutsideTests,
'no-core-browser-globals': noCoreBrowserGlobals,
+ 'no-zoom-in-scene-drawing': noZoomInSceneDrawing,
'no-direct-open-pencil-window-internals': noDirectOpenPencilWindowInternals,
'no-direct-open-pencil-browser-store': noDirectOpenPencilBrowserStore,
'no-direct-graph-emitter-subscriptions': noDirectGraphEmitterSubscriptions,
diff --git a/tools/checks/lint/src/rules/runtime.ts b/tools/checks/lint/src/rules/runtime.ts
index c3f882f4a..180b00288 100644
--- a/tools/checks/lint/src/rules/runtime.ts
+++ b/tools/checks/lint/src/rules/runtime.ts
@@ -214,6 +214,62 @@ const noCoreBrowserGlobals = {
}
} satisfies RuleDefinition
+/** Modules that draw document content into the cached scene. */
+const SCENE_DRAWING_FILES = [
+ '/packages/core/src/canvas/scene.ts',
+ '/packages/core/src/canvas/strokes.ts',
+ '/packages/core/src/canvas/fills.ts',
+ '/packages/core/src/canvas/boolean.ts'
+]
+const SCENE_DRAWING_DIRECTORIES = ['/packages/core/src/canvas/text/']
+
+/** Whether `node` is an argument of `effectRasterScale(...)`, not inside a function passed to it. */
+function inEffectRasterScale(node: TSESTree.Node): boolean {
+ for (let current = node.parent; current; current = current.parent) {
+ if (
+ current.type === 'ArrowFunctionExpression' ||
+ current.type === 'FunctionExpression' ||
+ current.type === 'FunctionDeclaration'
+ )
+ return false
+ if (
+ current.type === 'CallExpression' &&
+ current.callee.type === 'Identifier' &&
+ current.callee.name === 'effectRasterScale'
+ )
+ return true
+ }
+ return false
+}
+
+const noZoomInSceneDrawing = {
+ meta: {
+ docs: {
+ description:
+ 'Disallow reading the zoom where document content is drawn: the scene is cached and scaled, so screen-sized chrome belongs in the overlay pass'
+ }
+ },
+ create(context) {
+ const file = normalizedFilename(context)
+ const scene =
+ SCENE_DRAWING_FILES.some((suffix) => file.endsWith(suffix)) ||
+ SCENE_DRAWING_DIRECTORIES.some((directory) => file.includes(directory))
+ if (!scene) return {}
+
+ return {
+ MemberExpression(node) {
+ if (node.computed || node.property.type !== 'Identifier') return
+ if (node.property.name !== 'zoom' || inEffectRasterScale(node)) return
+ context.report({
+ node,
+ message:
+ 'Do not size scene drawing by the zoom; the scene is cached and scaled while navigating. Draw screen-sized chrome in the overlay pass with withScreenStroke.'
+ })
+ }
+ }
+ }
+} satisfies RuleDefinition
+
const noDirectGraphEmitterSubscriptions = {
meta: {
docs: {
@@ -402,6 +458,7 @@ export {
noReflectDeleteGlobalThisOutsideTests,
noTsSuppressionComments,
noCoreBrowserGlobals,
+ noZoomInSceneDrawing,
noDirectGraphEmitterSubscriptions,
noOnUnmountedInCompositionRoots,
noComposableStateWrappers,
diff --git a/tools/checks/lint/tests/zoom-in-scene.test.ts b/tools/checks/lint/tests/zoom-in-scene.test.ts
new file mode 100644
index 000000000..573d9159d
--- /dev/null
+++ b/tools/checks/lint/tests/zoom-in-scene.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, test } from 'bun:test'
+
+import { lint, ruleDiagnostics } from './helpers/lint.ts'
+
+const rule = 'no-zoom-in-scene-drawing'
+const rules = { [`open-pencil/${rule}`]: 'error' }
+const SCENE = 'packages/core/src/canvas/scene.ts'
+
+describe('no-zoom-in-scene-drawing', () => {
+ test.each([
+ 'paint.setStrokeWidth(1 / r.zoom)',
+ 'const dash = [4 / renderer.zoom, 4 / renderer.zoom]',
+ 'const scale = effectRasterScale(() => 1 / r.zoom)'
+ ])('rejects %s in scene drawing', async (source) => {
+ expect(ruleDiagnostics(await lint(source, rules, SCENE), rule).length).toBeGreaterThan(0)
+ })
+
+ test('rejects it in text drawing', async () => {
+ const source = 'const width = 1 / r.zoom'
+ expect(
+ ruleDiagnostics(await lint(source, rules, 'packages/core/src/canvas/text/derived.ts'), rule)
+ ).toHaveLength(1)
+ })
+
+ test.each([
+ ['raster quality for effects', 'const scale = effectRasterScale(r.zoom * r.dpr)', SCENE],
+ [
+ 'overlay drawing',
+ 'paint.setStrokeWidth(1 / r.zoom)',
+ 'packages/core/src/canvas/overlays/slots.ts'
+ ],
+ ['a computed property', "const value = settings['zoom']", SCENE]
+ ])('accepts %s', async (_name, source, file) => {
+ expect(ruleDiagnostics(await lint(source, rules, file), rule)).toHaveLength(0)
+ })
+})