David Catuhe 4 năm trước cách đây
mục cha
commit
9a709bef52
1 tập tin đã thay đổi với 30 bổ sung2 xóa
  1. 30 2
      src/Misc/screenshotTools.ts

+ 30 - 2
src/Misc/screenshotTools.ts

@@ -30,8 +30,9 @@ export class ScreenshotTools {
      * src parameter of an <img> to display it
      * @param mimeType defines the MIME type of the screenshot image (default: image/png).
      * Check your browser for supported MIME types
+     * @param forceDownload force the system to download the image even if a successCallback is provided
      */
-    public static CreateScreenshot(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
+    public static CreateScreenshot(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png", forceDownload = false): void {
         const { height, width } = ScreenshotTools._getScreenshotSize(engine, camera, size);
 
         if (!(height && width)) {
@@ -65,7 +66,14 @@ export class ScreenshotTools {
                 renderContext.drawImage(renderingCanvas, offsetX, offsetY, newWidth, newHeight);
             }
 
-            Tools.EncodeScreenshotCanvasData(successCallback, mimeType);
+            if (forceDownload) {
+                Tools.EncodeScreenshotCanvasData(undefined, mimeType);
+                if (successCallback) {
+                    successCallback("");
+                }
+            } else {
+                Tools.EncodeScreenshotCanvasData(successCallback, mimeType);
+            }
         });
     }
 
@@ -97,6 +105,26 @@ export class ScreenshotTools {
     }
 
     /**
+     * Captures a screenshot of the current rendering for a specific size. This will render the entire canvas but will generate a blink (due to canvas resize)
+     * @see https://doc.babylonjs.com/how_to/render_scene_on_a_png
+     * @param engine defines the rendering engine
+     * @param camera defines the source camera
+     * @param width defines the expected width
+     * @param height defines the expected height
+     * @param mimeType defines the MIME type of the screenshot image (default: image/png).
+     * Check your browser for supported MIME types
+     * @returns screenshot as a string of base64-encoded characters. This string can be assigned
+     * to the src parameter of an <img> to display it
+     */
+    public static CreateScreenshotWithResizeAsync(engine: Engine, camera: Camera, width: number, height: number, mimeType: string = "image/png"): Promise<void> {
+        return new Promise((resolve, reject) => {
+            ScreenshotTools.CreateScreenshot(engine, camera, { width: width, height: height }, () => {
+                resolve();
+            }, mimeType, true);
+        });
+    }
+
+    /**
      * Generates an image screenshot from the specified camera.
      * @see https://doc.babylonjs.com/how_to/render_scene_on_a_png
      * @param engine The engine to use for rendering