Files
GDevelop/Extensions/Screenshot/screenshottools.ts
2022-09-29 17:19:04 +02:00

40 lines
1.2 KiB
TypeScript

namespace gdjs {
export namespace screenshot {
const logger = new gdjs.Logger('Screenshot');
/**
* Save a screenshot of the game.
* @param instanceContainer The scene
* @param savePath The path where to save the screenshot
*/
export const takeScreenshot = function (
instanceContainer: gdjs.RuntimeInstanceContainer,
savePath: string
) {
const fs = typeof require !== 'undefined' ? require('fs') : null;
if (fs) {
const canvas = instanceContainer.getGame().getRenderer().getCanvas();
if (canvas) {
const content = canvas
.toDataURL('image/png')
.replace('data:image/png;base64,', '');
if (savePath.toLowerCase().indexOf('.png') == -1) {
savePath += '.png';
}
fs.writeFile(savePath, content, 'base64', (err) => {
if (err) {
logger.error(
'Unable to save the screenshot at path: ' + savePath
);
}
});
} else {
logger.error(
'Screenshot are not supported on rendering engines without canvas.'
);
}
}
};
}
}