# All files RNViewShot.web.ts

95.45% Statements 21/22
92.85% Branches 13/14
100% Functions 3/3
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    621x

    9x
    1x

    9x

    9x

    9x

    1x
    1x
    1x

    1x
    1x
    1x

    1x

    9x
    9x
    9x
    8x
    1x

    1x

    1x

    1x

     import html2canvas from "html2canvas";
    import type {CaptureOptions} from "./index";

    async function captureRef(
      view: HTMLElement,
      options: CaptureOptions,
    ): Promise<string> {
      if (options.result === "tmpfile") {
        console.warn(
          "Tmpfile is not implemented for web. Try base64 or file.\n" +
            "For compatibility, it currently returns the same result as data-uri",
        );
      }

      // TODO: implement snapshotContentContainer option

      const h2cOptions = {
        useCORS: true,
      };
      let renderedCanvas = await html2canvas(view, h2cOptions);

      if (options.width && options.height) {
        // Resize result
        const resizedCanvas = document.createElement("canvas");
        const resizedContext = resizedCanvas.getContext("2d");
        Iif (!resizedContext) {
          throw new Error("Failed to get 2d context from canvas");
        }
        resizedCanvas.height = options.height;
        resizedCanvas.width = options.width;
        resizedContext.drawImage(
          renderedCanvas,
          0,
          0,
          resizedCanvas.width,
          resizedCanvas.height,
        );
        renderedCanvas = resizedCanvas;
      }

      const mimeType =
        "image/" + (options.format === "jpg" ? "jpeg" : options.format);
      const dataUrl = renderedCanvas.toDataURL(mimeType, options.quality);
      if (options.result === "data-uri" || options.result === "tmpfile")
        return dataUrl;
      return dataUrl.replace(/data:image\/(\w+);base64,/, "");
    }

    function captureScreen(options: CaptureOptions): Promise<string> {
      return captureRef(window.document.body, options);
    }

    function releaseCapture(_uri: string): void {
      throw new Error("Tmpfile is not implemented for web. Try base64 or file");
    }

    export default {
      captureRef,
      captureScreen,
      releaseCapture,
    };