Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  121] [ 2]  / answers: 1 / hits: 5935  / 2 Years ago, sun, february 20, 2022, 12:00:00

I'm using use-react-screenshot to take a screenshot of a specific dom element, but the image gets weird, it looks like it partially renders the dom element


This


I'm using the sample code in create-react-app




import logo from ./logo.svg;
import ./App.css;
import { createRef } from react;
import { useScreenshot, createFileName } from use-react-screenshot;

function App() {
const ref = createRef(null);
const [image, takeScreenShot] = useScreenshot({
type: image/jpeg,
quality: 1.0,
});

const download = (image, { name = img, extension = jpg } = {}) => {
const a = document.createElement(a);
a.href = image;
a.download = createFileName(extension, name);
a.click();
};

const downloadScreenshot = () => takeScreenShot(ref.current).then(download);

return (
<div className=App>
<header className=App-header>
<button onClick={downloadScreenshot}>
Download screenshot
</button>
<img src={logo} className=App-logo alt=logo ref={ref} />
</header>
</div>
);
}

export default App;




A sandbox to play around with it


I'm not sure if it's related to html2canvas as a peerDependency. And wherever I put the ref to, the same error happens


More From » reactjs

 Answers
1

On github there were a lot of issues related with SVG not downloading properly with html2canvas. I suspect these to be the issues. Solutions / workaround provided by users includes code modifications to the internal code. This is again hard to maintain further along. Also Firefox showed blank image when downloaded.


My best solution would be to use an alternative library like html-to-image. Within a few minutes everything seems to be working with this. (Even Firefox)


import * as htmlToImage from "html-to-image";
...
const takeScreenShot = async (node) => {
const dataURI = await htmlToImage.toJpeg(node);
return dataURI;
};

const download = (image, { name = "img", extension = "jpg" } = {}) => {
const a = document.createElement("a");
a.href = image;
a.download = createFileName(extension, name);
a.click();
};

const downloadScreenshot = () => takeScreenShot(ref.current).then(download);
...

Codesandbox


Now this might not be the best solution, but it is the best alternative working solution right now.


[#338] Saturday, February 12, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mackennamelissac

Total Points: 110
Total Questions: 118
Total Answers: 103

Location: Sweden
Member since Sun, Jan 16, 2022
2 Years ago
mackennamelissac questions
Thu, Jun 3, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Dec 27, 20, 00:00, 3 Years ago
;