I am new to figma plugins. I am createing a figma plugin using plain html and javascript. What I want to do is to capture a snapshot of figma design on screen. And Convert it to png/jpg to store it on s3 Bucket.
What I tried
try {
const viewport = figma.viewport;
const image = await figma.viewport.snapshotAsync({
format: 'PNG',
viewport,
});
console.log("Snapshot captured:", image);
} catch (error) {
console.error("An error occurred:", error);
}
In above I just getting an error message " not a function" nothing else.
Thanks in advance. Note: I forgot to mention I am using figma windows applicaiton for development purpose.
This answer heavily inspires from an article written by James Bugherd that can be found at the time of this writing here: https://hackernoon.com/how-to-take-screenshots-in-the-browser-using-javascript-l92k3xq7
According to this SO answer, you can take a screenshot using
html2canvasviasee https://hackernoon.com/how-to-take-screenshots-in-the-browser-using-javascript-l92k3xq7
The idea is to get the structure from
document.body, pass it tohtml2canvasand then convert it into a base64 image in the callback which can then be used as you please.However,
html2canvasis not perfect in implementing CSS rules and external sources are also possibly quite blocked due to CORS.Alternatively you can use
navigator.mediaDevices.getDisplayMedia():A third possibility is to create a screenshot as a service:
You can return the result as a response to the request, see https://www.url2png.com/docs?ref=hackernoon.com
If you create a service at your server and either send to it your figma resource via your Figma plugin (be careful of CORS headers and CSP), or use your service to query Figma. Such a service is not necessary to take a screenshot of the content it receives (and which is not actually displayed in the screen), so, alternatively you can use the GET image API function Figme provides.