Skip to content

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

fix(fig): fill thumbnails with the drawn page's background (#1129)

rcoenena74ffe488169
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcabadf1b..84666528a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@
### Fixed
+- The thumbnail in a `.fig` file shows its page's own background between layers. Before, it took the colour of the page open in the editor, and files written from the CLI always showed light grey.
- People in a shared room see the overrides of instances in the shared document: text, slot content, and every other overridden field arrived empty, and editing such an instance could throw. Rooms opened by earlier builds do not meet rooms opened by this one.
- Showing or hiding a layer of an instance that a boolean component property drives sets the property, as in Figma, so every layer it drives follows and Figma shows the instance as OpenPencil does. Before, only that layer changed, which Figma ignored.
- Figma shows shader fills and images from older documents in `.fig` files OpenPencil writes: every image is stored under the SHA-1 of its bytes, the only name Figma reads, and a shader keeps its preset through `.fig` files and the Figma clipboard.
diff --git a/packages/core/src/io/formats/raster/render.ts b/packages/core/src/io/formats/raster/render.ts
index dae220d26..98a069461 100644
--- a/packages/core/src/io/formats/raster/render.ts
+++ b/packages/core/src/io/formats/raster/render.ts
@@ -10,6 +10,7 @@ import type { RenderColorSpace } from '@open-pencil/scene-graph/color'
import { computeDescendantVisualBounds, type VisualBounds } from '@open-pencil/scene-graph/geometry'
import type { SkiaRenderer } from '#core/canvas'
+import { getPageColor } from '#core/figma-api/page-backgrounds'
import { extractExportGraph, findPageId } from '#core/io/subgraph'
export type RasterExportFormat = 'PNG' | 'JPG' | 'WEBP'
@@ -344,9 +345,11 @@ export function renderThumbnail(
const scale = Math.min(width / contentW, height / contentH)
const pixelW = Math.max(1, Math.round(contentW * scale))
const pixelH = Math.max(1, Math.round(contentH * scale))
+ // The page's own background, which the editor's page colour matches only while it is open.
+ const background = getPageColor(page)
return renderToSurface(ck, renderer, graph, pageId, pixelW, pixelH, 'PNG', 100, (canvas) => {
- canvas.clear(ck.Color4f(renderer.pageColor.r, renderer.pageColor.g, renderer.pageColor.b, 1))
+ canvas.clear(ck.Color4f(background.r, background.g, background.b, 1))
canvas.translate(-bounds.minX * scale, -bounds.minY * scale)
canvas.scale(scale, scale)
})
diff --git a/packages/core/tests/io/formats/raster/thumbnail.test.ts b/packages/core/tests/io/formats/raster/thumbnail.test.ts
new file mode 100644
index 000000000..bf481695c
--- /dev/null
+++ b/packages/core/tests/io/formats/raster/thumbnail.test.ts
@@ -0,0 +1,31 @@
+import { expect, setDefaultTimeout, test } from 'bun:test'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+
+import { FigmaAPI } from '@open-pencil/core/figma-api'
+import {
+ createCanvasKitRasterCodec,
+ headlessRenderThumbnail,
+ initCanvasKit
+} from '@open-pencil/core/io/formats/raster'
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+setDefaultTimeout(60_000)
+
+test('a thumbnail shows the page background between its layers', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ for (const x of [0, 90]) {
+ graph.createNode('RECTANGLE', page.id, { x, y: 0, width: 10, height: 100 })
+ }
+ // Figma's dark-theme page background, #1E1E1E, which no renderer is set to.
+ new FigmaAPI(graph).currentPage.backgrounds = [
+ { type: 'SOLID', color: { r: 30 / 255, g: 30 / 255, b: 30 / 255 }, opacity: 1, visible: true }
+ ]
+
+ const png = expectDefined(await headlessRenderThumbnail(graph, page.id, 100, 100), 'thumbnail')
+ const image = expectDefined(createCanvasKitRasterCodec(await initCanvasKit()).decode(png))
+ const gap = (50 * image.width + 50) * 4
+
+ expect([...image.data.subarray(gap, gap + 4)]).toEqual([30, 30, 30, 255])
+})