import React, { useContext, useState } from "react"; // import { exportComponentAsPNG } from "react-component-export-image"; import "./CoverImage.css"; import { ImgContext } from "../utils/ImgContext"; import unsplash from "../utils/unsplashConfig"; import domtoimage from "dom-to-image"; const ComponentToImg = (props) => { const [loading, setLoading] = useState(false) const { unsplashImage } = useContext(ImgContext); const componentRef = React.createRef(); async function saveImage(data) { var a = document.createElement("A"); a.href = data; a.download = `cover.png`; document.body.appendChild(a); setLoading(false) a.click(); document.body.removeChild(a); } const downloadImage = async () => { // exportComponentAsPNG(componentRef, 'cover') setLoading(true) const element = componentRef.current; // console.log(element) // console.log(element.offsetHeight) let data = await domtoimage.toPng(componentRef.current, { height: element.offsetHeight * 2, width: element.offsetWidth * 2, style: { transform: "scale(" + 2 + ")", transformOrigin: "top left", width: element.offsetWidth + "px", height: element.offsetHeight + "px", } }) console.log(data) await saveImage(data); if (unsplashImage) { unsplash.photos.trackDownload({ downloadLocation: unsplashImage.downloadLink, }); } } return (
{props.children}
); } export default ComponentToImg;