Skip to content

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

fix(canvas): dash with round and square ends, joins, and smoothed corners as Figma does (#1317)

Danila Poyarkovd8a2b15c27bd
diff --git a/changes/1317/fixed.md b/changes/1317/fixed.md
new file mode 100644
index 000000000..c6c950886
--- /dev/null
+++ b/changes/1317/fixed.md
@@ -0,0 +1 @@
+- Dashed strokes with round or square ends, round or bevel joins, or smoothed corners are laid out as in Figma: each side and corner holds whole dashes, ends are capped only where dashes stop, and inside and outside strokes cap at their full weight.
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index ba07fe433..7d93d8372 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -50,6 +50,7 @@ import {
getStrokeJoinEntity,
normalizeDashPattern
} from './strokes'
+import { drawOutlinedDashes } from './strokes/outlined-dashes'
import { drawDashedRectStroke, strokeDashesByOutline, strokeSideWeights } from './strokes/sides'
import { textVerticalOffset, withTextParagraph } from './text'
import { drawTextLayout } from './text/decorations'
@@ -747,7 +748,8 @@ function drawVectorPathStrokes(
/**
* Draws a dashed stroke on a rectangle, frame, section, or component set with Figma's dash
- * layout, and returns false when the stroke has no dashes or the layout does not apply.
+ * layout, and returns false when the stroke has no dashes or has round or square dash ends on
+ * sides of their own weights, which that layout does not cover.
*/
function drawFigmaDashes(
r: SkiaRenderer,
@@ -756,8 +758,14 @@ function drawFigmaDashes(
stroke: SceneNode['strokes'][0]
): boolean {
const dash = normalizeDashPattern(stroke.dashPattern)
- if (dash.length === 0 || !strokeDashesByOutline(node, stroke)) return false
- drawDashedRectStroke(r, canvas, node, stroke, strokeSideWeights(node, stroke), dash)
+ if (dash.length === 0) return false
+ if (strokeDashesByOutline(node, stroke)) {
+ drawDashedRectStroke(r, canvas, node, stroke, strokeSideWeights(node, stroke), dash)
+ } else if (node.independentStrokeWeights) {
+ return false
+ } else {
+ drawOutlinedDashes(r, canvas, node, stroke, dash)
+ }
return true
}
diff --git a/packages/core/src/canvas/strokes/outlined-dashes.ts b/packages/core/src/canvas/strokes/outlined-dashes.ts
new file mode 100644
index 000000000..8164c82da
--- /dev/null
+++ b/packages/core/src/canvas/strokes/outlined-dashes.ts
@@ -0,0 +1,227 @@
+import type { Canvas, CanvasKit, Path, PathBuilder } from 'canvaskit-wasm'
+
+import type { SceneNode, Stroke } from '@open-pencil/scene-graph'
+import type { Vector } from '@open-pencil/scene-graph/primitives'
+
+import type { SkiaRenderer } from '#core/canvas/renderer'
+import { makeSmoothRRectPath, nodeHasSmoothCorners } from '#core/canvas/shapes'
+import { getStrokeCapEntity, getStrokeJoinEntity } from '#core/canvas/strokes'
+import { sideDashSpans } from '#core/canvas/strokes/sides'
+
+/** Lengths closer than this count as touching. */
+const EPSILON = 1e-3
+
+type Span = [number, number]
+
+/**
+ * Draws a dashed stroke on a rectangle of one weight as Figma 126 does when its dashes have round
+ * or square ends, its corners are smoothed, or its sharp corners do not miter. Each line and each
+ * curve of the shape's edge, which makes a rounded corner one curve and a smoothed corner three,
+ * is dashed on its own along the edge, and the dashes that meet where two of them join run on as
+ * one. Each dash is stroked with the stroke's
+ * cap and join, at double width and clipped to the shape for inside and outside strokes, as
+ * Figma builds those.
+ */
+export function drawOutlinedDashes(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ node: SceneNode,
+ stroke: Stroke,
+ pattern: readonly number[]
+): void {
+ const cap = stroke.cap ?? node.strokeCap
+ const join = stroke.join ?? node.strokeJoin
+ // Geometry changes do not all clear a node's paths, so the key carries what shapes the dashes.
+ const corners = node.independentCorners
+ ? `${node.topLeftRadius},${node.topRightRadius},${node.bottomRightRadius},${node.bottomLeftRadius}`
+ : `${node.cornerRadius}`
+ const shape = `${node.width}x${node.height}|${corners}|${node.cornerSmoothing}|${node.strokeMiterLimit}`
+ const key = `${node.id}|outlined-dashes|${shape}|${stroke.align}|${stroke.weight}|${cap}|${join}|${pattern.join(',')}`
+ let dashes = r.nodePaths.strokeOutline.get(key)
+ const edge = edgePath(r, node)
+ try {
+ if (!dashes) {
+ const path = dashPath(r, edge, pattern, {
+ width: stroke.align === 'CENTER' ? stroke.weight : stroke.weight * 2,
+ cap: getStrokeCapEntity(r, cap),
+ join: getStrokeJoinEntity(r, join),
+ miter_limit: node.strokeMiterLimit
+ })
+ dashes = path ? [path] : []
+ r.nodePaths.store(r.nodePaths.strokeOutline, key, dashes)
+ }
+ r.strokePaint.setPathEffect(null)
+ r.strokePaint.setStyle(r.ck.PaintStyle.Fill)
+ canvas.save()
+ try {
+ if (stroke.align === 'INSIDE') canvas.clipPath(edge, r.ck.ClipOp.Intersect, true)
+ if (stroke.align === 'OUTSIDE') canvas.clipPath(edge, r.ck.ClipOp.Difference, true)
+ for (const path of dashes) canvas.drawPath(path, r.strokePaint)
+ } finally {
+ canvas.restore()
+ r.strokePaint.setStyle(r.ck.PaintStyle.Stroke)
+ }
+ } finally {
+ edge.delete()
+ }
+}
+
+/** The shape's edge: its rectangle, rounded rectangle, or smoothed corners. */
+function edgePath(r: SkiaRenderer, node: SceneNode): Path {
+ if (nodeHasSmoothCorners(node)) return makeSmoothRRectPath(r, node)
+ const builder = new r.ck.PathBuilder()
+ builder.addRRect(r.makeRRect(node))
+ return builder.detachAndDelete()
+}
+
+/** All dashes along `edge`, stroked with `options`, as one path. */
+function dashPath(
+ r: SkiaRenderer,
+ edge: Path,
+ pattern: readonly number[],
+ options: Parameters<Path['makeStroked']>[0]
+): Path | null {
+ const pieces = outlinePieces(r, edge)
+ const total = pieces.reduce((sum, [, length]) => sum + length, 0)
+ if (total <= 0) return null
+ const spans = mergeTouching(
+ pieces.flatMap(([start, length]) =>
+ length > EPSILON
+ ? sideDashSpans(length, pattern).map(([s, e]): Span => [start + s, start + e])
+ : []
+ ),
+ total
+ )
+ const iter = new r.ck.ContourMeasureIter(edge, true, 1)
+ const contour = iter.next()
+ iter.delete()
+ if (!contour) return null
+ const union = new r.ck.PathBuilder()
+ try {
+ for (const [start, end] of spans) {
+ const line = new r.ck.PathBuilder()
+ appendSegment(r.ck, line, contour.getSegment(start, Math.min(end, total), true), false)
+ if (end > total) appendSegment(r.ck, line, contour.getSegment(0, end - total, true), true)
+ const centre = line.detachAndDelete()
+ const outline = centre.makeStroked(options)
+ centre.delete()
+ if (!outline) continue
+ union.addPath(outline)
+ outline.delete()
+ }
+ } finally {
+ contour.delete()
+ }
+ return union.detachAndDelete()
+}
+
+/**
+ * The outline split into the parts Figma dashes on its own, as [start, length] along it: each
+ * line and each curve of it, so a smoothed corner's three curves are dashed one by one.
+ */
+function outlinePieces(r: SkiaRenderer, edge: Path): Span[] {
+ const pieces: Span[] = []
+ let position = 0
+ let start: Vector = { x: 0, y: 0 }
+ let point: Vector = { x: 0, y: 0 }
+ const piece = (length: number, to: Vector) => {
+ pieces.push([position, length])
+ position += length
+ point = to
+ }
+ forEachVerb(r.ck, edge, (verb, args) => {
+ if (verb === r.ck.MOVE_VERB) {
+ start = point = { x: args[0], y: args[1] }
+ } else if (verb === r.ck.LINE_VERB) {
+ const to = { x: args[0], y: args[1] }
+ piece(Math.hypot(to.x - point.x, to.y - point.y), to)
+ } else if (verb === r.ck.CLOSE_VERB) {
+ const length = Math.hypot(start.x - point.x, start.y - point.y)
+ if (length > EPSILON) piece(length, start)
+ } else {
+ // A conic's weight follows its end point.
+ const at = args.length - (verb === r.ck.CONIC_VERB ? 3 : 2)
+ piece(curveLength(r, point, verb, args), { x: args[at], y: args[at + 1] })
+ }
+ })
+ return pieces
+}
+
+/** The length of one curve verb from `from`. */
+function curveLength(r: SkiaRenderer, from: Vector, verb: number, args: Float32Array): number {
+ const builder = new r.ck.PathBuilder()
+ builder.moveTo(from.x, from.y)
+ if (verb === r.ck.QUAD_VERB) builder.quadTo(args[0], args[1], args[2], args[3])
+ else if (verb === r.ck.CONIC_VERB) builder.conicTo(args[0], args[1], args[2], args[3], args[4])
+ else builder.cubicTo(args[0], args[1], args[2], args[3], args[4], args[5])
+ const path = builder.detachAndDelete()
+ const iter = new r.ck.ContourMeasureIter(path, false, 1)
+ const contour = iter.next()
+ const length = contour?.length() ?? 0
+ contour?.delete()
+ iter.delete()
+ path.delete()
+ return length
+}
+
+/**
+ * Spans in order along a closed outline of length `total`, with spans that touch joined into one,
+ * the last running past `total` into the first when they meet where the outline starts.
+ */
+function mergeTouching(spans: Span[], total: number): Span[] {
+ const merged: Span[] = []
+ for (const [start, end] of spans) {
+ const last = merged.at(-1)
+ if (last && start - last[1] < EPSILON) last[1] = Math.max(last[1], end)
+ else merged.push([start, end])
+ }
+ if (merged.length < 2) return merged
+ const first = merged[0]
+ const last = merged[merged.length - 1]
+ if (first[0] < EPSILON && total - last[1] < EPSILON) {
+ last[1] = total + first[1]
+ merged.shift()
+ }
+ return merged
+}
+
+/** Calls `visit` with each verb of `path` and its coordinates, a conic's weight last. */
+function forEachVerb(
+ ck: CanvasKit,
+ path: Path,
+ visit: (verb: number, args: Float32Array) => void
+): void {
+ const cmds = path.toCmds()
+ const counts = new Map([
+ [ck.MOVE_VERB, 2],
+ [ck.LINE_VERB, 2],
+ [ck.QUAD_VERB, 4],
+ [ck.CONIC_VERB, 5],
+ [ck.CUBIC_VERB, 6],
+ [ck.CLOSE_VERB, 0]
+ ])
+ for (let index = 0; index < cmds.length;) {
+ const verb = cmds[index]
+ const count = counts.get(verb) ?? 0
+ visit(verb, cmds.subarray(index + 1, index + 1 + count))
+ index += 1 + count
+ }
+}
+
+/** Appends `segment` to `builder`, running on from its last point when `continues`. */
+function appendSegment(
+ ck: CanvasKit,
+ builder: PathBuilder,
+ segment: Path,
+ continues: boolean
+): void {
+ forEachVerb(ck, segment, (verb, a) => {
+ if (verb === ck.MOVE_VERB) {
+ if (!continues) builder.moveTo(a[0], a[1])
+ } else if (verb === ck.LINE_VERB) builder.lineTo(a[0], a[1])
+ else if (verb === ck.QUAD_VERB) builder.quadTo(a[0], a[1], a[2], a[3])
+ else if (verb === ck.CONIC_VERB) builder.conicTo(a[0], a[1], a[2], a[3], a[4])
+ else if (verb === ck.CUBIC_VERB) builder.cubicTo(a[0], a[1], a[2], a[3], a[4], a[5])
+ })
+ segment.delete()
+}
diff --git a/packages/core/tests/canvas/strokes/outlined-dashes.test.ts b/packages/core/tests/canvas/strokes/outlined-dashes.test.ts
new file mode 100644
index 000000000..3e30059c1
--- /dev/null
+++ b/packages/core/tests/canvas/strokes/outlined-dashes.test.ts
@@ -0,0 +1,117 @@
+import { afterAll, describe, expect, test } from 'bun:test'
+
+import { SkiaRenderer } from '@open-pencil/core'
+import { initCanvasKit, renderNodesToImage } from '@open-pencil/core/io'
+import { SceneGraph, type SceneNode, type Stroke } from '@open-pencil/scene-graph'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+import { expectFigmaRow, renderRow } from '#core-tests/helpers/figma-rows'
+
+const ck = await initCanvasKit()
+const renderer = new SkiaRenderer(ck, expectDefined(ck.MakeSurface(1, 1)))
+afterAll(() => renderer.destroy())
+
+/** Rows of alpha from Figma 126 exports, read as `renderRow` reads ours. */
+const FIGMA = {
+ roundCaps: [
+ 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 239, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 0, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 0, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 239,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 239, 255, 255, 255, 255,
+ 255, 255, 255, 255, 0, 0, 0, 0
+ ],
+ squareCaps: [
+ 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 0, 0, 0, 0
+ ],
+ insideRoundCaps: [
+ 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 144, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 144, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 144, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 144, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 144, 255, 255, 255, 255, 255,
+ 255, 255, 0, 0, 0, 0, 0, 0
+ ],
+ roundJoin: [
+ 0, 0, 0, 32, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 0
+ ],
+ bevelJoin: [
+ 0, 0, 0, 0, 127, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 0
+ ],
+ smoothed: [
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 96, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 0
+ ]
+}
+
+/** A 150 × 90 rectangle at (120, 70) with an 8 px blue stroke dashed 20, 10. */
+function dashedRect(props: Partial<SceneNode> = {}, align: Stroke['align'] = 'CENTER') {
+ return (graph: SceneGraph, hostId: string) => {
+ graph.createNode('RECTANGLE', hostId, {
+ x: 120,
+ y: 70,
+ width: 150,
+ height: 90,
+ fills: [],
+ strokes: [
+ {
+ type: 'SOLID',
+ color: { r: 0, g: 0, b: 1, a: 1 },
+ opacity: 1,
+ visible: true,
+ weight: 8,
+ align,
+ dashPattern: [20, 10]
+ }
+ ],
+ strokeWeight: 8,
+ strokeAlign: align,
+ ...props
+ })
+ }
+}
+
+const row = (props: Partial<SceneNode>, y: number, x0: number, x1: number, align?: Stroke['align']) =>
+ renderRow(ck, renderer, dashedRect(props, align), y, x0, x1)
+
+describe('dashes with caps, joins, and smoothed corners, as in Figma', () => {
+ test('cap each dash end but where dashes meet at a corner', () => {
+ expectFigmaRow(row({ strokeCap: 'ROUND' }, 70, 112, 280), FIGMA.roundCaps)
+ expectFigmaRow(row({ strokeCap: 'SQUARE' }, 70, 112, 280), FIGMA.squareCaps)
+ })
+
+ test('cap inside strokes at double width, which nearly closes the gaps at this weight', () => {
+ expectFigmaRow(row({ strokeCap: 'ROUND' }, 76, 112, 280, 'INSIDE'), FIGMA.insideRoundCaps)
+ })
+
+ test('join the dashes that turn a sharp corner with the stroke’s join', () => {
+ expectFigmaRow(row({ strokeJoin: 'ROUND' }, 67, 110, 160), FIGMA.roundJoin)
+ expectFigmaRow(row({ strokeJoin: 'BEVEL' }, 67, 110, 160), FIGMA.bevelJoin)
+ })
+
+ test('dash each curve of a smoothed corner on its own', () => {
+ const smoothed = { width: 170, height: 100, cornerRadius: 25, cornerSmoothing: 0.6 }
+ expectFigmaRow(row(smoothed, 66, 112, 170), FIGMA.smoothed)
+ })
+
+ test('redraw the dashes when the layer is resized', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0].id
+ dashedRect({ strokeCap: 'ROUND' })(graph, page)
+ const [id] = expectDefined(graph.getNode(page)).childIds
+ const draw = (target: SkiaRenderer) =>
+ renderNodesToImage(ck, target, graph, page, [id], { scale: 1, format: 'PNG' })
+ draw(renderer)
+ graph.updateNode(id, { width: 170 })
+ const fresh = new SkiaRenderer(ck, expectDefined(ck.MakeSurface(1, 1)))
+ try {
+ expect(draw(renderer)).toEqual(draw(fresh))
+ } finally {
+ fresh.destroy()
+ }
+ })
+})
diff --git a/tests/e2e/canvas/dash-ends-visual.spec.ts b/tests/e2e/canvas/dash-ends-visual.spec.ts
new file mode 100644
index 000000000..dc157ff85
--- /dev/null
+++ b/tests/e2e/canvas/dash-ends-visual.spec.ts
@@ -0,0 +1,16 @@
+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
+import { strokeSceneDriver } from '#tests/helpers/canvas/strokes/driver'
+
+const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
+const scene = strokeSceneDriver(() => editor.page, 'createDashEndsScene')
+
+// The caps, joins, and smoothed corners are checked against Figma in
+// packages/core/tests/canvas/strokes/outlined-dashes.test.ts.
+test('dash ends, joins, and smoothed corners', async () => {
+ await scene.show()
+ await editor.canvas.waitForRender()
+ editor.canvas.assertNoErrors()
+ expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('dash-ends.png', {
+ threshold: 0.05
+ })
+})
diff --git a/tests/e2e/canvas/dash-ends-visual.spec.ts-snapshots/dash-ends-openpencil-darwin.png b/tests/e2e/canvas/dash-ends-visual.spec.ts-snapshots/dash-ends-openpencil-darwin.png
new file mode 100644
index 000000000..51a3ee4e0
Binary files /dev/null and b/tests/e2e/canvas/dash-ends-visual.spec.ts-snapshots/dash-ends-openpencil-darwin.png differ
diff --git a/tests/e2e/canvas/dashed-containers-visual.spec.ts b/tests/e2e/canvas/dashed-containers-visual.spec.ts
index 237af2a2c..50885b130 100644
--- a/tests/e2e/canvas/dashed-containers-visual.spec.ts
+++ b/tests/e2e/canvas/dashed-containers-visual.spec.ts
@@ -1,8 +1,8 @@
import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
-import { dashedContainersDriver } from '#tests/helpers/canvas/strokes/driver'
+import { strokeSceneDriver } from '#tests/helpers/canvas/strokes/driver'
const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
-const scene = dashedContainersDriver(() => editor.page)
+const scene = strokeSceneDriver(() => editor.page, 'createDashedContainersScene')
// The dash layout is checked against Figma in packages/core/tests/canvas/strokes/sides.test.ts.
test('sections and component sets dash their strokes as rectangles do', async () => {
diff --git a/tests/e2e/canvas/side-strokes-visual.spec.ts b/tests/e2e/canvas/side-strokes-visual.spec.ts
index 1b3379f91..ca8c10e03 100644
--- a/tests/e2e/canvas/side-strokes-visual.spec.ts
+++ b/tests/e2e/canvas/side-strokes-visual.spec.ts
@@ -1,8 +1,8 @@
import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
-import { sideStrokesDriver } from '#tests/helpers/canvas/strokes/driver'
+import { strokeSceneDriver } from '#tests/helpers/canvas/strokes/driver'
const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
-const scene = sideStrokesDriver(() => editor.page)
+const scene = strokeSceneDriver(() => editor.page, 'createSideStrokesScene')
// The ring, corner radii, and dashes are checked against Figma in packages/core/tests/canvas/strokes/sides.test.ts.
test('strokes with sides of their own weights', async () => {
diff --git a/tests/helpers/canvas/strokes/driver.ts b/tests/helpers/canvas/strokes/driver.ts
index dd30c8e19..d7ff61adc 100644
--- a/tests/helpers/canvas/strokes/driver.ts
+++ b/tests/helpers/canvas/strokes/driver.ts
@@ -2,36 +2,28 @@ import type { Page } from '@playwright/test'
import type * as StrokeScenes from '#tests/helpers/canvas/strokes/scene'
-/** Builds the side strokes scene on the open page and leaves it unselected for a snapshot. */
-export function sideStrokesDriver(page: () => Page) {
- return {
- show() {
- return page().evaluate(async () => {
- const store = window.openPencil?.getStore?.()
- if (!store) throw new Error('OpenPencil store not initialized')
- const sceneURL = '/tests/helpers/canvas/strokes/scene.ts'
- const scenes: typeof StrokeScenes = await import(sceneURL)
- scenes.createSideStrokesScene(store.graph, store.state.currentPageId)
- store.clearSelection()
- store.requestRender()
- })
- }
- }
-}
+/** A scene builder from `scene.ts`, by name. */
+export type StrokeScene = {
+ [Name in keyof typeof StrokeScenes]: (typeof StrokeScenes)[Name] extends (
+ ...args: never[]
+ ) => void
+ ? Name
+ : never
+}[keyof typeof StrokeScenes]
-/** Builds the dashed containers scene on the open page and leaves it unselected for a snapshot. */
-export function dashedContainersDriver(page: () => Page) {
+/** Builds the stroke scene `name` on the open page and leaves it unselected for a snapshot. */
+export function strokeSceneDriver(page: () => Page, name: StrokeScene) {
return {
show() {
- return page().evaluate(async () => {
+ return page().evaluate(async (sceneName) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const sceneURL = '/tests/helpers/canvas/strokes/scene.ts'
const scenes: typeof StrokeScenes = await import(sceneURL)
- scenes.createDashedContainersScene(store.graph, store.state.currentPageId)
+ scenes[sceneName](store.graph, store.state.currentPageId)
store.clearSelection()
store.requestRender()
- })
+ }, name)
}
}
}
diff --git a/tests/helpers/canvas/strokes/scene.ts b/tests/helpers/canvas/strokes/scene.ts
index a1d730adf..52e6ea7f6 100644
--- a/tests/helpers/canvas/strokes/scene.ts
+++ b/tests/helpers/canvas/strokes/scene.ts
@@ -1,4 +1,4 @@
-import type { SceneGraph } from '@open-pencil/scene-graph'
+import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
const ALIGNS = ['INSIDE', 'CENTER', 'OUTSIDE'] as const
const CORNERS = [
@@ -78,3 +78,45 @@ export function createDashedContainersScene(graph: SceneGraph, pageId: string):
}
}
}
+
+/**
+ * Dashed rectangles with round and square dash ends, inside and outside round ends, round and
+ * bevel joins, and smoothed corners with and without round ends.
+ */
+export function createDashEndsScene(graph: SceneGraph, pageId: string): void {
+ const cases: Array<[string, Partial<SceneNode>]> = [
+ ['round ends', { strokeCap: 'ROUND' }],
+ ['square ends', { strokeCap: 'SQUARE' }],
+ ['inside round ends', { strokeCap: 'ROUND', strokeAlign: 'INSIDE' }],
+ ['outside round ends', { strokeCap: 'ROUND', strokeAlign: 'OUTSIDE' }],
+ ['round join', { strokeJoin: 'ROUND' }],
+ ['bevel join', { strokeJoin: 'BEVEL' }],
+ ['smoothed', { cornerRadius: 25, cornerSmoothing: 0.6 }],
+ ['smoothed round ends', { cornerRadius: 25, cornerSmoothing: 0.6, strokeCap: 'ROUND' }]
+ ]
+ for (const [index, [name, props]] of cases.entries()) {
+ const align = props.strokeAlign ?? 'CENTER'
+ graph.createNode('RECTANGLE', pageId, {
+ name,
+ x: 60 + (index % 4) * 200,
+ y: 80 + Math.floor(index / 4) * 260,
+ width: 150,
+ height: 150,
+ fills: [],
+ strokes: [
+ {
+ type: 'SOLID',
+ color: { r: 0.6, g: 0.2, b: 0.9, a: 1 },
+ opacity: 1,
+ visible: true,
+ weight: 8,
+ align,
+ dashPattern: [20, 10]
+ }
+ ],
+ strokeWeight: 8,
+ ...props,
+ strokeAlign: align
+ })
+ }
+}