moved gettingSnapshot logic into TexturesHelper
This commit is contained in:
parent
8eaacdf2e5
commit
bbe539b785
3 changed files with 49 additions and 43 deletions
34
front/src/Phaser/Helpers/TexturesHelper.ts
Normal file
34
front/src/Phaser/Helpers/TexturesHelper.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
export class TexturesHelper {
|
||||
public static async getSnapshot(
|
||||
scene: Phaser.Scene,
|
||||
...sprites: { sprite: Phaser.GameObjects.Sprite; frame?: string | number }[]
|
||||
): Promise<string> {
|
||||
const rt = scene.make.renderTexture({}, false);
|
||||
try {
|
||||
for (const { sprite, frame } of sprites) {
|
||||
if (frame) {
|
||||
sprite.setFrame(frame);
|
||||
}
|
||||
rt.draw(sprite, sprite.displayWidth * 0.5, sprite.displayHeight * 0.5);
|
||||
}
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
try {
|
||||
rt.snapshot(
|
||||
(url) => {
|
||||
resolve((url as HTMLImageElement).src);
|
||||
rt.destroy();
|
||||
},
|
||||
"image/png",
|
||||
1
|
||||
);
|
||||
} catch (error) {
|
||||
rt.destroy();
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
rt.destroy();
|
||||
throw new Error("Could not get the snapshot");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue