Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved screenshots #2340

Merged
merged 1 commit into from
Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/renderer/components/ReactFlowBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,6 @@ export const ReactFlowBox = memo(({ wrapperRef, nodeTypes, edgeTypes }: ReactFlo

return (
<Box
bg="var(--chain-editor-bg)"
borderRadius="lg"
borderWidth="0px"
className={animateChain ? '' : 'no-chain-animation'}
h="100%"
ref={wrapperRef}
Expand All @@ -452,6 +449,7 @@ export const ReactFlowBox = memo(({ wrapperRef, nodeTypes, edgeTypes }: ReactFlo
style={{
zIndex: 0,
borderRadius: '0.5rem',
backgroundColor: 'var(--chain-editor-bg)',
}}
onConnect={createConnection}
onConnectEnd={onConnectStop}
Expand Down
20 changes: 13 additions & 7 deletions src/renderer/helpers/nodeScreenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const takeScreenshot = async (
): Promise<PngDataUrl> => {
const oldViewport = reactFlow.getViewport();

const rfElement = (currentFlowWrapper.firstElementChild ?? currentFlowWrapper) as HTMLElement;
const oldBorderRadius = rfElement.style.borderRadius;

const reactFlowViewport = currentFlowWrapper.getBoundingClientRect();
const nodes = reactFlow.getNodes();
const nodesBoundingBox = getNodesBoundingBox(nodes) ?? { x: 0, y: 0, width: 0, height: 0 };
Expand All @@ -52,17 +55,18 @@ export const takeScreenshot = async (
reactFlowViewport.height / paddedBoundingBox.height
);

reactFlow.setViewport({
x: paddedBoundingBox.x * -1 * exportZoom,
y: paddedBoundingBox.y * -1 * exportZoom,
zoom: exportZoom,
});

try {
reactFlow.setViewport({
x: paddedBoundingBox.x * -1 * exportZoom,
y: paddedBoundingBox.y * -1 * exportZoom,
zoom: exportZoom,
});
rfElement.style.borderRadius = '0';

// wait for the viewport to be updated
await delay(10);

const dataUrl = await toPng(currentFlowWrapper, {
const dataUrl = await toPng(rfElement, {
style: {
padding: '0',
margin: '0',
Expand All @@ -71,6 +75,7 @@ export const takeScreenshot = async (
pixelRatio: 1 / exportZoom,
width: paddedBoundingBox.width * exportZoom,
height: paddedBoundingBox.height * exportZoom,
backgroundColor: getComputedStyle(rfElement).backgroundColor,
filter: (node: unknown) => {
if (
node instanceof HTMLElement &&
Expand All @@ -86,6 +91,7 @@ export const takeScreenshot = async (
return dataUrl as PngDataUrl;
} finally {
reactFlow.setViewport(oldViewport);
rfElement.style.borderRadius = oldBorderRadius;
}
};

Expand Down