From da7b8f6e27b06f1b9c95f87190118c2ad70b4cfd Mon Sep 17 00:00:00 2001 From: Mike Hamilton Date: Wed, 12 Feb 2025 15:26:40 +0100 Subject: [PATCH 1/2] Fix: releaseCapture throwing error about tmpfile not implemented on web even when using base64 or file result --- src/RNViewShot.web.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/RNViewShot.web.js b/src/RNViewShot.web.js index 135ec8be..4c62739c 100644 --- a/src/RNViewShot.web.js +++ b/src/RNViewShot.web.js @@ -3,8 +3,10 @@ import html2canvas from "html2canvas"; async function captureRef(view, options) { 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"); + 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 @@ -14,17 +16,27 @@ async function captureRef(view, options) { if (options.width && options.height) { // Resize result - const resizedCanvas = document.createElement('canvas'); - const resizedContext = resizedCanvas.getContext('2d'); + const resizedCanvas = document.createElement("canvas"); + const resizedContext = resizedCanvas.getContext("2d"); resizedCanvas.height = options.height; resizedCanvas.width = options.width; - resizedContext.drawImage(renderedCanvas, 0, 0, resizedCanvas.width, resizedCanvas.height); + resizedContext.drawImage( + renderedCanvas, + 0, + 0, + resizedCanvas.width, + resizedCanvas.height + ); renderedCanvas = resizedCanvas; } - const dataUrl = renderedCanvas.toDataURL("image/" + options.format, options.quality); - if (options.result === "data-uri" || options.result === "tmpfile") return dataUrl; - return dataUrl.replace(/data:image\/(\w+);base64,/, ''); + const dataUrl = renderedCanvas.toDataURL( + "image/" + options.format, + options.quality + ); + if (options.result === "data-uri" || options.result === "tmpfile") + return dataUrl; + return dataUrl.replace(/data:image\/(\w+);base64,/, ""); } function captureScreen(options) { @@ -32,11 +44,13 @@ function captureScreen(options) { } function releaseCapture(uri) { - throw new Error("Tmpfile is not implemented for web. Try base64 or file"); + if (options.result === "tmpfile") { + throw new Error("Tmpfile is not implemented for web. Try base64 or file"); + } } export default { captureRef, captureScreen, - releaseCapture -} \ No newline at end of file + releaseCapture, +}; From ac6347b44235a2a65e180df6c180e3aea6b74fe6 Mon Sep 17 00:00:00 2001 From: Mike Hamilton Date: Wed, 12 Feb 2025 15:34:32 +0100 Subject: [PATCH 2/2] Fix: don't reformat file --- src/RNViewShot.web.js | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/RNViewShot.web.js b/src/RNViewShot.web.js index 4c62739c..961f5b2d 100644 --- a/src/RNViewShot.web.js +++ b/src/RNViewShot.web.js @@ -3,10 +3,8 @@ import html2canvas from "html2canvas"; async function captureRef(view, options) { 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" - ); + 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 @@ -16,27 +14,17 @@ async function captureRef(view, options) { if (options.width && options.height) { // Resize result - const resizedCanvas = document.createElement("canvas"); - const resizedContext = resizedCanvas.getContext("2d"); + const resizedCanvas = document.createElement('canvas'); + const resizedContext = resizedCanvas.getContext('2d'); resizedCanvas.height = options.height; resizedCanvas.width = options.width; - resizedContext.drawImage( - renderedCanvas, - 0, - 0, - resizedCanvas.width, - resizedCanvas.height - ); + resizedContext.drawImage(renderedCanvas, 0, 0, resizedCanvas.width, resizedCanvas.height); renderedCanvas = resizedCanvas; } - const dataUrl = renderedCanvas.toDataURL( - "image/" + options.format, - options.quality - ); - if (options.result === "data-uri" || options.result === "tmpfile") - return dataUrl; - return dataUrl.replace(/data:image\/(\w+);base64,/, ""); + const dataUrl = renderedCanvas.toDataURL("image/" + options.format, options.quality); + if (options.result === "data-uri" || options.result === "tmpfile") return dataUrl; + return dataUrl.replace(/data:image\/(\w+);base64,/, ''); } function captureScreen(options) { @@ -52,5 +40,5 @@ function releaseCapture(uri) { export default { captureRef, captureScreen, - releaseCapture, -}; + releaseCapture +} \ No newline at end of file