Skip to content

Commit

Permalink
Add capture image
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-github committed Aug 1, 2024
1 parent d4314f3 commit 637f574
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/components/ExtraContent/extraContentItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,46 @@ const ExtraContentItem = ({
}
}

const captureImage = useCallback(() => {
if (type === "camera" || type === "image") {
const image = document.getElementById(element_id);
if (image && image.complete) {
const canvas = document.createElement('canvas');
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);

canvas.toBlob((blob) => {
const typeImage = type === "camera" ? "image/jpeg" : blob.type;
const filename = `snap.${typeImage.split("/")[1]}`;

if (window.navigator.msSaveOrOpenBlob) {
// For IE10+
window.navigator.msSaveOrOpenBlob(blob, filename);
} else {
// For modern browsers
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();

// Clean up
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
}
}, type === "camera" ? "image/jpeg" : "image/png");
} else {
console.error("Image not loaded or not found");
}
}
}, [type, element_id]);


const renderContent = useMemo(() => {
if (isLoading && type !== "image" && type !== "camera") {
Expand Down Expand Up @@ -193,9 +233,7 @@ const ExtraContentItem = ({
tooltip
data-tooltip={T("S186")}
icon={<Aperture />}
onclick={() => {
// Handle screenshot functionality here
}}
onclick={captureImage}
/>
)}
{parseInt(refreshtime) > 0 && type !== "extension" && (
Expand Down

0 comments on commit 637f574

Please sign in to comment.