Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/canvas/nodes.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
96 changes: 96 additions & 0 deletions tests/browser/canvas_png_alpha.mjs
Original file line number Diff line number Diff line change
@@ -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: "<html><body></body></html>" }));
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();
}
5 changes: 5 additions & 0 deletions tests/browser/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Loading