浏览代码

ScreenshotTools. Implementation usage of _getScreenshotSize() method

> https://forum.babylonjs.com/t/define-typescript-interface-for-options-of-method-to-make-screenshots/4713
Dok11 6 年之前
父节点
当前提交
4a6a7bb0fe
共有 1 个文件被更改,包括 5 次插入75 次删除
  1. 5 75
      src/Misc/screenshotTools.ts

+ 5 - 75
src/Misc/screenshotTools.ts

@@ -32,41 +32,9 @@ export class ScreenshotTools {
      * Check your browser for supported MIME types
      */
     public static CreateScreenshot(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
-        let width = 0;
-        let height = 0;
+        const { height, width } = ScreenshotTools._getScreenshotSize(engine, camera, size);
 
-        //If a size value defined as object
-        if (typeof(size) === 'object') {
-            // If a precision value is specified
-            if (size.precision) {
-                width = Math.round(engine.getRenderWidth() * size.precision);
-                height = Math.round(width / engine.getAspectRatio(camera));
-            }
-            else if (size.width && size.height) {
-                width = size.width;
-                height = size.height;
-            }
-            //If passing only width, computing height to keep display canvas ratio.
-            else if (size.width && !size.height) {
-                width = size.width;
-                height = Math.round(width / engine.getAspectRatio(camera));
-            }
-            //If passing only height, computing width to keep display canvas ratio.
-            else if (size.height && !size.width) {
-                height = size.height;
-                width = Math.round(height * engine.getAspectRatio(camera));
-            }
-            else {
-                width = Math.round(engine.getRenderWidth());
-                height = Math.round(width / engine.getAspectRatio(camera));
-            }
-        }
-        //Assuming here that "size" parameter is a number
-        else if (!isNaN(size)) {
-            height = size;
-            width = size;
-        }
-        else {
+        if (!(height && width)) {
             Logger.Error("Invalid 'size' parameter !");
             return;
         }
@@ -146,48 +114,10 @@ export class ScreenshotTools {
      * @param fileName A name for for the downloaded file.
      */
     public static CreateScreenshotUsingRenderTarget(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): void {
-        let width = 0;
-        let height = 0;
-        let targetTextureSize: number | { width: number, height: number } = 0;
-
-        //If a size value defined as object
-        if (typeof(size) === 'object') {
-            //If a precision value is specified
-            if (size.precision) {
-                width = Math.round(engine.getRenderWidth() * size.precision);
-                height = Math.round(width / engine.getAspectRatio(camera));
-                size = { width: width, height: height };
-            }
-            else if (size.width && size.height) {
-                width = size.width;
-                height = size.height;
-            }
-            //If passing only width, computing height to keep display canvas ratio.
-            else if (size.width && !size.height) {
-                width = size.width;
-                height = Math.round(width / engine.getAspectRatio(camera));
-                size = { width: width, height: height };
-            }
-            //If passing only height, computing width to keep display canvas ratio.
-            else if (size.height && !size.width) {
-                height = size.height;
-                width = Math.round(height * engine.getAspectRatio(camera));
-                size = { width: width, height: height };
-            }
-            else {
-                width = Math.round(engine.getRenderWidth());
-                height = Math.round(width / engine.getAspectRatio(camera));
-            }
+        const { height, width } = ScreenshotTools._getScreenshotSize(engine, camera, size);
+        let targetTextureSize = { width, height };
 
-            targetTextureSize = {width, height};
-        }
-        //Assuming here that "size" parameter is a number
-        else if (!isNaN(size)) {
-            height = size;
-            width = size;
-            targetTextureSize = size;
-        }
-        else {
+        if (!(height && width)) {
             Logger.Error("Invalid 'size' parameter !");
             return;
         }