AI-native design editor. Open-source Figma alternative.
fix(canvas): draw faded and blended layers whole when rotated (#1356)
Andrew Avsenin806380c6d0a3
diff --git a/changes/1356/fixed.md b/changes/1356/fixed.mdnew file mode 100644index 000000000..5b39b2410--- /dev/null+++ b/changes/1356/fixed.md@@ -0,0 +1 @@+- Layers with opacity below 100% or a blend mode draw whole when rotated or inside a rotated frame, instead of being cut off.diff --git a/packages/core/src/canvas/renderer/visual-bounds.ts b/packages/core/src/canvas/renderer/visual-bounds.tsindex 1cc4f34d6..1c04224da 100644--- a/packages/core/src/canvas/renderer/visual-bounds.ts+++ b/packages/core/src/canvas/renderer/visual-bounds.ts@@ -1,24 +1,28 @@import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'-import { getWorldMatrix } from '@open-pencil/scene-graph/coordinate'+import { getNodeLocalMatrix, getWorldMatrix } from '@open-pencil/scene-graph/coordinate'import {effectOverflow,+ geometryBlobBounds,strokeOverflow,+ textDecorationOverflow,+ unionVisualBounds,type VisualBounds} from '@open-pencil/scene-graph/geometry'-import Matrix from '@open-pencil/scene-graph/matrix'+import Matrix, { type Mat3 } from '@open-pencil/scene-graph/matrix'-export function worldNodeVisualBounds(graph: SceneGraph, node: SceneNode): VisualBounds {- const stroke = strokeOverflow(node.strokes, node.strokeCap, node.vectorNetwork, node)- const effects = effectOverflow(node.effects)- const points = Matrix.mapPoints(getWorldMatrix(node, graph), [- -stroke - effects.left,- -stroke - effects.top,- node.width + stroke + effects.right,- -stroke - effects.top,- node.width + stroke + effects.right,- node.height + stroke + effects.bottom,- -stroke - effects.left,- node.height + stroke + effects.bottom+import { projectedNode, type RotationPreview } from '#core/geometry'++/** The axis-aligned box around `bounds` once `matrix` maps it. */+export function mapVisualBounds(matrix: Mat3, bounds: VisualBounds): VisualBounds {+ const points = Matrix.mapPoints(matrix, [+ bounds.minX,+ bounds.minY,+ bounds.maxX,+ bounds.minY,+ bounds.maxX,+ bounds.maxY,+ bounds.minX,+ bounds.maxY])const xs = [points[0], points[2], points[4], points[6]]const ys = [points[1], points[3], points[5], points[7]]@@ -29,3 +33,79 @@ export function worldNodeVisualBounds(graph: SceneGraph, node: SceneNode): VisuamaxY: Math.max(...ys)}}++export function worldNodeVisualBounds(graph: SceneGraph, node: SceneNode): VisualBounds {+ const stroke = strokeOverflow(node.strokes, node.strokeCap, node.vectorNetwork, node)+ const effects = effectOverflow(node.effects)+ return mapVisualBounds(getWorldMatrix(node, graph), {+ minX: -stroke - effects.left,+ minY: -stroke - effects.top,+ maxX: node.width + stroke + effects.right,+ maxY: node.height + stroke + effects.bottom+ })+}++/**+ * What a node draws itself, in its own axes before position, rotation, and flips: the box grown+ * by strokes and effects, its path geometry, and text decoration. Children are not included.+ */+function localVisualBounds(node: SceneNode): VisualBounds {+ const stroke = strokeOverflow(node.strokes, node.strokeCap, node.vectorNetwork, node)+ const effects = effectOverflow(node.effects)+ const box: VisualBounds = {+ minX: -stroke - effects.left,+ minY: -stroke - effects.top,+ maxX: node.width + stroke + effects.right,+ maxY: node.height + stroke + effects.bottom + textDecorationOverflow(node)+ }+ const hasNonInsideStroke = node.strokes.some(+ (stroke) => stroke.visible && stroke.align !== 'INSIDE'+ )+ const geometry = geometryBlobBounds([+ ...node.fillGeometry,+ ...(hasNonInsideStroke ? node.strokeGeometry : [])+ ])+ if (!geometry) return box+ return {+ minX: Math.min(box.minX, geometry.x),+ minY: Math.min(box.minY, geometry.y),+ maxX: Math.max(box.maxX, geometry.x + geometry.width),+ maxY: Math.max(box.maxY, geometry.y + geometry.height)+ }+}++function clipsChildren(node: SceneNode): boolean {+ return (+ node.clipsContent &&+ (node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE')+ )+}++/**+ * What a node and its visible descendants draw, in the frame `toFrame` maps the node's own axes+ * into. One pass down the subtree composes each child's local matrix, so no node walks its+ * ancestors. A container that clips its content ends the walk: its children draw inside its box,+ * which its own bounds already hold.+ */+export function subtreeVisualBounds(+ graph: SceneGraph,+ node: SceneNode,+ toFrame: Mat3,+ preview?: RotationPreview | null+): VisualBounds {+ let bounds = mapVisualBounds(toFrame, localVisualBounds(node))+ const pending = [{ node, matrix: toFrame }]+ for (let entry = pending.pop(); entry; entry = pending.pop()) {+ if (clipsChildren(entry.node)) continue+ for (const childId of entry.node.childIds) {+ const child = graph.getNode(childId)+ if (!child?.visible) continue+ const local = getNodeLocalMatrix(projectedNode(child, preview))+ const matrix = Matrix.multiply(entry.matrix, local)+ bounds =+ unionVisualBounds(bounds, mapVisualBounds(matrix, localVisualBounds(child))) ?? bounds+ pending.push({ node: child, matrix })+ }+ }+ return bounds+}diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.tsindex 3b341bb2e..f4a67b9ec 100644--- a/packages/core/src/canvas/scene.ts+++ b/packages/core/src/canvas/scene.ts@@ -10,7 +10,6 @@ import {lineArrowEndpoints,trimArrowShafts} from '@open-pencil/scene-graph/arrow-caps'-import { computeDescendantVisualBounds } from '@open-pencil/scene-graph/geometry'import Matrix from '@open-pencil/scene-graph/matrix'import type { Color } from '@open-pencil/scene-graph/primitives'@@ -37,6 +36,7 @@ import {effectRasterScale,effectRasterScaleMatches} from './renderer/effect-raster-cache'+import { mapVisualBounds, subtreeVisualBounds } from './renderer/visual-bounds'import { renderLayerShadows } from './shadows'import { isLayerContainer } from './shadows/silhouette'import { makeSmoothRRectPath, nodeHasRadius, nodeHasSmoothCorners } from './shapes'@@ -291,45 +291,34 @@ function viewportLayerBounds(const inverse = Matrix.invert(createSceneGeometry(graph, preview).worldMatrix(node))if (!inverse) return nullconst viewport = r.worldViewport- const points = Matrix.mapPoints(inverse, [- viewport.x,- viewport.y,- viewport.x + viewport.w,- viewport.y,- viewport.x + viewport.w,- viewport.y + viewport.h,- viewport.x,- viewport.y + viewport.h- ])- const xs = [points[0], points[2], points[4], points[6]]- const ys = [points[1], points[3], points[5], points[7]]+ const bounds = mapVisualBounds(inverse, {+ minX: viewport.x,+ minY: viewport.y,+ maxX: viewport.x + viewport.w,+ maxY: viewport.y + viewport.h+ })return r.ck.LTRBRect(- Math.min(...xs) - padding,- Math.min(...ys) - padding,- Math.max(...xs) + padding,- Math.max(...ys) + padding+ bounds.minX - padding,+ bounds.minY - padding,+ bounds.maxX + padding,+ bounds.maxY + padding)}+/**+ * The layer opens after the node's x/y translation and before its rotation and flips, so the+ * subtree is measured through its orientation matrix.+ */function nodeIsolationLayerBounds(r: SkiaRenderer,graph: SceneGraph,node: SceneNode,- nodeId: string,- absX: number,- absY: number,preview?: RenderOverlays['rotationPreview']): Float32Array {const viewportBounds = viewportLayerBounds(r, graph, node, 0, preview)if (viewportBounds) return viewportBounds- const bounds = computeDescendantVisualBounds(- [nodeId],- (id) => graph.getNode(id) ?? undefined,- (id) => graph.getAbsolutePosition(id)- )- return bounds- ? r.ck.LTRBRect(bounds.minX - absX, bounds.minY - absY, bounds.maxX - absX, bounds.maxY - absY)- : r.ck.LTRBRect(0, 0, node.width, node.height)+ const bounds = subtreeVisualBounds(graph, node, nodeOrientationMatrix(node, preview), preview)+ return r.ck.LTRBRect(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY)}/**@@ -371,15 +360,7 @@ export function renderNode(const needsNodeLayer = node.opacity < 1 || needsIsolatedBlendLayer(node.blendMode)if (needsNodeLayer) {- const layerBounds = nodeIsolationLayerBounds(- r,- graph,- node,- nodeId,- absX,- absY,- overlays.rotationPreview- )+ const layerBounds = nodeIsolationLayerBounds(r, graph, node, overlays.rotationPreview)r.opacityPaint.setAlphaf(node.opacity)r.opacityPaint.setBlendMode(figmaBlendModeToSkia(r.ck, node.blendMode))canvas.saveLayer(r.opacityPaint, layerBounds)diff --git a/packages/core/tests/canvas/isolation-layer.test.ts b/packages/core/tests/canvas/isolation-layer.test.tsnew file mode 100644index 000000000..7e140189c--- /dev/null+++ b/packages/core/tests/canvas/isolation-layer.test.ts@@ -0,0 +1,183 @@+import { afterAll, describe, expect, test } from 'bun:test'++import { expectDefined } from '#core-tests/helpers/assert'+import type { Canvas } from 'canvaskit-wasm'++import { SkiaRenderer } from '@open-pencil/core'+import { initCanvasKit } from '@open-pencil/core/io'+import { SceneGraph, type SceneNode } from '@open-pencil/scene-graph'++const SIZE = 300++const ck = await initCanvasKit()+const renderer = new SkiaRenderer(ck, expectDefined(ck.MakeSurface(SIZE, SIZE), 'surface'))+afterAll(() => renderer.destroy())++const RED = [+ { type: 'SOLID' as const, color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true }+]++const SHADOW = {+ type: 'DROP_SHADOW' as const,+ color: { r: 0, g: 0, b: 0, a: 1 },+ offset: { x: 14, y: 14 },+ radius: 6,+ spread: 0,+ visible: true,+ blendMode: 'NORMAL' as const,+ showShadowBehindNode: false+}++type Scene = (graph: SceneGraph, parentId: string, opacity: number) => void++function renderPreviewed(canvas: Canvas, graph: SceneGraph, nodeId: string, angle: number) {+ const viewport = renderer.worldViewport+ renderer.worldViewport = { x: -SIZE, y: -SIZE, w: 3 * SIZE, h: 3 * SIZE }+ try {+ renderer.renderNode(canvas, graph, nodeId, { rotationPreview: { nodeId, angle } })+ } finally {+ renderer.worldViewport = viewport+ }+}++function alphaChannel(scene: Scene, opacity: number, previewAngle?: number): Uint8Array {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ scene(graph, page.id, opacity)+ renderer.pageId = page.id+ const canvas = renderer.surface.getCanvas()+ canvas.clear(ck.TRANSPARENT)+ if (previewAngle === undefined) {+ renderer.renderSceneToCanvas(canvas, graph, page.id)+ } else {+ renderPreviewed(canvas, graph, page.childIds[0], previewAngle)+ }+ renderer.surface.flush()+ const image = renderer.surface.makeImageSnapshot()+ try {+ const rgba = expectDefined(+ image.readPixels(0, 0, {+ width: SIZE,+ height: SIZE,+ colorType: ck.ColorType.RGBA_8888,+ alphaType: ck.AlphaType.Unpremul,+ colorSpace: ck.ColorSpace.SRGB+ })+ )+ return Uint8Array.from({ length: SIZE * SIZE }, (_, pixel) => rgba[pixel * 4 + 3])+ } finally {+ image.delete()+ }+}++function worstDifference(actual: Uint8Array, expected: ArrayLike<number>): number {+ return actual.reduce(+ (worst, alpha, pixel) => Math.max(worst, Math.abs(alpha - expected[pixel])),+ 0+ )+}++function expectHalfAsOpaque(scene: Scene, previewAngle?: number) {+ const opaque = alphaChannel(scene, 1, previewAngle)+ const faded = alphaChannel(scene, 0.5, previewAngle)+ expect(opaque.filter((alpha) => alpha > 0).length).toBeGreaterThan(1000)+ const half = Array.from(opaque, (alpha) => alpha / 2)+ expect(worstDifference(faded, half)).toBeLessThanOrEqual(1)+}++function rectangle(props: Partial<SceneNode>): Scene {+ return (graph, parentId, opacity) => {+ graph.createNode('RECTANGLE', parentId, {+ x: 100,+ y: 80,+ width: 120,+ height: 60,+ fills: RED,+ opacity,+ ...props+ })+ }+}++describe('a layer drawn into an isolation layer', () => {+ test('draws whole when it is not transformed', () => {+ expectHalfAsOpaque(rectangle({}))+ })++ test('draws whole when it is rotated', () => {+ expectHalfAsOpaque(rectangle({ rotation: 30 }))+ })++ test('draws whole when it is rotated far from the page origin', () => {+ expectHalfAsOpaque(rectangle({ x: 150, y: 150, width: 100, height: 100, rotation: 45 }))+ })++ test('draws whole when it is rotated and flipped', () => {+ expectHalfAsOpaque(rectangle({ rotation: 30, flipX: true }))+ })++ test('draws its drop shadow whole when it is rotated', () => {+ expectHalfAsOpaque(rectangle({ rotation: 30, effects: [SHADOW] }))+ })++ test('draws whole inside a rotated frame', () => {+ expectHalfAsOpaque((graph, parentId, opacity) => {+ const frame = graph.createNode('FRAME', parentId, {+ x: 40,+ y: 20,+ width: 220,+ height: 200,+ rotation: 20,+ fills: [],+ clipsContent: false+ })+ rectangle({ x: 60, y: 40 })(graph, frame.id, opacity)+ })+ })++ test('draws a rotated clipping frame whole with its shadow and clipped children', () => {+ expectHalfAsOpaque((graph, parentId, opacity) => {+ const frame = graph.createNode('FRAME', parentId, {+ x: 100,+ y: 60,+ width: 120,+ height: 90,+ rotation: 30,+ fills: [],+ effects: [SHADOW],+ clipsContent: true,+ opacity+ })+ rectangle({ x: 60, y: 30, width: 200, height: 200 })(graph, frame.id, 1)+ })+ })++ test('draws whole while a rotation preview turns it', () => {+ expectHalfAsOpaque(rectangle({}), 90)+ })++ test('draws whole while a rotation preview turns it from a rotation', () => {+ expectHalfAsOpaque(rectangle({ rotation: 30 }), 75)+ })++ test('draws its children whole while a rotation preview turns it', () => {+ expectHalfAsOpaque((graph, parentId, opacity) => {+ const frame = graph.createNode('FRAME', parentId, {+ x: 100,+ y: 100,+ width: 120,+ height: 40,+ fills: [],+ clipsContent: false,+ opacity+ })+ rectangle({ x: 20, y: 0, width: 140, height: 40 })(graph, frame.id, 1)+ }, 90)+ })++ test('draws whole with a blend mode when it is rotated', () => {+ const normal = alphaChannel(rectangle({ rotation: 30 }), 1)+ const multiplied = alphaChannel(rectangle({ rotation: 30, blendMode: 'MULTIPLY' }), 1)+ expect(worstDifference(multiplied, normal)).toBeLessThanOrEqual(1)+ })+})diff --git a/packages/scene-graph/src/geometry.ts b/packages/scene-graph/src/geometry.tsindex 28d0372ee..685c68cf9 100644--- a/packages/scene-graph/src/geometry.ts+++ b/packages/scene-graph/src/geometry.ts@@ -402,16 +402,20 @@ export function nodeVisualBounds(bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds}- if (node.type === 'TEXT' && node.textDecoration && node.textDecoration !== 'NONE') {- const fontSize = node.fontSize ?? 14- const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18- const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)- bounds.maxY += underlineOffset + thickness + fontSize * 0.35- }+ bounds.maxY += textDecorationOverflow(node)return bounds}+/** How far a text node's underline or strikethrough reaches below its box. */+export function textDecorationOverflow(node: VisualBoundsNode): number {+ if (node.type !== 'TEXT' || !node.textDecoration || node.textDecoration === 'NONE') return 0+ const fontSize = node.fontSize ?? 14+ const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18+ const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)+ return underlineOffset + thickness + fontSize * 0.35+}+function collectDescendantVisualBounds(nodeId: string,getNode: (id: string) => VisualBoundsNode | undefined,