Skip to content

Commit

Permalink
fix: fix exceptional canvas options when init in node env (#5788)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored May 30, 2024
1 parent 04239f8 commit 0982c1e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/g6/__tests__/utils/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ export function createGraphCanvas(
container.style.height = `${height}px`;

resetEntityCounter();

let extraOptions = {};

if (globalThis.process) {
const offscreenNodeCanvas = {
getContext: () => context,
} as unknown as HTMLCanvasElement;
const context = new OffscreenCanvasContext(offscreenNodeCanvas);
// 下列参数仅在 node 环境下需要传入 / These parameters only need to be passed in the node environment
extraOptions = {
document: container.ownerDocument,
offscreenCanvas: offscreenNodeCanvas,
};
}

const offscreenNodeCanvas = {
getContext: () => context,
} as unknown as HTMLCanvasElement;
Expand All @@ -51,9 +66,7 @@ export function createGraphCanvas(
width,
height,
renderer: () => instance,
// @ts-expect-error document offscreenCanvas is not in the type definition
document: container.ownerDocument,
offscreenCanvas: offscreenNodeCanvas,
...extraOptions,
});
}

Expand Down

0 comments on commit 0982c1e

Please sign in to comment.