MCPcopy Index your code
hub / github.com/bernaferrari/FigmaToCode / exportNodeAsBase64PNG

Function exportNodeAsBase64PNG

packages/backend/src/common/images.ts:85–133  ·  view source on GitHub ↗
(
  node: AltNode<T>,
  excludeChildren: boolean,
)

Source from the content-addressed store, hash-verified

83};
84
85export const exportNodeAsBase64PNG = async <T extends ExportableNode>(
86 node: AltNode<T>,
87 excludeChildren: boolean,
88) => {
89 // Shorcut export if the node has already been converted.
90 if (node.base64 !== undefined && node.base64 !== "") {
91 return node.base64;
92 }
93
94 const n: ExportableNode = node;
95
96 const temporarilyHideChildren =
97 excludeChildren && "children" in n && n.children.length > 0;
98 const parent = n as ChildrenMixin;
99 const originalVisibility = new Map<SceneNode, boolean>();
100
101 if (temporarilyHideChildren) {
102 // Store the original visible state of children
103 parent.children.map((child: SceneNode) =>
104 originalVisibility.set(child, child.visible),
105 ),
106 // Temporarily hide all children
107 parent.children.forEach((child) => {
108 child.visible = false;
109 });
110 }
111
112 // export the image as bytes
113 const exportSettings: ExportSettingsImage = {
114 format: "PNG",
115 constraint: { type: "SCALE", value: 1 },
116 };
117 const bytes = await exportAsyncProxy(n, exportSettings);
118
119 if (temporarilyHideChildren) {
120 // After export, restore visibility
121 parent.children.forEach((child) => {
122 child.visible = originalVisibility.get(child) ?? false;
123 });
124 }
125
126 addWarning("Some images exported as Base64 PNG");
127
128 // Encode binary string to base64
129 const base64 = imageBytesToBase64(bytes);
130 // Save the value so it's only calculated once.
131 node.base64 = base64;
132 return base64;
133};

Callers 1

htmlContainerFunction · 0.90

Calls 3

exportAsyncProxyFunction · 0.90
addWarningFunction · 0.90
imageBytesToBase64Function · 0.85

Tested by

no test coverage detected