AI-native design editor. Open-source Figma alternative.
fix(canvas): skip an image stroke until its image is decoded (#1372)
Danila Poyarkov7611e9c65866
diff --git a/changes/1368/fixed.md b/changes/1368/fixed.mdnew file mode 100644index 000000000..c73e50eed--- /dev/null+++ b/changes/1368/fixed.md@@ -0,0 +1 @@+- A gradient stroke on a straight line, or a gradient on any layer with no width or height, draws along it instead of stopping the canvas, as in 0.15.diff --git a/changes/1372/fixed.md b/changes/1372/fixed.mdnew file mode 100644index 000000000..d4897aba8--- /dev/null+++ b/changes/1372/fixed.md@@ -0,0 +1 @@+- An image stroke draws once its image is ready, instead of drawing black while it loads.diff --git a/packages/core/AGENTS.md b/packages/core/AGENTS.mdindex 0a2e341c8..8e5e58c7f 100644--- a/packages/core/AGENTS.md+++ b/packages/core/AGENTS.md@@ -73,7 +73,7 @@ Canvas is CanvasKit (Skia WASM) on a WebGL surface, not DOM.### Paints-- A gradient or image paint builds a Skia shader through `applyGradientFill` and `applyImageFill` (`packages/core/src/canvas/fills.ts`), which take the target `Paint`, so a stroke reuses them instead of a second shader path.+- A gradient or image paint builds a Skia shader through `applyGradientFill` and `applyImageFill` (`packages/core/src/canvas/fills.ts`), which take the target `Paint`, so a stroke reuses them instead of a second shader path. Both say whether they set one; a caller skips the paint when they did not, since CanvasKit builds no gradient from a transform it cannot invert and an image may not be decoded yet (`packages/core/tests/canvas/paints.test.ts`).- `forVisibleStrokes` (`packages/core/src/canvas/scene.ts`) is where a stroke's shader is set and cleared; stroke draw helpers take an already-configured `strokePaint` and must not reset its shader.- Shadows are cast from what a layer draws, children included, through `drawSilhouette` (`packages/core/src/canvas/shadows/silhouette.ts`); a container's shadows are drawn by `renderLayerShadows` around its children, not by `renderShape`. A faster path for a shape must give the same pixels as its silhouette (`packages/core/tests/canvas/shadows.test.ts`).diff --git a/packages/core/src/canvas/fills.ts b/packages/core/src/canvas/fills.tsindex 9334a96c8..d7de4c2e9 100644--- a/packages/core/src/canvas/fills.ts+++ b/packages/core/src/canvas/fills.ts@@ -1,4 +1,4 @@-import type { Canvas, Image, Paint } from 'canvaskit-wasm'+import type { Canvas, Image, Paint, Shader } from 'canvaskit-wasm'import type { SceneNode, SceneGraph, Fill } from '@open-pencil/scene-graph'import { invertGradientTransform } from '@open-pencil/scene-graph/gradient'@@ -147,8 +147,7 @@ export function applyFill(}if (fill.type.startsWith('GRADIENT') && fill.gradientStops && fill.gradientTransform) {- r.applyGradientFill(fill, node, graph)- return true+ return r.applyGradientFill(fill, node, graph)}if (fill.type === 'IMAGE' && fill.imageHash) {@@ -285,7 +284,10 @@ function applyPatternFill(return true}-/** Gradient space to the layer's pixels: Figma's transform maps the other way, so it is inverted. */+/**+ * Gradient space to the layer's pixels: Figma's transform maps the other way, so it is inverted.+ * A side with no length counts as one pixel, so a straight line still carries its gradient.+ */function makeGradientLocalMatrix(r: SkiaRenderer,width: number,@@ -293,7 +295,7 @@ function makeGradientLocalMatrix(transform: NonNullable<Fill['gradientTransform']>) {const t = invertGradientTransform(transform)- return r.ck.Matrix.multiply(r.ck.Matrix.scaled(width, height), [+ return r.ck.Matrix.multiply(r.ck.Matrix.scaled(width || 1, height || 1), [t.m00,t.m01,t.m02,@@ -309,6 +311,11 @@ function makeGradientLocalMatrix(/** Resolves one gradient stop's color, so a stroke resolves its own bindings, not a fill's. */export type ResolveGradientStop = (color: Color, stopIndex: number) => Color+/**+ * Sets `paint`'s shader to `fill`'s gradient on `node` and returns whether it did. CanvasKit+ * builds no shader when the local matrix cannot be inverted, as for a transform that holds no+ * numbers; the paint then keeps no shader, and the caller draws nothing rather than its color.+ */export function applyGradientFill(r: SkiaRenderer,fill: Fill,@@ -322,10 +329,10 @@ export function applyGradientFill(node,graph).color-): void {+): boolean {const stops = fill.gradientStopsconst t = fill.gradientTransform- if (!stops || !t) return+ if (!stops || !t) return falseconst colors = stops.map((s, index) => {const c = resolveStop(s.color, index)return r.ck.Color4f(c.r, c.g, c.b, c.a)@@ -336,12 +343,14 @@ export function applyGradientFill(const h = node.height/** The paint keeps its own reference, so the caller's handle has to go or WASM memory grows. */- const setShader = (shader: ReturnType<typeof makeDiamondGradient>) => {+ const setShader = (shader: Shader | null): boolean => {+ if (!shader) return falsetry {paint.setShader(shader)} finally {shader.delete()}+ return true}if (fill.type === 'GRADIENT_LINEAR') {@@ -355,9 +364,9 @@ export function applyGradientFill(r.ck.TileMode.Clamp,makeGradientLocalMatrix(r, w, h, t))- setShader(shader)+ return setShader(shader)} else if (fill.type === 'GRADIENT_DIAMOND') {- setShader(makeDiamondGradient(r, colors, positions, makeGradientLocalMatrix(r, w, h, t)))+ return setShader(makeDiamondGradient(r, colors, positions, makeGradientLocalMatrix(r, w, h, t)))} else if (fill.type === 'GRADIENT_RADIAL') {// Gradient space is centred on (0.5, 0.5) with radius 0.5; the local matrix takes it to the// layer's pixels.@@ -370,7 +379,7 @@ export function applyGradientFill(r.ck.TileMode.Clamp,localMatrix)- setShader(shader)+ return setShader(shader)} else if (fill.type === 'GRADIENT_ANGULAR') {const localMatrix = makeGradientLocalMatrix(r, w, h, t)const shader = r.ck.Shader.MakeSweepGradient(@@ -381,8 +390,9 @@ export function applyGradientFill(r.ck.TileMode.Clamp,localMatrix)- setShader(shader)+ return setShader(shader)}+ return false}/**diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.tsindex 7f497a4c6..0ff7d070b 100644--- a/packages/core/src/canvas/renderer.ts+++ b/packages/core/src/canvas/renderer.ts@@ -385,7 +385,7 @@ export class SkiaRenderer {fill?: Fill) => voiddeclare applyFill: (fill: Fill, node: SceneNode, graph: SceneGraph, fillIndex?: number) => boolean- declare applyGradientFill: (fill: Fill, node: SceneNode, graph: SceneGraph) => void+ declare applyGradientFill: (fill: Fill, node: SceneNode, graph: SceneGraph) => booleandeclare applyImageFill: (fill: Fill, node: SceneNode, graph: SceneGraph) => booleandeclare drawArc: (canvas: Canvas, node: SceneNode, paint: Paint) => voiddeclare drawNodeStroke: (diff --git a/packages/core/src/canvas/renderer/methods.ts b/packages/core/src/canvas/renderer/methods.tsindex be61c51be..e1fea9549 100644--- a/packages/core/src/canvas/renderer/methods.ts+++ b/packages/core/src/canvas/renderer/methods.ts@@ -280,8 +280,8 @@ const rendererMethods: ThisType<SkiaRenderer> = {return Fills.applyFill(this, fill, node, graph, fillIndex)},- applyGradientFill(fill: Fill, node: SceneNode, graph: SceneGraph): void {- Fills.applyGradientFill(this, fill, node, graph)+ applyGradientFill(fill: Fill, node: SceneNode, graph: SceneGraph): boolean {+ return Fills.applyGradientFill(this, fill, node, graph)},applyImageFill(fill: Fill, node: SceneNode, graph: SceneGraph): boolean {diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.tsindex f4a67b9ec..46e670ba8 100644--- a/packages/core/src/canvas/scene.ts+++ b/packages/core/src/canvas/scene.ts@@ -438,9 +438,9 @@ function forVisibleStrokes(for (let index = 0; index < node.strokes.length; index++) {const stroke = node.strokes[index]if (!stroke.visible) continue- applyStrokeShader(r, stroke, index, node, graph)+ const draws = applyStrokeShader(r, stroke, index, node, graph)try {- draw(stroke, r.resolveStrokeColor(stroke, index, node, graph))+ if (draws) draw(stroke, r.resolveStrokeColor(stroke, index, node, graph))} finally {r.strokePaint.setShader(null)r.fillPaint.setShader(null)diff --git a/packages/core/src/canvas/strokes.ts b/packages/core/src/canvas/strokes.tsindex f34fd6810..4d3473a17 100644--- a/packages/core/src/canvas/strokes.ts+++ b/packages/core/src/canvas/strokes.ts@@ -192,7 +192,9 @@ function fillHead(* A gradient or image stroke paints through a shader, the way the same paint does as a fill.* Both paints get it: a vector stroke's outline and an arrowhead are filled shapes drawn with* `fillPaint`, so the shader has to be on whichever paint the draw helper reaches for.- * Returns false for a solid stroke, which paints through the paint's color instead.+ * Returns whether the stroke draws: a solid one paints through the paint's color, while a gradient+ * CanvasKit cannot build, as for a transform that holds no numbers, or an image not yet decoded is+ * skipped rather than drawn in that color, the way a fill is.*/export function applyStrokeShader(r: SkiaRenderer,@@ -204,33 +206,35 @@ export function applyStrokeShader(r.strokePaint.setShader(null)r.fillPaint.setShader(null)if (stroke.type.startsWith('GRADIENT') && stroke.gradientStops && stroke.gradientTransform) {+ let built = truefor (const paint of [r.strokePaint, r.fillPaint]) {- applyGradientFill(- r,- stroke,- node,- graph,- paint,- // Every stop resolves against this stroke's own binding, not the stop's position.- (color) =>- r.resolveStrokeColorInfo(- { ...stroke, type: 'SOLID', color, opacity: color.a, visible: true },- strokeIndex,- node,- graph- ).color- )+ built =+ applyGradientFill(+ r,+ stroke,+ node,+ graph,+ paint,+ // Every stop resolves against this stroke's own binding, not the stop's position.+ (color) =>+ r.resolveStrokeColorInfo(+ { ...stroke, type: 'SOLID', color, opacity: color.a, visible: true },+ strokeIndex,+ node,+ graph+ ).color+ ) && built}- return true+ return built}if (stroke.type === 'IMAGE' && stroke.imageHash) {- let applied = false+ let applied = truefor (const paint of [r.strokePaint, r.fillPaint]) {- applied = applyImageFill(r, stroke, node, graph, paint) || applied+ applied = applyImageFill(r, stroke, node, graph, paint) && applied}return applied}- return false+ return true}/** Leaves any shader in place: a shaded stroke's color is unused, but its alpha still applies. */diff --git a/packages/core/tests/canvas/paints.test.ts b/packages/core/tests/canvas/paints.test.tsnew file mode 100644index 000000000..d1b43e65d--- /dev/null+++ b/packages/core/tests/canvas/paints.test.ts@@ -0,0 +1,154 @@+import { afterAll, describe, expect, test } from 'bun:test'++import { expectDefined } from '#core-tests/helpers/assert'++import { SkiaRenderer } from '@open-pencil/core'+import { initCanvasKit } from '@open-pencil/core/io'+import { SceneGraph, type Fill, type SceneNode } from '@open-pencil/scene-graph'+import { DEFAULT_GRADIENT_TRANSFORM } from '@open-pencil/scene-graph/gradient'++const WIDTH = 400+const HEIGHT = 40++const ck = await initCanvasKit()+const surface = expectDefined(ck.MakeSurface(WIDTH, HEIGHT), 'surface')+const renderer = new SkiaRenderer(ck, surface)+afterAll(() => renderer.destroy())++const RED = { r: 1, g: 0, b: 0, a: 1 }+const BLUE = { r: 0, g: 0, b: 1, a: 1 }++const gradient: Fill = {+ type: 'GRADIENT_LINEAR',+ color: RED,+ opacity: 1,+ visible: true,+ gradientStops: [+ { position: 0, color: RED },+ { position: 1, color: BLUE }+ ],+ gradientTransform: { ...DEFAULT_GRADIENT_TRANSFORM }+}++/** Draws a rectangle alone on a cleared surface and returns its pixels. */+function draw(fields: Partial<SceneNode>): Uint8Array | Float32Array {+ const graph = new SceneGraph()+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, fields)+ const canvas = surface.getCanvas()+ canvas.clear(ck.TRANSPARENT)+ renderer.renderShape(canvas, node, graph)+ surface.flush()+ const image = surface.makeImageSnapshot()+ try {+ const pixels = expectDefined(+ image.readPixels(0, 0, {+ width: WIDTH,+ height: HEIGHT,+ colorType: ck.ColorType.RGBA_8888,+ alphaType: ck.AlphaType.Unpremul,+ colorSpace: ck.ColorSpace.SRGB+ }),+ 'pixels'+ )+ return pixels+ } finally {+ image.delete()+ }+}++function paintsAnything(fields: Partial<SceneNode>): boolean {+ return draw(fields).some((value, index) => index % 4 === 3 && value > 0)+}++function colorAt(pixels: Uint8Array | Float32Array, x: number, y: number): number[] {+ const offset = (y * WIDTH + x) * 4+ return Array.from(pixels.slice(offset, offset + 4))+}++// A layer with no width or height, such as a straight line, used to scale the gradient's unit+// square to nothing, and a transform that is not a matrix gives NaN. CanvasKit builds no shader+// for either, and the renderer threw on every frame and stopped the canvas.+describe('a gradient on a layer with no height or with a broken transform', () => {+ test('a fill on a layer with no height paints nothing and does not throw', () => {+ expect(paintsAnything({ width: WIDTH, height: 0, fills: [gradient] })).toBe(false)+ })++ test('a stroke runs along a line with no height', () => {+ const leftToRight = { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 }+ const stroke = {+ ...gradient,+ gradientTransform: leftToRight,+ weight: 4,+ align: 'CENTER' as const+ }+ const pixels = draw({ width: WIDTH, height: 0, fills: [], strokes: [stroke], strokeWeight: 4 })+ const [startRed, , startBlue, startAlpha] = colorAt(pixels, 4, 1)+ const [endRed, , endBlue, endAlpha] = colorAt(pixels, WIDTH - 5, 1)+ expect(startAlpha).toBe(255)+ expect(endAlpha).toBe(255)+ expect(startRed).toBeGreaterThan(240)+ expect(startBlue).toBeLessThan(15)+ expect(endBlue).toBeGreaterThan(240)+ expect(endRed).toBeLessThan(15)+ })++ test('draws nothing when its transform holds no numbers, rather than the paint colour', () => {+ const nan = Number.NaN+ const broken: Fill = {+ ...gradient,+ gradientTransform: { m00: nan, m01: nan, m02: nan, m10: nan, m11: nan, m12: nan }+ }+ expect(paintsAnything({ width: WIDTH, height: HEIGHT, fills: [broken] })).toBe(false)+ expect(+ paintsAnything({+ width: WIDTH,+ height: HEIGHT,+ fills: [],+ strokes: [{ ...broken, weight: 4, align: 'CENTER' }],+ strokeWeight: 4+ })+ ).toBe(false)+ })++ test('a solid stroke on a layer with no height still draws', () => {+ const stroke = { type: 'SOLID' as const, color: RED, opacity: 1, visible: true }+ expect(+ paintsAnything({+ width: WIDTH,+ height: 0,+ fills: [],+ strokes: [{ ...stroke, weight: 4, align: 'CENTER' }],+ strokeWeight: 4+ })+ ).toBe(true)+ })+})++// An image paint has no shader until its bytes are decoded. A fill waits for it, and a stroke used+// to draw in its paint's colour meanwhile, black for most imported strokes.+describe('an image paint whose image is not decoded yet', () => {+ const image: Fill = {+ type: 'IMAGE',+ color: { r: 0, g: 0, b: 0, a: 1 },+ opacity: 1,+ visible: true,+ imageHash: 'not-decoded',+ imageScaleMode: 'FILL'+ }++ test('a fill draws nothing', () => {+ expect(paintsAnything({ width: WIDTH, height: HEIGHT, fills: [image] })).toBe(false)+ })++ test('a stroke draws nothing rather than its colour', () => {+ expect(+ paintsAnything({+ width: WIDTH,+ height: HEIGHT,+ fills: [],+ strokes: [{ ...image, weight: 4, align: 'CENTER' }],+ strokeWeight: 4+ })+ ).toBe(false)+ })+})diff --git a/tests/e2e/canvas/stroke-paint-visual.spec.ts b/tests/e2e/canvas/stroke-paint-visual.spec.tsindex cb01547cd..d38e62fb7 100644--- a/tests/e2e/canvas/stroke-paint-visual.spec.ts+++ b/tests/e2e/canvas/stroke-paint-visual.spec.ts@@ -1,8 +1,10 @@import type { StrokeCap } from '@open-pencil/scene-graph'import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'+import { strokeSceneDriver } from '#tests/helpers/canvas/strokes/driver'const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')+const gradientLines = strokeSceneDriver(() => editor.page, 'createGradientLinesScene')/*** A gradient stroke used to draw opaque black, because a stroke could only hold one color.@@ -116,3 +118,17 @@ test('gradient strokes', async () => {const buffer = await editor.canvas.screenshotCanvasRegion()expect(buffer).toMatchSnapshot('gradient-strokes.png')})++// A line has no height, or no width, and a gradient placed on a layer of that size used to throw+// on every frame. The gradient's colours along a line are checked in+// packages/core/tests/canvas/paints.test.ts. A line is a small share of the picture, so the+// pixel budget is tighter than the default 1%, which would let a vanished line pass.+test('gradient strokes on straight lines', async () => {+ await gradientLines.show()+ await editor.canvas.waitForRender()+ editor.canvas.assertNoErrors()+ expect(await editor.canvas.screenshotCanvasRegion(500, 460)).toMatchSnapshot(+ 'gradient-lines.png',+ { threshold: 0.05, maxDiffPixelRatio: 0.002 }+ )+})diff --git a/tests/e2e/canvas/stroke-paint-visual.spec.ts-snapshots/gradient-lines-openpencil-darwin.png b/tests/e2e/canvas/stroke-paint-visual.spec.ts-snapshots/gradient-lines-openpencil-darwin.pngnew file mode 100644index 000000000..9cec82b3cBinary files /dev/null and b/tests/e2e/canvas/stroke-paint-visual.spec.ts-snapshots/gradient-lines-openpencil-darwin.png differdiff --git a/tests/helpers/canvas/strokes/scene.ts b/tests/helpers/canvas/strokes/scene.tsindex 52e6ea7f6..6d90f9382 100644--- a/tests/helpers/canvas/strokes/scene.ts+++ b/tests/helpers/canvas/strokes/scene.ts@@ -1,4 +1,8 @@-import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'+import type { GradientTransform, SceneGraph, SceneNode, Stroke } from '@open-pencil/scene-graph'+import {+ DEFAULT_GRADIENT_TRANSFORM,+ IDENTITY_GRADIENT_TRANSFORM+} from '@open-pencil/scene-graph/gradient'const ALIGNS = ['INSIDE', 'CENTER', 'OUTSIDE'] as constconst CORNERS = [@@ -120,3 +124,52 @@ export function createDashEndsScene(graph: SceneGraph, pageId: string): void {})}}++const RED = { r: 1, g: 0, b: 0, a: 1 }+const BLUE = { r: 0, g: 0, b: 1, a: 1 }++function gradientStroke(gradientTransform: GradientTransform): Stroke {+ return {+ type: 'GRADIENT_LINEAR',+ color: RED,+ opacity: 1,+ visible: true,+ weight: 6,+ align: 'CENTER',+ gradientStops: [+ { position: 0, color: RED },+ { position: 1, color: BLUE }+ ],+ gradientTransform+ }+}++/**+ * A solid rectangle as a landmark, and three straight lines with a red to blue gradient stroke: a+ * horizontal one with no height, a vertical one with no width and Figma's default top to bottom+ * transform, and a rotated one. Each runs red at its start to blue at its end.+ */+export function createGradientLinesScene(graph: SceneGraph, pageId: string): void {+ graph.createNode('RECTANGLE', pageId, {+ name: 'landmark',+ x: 60,+ y: 60,+ width: 120,+ height: 80,+ fills: [{ type: 'SOLID', color: { r: 0.2, g: 0.7, b: 0.4, a: 1 }, opacity: 1, visible: true }]+ })+ const lines: Array<[string, Partial<SceneNode>, GradientTransform]> = [+ ['horizontal', { x: 60, y: 200, width: 300, height: 0 }, IDENTITY_GRADIENT_TRANSFORM],+ ['vertical', { x: 460, y: 60, width: 0, height: 200 }, DEFAULT_GRADIENT_TRANSFORM],+ ['rotated', { x: 60, y: 280, width: 300, height: 0, rotation: 30 }, IDENTITY_GRADIENT_TRANSFORM]+ ]+ for (const [name, placement, transform] of lines) {+ graph.createNode('LINE', pageId, {+ name: `${name} line`,+ fills: [],+ strokes: [gradientStroke(transform)],+ strokeWeight: 6,+ ...placement+ })+ }+}