Skip to content

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

fix(dom-css): blur and shadow HTML as the canvas draws them (#1747)

Danila Poyarkov7d359c4e9312
diff --git a/changes/1747/fixed.md b/changes/1747/fixed.md
new file mode 100644
index 000000000..3fa8962b6
--- /dev/null
+++ b/changes/1747/fixed.md
@@ -0,0 +1 @@
+- HTML export, Tailwind JSX, canvas islands, and `getCSSAsync()` blur layers and shadows as the canvas draws them, and text keeps its shadows; layer and background blurs were 2.3 times as strong, and text shadows were dropped. Imported CSS shadows draw as the browser draws them.
diff --git a/packages/core/src/canvas/effects.ts b/packages/core/src/canvas/effects.ts
index e1fa56af4..c424727b7 100644
--- a/packages/core/src/canvas/effects.ts
+++ b/packages/core/src/canvas/effects.ts
@@ -2,7 +2,6 @@ import type { Canvas, ImageFilter, MaskFilter, Path } from 'canvaskit-wasm'
import { readEffectiveFigmaRawField } from '@open-pencil/fig'
import type { SceneNode } from '@open-pencil/scene-graph'
-import { drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
import type { Color, Vector } from '@open-pencil/scene-graph/primitives'
import { ResourceCache } from '#core/cache/resource'
@@ -28,15 +27,6 @@ export function createMaskFilterCache(): MaskFilterCache {
return new ResourceCache({ maxEntries: MAX_CACHED_FILTERS, dispose: (filter) => filter.delete() })
}
-/**
- * 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 drawnBlurRadius(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 070a45156..7df353328 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -2,7 +2,7 @@ import type { Canvas } from 'canvaskit-wasm'
import { type SceneNode, type SceneGraph, type Fill } from '@open-pencil/scene-graph'
import { getNodeLocalMatrix } from '@open-pencil/scene-graph/coordinate'
-import { drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
+import { blurSigma, drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
import Matrix, { type Mat3 } from '@open-pencil/scene-graph/matrix'
import { isLayerContainer } from '@open-pencil/scene-graph/node-defaults'
import { textLinesOverflow } from '@open-pencil/scene-graph/text'
@@ -13,7 +13,6 @@ import { fontManager } from '#core/text/font/manager'
import { needsIsolatedBlendLayer, setPaintBlendMode } from './blend'
import { renderBooleanOperation } from './boolean'
-import { blurSigma } from './effects'
import { drawVectorMultiStyleFills, paintFills } from './fills'
import { drawLayoutGrids } from './layout-grids'
import { renderMaskedChildIds } from './masks'
diff --git a/packages/core/src/canvas/shadows.ts b/packages/core/src/canvas/shadows.ts
index d88b83909..eebccceea 100644
--- a/packages/core/src/canvas/shadows.ts
+++ b/packages/core/src/canvas/shadows.ts
@@ -1,7 +1,7 @@
import type { Canvas, ImageFilter } from 'canvaskit-wasm'
import type { BlendMode, Effect, SceneGraph, SceneNode } from '@open-pencil/scene-graph'
-import { drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
+import { blurSigma, drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
import Matrix from '@open-pencil/scene-graph/matrix'
import { isLayerContainer } from '@open-pencil/scene-graph/node-defaults'
import { regionFills } from '@open-pencil/scene-graph/vector-network'
@@ -10,7 +10,7 @@ import { nodeOrientationMatrix } from '#core/geometry'
import { needsIsolatedBlendLayer, setPaintBlendMode } from './blend'
import { makeBooleanOperationPath } from './boolean'
-import { blurSigma, renderNoiseEffects } from './effects'
+import { renderNoiseEffects } from './effects'
import type { SkiaRenderer } from './renderer'
import {
castsFromOwnShape,
diff --git a/packages/core/src/canvas/shadows/shape.ts b/packages/core/src/canvas/shadows/shape.ts
index fc536d1c7..df5cf3936 100644
--- a/packages/core/src/canvas/shadows/shape.ts
+++ b/packages/core/src/canvas/shadows/shape.ts
@@ -1,10 +1,9 @@
import type { Canvas, Path } from 'canvaskit-wasm'
import type { BlendMode, Effect, SceneNode, Stroke } from '@open-pencil/scene-graph'
-import { drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
+import { blurSigma, drawnBlurRadius } from '@open-pencil/scene-graph/geometry'
import { setPaintBlendMode } from '#core/canvas/blend'
-import { blurSigma } from '#core/canvas/effects'
import type { SkiaRenderer } from '#core/canvas/renderer'
import { drawOutline, makeNodeShapePath, nodeHasRadius, withNodeOutline } from '#core/canvas/shapes'
import { sideStrokeRRects, strokeAlignShare } from '#core/canvas/strokes/sides'
diff --git a/packages/dom-css/src/export/css.ts b/packages/dom-css/src/export/css.ts
index f9063f527..9bd229b3f 100644
--- a/packages/dom-css/src/export/css.ts
+++ b/packages/dom-css/src/export/css.ts
@@ -1,5 +1,6 @@
import type { Effect, Fill, Stroke, TextLength } from '@open-pencil/scene-graph'
import { colorToCSS, colorToHex } from '@open-pencil/scene-graph/color'
+import { blurSigma } from '@open-pencil/scene-graph/geometry'
import { gradientPoint } from '@open-pencil/scene-graph/gradient'
import type { Color } from '@open-pencil/scene-graph/primitives'
@@ -25,44 +26,102 @@ export type EffectFieldCSS = (field: string, literal: string) => string
const LITERAL: EffectFieldCSS = (_field, literal) => literal
-function shadowToCSS(effect: Effect, index: number, css: EffectFieldCSS): string {
+function px(value: number): string {
+ return `${Number(value.toFixed(3))}px`
+}
+
+/**
+ * An effect's blur as a CSS length of `sigmas` times the Gaussian the canvas blurs it by: a
+ * literal length, or the bound variable, whose value is a Figma radius, scaled in `calc()`.
+ */
+function blurLength(effect: Effect, index: number, css: EffectFieldCSS, sigmas: number): string {
+ const literal = `${effect.radius}px`
+ const bound = css(`effects/${index}/radius`, literal)
+ if (bound !== literal) return `calc(${bound} * ${Number((sigmas * blurSigma(1)).toFixed(6))})`
+ return px(sigmas * blurSigma(effect.radius))
+}
+
+/**
+ * A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
+ * shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
+ */
+function shadowToCSS(effect: Effect, index: number, css: EffectFieldCSS, spread = true): string {
const inset = effect.type === 'INNER_SHADOW' ? 'inset ' : ''
const length = (property: string, value: number) =>
css(`effects/${index}/${property}`, `${value}px`)
- return [
+ const parts = [
`${inset}${length('offsetX', effect.offset.x)}`,
length('offsetY', effect.offset.y),
- length('radius', effect.radius),
- length('spread', effect.spread),
- css(`effects/${index}/color`, cssColor(effect.color))
- ].join(' ')
+ blurLength(effect, index, css, 2)
+ ]
+ if (spread) parts.push(length('spread', effect.spread))
+ parts.push(css(`effects/${index}/color`, cssColor(effect.color)))
+ return parts.join(' ')
}
-/** The first effect as a drop shadow, as a text's `text-shadow` writes it. */
-export function dropShadowToCSS(effect: Effect | undefined, css = LITERAL): string | undefined {
- if (effect?.type !== 'DROP_SHADOW' || !effect.visible) return undefined
- return shadowToCSS(effect, 0, css)
+/** A blur as CSS's `blur()` takes it: the sigma itself, as the canvas blurs by. */
+function blurToCSS(effect: Effect, index: number, css: EffectFieldCSS): string {
+ return `blur(${blurLength(effect, index, css, 1)})`
}
const isShadow = (effect: Effect) => effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW'
+/** The visible effects with the index a bound field names them by. */
+function visibleEffects(effects: Effect[]): [number, Effect][] {
+ return [...effects.entries()].filter(([, effect]) => effect.visible)
+}
+
+/**
+ * The blurs as the canvas draws them: the first visible layer blur as `filter`, and every
+ * background blur in turn as `backdrop-filter`.
+ */
+function addBlurs(
+ style: DesignStyleDeclaration,
+ visible: [number, Effect][],
+ css: EffectFieldCSS
+): void {
+ const layer = visible.find(
+ ([, effect]) => effect.type === 'LAYER_BLUR' || effect.type === 'FOREGROUND_BLUR'
+ )
+ if (layer) style.filter = blurToCSS(layer[1], layer[0], css)
+ const backdrop = visible.filter(([, effect]) => effect.type === 'BACKGROUND_BLUR')
+ if (backdrop.length > 0) {
+ style['backdrop-filter'] = backdrop
+ .map(([index, effect]) => blurToCSS(effect, index, css))
+ .join(' ')
+ }
+}
+
/**
* Every visible shadow, in order, plus layer and background blur. `css` names the variable a
* bound field, such as `effects/0/radius`, draws from.
*/
export function effectsToCSS(effects: Effect[], css = LITERAL): DesignStyleDeclaration {
const style: DesignStyleDeclaration = {}
- const visible = [...effects.entries()].filter(([, effect]) => effect.visible)
+ const visible = visibleEffects(effects)
const shadows = visible.filter(([, effect]) => isShadow(effect))
if (shadows.length > 0)
style['box-shadow'] = shadows
.map(([index, effect]) => shadowToCSS(effect, index, css))
.join(', ')
- for (const [index, effect] of visible) {
- const blur = () => `blur(${css(`effects/${index}/radius`, `${effect.radius}px`)})`
- if (effect.type === 'LAYER_BLUR' || effect.type === 'FOREGROUND_BLUR') style.filter = blur()
- if (effect.type === 'BACKGROUND_BLUR') style['backdrop-filter'] = blur()
+ addBlurs(style, visible, css)
+ return style
+}
+
+/**
+ * Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
+ * glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
+ */
+export function textEffectsToCSS(effects: Effect[], css = LITERAL): DesignStyleDeclaration {
+ const style: DesignStyleDeclaration = {}
+ const visible = visibleEffects(effects)
+ const shadows = visible.filter(([, effect]) => effect.type === 'DROP_SHADOW')
+ if (shadows.length > 0) {
+ style['text-shadow'] = shadows
+ .map(([index, effect]) => shadowToCSS(effect, index, css, false))
+ .join(', ')
}
+ addBlurs(style, visible, css)
return style
}
diff --git a/packages/dom-css/src/export/projection.ts b/packages/dom-css/src/export/projection.ts
index a100bcb4a..bc1a75fbb 100644
--- a/packages/dom-css/src/export/projection.ts
+++ b/packages/dom-css/src/export/projection.ts
@@ -14,13 +14,13 @@ import { DesignTokens, type CSSModes } from '../tokens/references'
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
import {
cssColor,
- dropShadowToCSS,
effectsToCSS,
fillToCSS,
gradientToCSS,
letterSpacingToCSS,
lineHeightToCSS,
- strokeColorToCSS
+ strokeColorToCSS,
+ textEffectsToCSS
} from './css'
import { addGridContainer, addGridPlacement } from './grid'
import { textCaseToCSS, textElementContent } from './text'
@@ -417,8 +417,7 @@ function styleFromTextNode(
if (node.textAlignHorizontal !== 'LEFT')
style['text-align'] = node.textAlignHorizontal.toLowerCase()
if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
- const shadow = dropShadowToCSS(node.effects[0], css)
- if (shadow) style['text-shadow'] = shadow
+ Object.assign(style, textEffectsToCSS(node.effects, css))
if (node.textDecoration !== 'NONE') {
style['text-decoration-line'] =
node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'
diff --git a/packages/dom-css/src/import/scene-graph.ts b/packages/dom-css/src/import/scene-graph.ts
index 10abeab03..7a0ccee93 100644
--- a/packages/dom-css/src/import/scene-graph.ts
+++ b/packages/dom-css/src/import/scene-graph.ts
@@ -3,6 +3,7 @@ import { isValid, toUint8Array } from 'js-base64'
import {
SceneGraph,
+ type Effect,
type Fill,
type ImageScaleMode,
type SceneNode,
@@ -10,6 +11,7 @@ import {
} from '@open-pencil/scene-graph'
import { TRANSPARENT } from '@open-pencil/scene-graph/constants'
import { parseCSSAlignSelf, parseCSSNumber, parseCSSShadows } from '@open-pencil/scene-graph/css'
+import { blurRadiusForSigma } from '@open-pencil/scene-graph/geometry'
import { computeImageHash } from '@open-pencil/scene-graph/images'
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
@@ -40,6 +42,17 @@ export interface DesignDocumentToSceneGraphOptions {
pageName?: string
}
+/**
+ * CSS shadows as the layer's effects, each blurred as the browser blurs it: CSS blurs a shadow
+ * by half its blur length, which the canvas draws from the radius that gives that sigma.
+ */
+function cssShadows(value: string | undefined): Effect[] {
+ return parseCSSShadows(value).map((effect) => ({
+ ...effect,
+ radius: blurRadiusForSigma(effect.radius / 2)
+ }))
+}
+
function textContent(node: DesignNode): string {
if (node.type === 'text') return node.text
return node.children.map(textContent).join('')
@@ -327,7 +340,7 @@ function applyElementStyle(
setBorderWeights(node, style, strokes[0])
}
- const effects = parseCSSShadows(pickStyle(style, 'box-shadow'))
+ const effects = cssShadows(pickStyle(style, 'box-shadow'))
if (effects.length > 0) node.effects = effects
const opacity = parseCSSNumber(pickStyle(style, 'opacity'))
@@ -367,7 +380,7 @@ function applyTextStyle(node: SceneNode, style: DesignStyleDeclaration): void {
const opacity = parseCSSNumber(pickStyle(style, 'opacity'))
if (opacity !== null) node.opacity = opacity
- const effects = parseCSSShadows(pickStyle(style, 'text-shadow'))
+ const effects = cssShadows(pickStyle(style, 'text-shadow'))
if (effects.length > 0) node.effects = effects
const textAlign = pickStyle(style, 'text-align')?.toUpperCase()
diff --git a/packages/dom-css/tests/export/effects.test.ts b/packages/dom-css/tests/export/effects.test.ts
new file mode 100644
index 000000000..018ba311b
--- /dev/null
+++ b/packages/dom-css/tests/export/effects.test.ts
@@ -0,0 +1,100 @@
+import { describe, expect, test } from 'bun:test'
+
+import { sceneNodeToDesignDocument } from '@open-pencil/dom-css/export'
+import { SceneGraph, type Effect, type SceneNode } from '@open-pencil/scene-graph'
+
+// The canvas draws every Figma blur as a Gaussian of 0.43 × radius. CSS's `blur()` takes that
+// sigma, and a shadow's blur length is twice it.
+
+function effect(type: Effect['type'], radius: number, props: Partial<Effect> = {}): Effect {
+ return {
+ type,
+ radius,
+ spread: 0,
+ offset: { x: 0, y: 0 },
+ color: { r: 0, g: 0, b: 0, a: 0.5 },
+ visible: true,
+ blendMode: 'NORMAL',
+ ...props
+ }
+}
+
+function style(type: SceneNode['type'], effects: Effect[]) {
+ const graph = new SceneGraph()
+ const node = graph.createNode(type, graph.getPages()[0].id, {
+ width: 40,
+ height: 20,
+ text: 'Hi',
+ effects
+ })
+ const [element] = sceneNodeToDesignDocument(graph, node.id).children
+ if (element?.type !== 'element') throw new Error('Expected an element')
+ return element.inlineStyle
+}
+
+describe('effects in HTML export', () => {
+ test('scale a bound blur radius in calc(), since its token holds the Figma radius', () => {
+ const graph = new SceneGraph()
+ const collection = graph.createCollection('Tokens')
+ const blur = graph.createVariable('blur/md', 'FLOAT', collection.id, 10)
+ const soft = graph.createVariable('shadow/soft', 'FLOAT', collection.id, 10)
+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, {
+ effects: [effect('LAYER_BLUR', 10), effect('DROP_SHADOW', 10)]
+ })
+ graph.bindVariable(node.id, 'effects/0/radius', blur.id)
+ graph.bindVariable(node.id, 'effects/1/radius', soft.id)
+ const [element] = sceneNodeToDesignDocument(graph, node.id).children
+ if (element?.type !== 'element') throw new Error('Expected an element')
+ // Each effect reads its own binding.
+ expect(element.inlineStyle?.filter).toBe('blur(calc(var(--blur-md) * 0.43))')
+ expect(element.inlineStyle?.['box-shadow']).toContain('calc(var(--shadow-soft) * 0.86)')
+ })
+
+ test('blur by the first layer blur and every background blur, as the canvas draws them', () => {
+ const blurs = style('RECTANGLE', [
+ // Hidden blurs are neither the first layer blur nor among the background blurs.
+ effect('LAYER_BLUR', 30, { visible: false }),
+ effect('BACKGROUND_BLUR', 30, { visible: false }),
+ effect('LAYER_BLUR', 10),
+ effect('LAYER_BLUR', 20),
+ effect('BACKGROUND_BLUR', 10),
+ effect('BACKGROUND_BLUR', 20)
+ ])
+ expect(blurs?.filter).toBe('blur(4.3px)')
+ expect(blurs?.['backdrop-filter']).toBe('blur(4.3px) blur(8.6px)')
+ })
+
+ test('blur as the canvas blurs, by the sigma CSS takes', () => {
+ expect(style('RECTANGLE', [effect('LAYER_BLUR', 10)])?.filter).toBe('blur(4.3px)')
+ expect(style('RECTANGLE', [effect('BACKGROUND_BLUR', 10)])?.['backdrop-filter']).toBe(
+ 'blur(4.3px)'
+ )
+ })
+
+ test('write a shadow’s blur length as twice that sigma', () => {
+ const shadows = [
+ effect('DROP_SHADOW', 10, { offset: { x: 1, y: 2 }, spread: 3 }),
+ effect('INNER_SHADOW', 5)
+ ]
+ expect(style('RECTANGLE', shadows)?.['box-shadow']).toBe(
+ '1px 2px 8.6px 3px rgba(0, 0, 0, 0.5), inset 0px 0px 4.3px 0px rgba(0, 0, 0, 0.5)'
+ )
+ })
+
+ test('shadow text’s glyphs with every drop shadow, without the spread text-shadow lacks', () => {
+ const text = style('TEXT', [
+ effect('DROP_SHADOW', 4, { offset: { x: 1, y: 2 }, spread: 2 }),
+ effect('DROP_SHADOW', 4, { offset: { x: -1, y: 2 } }),
+ effect('INNER_SHADOW', 4),
+ effect('DROP_SHADOW', 4, { visible: false, offset: { x: 3, y: 3 } }),
+ effect('LAYER_BLUR', 10),
+ effect('BACKGROUND_BLUR', 20)
+ ])
+ expect(text?.['text-shadow']).toBe(
+ '1px 2px 3.44px rgba(0, 0, 0, 0.5), -1px 2px 3.44px rgba(0, 0, 0, 0.5)'
+ )
+ expect(text?.filter).toBe('blur(4.3px)')
+ expect(text?.['backdrop-filter']).toBe('blur(8.6px)')
+ expect(text?.['box-shadow']).toBeUndefined()
+ })
+})
diff --git a/packages/dom-css/tests/export/projection/tokens.test.ts b/packages/dom-css/tests/export/projection/tokens.test.ts
index 845dd2441..5bed9f76a 100644
--- a/packages/dom-css/tests/export/projection/tokens.test.ts
+++ b/packages/dom-css/tests/export/projection/tokens.test.ts
@@ -131,8 +131,9 @@ describe('token references in effects', () => {
graph.bindVariable(frame.id, 'effects/0/offsetY', 'gutter')
graph.bindVariable(frame.id, 'effects/0/color', 'primary')
+ // A shadow's blur length is twice the sigma the canvas blurs radius 8 by.
expect(element(graph, frame.id).inlineStyle?.['box-shadow']).toBe(
- '0px var(--spacing-gutter) 8px 0px var(--color-primary)'
+ '0px var(--spacing-gutter) 6.88px 0px var(--color-primary)'
)
})
@@ -143,7 +144,10 @@ describe('token references in effects', () => {
})
graph.bindVariable(frame.id, 'effects/0/radius', 'gutter')
- expect(element(graph, frame.id).inlineStyle?.filter).toBe('blur(var(--spacing-gutter))')
+ // The token holds the Figma radius; `blur()` takes the sigma the canvas draws it with.
+ expect(element(graph, frame.id).inlineStyle?.filter).toBe(
+ 'blur(calc(var(--spacing-gutter) * 0.43))'
+ )
})
test('keep the literal where the shadow draws another value, as a scaled instance does', () => {
@@ -151,6 +155,6 @@ describe('token references in effects', () => {
const frame = card(graph, graph.getPages()[0].id, BLUE_500, { effects: [shadow(48)] })
graph.updateNode(frame.id, { boundVariables: { 'effects/0/offsetY': 'gutter' } })
- expect(element(graph, frame.id).inlineStyle?.['box-shadow']).toBe('0px 48px 8px 0px #3B82F5')
+ expect(element(graph, frame.id).inlineStyle?.['box-shadow']).toBe('0px 48px 6.88px 0px #3B82F5')
})
})
diff --git a/packages/dom-css/tests/export/tailwind-jsx/appearance.test.ts b/packages/dom-css/tests/export/tailwind-jsx/appearance.test.ts
index e9da489f7..ccaa8c9d5 100644
--- a/packages/dom-css/tests/export/tailwind-jsx/appearance.test.ts
+++ b/packages/dom-css/tests/export/tailwind-jsx/appearance.test.ts
@@ -136,7 +136,8 @@ describe('Tailwind JSX appearance classes', () => {
}
]
})
- expect(tw(graph, node.id)).toMatch(/shadow-\[0px_4px_8px/)
+ // CSS blurs a shadow by half its blur length: 2 × 0.43 × radius 8.
+ expect(tw(graph, node.id)).toMatch(/shadow-\[0px_4px_6.88px/)
})
test('inner shadow includes inset and spread', () => {
@@ -155,7 +156,7 @@ describe('Tailwind JSX appearance classes', () => {
}
]
})
- expect(tw(graph, node.id)).toMatch(/shadow-\[inset_1px_2px_3px_4px/)
+ expect(tw(graph, node.id)).toMatch(/shadow-\[inset_1px_2px_2.58px_4px/)
})
test('blur emits blur class', () => {
@@ -174,7 +175,8 @@ describe('Tailwind JSX appearance classes', () => {
}
]
})
- expect(tw(graph, node.id)).toContain('blur-[4px]')
+ // CSS's blur() takes the sigma: 0.43 × radius 4.
+ expect(tw(graph, node.id)).toContain('blur-[1.72px]')
})
test('font size — named values', () => {
diff --git a/packages/scene-graph/src/geometry.ts b/packages/scene-graph/src/geometry.ts
index a993aa25a..a84451478 100644
--- a/packages/scene-graph/src/geometry.ts
+++ b/packages/scene-graph/src/geometry.ts
@@ -226,6 +226,22 @@ export function drawnBlurRadius(radius: number): number {
return Math.max(0, radius)
}
+const BLUR_SIGMA_PER_RADIUS = 0.43
+
+/**
+ * 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 drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS
+}
+
+/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
+export function blurRadiusForSigma(sigma: number): number {
+ return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS
+}
+
export function effectOverflow(effects?: Effect[]) {
let left = 0
let right = 0
diff --git a/tests/engine/dom-css/conversion.test.ts b/tests/engine/dom-css/conversion.test.ts
index e7b757aa3..026416927 100644
--- a/tests/engine/dom-css/conversion.test.ts
+++ b/tests/engine/dom-css/conversion.test.ts
@@ -227,7 +227,8 @@ describe('@open-pencil/dom-css conversion', () => {
// A color function in the `border` shorthand, and a shadow color after its lengths.
expect(card.strokes[0]?.color).toEqual(parseColor(DOM_CSS_COLORS.slate200))
expect(card.effects[0]?.type).toBe('DROP_SHADOW')
- expect(card.effects[0]?.radius).toBe(24)
+ // A 24px CSS blur is a Gaussian of 12, which the canvas draws at 0.43 × radius.
+ expect(card.effects[0]?.radius).toBeCloseTo(12 / 0.43, 6)
expect(card.effects[0]?.color).toEqual(parseColor(DOM_CSS_COLORS.slateShadow))
const [title, description] = graph.getChildren(card.id)
@@ -493,7 +494,7 @@ describe('@open-pencil/dom-css conversion', () => {
expect(dialog.maxWidth).toBe(420 + 2 * 24 + 2 * 1)
expect(dialog.itemSpacing).toBe(16)
expect(dialog.effects[0]?.type).toBe('DROP_SHADOW')
- expect(dialog.effects[0]?.radius).toBe(40)
+ expect(dialog.effects[0]?.radius).toBeCloseTo(20 / 0.43, 6)
const [, , inputNode, buttonNode] = graph.getChildren(dialog.id)
const input = expectFrame(inputNode)