Skip to content

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

fix: draw blurs with Figma's falloff and load SVG text fonts in import_svg (#1033)

Danila Poyarkov44625e02a468
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 13c646164..03a301c1b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -149,6 +149,8 @@
### Fixed
+- Draw layer blur, drop and inner shadows, and background blur with Figma's falloff. They spread about 15% too far, with a long faint edge Figma does not draw; SVG export keeps Figma's own `stdDeviation` of half the radius.
+- Load the fonts of SVG text before placing it from `import_svg`, so centred and right-aligned text sits where Figma puts it; `figma.loadFontAsync` in scripts now loads the font instead of doing nothing.
- Gradient stops in the fill picker show their full position and opacity, such as 100%, instead of cutting them off; each stop's colour, hex, and opacity share one field as in the properties panel.
- Keep design variable bindings and honor `replace_id` and `insert_index` when `render` runs through the stdio MCP server (`openpencil-mcp`) against the desktop or web app, as it already did through the HTTP endpoint (#830).
- Draw layers masked by a group, such as Figma's clip path groups, instead of hiding them; a group used as a mask masks with what its layers draw.
diff --git a/packages/core/src/canvas/effects.ts b/packages/core/src/canvas/effects.ts
index 05281dc34..26428f0bb 100644
--- a/packages/core/src/canvas/effects.ts
+++ b/packages/core/src/canvas/effects.ts
@@ -4,6 +4,15 @@ import type { SceneNode } from '@open-pencil/scene-graph'
import type { SkiaRenderer } from './renderer'
+/**
+ * The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,
+ * layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's
+ * SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).
+ */
+export function blurSigma(radius: number): number {
+ return radius * 0.43
+}
+
export function getCachedDropShadow(
r: SkiaRenderer,
dx: number,
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index 13bd75681..5bf53d5b8 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -19,6 +19,7 @@ import { vectorNetworkToCenterlinePath } from '#core/vector'
import { figmaBlendModeToSkia, needsIsolatedBlendLayer } from './blend'
import { renderBooleanOperation } from './boolean'
+import { blurSigma } from './effects'
import { drawVectorMultiStyleFills, paintFills } from './fills'
import { drawLayoutGrids } from './layout-grids'
import { renderMaskedChildIds } from './masks'
@@ -154,7 +155,7 @@ function renderMaskNodeContent(
const padding = (blur?.radius ?? 0) * 2
r.opacityPaint.setAlphaf(node.opacity)
r.opacityPaint.setBlendMode(r.ck.BlendMode.SrcOver)
- r.opacityPaint.setImageFilter(blur ? r.getCachedBlur(blur.radius / 2) : null)
+ r.opacityPaint.setImageFilter(blur ? r.getCachedBlur(blurSigma(blur.radius)) : null)
canvas.saveLayer(
r.opacityPaint,
r.ck.LTRBRect(-padding, -padding, node.width + padding, node.height + padding)
@@ -376,7 +377,7 @@ export function renderNode(
r.effectLayerPaint.setColorFilter(null)
r.effectLayerPaint.setBlendMode(r.ck.BlendMode.SrcOver)
- r.effectLayerPaint.setImageFilter(r.getCachedBlur(layerBlur.radius / 2))
+ r.effectLayerPaint.setImageFilter(r.getCachedBlur(blurSigma(layerBlur.radius)))
const blurPadding = layerBlur.radius * 2
canvas.saveLayer(
r.effectLayerPaint,
diff --git a/packages/core/src/canvas/shadows.ts b/packages/core/src/canvas/shadows.ts
index 4e4e3ec4b..d57e2e441 100644
--- a/packages/core/src/canvas/shadows.ts
+++ b/packages/core/src/canvas/shadows.ts
@@ -7,6 +7,7 @@ import type { Color, Vector } from '@open-pencil/scene-graph/primitives'
import { BLACK } from '#core/constants'
import { figmaBlendModeToSkia } from './blend'
+import { blurSigma } from './effects'
import type { SkiaRenderer } from './renderer'
import {
makeNodeShapePath,
@@ -245,7 +246,7 @@ function drawShapeDropShadow(
}
r.auxFill.setColor(r.color4f(effect.color.r, effect.color.g, effect.color.b, effect.color.a))
- r.auxFill.setMaskFilter(r.getCachedMaskBlur(effect.radius / 2))
+ r.auxFill.setMaskFilter(r.getCachedMaskBlur(blurSigma(effect.radius)))
r.auxFill.setImageFilter(null)
r.auxFill.setBlendMode(figmaBlendModeToSkia(r.ck, effect.blendMode))
canvas.save()
@@ -307,7 +308,7 @@ function renderDropShadow(
}
const shadowColor = r.ck.Color4f(effect.color.r, effect.color.g, effect.color.b, effect.color.a)
- const dropFilter = r.getCachedDropShadow(0, 0, effect.radius / 2, shadowColor)
+ const dropFilter = r.getCachedDropShadow(0, 0, blurSigma(effect.radius), shadowColor)
canvas.save()
let savedLayer = false
@@ -368,7 +369,7 @@ function drawTextInnerShadow(
r.effectLayerPaint.setBlendMode(ck.BlendMode.SrcOver)
r.effectLayerPaint.setColorFilter(null)
- r.effectLayerPaint.setImageFilter(r.getCachedDecalBlur(effect.radius / 2))
+ r.effectLayerPaint.setImageFilter(r.getCachedDecalBlur(blurSigma(effect.radius)))
canvas.saveLayer(r.effectLayerPaint, bounds)
restoreCount++
@@ -413,7 +414,7 @@ function drawShapeInnerShadow(
const sp = effect.spread
const shapeNode = shadowShapeChild ?? node
r.auxFill.setColor(r.ck.Color4f(effect.color.r, effect.color.g, effect.color.b, effect.color.a))
- r.auxFill.setImageFilter(r.getCachedDecalBlur(effect.radius / 2))
+ r.auxFill.setImageFilter(r.getCachedDecalBlur(blurSigma(effect.radius)))
r.auxFill.setBlendMode(figmaBlendModeToSkia(r.ck, effect.blendMode))
const shapeRect = shadowShapeChild ? r.ck.LTRBRect(0, 0, shapeNode.width, shapeNode.height) : rect
@@ -539,7 +540,7 @@ export function renderEffects(
}
if (pass === 'behind' && effect.type === 'BACKGROUND_BLUR') {
- r.applyClippedBlur(canvas, node, rect, hasRadius, effect.radius / 2)
+ r.applyClippedBlur(canvas, node, rect, hasRadius, blurSigma(effect.radius))
}
if (pass === 'front' && effect.type === 'INNER_SHADOW') {
diff --git a/packages/core/src/figma-api/index.ts b/packages/core/src/figma-api/index.ts
index c8e43b998..62fdaa740 100644
--- a/packages/core/src/figma-api/index.ts
+++ b/packages/core/src/figma-api/index.ts
@@ -30,6 +30,7 @@ import { createSVGNodes } from '#core/io/formats/svg'
import { textAutoResizeChanges } from '#core/layout/text-auto-resize'
import { reconcileVariableLayouts } from '#core/layout/variables'
import { documentFontStatus, type DocumentFontStatus } from '#core/text/font/status'
+import { fontManager } from '#core/text/fonts'
import { combineComponentsAsVariants, componentFromNode, exposeInstanceSwap } from './components'
import type {
@@ -547,8 +548,12 @@ export class FigmaAPI implements NodeProxyHost {
// --- Stubs ---
- async loadFontAsync(_fontName: FigmaFontName): Promise<void> {
- // No-op: we don't gate text editing on font loading
+ /**
+ * Loads the font so text measures and draws with it from then on. Hosts with their own font
+ * sources replace this; a font that cannot load is skipped, where Figma would reject.
+ */
+ async loadFontAsync(fontName: FigmaFontName): Promise<void> {
+ await fontManager.loadFont(fontName.family, fontName.style).catch(() => null)
}
async listAvailableFontsAsync(): Promise<FigmaFont[]> {
diff --git a/packages/core/src/tools/create/svg.ts b/packages/core/src/tools/create/svg.ts
index e45c94f72..59f983212 100644
--- a/packages/core/src/tools/create/svg.ts
+++ b/packages/core/src/tools/create/svg.ts
@@ -1,6 +1,6 @@
import * as v from 'valibot'
-import { createSVGNodes } from '#core/io/formats/svg'
+import { createSVGNodesFromImport, prepareSVGImport, svgImportFonts } from '#core/io/formats/svg'
import { toolNumber } from '#core/tools/input'
import { defineTool } from '#core/tools/schema'
@@ -36,12 +36,20 @@ export const importSVG = defineTool({
execute: async (figma, args) => {
if (!args.svg || typeof args.svg !== 'string') return { error: 'svg parameter is required' }
- const frame = createSVGNodes(figma.graph, args.parent_id ?? figma.currentPage.id, args.svg, {
- name: args.name,
- defaultColor: args.color,
- x: args.x,
- y: args.y
- })
+ const data = prepareSVGImport(args.svg, { defaultColor: args.color })
+ // Text is measured and placed as it is created, so its fonts load first.
+ await Promise.all(
+ (data ? svgImportFonts(data) : []).map(({ family, style }) =>
+ figma.loadFontAsync({ family, style })
+ )
+ )
+ const frame =
+ data &&
+ createSVGNodesFromImport(figma.graph, args.parent_id ?? figma.currentPage.id, data, {
+ name: args.name,
+ x: args.x,
+ y: args.y
+ })
if (!frame) return { error: 'No supported SVG elements found in the markup' }
return { id: frame.id, name: frame.name, type: frame.type }
}
diff --git a/packages/core/tests/canvas/blur.test.ts b/packages/core/tests/canvas/blur.test.ts
new file mode 100644
index 000000000..c90f6c290
--- /dev/null
+++ b/packages/core/tests/canvas/blur.test.ts
@@ -0,0 +1,175 @@
+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 Effect, type SceneNode } from '@open-pencil/scene-graph'
+
+import { expectDefined } from '../helpers/assert'
+
+const ck = await initCanvasKit()
+const renderer = new SkiaRenderer(ck, expectDefined(ck.MakeSurface(1, 1)))
+afterAll(() => renderer.destroy())
+
+/**
+ * A row of pixels across the left edge of a 200px square at x = 100, from x = 60 to 140, in a
+ * 400px frame, read from Figma 126 exports: alpha for layer blur and drop shadow, red for inner
+ * shadow on white and for background blur over a black-to-white edge.
+ */
+const FIGMA: Record<string, number[]> = {
+ 'LAYER_BLUR 4': [
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 3, 19, 51, 99, 157, 204, 237, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255
+ ],
+ 'LAYER_BLUR 16': [
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 4, 6, 10, 15, 21,
+ 28, 36, 46, 55, 66, 77, 91, 105, 120, 135, 150, 164, 177, 188, 200, 210, 219, 227, 234, 240,
+ 245, 249, 252, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
+ ],
+ 'DROP_SHADOW 16': [
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 4, 6, 10, 15, 21,
+ 28, 36, 46, 55, 66, 77, 91, 105, 120, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
+ ],
+ 'INNER_SHADOW 16': [
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 135, 151, 164, 178, 190, 201, 211, 220, 227, 235, 239, 245, 249, 252,
+ 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255
+ ],
+ 'BACKGROUND_BLUR 16': [
+ 3, 4, 4, 4, 3, 4, 3, 4, 4, 3, 3, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 4, 7, 9, 12, 18, 23,
+ 31, 39, 48, 58, 69, 80, 94, 107, 122, 137, 152, 166, 179, 190, 202, 210, 219, 227, 234, 241,
+ 245, 249, 252, 254, 254, 255, 255, 254, 255, 255, 255, 254, 255, 254, 254, 254, 255, 255, 255,
+ 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 255
+ ]
+}
+
+const solid = (r: number, g: number, b: number, opacity = 1) => ({
+ type: 'SOLID' as const,
+ color: { r, g, b, a: 1 },
+ opacity,
+ visible: true
+})
+
+function effect(type: Effect['type'], radius: number): Effect {
+ return {
+ type,
+ radius,
+ spread: 0,
+ offset: { x: 0, y: 0 },
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ visible: true,
+ blendMode: 'NORMAL',
+ showShadowBehindNode: false
+ }
+}
+
+function edgeRow(build: (graph: SceneGraph, frameId: string) => void, channel: 0 | 3): number[] {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0].id
+ const frame = graph.createNode('FRAME', page, {
+ width: 400,
+ height: 400,
+ fills: [],
+ clipsContent: false
+ })
+ build(graph, frame.id)
+ const png = expectDefined(
+ renderNodesToImage(ck, renderer, graph, page, [frame.id], {
+ scale: 1,
+ format: 'PNG',
+ trimTransparent: false
+ })
+ )
+ const image = expectDefined(ck.MakeImageFromEncoded(png))
+ try {
+ const offset = (image.width() - 400) / 2
+ const pixels = expectDefined(
+ image.readPixels(0, 0, {
+ width: image.width(),
+ height: image.height(),
+ alphaType: ck.AlphaType.Unpremul,
+ colorType: ck.ColorType.RGBA_8888,
+ colorSpace: ck.ColorSpace.SRGB
+ })
+ )
+ const row: number[] = []
+ for (let x = 60; x <= 140; x++) {
+ row.push(pixels[((200 + offset) * image.width() + x + offset) * 4 + channel])
+ }
+ return row
+ } finally {
+ image.delete()
+ }
+}
+
+function square(graph: SceneGraph, frameId: string, props: Partial<SceneNode>) {
+ graph.createNode('RECTANGLE', frameId, { x: 100, y: 100, width: 200, height: 200, ...props })
+}
+
+function expectFigmaEdge(row: number[], figma: number[], from = 60) {
+ const differences = row.flatMap((value, index) =>
+ 60 + index < from ? [] : [Math.abs(value - figma[index])]
+ )
+ const rms = Math.sqrt(differences.reduce((sum, d) => sum + d * d, 0) / differences.length)
+ expect(Math.max(...differences)).toBeLessThanOrEqual(6)
+ expect(rms).toBeLessThanOrEqual(2)
+}
+
+describe('blur effects fall off as in Figma', () => {
+ for (const radius of [4, 16]) {
+ test(`layer blur, radius ${radius}`, () => {
+ const row = edgeRow(
+ (graph, frameId) =>
+ square(graph, frameId, {
+ fills: [solid(1, 0, 0)],
+ effects: [effect('LAYER_BLUR', radius)]
+ }),
+ 3
+ )
+ expectFigmaEdge(row, FIGMA[`LAYER_BLUR ${radius}`])
+ })
+ }
+
+ test('drop shadow', () => {
+ const row = edgeRow(
+ (graph, frameId) =>
+ square(graph, frameId, { fills: [solid(1, 1, 1)], effects: [effect('DROP_SHADOW', 16)] }),
+ 3
+ )
+ expectFigmaEdge(row, FIGMA['DROP_SHADOW 16'])
+ })
+
+ test('inner shadow', () => {
+ const row = edgeRow(
+ (graph, frameId) =>
+ square(graph, frameId, { fills: [solid(1, 1, 1)], effects: [effect('INNER_SHADOW', 16)] }),
+ 0
+ )
+ // Outside the square nothing is drawn, so the comparison starts at its edge.
+ expectFigmaEdge(row, FIGMA['INNER_SHADOW 16'], 100)
+ })
+
+ test('background blur', () => {
+ const row = edgeRow((graph, frameId) => {
+ graph.createNode('RECTANGLE', frameId, { width: 100, height: 400, fills: [solid(0, 0, 0)] })
+ graph.createNode('RECTANGLE', frameId, {
+ x: 100,
+ width: 300,
+ height: 400,
+ fills: [solid(1, 1, 1)]
+ })
+ graph.createNode('RECTANGLE', frameId, {
+ width: 400,
+ height: 400,
+ fills: [solid(1, 1, 1, 0.01)],
+ effects: [effect('BACKGROUND_BLUR', 16)]
+ })
+ }, 0)
+ expectFigmaEdge(row, FIGMA['BACKGROUND_BLUR 16'])
+ })
+})
diff --git a/packages/core/tests/figma-api/load-font.test.ts b/packages/core/tests/figma-api/load-font.test.ts
new file mode 100644
index 000000000..97fb366c5
--- /dev/null
+++ b/packages/core/tests/figma-api/load-font.test.ts
@@ -0,0 +1,25 @@
+import { expect, spyOn, test } from 'bun:test'
+
+import { FigmaAPI } from '@open-pencil/core/figma-api'
+import { fontManager } from '@open-pencil/core/text'
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+test('figma.loadFontAsync loads the font, and goes on when it cannot', async () => {
+ const load = spyOn(fontManager, 'loadFont').mockImplementation(async (family) => {
+ if (family === 'Missing') throw new Error('not found')
+ return null
+ })
+ try {
+ const figma = new FigmaAPI(new SceneGraph())
+
+ await figma.loadFontAsync({ family: 'Inter', style: 'Bold' })
+ await figma.loadFontAsync({ family: 'Missing', style: 'Regular' })
+
+ expect(load.mock.calls.map(([family, style]) => [family, style])).toEqual([
+ ['Inter', 'Bold'],
+ ['Missing', 'Regular']
+ ])
+ } finally {
+ load.mockRestore()
+ }
+})
diff --git a/packages/core/tests/tools/create/svg.test.ts b/packages/core/tests/tools/create/svg.test.ts
new file mode 100644
index 000000000..629dc2027
--- /dev/null
+++ b/packages/core/tests/tools/create/svg.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, test } from 'bun:test'
+
+import { FigmaAPI } from '@open-pencil/core/figma-api'
+import { importSVG } from '@open-pencil/core/tools'
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+describe('import_svg', () => {
+ test('loads the fonts its text uses before creating the text', async () => {
+ const graph = new SceneGraph()
+ const figma = new FigmaAPI(graph)
+ const loaded: Array<{ family: string; style: string; textsSoFar: number }> = []
+ figma.loadFontAsync = async (font) => {
+ const textsSoFar = [...graph.getAllNodes()].filter((node) => node.type === 'TEXT').length
+ loaded.push({ ...font, textsSoFar })
+ }
+
+ await importSVG.execute(figma, {
+ svg: '<svg viewBox="0 0 200 60"><text x="100" y="30" font-size="20" font-weight="700" text-anchor="middle">Bold <tspan font-style="italic">tilt</tspan></text></svg>'
+ })
+
+ expect(loaded).toEqual([
+ { family: 'Inter', style: 'Bold', textsSoFar: 0 },
+ { family: 'Inter', style: 'Bold Italic', textsSoFar: 0 }
+ ])
+ })
+})
diff --git a/src/app/automation/bridge/figma-factory.ts b/src/app/automation/bridge/figma-factory.ts
index 4f817b6aa..bc6219496 100644
--- a/src/app/automation/bridge/figma-factory.ts
+++ b/src/app/automation/bridge/figma-factory.ts
@@ -2,7 +2,7 @@ import { FigmaAPI } from '@open-pencil/core/figma-api'
import { createCanvasKitRasterCodec } from '@open-pencil/core/io/formats/raster'
import type { EditorStore } from '@/app/editor/active-store'
-import { listFamilies, listFonts } from '@/app/editor/fonts'
+import { listFamilies, listFonts, loadFont } from '@/app/editor/fonts'
export function makeFigmaFromStore(
store: EditorStore,
@@ -30,6 +30,9 @@ export function makeFigmaFromStore(
api.exportImage = (nodeIds, opts) =>
store.renderExportImage(nodeIds, opts.scale ?? 1, opts.format ?? 'PNG', opts.pageId ?? pageId)
if (store.renderer) api.rasterCodec = createCanvasKitRasterCodec(store.renderer.ck)
+ api.loadFontAsync = async ({ family, style }) => {
+ await loadFont(family, style).catch(() => null)
+ }
api.listAvailableFontsAsync = async () => {
const [systemFonts, familyOptions] = await Promise.all([listFonts(), listFamilies()])
const fonts = systemFonts.flatMap(({ family, styles }) =>
diff --git a/tests/e2e/canvas/blur-effects-visual.spec.ts b/tests/e2e/canvas/blur-effects-visual.spec.ts
new file mode 100644
index 000000000..cdd2f95ad
--- /dev/null
+++ b/tests/e2e/canvas/blur-effects-visual.spec.ts
@@ -0,0 +1,13 @@
+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
+import { blurEffectsDriver } from '#tests/helpers/canvas/effects/driver'
+
+const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
+const scene = blurEffectsDriver(() => editor.page)
+
+// The falloff itself is checked against Figma's pixels in packages/core/tests/canvas/blur.test.ts.
+test('blur effects draw with Figma’s falloff', async () => {
+ await scene.show()
+ await editor.canvas.waitForRender()
+ editor.canvas.assertNoErrors()
+ expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('blur-effects.png')
+})
diff --git a/tests/e2e/canvas/blur-effects-visual.spec.ts-snapshots/blur-effects-openpencil-darwin.png b/tests/e2e/canvas/blur-effects-visual.spec.ts-snapshots/blur-effects-openpencil-darwin.png
new file mode 100644
index 000000000..4a89ee5cf
Binary files /dev/null and b/tests/e2e/canvas/blur-effects-visual.spec.ts-snapshots/blur-effects-openpencil-darwin.png differ
diff --git a/tests/engine/render/canvas/effects/edge-cases.test.ts b/tests/engine/render/canvas/effects/edge-cases.test.ts
index 3397428b0..b4d359cac 100644
--- a/tests/engine/render/canvas/effects/edge-cases.test.ts
+++ b/tests/engine/render/canvas/effects/edge-cases.test.ts
@@ -286,7 +286,7 @@ describe('Edge cases and bug fixes', () => {
expect(translateCalls[0]).toEqual([15, 25]) // offset + child position
// The filter should NOT have the offset (neutralized to 0,0)
- expect(r.getCachedDropShadow).toHaveBeenCalledWith(0, 0, 5, expect.anything())
+ expect(r.getCachedDropShadow).toHaveBeenCalledWith(0, 0, expect.closeTo(4.3), expect.anything())
})
test('INNER_SHADOW with large offset does not vanish (bounding box union)', () => {
diff --git a/tests/engine/render/canvas/effects/types.test.ts b/tests/engine/render/canvas/effects/types.test.ts
index 7fae030fb..b687121c7 100644
--- a/tests/engine/render/canvas/effects/types.test.ts
+++ b/tests/engine/render/canvas/effects/types.test.ts
@@ -318,7 +318,8 @@ describe('Renderer handles all effect types (Behavioral)', () => {
getNode: mock(() => node as SceneNode)
}
renderNode(r, asCanvas(canvas), graph as SceneGraph, 'n1', {})
- expect(r.getCachedBlur).toHaveBeenCalledWith(5)
+ // Figma draws a blur radius as a Gaussian of 0.43 × radius.
+ expect(r.getCachedBlur).toHaveBeenCalledWith(expect.closeTo(4.3))
expect(canvas.saveLayer).toHaveBeenCalledWith(r.effectLayerPaint, expect.any(Float32Array))
expect(r.ck.LTRBRect).toHaveBeenCalledWith(-20, -20, 120, 120)
})
@@ -352,7 +353,7 @@ describe('Renderer handles all effect types (Behavioral)', () => {
getNode: mock(() => node as SceneNode)
}
renderNode(r, asCanvas(canvas), graph as SceneGraph, 'n1', {})
- expect(r.getCachedBlur).toHaveBeenCalledWith(10)
+ expect(r.getCachedBlur).toHaveBeenCalledWith(expect.closeTo(8.6))
expect(canvas.saveLayer).toHaveBeenCalledWith(r.effectLayerPaint, expect.any(Float32Array))
expect(r.ck.LTRBRect).toHaveBeenCalledWith(-40, -40, 140, 140)
})
diff --git a/tests/helpers/canvas/effects/driver.ts b/tests/helpers/canvas/effects/driver.ts
new file mode 100644
index 000000000..6dafd161c
--- /dev/null
+++ b/tests/helpers/canvas/effects/driver.ts
@@ -0,0 +1,20 @@
+import type { Page } from '@playwright/test'
+
+import type * as EffectScenes from '#tests/helpers/canvas/effects/scene'
+
+/** Builds the blur effects scene on the open page and leaves it unselected for a snapshot. */
+export function blurEffectsDriver(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/effects/scene.ts'
+ const scenes: typeof EffectScenes = await import(sceneURL)
+ scenes.createBlurEffectsScene(store.graph, store.state.currentPageId)
+ store.clearSelection()
+ store.requestRender()
+ })
+ }
+ }
+}
diff --git a/tests/helpers/canvas/effects/scene.ts b/tests/helpers/canvas/effects/scene.ts
new file mode 100644
index 000000000..e3916ae54
--- /dev/null
+++ b/tests/helpers/canvas/effects/scene.ts
@@ -0,0 +1,71 @@
+import type { Effect, SceneGraph } from '@open-pencil/scene-graph'
+
+const solid = (r: number, g: number, b: number, opacity = 1) => ({
+ type: 'SOLID' as const,
+ color: { r, g, b, a: 1 },
+ opacity,
+ visible: true
+})
+
+function effect(type: Effect['type'], radius: number, y = 0): Effect {
+ return {
+ type,
+ radius,
+ spread: 0,
+ offset: { x: 0, y },
+ color: { r: 0.06, g: 0.09, b: 0.16, a: 0.6 },
+ visible: true,
+ blendMode: 'NORMAL',
+ showShadowBehindNode: false
+ }
+}
+
+/** Layer blur, drop and inner shadow, and background blur, each at a 16px radius. */
+export function createBlurEffectsScene(graph: SceneGraph, pageId: string): void {
+ const card = { y: 100, width: 120, height: 120, cornerRadius: 20 }
+ graph.createNode('RECTANGLE', pageId, {
+ name: 'Layer blur',
+ x: 80,
+ ...card,
+ fills: [solid(0.31, 0.27, 0.9)],
+ effects: [effect('LAYER_BLUR', 16)]
+ })
+ graph.createNode('RECTANGLE', pageId, {
+ name: 'Drop shadow',
+ x: 260,
+ ...card,
+ fills: [solid(1, 1, 1)],
+ effects: [effect('DROP_SHADOW', 16, 8)]
+ })
+ graph.createNode('RECTANGLE', pageId, {
+ name: 'Inner shadow',
+ x: 440,
+ ...card,
+ fills: [solid(1, 1, 1)],
+ effects: [effect('INNER_SHADOW', 16, 8)]
+ })
+ for (const [index, color] of [
+ [0.96, 0.35, 0.35],
+ [0.08, 0.73, 0.73],
+ [0.23, 0.51, 0.96]
+ ].entries()) {
+ graph.createNode('RECTANGLE', pageId, {
+ name: `Stripe ${index + 1}`,
+ x: 620 + index * 40,
+ y: 100,
+ width: 40,
+ height: 120,
+ fills: [solid(color[0], color[1], color[2])]
+ })
+ }
+ graph.createNode('RECTANGLE', pageId, {
+ name: 'Background blur',
+ x: 640,
+ y: 120,
+ width: 80,
+ height: 80,
+ cornerRadius: 16,
+ fills: [solid(1, 1, 1, 0.2)],
+ effects: [effect('BACKGROUND_BLUR', 16)]
+ })
+}