Skip to content

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

fix(canvas): draw no drop shadow under a fill at 0% opacity (#1255)

Niraj Sanganidd8b0ae1c6f9
diff --git a/changes/1255/fixed.md b/changes/1255/fixed.md
new file mode 100644
index 000000000..5a282e61c
--- /dev/null
+++ b/changes/1255/fixed.md
@@ -0,0 +1 @@
+- A shape whose fills are all at 0% opacity casts no drop shadow and takes no inner shadow, as in Figma, and a frame with a 0% fill shadows its children instead of its own bounds.
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index b0e1496d3..e57e96df6 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -37,6 +37,7 @@ import {
effectRasterScale,
effectRasterScaleMatches
} from './renderer/effect-raster-cache'
+import { paintCastsShadow } from './shadows'
import { makeSmoothRRectPath, nodeHasRadius, nodeHasSmoothCorners } from './shapes'
import {
configureStrokePaint,
@@ -643,8 +644,8 @@ export function renderShape(
}
function getShadowShapeChild(node: SceneNode, graph: SceneGraph): SceneNode | null {
- if (node.fills.some((f) => f.visible)) return null
- if (node.strokes.some((stroke) => stroke.visible)) return null
+ if (node.fills.some(paintCastsShadow)) return null
+ if (node.strokes.some(paintCastsShadow)) return null
if (node.childIds.length === 0) return null
const child = graph.getNode(node.childIds[0])
if (!child?.visible) return null
diff --git a/packages/core/src/canvas/shadows.ts b/packages/core/src/canvas/shadows.ts
index d57e2e441..1a337fa39 100644
--- a/packages/core/src/canvas/shadows.ts
+++ b/packages/core/src/canvas/shadows.ts
@@ -1,7 +1,7 @@
import type { Canvas, Path } from 'canvaskit-wasm'
import { readEffectiveFigmaRawField } from '@open-pencil/fig'
-import type { SceneNode } from '@open-pencil/scene-graph'
+import type { Fill, SceneNode } from '@open-pencil/scene-graph'
import type { Color, Vector } from '@open-pencil/scene-graph/primitives'
import { BLACK } from '#core/constants'
@@ -224,6 +224,20 @@ function drawShadowCutout(
}
}
+/**
+ * Figma casts a full-strength shadow from any paint above 0% opacity and none from a paint at
+ * 0% or a gradient whose stops are all transparent, so such a paint counts as no paint at all.
+ */
+export function paintCastsShadow(paint: Fill): boolean {
+ if (!paint.visible || paint.opacity === 0) return false
+ return paint.gradientStops?.some((stop) => stop.color.a > 0) ?? true
+}
+
+function castsShadow(node: SceneNode, shadowShapeChild?: SceneNode | null): boolean {
+ if (shadowShapeChild) return true
+ return node.fills.some(paintCastsShadow) || node.strokes.some(paintCastsShadow)
+}
+
function drawShapeDropShadow(
r: SkiaRenderer,
canvas: Canvas,
@@ -235,10 +249,10 @@ function drawShapeDropShadow(
const sp = effect.spread
const shapeNode = shadowShapeChild ?? node
const shapeHasRadius = shadowShapeChild ? nodeHasRadius(shadowShapeChild) : hasRadius
- const hasVisibleFill = node.fills.some((fill) => fill.visible)
+ const hasCastingFill = node.fills.some(paintCastsShadow)
let geometryShadow: Path[] | null = null
if (!shadowShapeChild) {
- if (hasVisibleFill) {
+ if (hasCastingFill) {
geometryShadow = r.getFillGeometry(node)
} else if (node.childIds.length === 0 && node.strokeGeometry.length > 0) {
geometryShadow = r.getStrokeGeometry(node)
@@ -256,7 +270,7 @@ function drawShapeDropShadow(
else canvas.translate(effect.offset.x, effect.offset.y)
const shouldHideShadowBehindUnfilledNode =
- effect.showShadowBehindNode === false && !hasVisibleFill && !shadowShapeChild
+ effect.showShadowBehindNode === false && !hasCastingFill && !shadowShapeChild
if (shouldHideShadowBehindUnfilledNode) {
resetEffectLayerPaint(r)
canvas.saveLayer(r.effectLayerPaint, effectLayerBounds(r, shapeNode, effect))
@@ -302,6 +316,7 @@ function renderDropShadow(
const shapeNode = shadowShapeChild ?? node
if (shapeNode.type !== 'TEXT') {
+ if (!castsShadow(node, shadowShapeChild)) return
drawShapeDropShadow(r, canvas, node, effect, hasRadius, shadowShapeChild)
resetEffectLayerPaint(r)
return
@@ -545,6 +560,7 @@ export function renderEffects(
if (pass === 'front' && effect.type === 'INNER_SHADOW') {
const shapeNode = shadowShapeChild ?? node
+ if (shapeNode.type !== 'TEXT' && !castsShadow(node, shadowShapeChild)) continue
if (shapeNode.type === 'TEXT') {
drawTextInnerShadow(r, canvas, node, effect, shadowShapeChild)
} else {
diff --git a/packages/core/tests/canvas/blur.test.ts b/packages/core/tests/canvas/blur.test.ts
index c90f6c290..db6541b9e 100644
--- a/packages/core/tests/canvas/blur.test.ts
+++ b/packages/core/tests/canvas/blur.test.ts
@@ -173,3 +173,62 @@ describe('blur effects fall off as in Figma', () => {
expectFigmaEdge(row, FIGMA['BACKGROUND_BLUR 16'])
})
})
+
+describe('shadows of a transparent fill', () => {
+ const shadowRow = (opacity: number, showShadowBehindNode = false) =>
+ edgeRow(
+ (graph, frameId) =>
+ square(graph, frameId, {
+ fills: [solid(1, 1, 1, opacity)],
+ effects: [{ ...effect('DROP_SHADOW', 16), showShadowBehindNode }]
+ }),
+ 3
+ )
+
+ test('a fill at 0% opacity casts no shadow', () => {
+ expect(Math.max(...shadowRow(0))).toBe(0)
+ })
+
+ test('a fill at 1% opacity casts a full one', () => {
+ expect(shadowRow(0.01).slice(0, 40)).toEqual(shadowRow(1).slice(0, 40))
+ })
+
+ test('a fill at 0% opacity casts none when the shadow shows behind the node', () => {
+ expect(Math.max(...shadowRow(0, true))).toBe(0)
+ })
+
+ test('a fill at 0% opacity takes no inner shadow', () => {
+ const row = edgeRow(
+ (graph, frameId) =>
+ square(graph, frameId, {
+ fills: [solid(1, 1, 1, 0)],
+ effects: [effect('INNER_SHADOW', 16)]
+ }),
+ 3
+ )
+ expect(Math.max(...row)).toBe(0)
+ })
+
+ test('a frame with a fill at 0% opacity casts only its child shadow', () => {
+ const row = edgeRow((graph, frameId) => {
+ const frame = graph.createNode('FRAME', frameId, {
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 200,
+ fills: [solid(1, 1, 1, 0)],
+ clipsContent: false,
+ effects: [effect('DROP_SHADOW', 16)]
+ })
+ graph.createNode('RECTANGLE', frame.id, {
+ x: 40,
+ y: 40,
+ width: 120,
+ height: 120,
+ fills: [solid(0, 0, 1)]
+ })
+ }, 3)
+ expect(Math.max(...row.slice(0, 41))).toBe(0)
+ expect(row[79]).toBeGreaterThan(0)
+ })
+})
diff --git a/tests/engine/render/canvas/effects/edge-cases.test.ts b/tests/engine/render/canvas/effects/edge-cases.test.ts
index b4d359cac..c673962af 100644
--- a/tests/engine/render/canvas/effects/edge-cases.test.ts
+++ b/tests/engine/render/canvas/effects/edge-cases.test.ts
@@ -2,12 +2,12 @@ import { describe, expect, mock, test } from 'bun:test'
import type { Path } from 'canvaskit-wasm'
-import type { SceneNode } from '@open-pencil/scene-graph'
+import { createDefaultNode, type SceneNode } from '@open-pencil/scene-graph'
import { renderEffects } from '#core/canvas/shadows'
import { asCanvas } from '../helpers'
-import { createMockCanvas, createMockRenderer, mockCalls } from './helpers'
+import { createMockCanvas, createMockRenderer, mockCalls, OPAQUE_FILL } from './helpers'
describe('Edge cases and bug fixes', () => {
test('drop shadow cast from stroke when node has no fill', () => {
@@ -49,6 +49,35 @@ describe('Edge cases and bug fixes', () => {
expect(canvas.drawPath).toHaveBeenCalled()
})
+ test('drop shadow is not drawn under a fill at 0% opacity', () => {
+ const r = createMockRenderer()
+ const canvas = createMockCanvas()
+ const node = createDefaultNode(() => 'transparent-fill', 'RECTANGLE', {
+ width: 200,
+ height: 200,
+ fills: [{ visible: true, type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 0 }],
+ effects: [
+ {
+ type: 'DROP_SHADOW',
+ visible: true,
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ offset: { x: 0, y: 0 },
+ radius: 8,
+ spread: 0,
+ showShadowBehindNode: false
+ }
+ ],
+ strokes: [],
+ strokeGeometry: [],
+ childIds: []
+ })
+
+ renderEffects(r, asCanvas(canvas), node, new Float32Array(4), false, 'behind')
+
+ expect(canvas.drawRect).not.toHaveBeenCalled()
+ expect(canvas.drawPath).not.toHaveBeenCalled()
+ })
+
test('drop shadow on stroked containers uses the container bounds', () => {
const r = createMockRenderer()
const canvas = createMockCanvas()
@@ -296,7 +325,7 @@ describe('Edge cases and bug fixes', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
effects: [
{
type: 'INNER_SHADOW',
@@ -337,7 +366,7 @@ describe('Edge cases and bug fixes', () => {
width: 100,
height: 100,
cornerRadius: 10,
- fills: [],
+ fills: [OPAQUE_FILL],
effects: [
{
type: 'INNER_SHADOW',
@@ -374,7 +403,7 @@ describe('Edge cases and bug fixes', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
effects: [
{
type: 'DROP_SHADOW',
diff --git a/tests/engine/render/canvas/effects/helpers.ts b/tests/engine/render/canvas/effects/helpers.ts
index 47f8f72f9..9fd1d5c9c 100644
--- a/tests/engine/render/canvas/effects/helpers.ts
+++ b/tests/engine/render/canvas/effects/helpers.ts
@@ -2,6 +2,8 @@ import { mock } from 'bun:test'
import type { InputRect, Paint } from 'canvaskit-wasm'
+import type { Fill } from '@open-pencil/scene-graph'
+
import type { SkiaRenderer } from '#core/canvas/renderer'
import { renderEffects } from '#core/canvas/shadows'
@@ -18,6 +20,14 @@ export function mockCalls(fn: unknown): unknown[][] {
return calls
}
+/** A fill to cast from: Figma casts no shadow from a shape with no fill, stroke, or child. */
+export const OPAQUE_FILL: Fill = {
+ type: 'SOLID',
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ opacity: 1,
+ visible: true
+}
+
export function createMockRenderer(overrides: Partial<SkiaRenderer> = {}): SkiaRenderer {
class MockPath {
delete = mock(() => undefined)
diff --git a/tests/engine/render/canvas/effects/path-shapes.test.ts b/tests/engine/render/canvas/effects/path-shapes.test.ts
index 4f001999d..2a5f3289e 100644
--- a/tests/engine/render/canvas/effects/path-shapes.test.ts
+++ b/tests/engine/render/canvas/effects/path-shapes.test.ts
@@ -5,7 +5,7 @@ import type { SceneNode } from '@open-pencil/scene-graph'
import { renderEffects } from '#core/canvas/shadows'
import { asCanvas } from '../helpers'
-import { createMockCanvas, createMockRenderer, mockCalls } from './helpers'
+import { createMockCanvas, createMockRenderer, mockCalls, OPAQUE_FILL } from './helpers'
const requiredNodeFields = { childIds: [] as string[], strokeGeometry: [] }
@@ -18,7 +18,7 @@ describe('path shape shadows', () => {
type: 'STAR',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
effects: [
{
type: 'DROP_SHADOW',
@@ -83,7 +83,7 @@ describe('path shape shadows', () => {
type: 'POLYGON',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
effects: [
{
type: 'INNER_SHADOW',
diff --git a/tests/engine/render/canvas/effects/spread.test.ts b/tests/engine/render/canvas/effects/spread.test.ts
index 14b72bccf..98c63cce5 100644
--- a/tests/engine/render/canvas/effects/spread.test.ts
+++ b/tests/engine/render/canvas/effects/spread.test.ts
@@ -5,7 +5,7 @@ import type { SceneNode } from '@open-pencil/scene-graph'
import { renderEffects } from '#core/canvas/shadows'
import { asCanvas } from '../helpers'
-import { createMockCanvas, createMockRenderer } from './helpers'
+import { createMockCanvas, createMockRenderer, OPAQUE_FILL } from './helpers'
describe('Shadow spread support (Behavioral)', () => {
test('drop shadow uses spread for shape expansion', () => {
@@ -71,7 +71,7 @@ describe('Shadow spread support (Behavioral)', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
childIds: [],
effects: [
{
diff --git a/tests/engine/render/canvas/effects/types.test.ts b/tests/engine/render/canvas/effects/types.test.ts
index b687121c7..e6bd35042 100644
--- a/tests/engine/render/canvas/effects/types.test.ts
+++ b/tests/engine/render/canvas/effects/types.test.ts
@@ -10,7 +10,7 @@ import { renderNode } from '#core/canvas/scene'
import { renderEffects } from '#core/canvas/shadows'
import { asCanvas, asDouble } from '../helpers'
-import { createMockCanvas, createMockRenderer, mockCalls } from './helpers'
+import { createMockCanvas, createMockRenderer, mockCalls, OPAQUE_FILL } from './helpers'
describe('Renderer handles all effect types (Behavioral)', () => {
test('handles DROP_SHADOW', () => {
@@ -20,7 +20,7 @@ describe('Renderer handles all effect types (Behavioral)', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
childIds: [],
strokeGeometry: [],
effects: [
@@ -51,6 +51,7 @@ describe('Renderer handles all effect types (Behavioral)', () => {
width: 100,
height: 100,
fills: [],
+ strokes: [{ ...OPAQUE_FILL, weight: 1, align: 'CENTER' }],
childIds: [],
strokeGeometry: [{ windingRule: 'NONZERO', commandsBlob: new Uint8Array([0]) }],
effects: [
@@ -82,7 +83,7 @@ describe('Renderer handles all effect types (Behavioral)', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
childIds: [],
strokeGeometry: [],
effects: [
@@ -111,7 +112,7 @@ describe('Renderer handles all effect types (Behavioral)', () => {
type: 'RECTANGLE',
width: 100,
height: 100,
- fills: [],
+ fills: [OPAQUE_FILL],
childIds: [],
effects: [
{
diff --git a/tests/helpers/canvas/effects/scene.ts b/tests/helpers/canvas/effects/scene.ts
index e3916ae54..6fb67cff8 100644
--- a/tests/helpers/canvas/effects/scene.ts
+++ b/tests/helpers/canvas/effects/scene.ts
@@ -20,7 +20,10 @@ function effect(type: Effect['type'], radius: number, y = 0): Effect {
}
}
-/** Layer blur, drop and inner shadow, and background blur, each at a 16px radius. */
+/**
+ * Layer blur, drop and inner shadow, and background blur, each at a 16px radius, plus a drop
+ * shadow, shown behind the node, under a fill at 0% opacity, which casts nothing.
+ */
export function createBlurEffectsScene(graph: SceneGraph, pageId: string): void {
const card = { y: 100, width: 120, height: 120, cornerRadius: 20 }
graph.createNode('RECTANGLE', pageId, {
@@ -68,4 +71,18 @@ export function createBlurEffectsScene(graph: SceneGraph, pageId: string): void
fills: [solid(1, 1, 1, 0.2)],
effects: [effect('BACKGROUND_BLUR', 16)]
})
+ graph.createNode('RECTANGLE', pageId, {
+ name: 'Transparent fill shadow',
+ x: 80,
+ ...card,
+ y: 280,
+ fills: [solid(1, 1, 1, 0)],
+ effects: [
+ {
+ ...effect('DROP_SHADOW', 16, 8),
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ showShadowBehindNode: true
+ }
+ ]
+ })
}