|
1 | 1 | import MenuItem from "./MenuItem";
|
2 | 2 | import Socials from "./Socials";
|
3 | 3 | import SponsorBtn from "./SponsorBtn";
|
| 4 | +import { useRecoilValue } from "recoil"; |
| 5 | +import { canvasState, canvasColors } from "../atoms"; |
| 6 | +import { jsPDF } from "jspdf"; |
4 | 7 |
|
5 | 8 | function Menu(){
|
6 | 9 |
|
7 |
| - function clickHandlerPdf(){ |
8 |
| - |
| 10 | + const canvas = useRecoilValue(canvasState); |
| 11 | + const canvasColor = useRecoilValue(canvasColors); |
| 12 | + |
| 13 | + // function to save canvas as pdf |
| 14 | + function saveAsPdf() { |
| 15 | + const canvasDataURL = canvas.toDataURL("imgae/png"); |
| 16 | + const pdf = new jsPDF({ |
| 17 | + orientation: "landscape", |
| 18 | + unit: "px", |
| 19 | + format: [canvas.width, canvas.height], |
| 20 | + }); |
| 21 | + pdf.setFillColor(canvasColor); |
| 22 | + pdf.rect(0, 0, canvas.width, canvas.height, "F"); |
| 23 | + pdf.addImage(canvasDataURL, "PNG", 0, 0, canvas.width, canvas.height); |
| 24 | + pdf.save("drawing.pdf"); |
9 | 25 | }
|
10 | 26 |
|
11 | 27 | return (
|
12 | 28 | <div className="w-52 h-71 rounded-xl bg-gradient-to-r from-slate-900 to-slate-700 absolute left-52 top-8 rounded-lg shadow-xl">
|
13 | 29 | <MenuItem feat="Start Collaboration"></MenuItem>
|
14 | 30 | <MenuItem feat="Start Chat"></MenuItem>
|
15 |
| - <MenuItem feat="Save as pdf" onClick={clickHandlerPdf}></MenuItem> |
| 31 | + <MenuItem feat="Save as pdf" clickHandler={saveAsPdf}></MenuItem> |
16 | 32 | <MenuItem feat="Save as png"></MenuItem>
|
17 | 33 | <SponsorBtn></SponsorBtn>
|
18 | 34 | <Socials></Socials>
|
|
0 commit comments