Skip to content

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

feat(canvas): flatten container and stroke paths

Danila Poyarkovaf70b0eeafd2
diff --git a/packages/core/src/canvas/boolean.ts b/packages/core/src/canvas/boolean.ts
index 8077648d8..12a4619c5 100644
--- a/packages/core/src/canvas/boolean.ts
+++ b/packages/core/src/canvas/boolean.ts
@@ -49,6 +49,71 @@ function lineStrokePath(r: SkiaRenderer, node: SceneNode): Path | null {
return path.stroke({ width: stroke?.weight ?? 1 })
}
+function baseShapePath(r: SkiaRenderer, node: SceneNode): Path | null {
+ if (node.type === 'LINE') return lineStrokePath(r, node)
+ if (node.type === 'ELLIPSE' && node.arcData) return makeArcPath(r, node)
+
+ const rect = r.ck.LTRBRect(0, 0, node.width, node.height)
+ return r.makeNodeShapePath(node, rect, nodeHasRadius(node))
+}
+
+function nodeHasVisibleFill(node: SceneNode): boolean {
+ return node.fills.some((fill) => fill.visible)
+}
+
+function addVisibleStrokeOutlines(target: Path, source: Path, node: SceneNode): void {
+ for (const stroke of node.strokes) {
+ if (!stroke.visible || stroke.weight <= 0) continue
+ const outline = source.stroke({ width: stroke.weight })
+ if (!outline) continue
+ target.addPath(outline)
+ }
+}
+
+function canContainFlattenableChildren(node: SceneNode): boolean {
+ return (
+ node.type === 'GROUP' ||
+ node.type === 'FRAME' ||
+ node.type === 'COMPONENT' ||
+ node.type === 'INSTANCE'
+ )
+}
+
+function containerSourcePath(r: SkiaRenderer, node: SceneNode, graph: SceneGraph): Path | null {
+ const path = new r.ck.Path()
+ let hasPath = false
+
+ if (nodeHasVisibleFill(node) || node.strokes.some((stroke) => stroke.visible)) {
+ const ownPath = baseShapePath(r, node)
+ if (ownPath) {
+ if (nodeHasVisibleFill(node)) path.addPath(ownPath)
+ addVisibleStrokeOutlines(path, ownPath, node)
+ ownPath.delete()
+ hasPath = true
+ }
+ }
+
+ for (const childId of node.childIds) {
+ const child = graph.getNode(childId)
+ if (!child || !child.visible) continue
+ const childPath = makeBooleanSourcePath(r, child, graph)
+ if (!childPath) {
+ path.delete()
+ return null
+ }
+ childPath.transform(nodePathTransform(r, child))
+ path.addPath(childPath)
+ childPath.delete()
+ hasPath = true
+ }
+
+ if (!hasPath) {
+ path.delete()
+ return null
+ }
+ return path
+}
+
export function makeBooleanSourcePath(
r: SkiaRenderer,
node: SceneNode,
@@ -56,11 +121,13 @@ export function makeBooleanSourcePath(
): Path | null {
if (node.type === 'BOOLEAN_OPERATION') return makeBooleanOperationPath(r, node, graph)
if (!canMakeBooleanSourcePath(node)) return null
- if (node.type === 'LINE') return lineStrokePath(r, node)
- if (node.type === 'ELLIPSE' && node.arcData) return makeArcPath(r, node)
+ if (canContainFlattenableChildren(node)) return containerSourcePath(r, node, graph)
+ if (node.type === 'LINE') return baseShapePath(r, node)
- const rect = r.ck.LTRBRect(0, 0, node.width, node.height)
- return r.makeNodeShapePath(node, rect, nodeHasRadius(node))
+ const path = baseShapePath(r, node)
+ if (!path) return null
+ if (node.strokes.some((stroke) => stroke.visible)) addVisibleStrokeOutlines(path, path, node)
+ return path
}
function transformedShapePath(r: SkiaRenderer, child: SceneNode, graph: SceneGraph): Path | null {
diff --git a/tests/engine/editor/structure/flatten.test.ts b/tests/engine/editor/structure/flatten.test.ts
index bc7394633..5c713acbf 100644
--- a/tests/engine/editor/structure/flatten.test.ts
+++ b/tests/engine/editor/structure/flatten.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, test } from 'bun:test'
import { initCanvasKit } from '#cli/headless'
import { SkiaRenderer } from '#core/canvas'
+import { BLACK } from '#core/constants'
import { createEditor } from '#core/editor'
async function createEditorWithRenderer() {
@@ -52,6 +53,48 @@ describe('flattenSelected', () => {
surface.delete()
})
+ test('flattens visual descendants from groups', async () => {
+ const { editor, surface } = await createEditorWithRenderer()
+ const pageId = editor.state.currentPageId
+ const rect = editor.graph.createNode('RECTANGLE', pageId, { x: 10, y: 20, width: 50, height: 40 })
+ const ellipse = editor.graph.createNode('ELLIPSE', pageId, { x: 70, y: 20, width: 30, height: 30 })
+ editor.select([rect.id, ellipse.id])
+ editor.groupSelected()
+ const [groupId] = [...editor.state.selectedIds]
+
+ editor.flattenSelected()
+
+ const [vectorId] = [...editor.state.selectedIds]
+ const vector = editor.graph.getNode(vectorId)
+ expect(vector?.type).toBe('VECTOR')
+ expect(vector?.vectorNetwork?.vertices.length).toBeGreaterThan(0)
+ expect(editor.graph.getNode(groupId)).toBeUndefined()
+ surface.delete()
+ })
+
+ test('includes stroke-only rectangles as flattenable outlines', async () => {
+ const { editor, surface } = await createEditorWithRenderer()
+ const pageId = editor.state.currentPageId
+ const rect = editor.graph.createNode('RECTANGLE', pageId, {
+ x: 10,
+ y: 20,
+ width: 50,
+ height: 40,
+ fills: [],
+ strokes: [{ type: 'SOLID', color: BLACK, opacity: 1, visible: true, weight: 8, align: 'CENTER' }]
+ })
+
+ editor.select([rect.id])
+ editor.flattenSelected()
+
+ const [vectorId] = [...editor.state.selectedIds]
+ const vector = editor.graph.getNode(vectorId)
+ expect(vector?.type).toBe('VECTOR')
+ expect(vector?.width).toBeGreaterThan(50)
+ expect(vector?.height).toBeGreaterThan(40)
+ surface.delete()
+ })
+
test('undo and redo restore flattened children and vector', async () => {
const { editor, surface } = await createEditorWithRenderer()
const pageId = editor.state.currentPageId