I am working on a plugin, that clones some layers and after that I export them as images.
Sometimes I get blank image and only setTimeout helps with it. But maybe there’s more elegant method?
let nodeToClone:any = layerData.layerData;
const bytes = await nodeToClone.exportAsync({
format: 'PNG',
constraint: { type: 'SCALE', value: 1 },
})
const image = figma.createImage(bytes)
const frame = figma.createFrame()
frame.x = layerData.frameX;
frame.y = layerData.frameY;
frame.resize(layerData.width, layerData.height);
const node = figma.createRectangle()
frame.appendChild(node);
const { width, height } = await image.getSizeAsync()
node.resize(width, height);
node.x = nodeToClone.absoluteBoundingBox.x - frame.x
node.y = nodeToClone.absoluteBoundingBox.y - frame.y
node.fills = [
{
type: 'IMAGE',
imageHash: image.hash,
scaleMode: 'FILL'
}
];
setTimeout(function() {
exportImage(frame,layerData);
},150); //THIS CAN EXPORT BLANK IMAGE
async function exportImage(frame:any, layerData:any){
let json = await frame.exportAsync({
format: "PNG",
constraint: { type: 'SCALE', value: 1 }
})
// and here sometimes output is empty image
}