Skip to content

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

fix(export): leave page neighbours out of a node export that blends (#1612)

Andrew Avseninb8fbfa6d72de
diff --git a/changes/1612/fixed.md b/changes/1612/fixed.md
new file mode 100644
index 000000000..067834988
--- /dev/null
+++ b/changes/1612/fixed.md
@@ -0,0 +1 @@
+- Exporting a layer that blends or blurs its background leaves out the layers around and above it, as in Figma.
diff --git a/packages/core/src/io/formats/raster/render.ts b/packages/core/src/io/formats/raster/render.ts
index 6be951a86..1559e7ea0 100644
--- a/packages/core/src/io/formats/raster/render.ts
+++ b/packages/core/src/io/formats/raster/render.ts
@@ -25,16 +25,6 @@ function ensureSinglePageSelection(graph: SceneGraph, pageId: string, nodeIds: s
return nodeIds.every((nodeId) => findPageId(graph, nodeId) === pageId)
}
-function nodeNeedsSceneBackdrop(graph: SceneGraph, nodeId: string): boolean {
- const node = graph.getNode(nodeId)
- if (!node) return false
- if (node.blendMode !== 'NORMAL' && node.blendMode !== 'PASS_THROUGH') return true
- if (node.effects.some((effect) => effect.visible && effect.type === 'BACKGROUND_BLUR')) {
- return true
- }
- return node.childIds.some((childId) => nodeNeedsSceneBackdrop(graph, childId))
-}
-
export function computeContentBounds(graph: SceneGraph, nodeIds: string[]) {
return computeDescendantVisualBounds(
nodeIds,
@@ -226,6 +216,24 @@ function renderToSurface(
}
}
+/**
+ * `nodeIds` in the order the source page paints them, bottom to top, whatever order they came in,
+ * without those a selected ancestor already paints.
+ */
+function inPaintOrder(source: SceneGraph, pageId: string, nodeIds: readonly string[]): string[] {
+ const selected = new Set(nodeIds)
+ const ordered: string[] = []
+ const visit = (id: string): void => {
+ if (selected.has(id)) {
+ ordered.push(id)
+ return
+ }
+ for (const childId of source.getNode(id)?.childIds ?? []) visit(childId)
+ }
+ visit(pageId)
+ return ordered
+}
+
export function prepareSelectionRenderGraph(
source: SceneGraph,
renderGraph: SceneGraph,
@@ -235,7 +243,9 @@ export function prepareSelectionRenderGraph(
const page = renderGraph.getNode(pageId)
if (!page) return
- page.childIds = nodeIds.filter((nodeId) => renderGraph.getNode(nodeId) !== undefined)
+ page.childIds = inPaintOrder(source, pageId, nodeIds).filter(
+ (nodeId) => renderGraph.getNode(nodeId) !== undefined
+ )
for (const nodeId of page.childIds) {
const node = renderGraph.getNode(nodeId)
const sourceNode = source.getNode(nodeId)
@@ -291,23 +301,17 @@ export function renderNodesToImage(
const pixelH = Math.ceil(contentH * options.scale)
if (pixelW <= 0 || pixelH <= 0) return null
+ // Figma exports a layer by itself, without the page behind it, even when it blends.
const extracted = extractExportGraph(graph, { scope: 'selection', nodeIds })
if (!extracted.pageId) return null
-
- const renderGraph = nodeIds.some((nodeId) => nodeNeedsSceneBackdrop(graph, nodeId))
- ? graph
- : extracted.graph
- const renderPageId = renderGraph === graph ? pageId : extracted.pageId
- if (renderGraph !== graph) {
- prepareSelectionRenderGraph(graph, renderGraph, renderPageId, nodeIds)
- }
+ prepareSelectionRenderGraph(graph, extracted.graph, extracted.pageId, nodeIds)
const quality = options.quality ?? (options.format === 'PNG' ? 100 : 90)
return renderToSurface(
ck,
renderer,
- renderGraph,
- renderPageId,
+ extracted.graph,
+ extracted.pageId,
pixelW,
pixelH,
options.format,
diff --git a/packages/core/tests/io/formats/raster/selection.test.ts b/packages/core/tests/io/formats/raster/selection.test.ts
new file mode 100644
index 000000000..281c7bb4b
--- /dev/null
+++ b/packages/core/tests/io/formats/raster/selection.test.ts
@@ -0,0 +1,210 @@
+import { expect, test } from 'bun:test'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+
+import { SkiaRenderer } from '@open-pencil/core/canvas'
+import { initCanvasKit, renderNodesToImage } from '@open-pencil/core/io/formats/raster'
+import { SceneGraph, type Effect } from '@open-pencil/scene-graph'
+
+const ck = await initCanvasKit()
+
+function solidFill(r: number, g: number, b: number) {
+ return [{ type: 'SOLID' as const, color: { r, g, b, a: 1 }, opacity: 1, visible: true }]
+}
+
+function exportedRgba(graph: SceneGraph, pageId: string, nodeIds: string | string[]) {
+ const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')
+ const renderer = new SkiaRenderer(ck, surface)
+ try {
+ const png = expectDefined(
+ renderNodesToImage(ck, renderer, graph, pageId, [nodeIds].flat(), {
+ scale: 1,
+ format: 'PNG'
+ }),
+ 'png'
+ )
+ const image = expectDefined(ck.MakeImageFromEncoded(png), 'image')
+ const width = image.width()
+ const pixels = expectDefined(
+ image.readPixels(0, 0, {
+ alphaType: ck.AlphaType.Unpremul,
+ colorType: ck.ColorType.RGBA_8888,
+ colorSpace: ck.ColorSpace.SRGB,
+ width,
+ height: image.height()
+ }),
+ 'pixels'
+ )
+ image.delete()
+ return (x: number, y: number) => {
+ const offset = (y * width + x) * 4
+ return Array.from(pixels.subarray(offset, offset + 4))
+ }
+ } finally {
+ renderer.destroy()
+ }
+}
+
+function frameOverNeighbour(
+ frameBlend: 'PASS_THROUGH' | 'MULTIPLY',
+ childBlend: 'NORMAL' | 'MULTIPLY',
+ frameEffects: Effect[] = []
+) {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ graph.createNode('RECTANGLE', page.id, {
+ x: 0,
+ y: 0,
+ width: 40,
+ height: 40,
+ fills: solidFill(0, 0, 1)
+ })
+ const frame = graph.createNode('FRAME', page.id, {
+ x: 20,
+ y: 20,
+ width: 40,
+ height: 40,
+ fills: [],
+ blendMode: frameBlend,
+ effects: frameEffects
+ })
+ graph.createNode('RECTANGLE', frame.id, {
+ x: 25,
+ y: 25,
+ width: 10,
+ height: 10,
+ fills: solidFill(1, 1, 1),
+ blendMode: childBlend
+ })
+ return { graph, pageId: page.id, frameId: frame.id }
+}
+
+// Figma exports a layer by itself, without the page around and behind it, even when it blends.
+test('selection export leaves out page neighbours when only a descendant blends', () => {
+ const { graph, pageId, frameId } = frameOverNeighbour('PASS_THROUGH', 'MULTIPLY')
+ const pixelAt = exportedRgba(graph, pageId, frameId)
+
+ expect(pixelAt(5, 5)[3]).toBe(0)
+ expect(pixelAt(30, 30)).toEqual([255, 255, 255, 255])
+})
+
+test('selection export leaves out page neighbours when the node itself blends', () => {
+ const { graph, pageId, frameId } = frameOverNeighbour('MULTIPLY', 'NORMAL')
+ const pixelAt = exportedRgba(graph, pageId, frameId)
+
+ expect(pixelAt(5, 5)[3]).toBe(0)
+ expect(pixelAt(30, 30)).toEqual([255, 255, 255, 255])
+})
+
+test('selection export leaves out page neighbours when the node blurs its background', () => {
+ const backgroundBlur: Effect = {
+ type: 'BACKGROUND_BLUR',
+ radius: 16,
+ spread: 0,
+ offset: { x: 0, y: 0 },
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ visible: true,
+ blendMode: 'NORMAL',
+ showShadowBehindNode: false
+ }
+ const { graph, pageId, frameId } = frameOverNeighbour('PASS_THROUGH', 'NORMAL', [backgroundBlur])
+ const pixelAt = exportedRgba(graph, pageId, frameId)
+
+ expect(pixelAt(5, 5)[3]).toBe(0)
+ expect(pixelAt(30, 30)).toEqual([255, 255, 255, 255])
+})
+
+/** A blue square under a white one that overlaps its right half, both on the page. */
+function overlappingPair(topBlend: 'NORMAL' | 'MULTIPLY', topColor = solidFill(1, 1, 1)) {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const bottom = graph.createNode('RECTANGLE', page.id, {
+ width: 40,
+ height: 40,
+ fills: solidFill(0, 0, 1)
+ })
+ const top = graph.createNode('RECTANGLE', page.id, {
+ x: 20,
+ width: 40,
+ height: 40,
+ fills: topColor,
+ blendMode: topBlend
+ })
+ return { graph, pageId: page.id, ids: [top.id, bottom.id] }
+}
+
+test('a multi-layer export stacks the layers as the page does, whatever order they are given in', () => {
+ const { graph, pageId, ids } = overlappingPair('NORMAL', solidFill(1, 0, 0))
+ expect(exportedRgba(graph, pageId, ids)(30, 20)).toEqual([255, 0, 0, 255])
+})
+
+test('a layer blends with the selected layers under it', () => {
+ const multiply = overlappingPair('MULTIPLY')
+ expect(exportedRgba(multiply.graph, multiply.pageId, multiply.ids)(30, 20)).toEqual([
+ 0, 0, 255, 255
+ ])
+ const normal = overlappingPair('NORMAL')
+ expect(exportedRgba(normal.graph, normal.pageId, normal.ids)(30, 20)).toEqual([
+ 255, 255, 255, 255
+ ])
+})
+
+test('a background blur blurs the selected layers behind it', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const left = graph.createNode('RECTANGLE', page.id, {
+ width: 20,
+ height: 40,
+ fills: solidFill(0, 0, 0)
+ })
+ const right = graph.createNode('RECTANGLE', page.id, {
+ x: 20,
+ width: 20,
+ height: 40,
+ fills: solidFill(1, 1, 1)
+ })
+ const glass = graph.createNode('FRAME', page.id, {
+ width: 40,
+ height: 40,
+ fills: [{ ...solidFill(1, 1, 1)[0], opacity: 0.01 }],
+ effects: [
+ {
+ type: 'BACKGROUND_BLUR',
+ radius: 16,
+ spread: 0,
+ offset: { x: 0, y: 0 },
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ visible: true,
+ blendMode: 'NORMAL',
+ showShadowBehindNode: false
+ }
+ ]
+ })
+ const [red] = exportedRgba(graph, page.id, [left.id, right.id, glass.id])(18, 20)
+ // Sharp, the black half reads 0 beside the edge; blurred, the white half bleeds into it.
+ expect(red).toBeGreaterThan(40)
+})
+
+test('selection export draws a selected layer once when its frame is selected too', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const frame = graph.createNode('FRAME', page.id, {
+ x: 20,
+ y: 20,
+ width: 40,
+ height: 40,
+ fills: []
+ })
+ const child = graph.createNode('RECTANGLE', frame.id, {
+ x: 0,
+ y: 0,
+ width: 20,
+ height: 20,
+ fills: solidFill(1, 1, 1),
+ opacity: 0.5
+ })
+ const pixelAt = exportedRgba(graph, page.id, [frame.id, child.id])
+
+ expect(pixelAt(10, 10)[3]).toBe(128)
+ expect(pixelAt(30, 30)[3]).toBe(0)
+})