From 37c8200bcb6bee745f879f7867e2fdb9049d9af1 Mon Sep 17 00:00:00 2001 From: Weiming Liao Date: Fri, 4 Dec 2026 02:46:57 +0800 Subject: [PATCH] [bugfix] canvas: preserve PNG transparency over other nodes Signed-off-by: Weiming Liao --- src/canvas/nodes.css | 4 ++ tests/browser/canvas_png_alpha.mjs | 96 ++++++++++++++++++++++++++++++ tests/browser/manifest.json | 5 ++ 3 files changed, 105 insertions(+) create mode 100644 tests/browser/canvas_png_alpha.mjs diff --git a/src/canvas/nodes.css b/src/canvas/nodes.css index 5fb515553..03516a2a4 100644 --- a/src/canvas/nodes.css +++ b/src/canvas/nodes.css @@ -96,6 +96,10 @@ overrides this so a new node does not read as a hole in the canvas. */ background: var(--cw-media-well, #202124); } +.cw-node.cw-node-image:has(> .cw-node-media > .cw-image-preview), +.cw-node-image > .cw-node-media { + background: transparent; +} .cw-image-preview, .cw-video-preview { display: block; diff --git a/tests/browser/canvas_png_alpha.mjs b/tests/browser/canvas_png_alpha.mjs new file mode 100644 index 000000000..afe8eacdf --- /dev/null +++ b/tests/browser/canvas_png_alpha.mjs @@ -0,0 +1,96 @@ +import assert from "node:assert/strict"; +import { launchBrowser } from "./engine.mjs"; + +const base = process.env.CREATION_WEB_URL ?? "http://127.0.0.1:5179"; +const health = await fetch(`${base}/api/creation-test-health`, { + headers: { "X-OpenDirector-Request": "1" }, +}); +assert.ok(health.ok && (await health.json()).isolated, "Requires an isolated creation API"); +const browser = await launchBrowser(); + +try { + const page = await browser.newPage({ locale: "en-US", viewport: { width: 1360, height: 940 }, deviceScaleFactor: 1 }); + await page.addInitScript(() => localStorage.setItem("opendirector.modelSetup.v1", "dismissed")); + await page.route((url) => url.pathname === "/", (route) => + route.fulfill({ contentType: "text/html", body: "" })); + await page.goto(`${base}/`); + const fixture = await page.evaluate(async () => { + const { createDocument, createNode } = await import("/src/canvas/model.ts"); + const { importWorkspaceDocument } = await import("/src/backend/workspace.ts"); + const { creationClient } = await import("/src/backend/creationClient.ts"); + const { releaseBackendProject } = await import("/src/backend/projects.ts"); + function picture(overlay) { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 100; + const context = canvas.getContext("2d"); + if (overlay) { + context.fillStyle = "rgba(255,0,0,0.5)"; + context.fillRect(0, 20, 100, 25); + context.fillStyle = "#fff"; + context.fillRect(0, 60, 100, 35); + } else { + context.fillStyle = "#00dc90"; + context.fillRect(0, 0, 100, 100); + } + return canvas.toDataURL("image/png"); + } + const doc = createDocument(`PNG alpha ${crypto.randomUUID().slice(0, 8)}`); + const image = (title, x, src) => createNode("image", { x, y: 180 }, { + title, mediaRole: "asset", src, mimeType: "image/png", assetState: "ready", + }); + const underlay = image("Opaque image", 150, picture(false)); + const overlay = image("Transparent PNG", 225, picture(true)); + const empty = createNode("image", { x: 690, y: 180 }, { title: "Empty image" }); + doc.nodes = [underlay, overlay, empty]; + const imported = await importWorkspaceDocument(doc); + await creationClient(imported.id).view({ viewport: { x: 0, y: 0, zoom: 1 } }); + await releaseBackendProject(); + return { id: imported.id, underlay: underlay.id, overlay: overlay.id, empty: empty.id }; + }); + await page.goto(`${base}/ai-tool/ai-canvas/${fixture.id}`); + const frame = (id) => page.locator(`.react-flow__node[data-id="${id}"] .cw-node`); + const picture = (id) => frame(id).locator(".cw-image-preview"); + const sampleAt = (box, x, y) => [Math.round(box.x + box.width * x), Math.round(box.y + box.height * y)]; + for (const theme of ["dark", "light"]) { + await page.evaluate((value) => { document.documentElement.dataset.theme = value; }, theme); + for (const image of [picture(fixture.underlay), picture(fixture.overlay)]) { + await image.waitFor(); + await image.evaluate((element) => element.decode()); + } + const underlay = await picture(fixture.underlay).boundingBox(); + const overlay = await picture(fixture.overlay).boundingBox(); + const points = [ + sampleAt(underlay, 0.1, 0.12), + sampleAt(overlay, 0.12, 0.12), + sampleAt(overlay, 0.34, 0.79), + sampleAt(overlay, 0.12, 0.3), + ]; + const screenshot = await page.screenshot({ + path: process.env.CANVAS_ALPHA_SCREENSHOT + ? `${process.env.CANVAS_ALPHA_SCREENSHOT}-${theme}.png` + : undefined, + }); + const [plain, transparent, opaque, translucent] = await page.evaluate(async ({ png, points }) => { + const captured = new Image(); + captured.src = png; + await captured.decode(); + const canvas = document.createElement("canvas"); + canvas.width = captured.width; + canvas.height = captured.height; + const context = canvas.getContext("2d"); + context.drawImage(captured, 0, 0); + return points.map(([x, y]) => Array.from(context.getImageData(x, y, 1, 1).data)); + }, { png: `data:image/png;base64,${screenshot.toString("base64")}`, points }); + console.log(theme, { plain, transparent, opaque, translucent }); + assert.deepEqual(plain, [0, 220, 144, 255]); + assert.deepEqual(transparent, plain, `${theme}: the lower image should be visible through transparent pixels`); + assert.deepEqual(opaque, [255, 255, 255, 255], `${theme}: opaque pixels should cover the lower image`); + assert.ok( + translucent.every((channel, index) => Math.abs(channel - [128, 110, 72, 255][index]) <= 2), + `${theme}: red at half opacity should blend with the lower image`, + ); + assert.match(await frame(fixture.empty).evaluate((node) => getComputedStyle(node).backgroundImage), /linear-gradient/); + } +} finally { + await browser.close(); +} diff --git a/tests/browser/manifest.json b/tests/browser/manifest.json index b195c1ff4..29b719a17 100644 --- a/tests/browser/manifest.json +++ b/tests/browser/manifest.json @@ -255,6 +255,11 @@ "gate" ] }, + { + "file": "canvas_png_alpha.mjs", + "lanes": ["extended"], + "note": "Checks transparent, translucent and opaque canvas image pixels over another node in both themes. Seeds projects through /src on Vite." + }, { "file": "canvas_progressive_load.mjs", "lanes": ["gate"],