Skip to content

Commit 80cc094

Browse files
committed
implement save-as-png feature
1 parent eb6c547 commit 80cc094

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

client/src/components/Menu.jsx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import { canvasState, canvasColors } from "../atoms";
66
import { jsPDF } from "jspdf";
77

88
function Menu(){
9-
109
const canvas = useRecoilValue(canvasState);
1110
const canvasColor = useRecoilValue(canvasColors);
1211

1312
// function to save canvas as pdf
1413
function saveAsPdf() {
15-
const canvasDataURL = canvas.toDataURL("imgae/png");
14+
const canvasDataURL = canvas.toDataURL("image/png");
1615
const pdf = new jsPDF({
1716
orientation: "landscape",
1817
unit: "px",
@@ -24,16 +23,40 @@ function Menu(){
2423
pdf.save("drawing.pdf");
2524
}
2625

27-
return (
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">
29-
<MenuItem feat="Start Collaboration"></MenuItem>
30-
<MenuItem feat="Start Chat"></MenuItem>
31-
<MenuItem feat="Save as pdf" clickHandler={saveAsPdf}></MenuItem>
32-
<MenuItem feat="Save as png"></MenuItem>
33-
<SponsorBtn></SponsorBtn>
34-
<Socials></Socials>
35-
</div>
36-
);
26+
// function to save canvas as pdf
27+
function saveAsPng() {
28+
29+
// create a temporary canvas to make a png
30+
const tempCanvas = document.createElement('canvas');
31+
const tempCanvasCtx = tempCanvas.getContext('2d');
32+
33+
tempCanvas.width = canvas.width;
34+
tempCanvas.height = canvas.height;
35+
36+
tempCanvasCtx.fillStyle = canvasColor;
37+
tempCanvasCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
38+
tempCanvasCtx.drawImage(canvas, 0, 0);
39+
40+
41+
const canvasDataURL = tempCanvas.toDataURL("image/png");
42+
const link = document.createElement("a");
43+
link.href = canvasDataURL;
44+
link.download = "drawing.png";
45+
document.body.appendChild(link);
46+
link.click();
47+
document.body.removeChild(link);
48+
}
49+
50+
return (
51+
<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">
52+
<MenuItem feat="Start Collaboration"></MenuItem>
53+
<MenuItem feat="Start Chat"></MenuItem>
54+
<MenuItem feat="Save as pdf" clickHandler={saveAsPdf}></MenuItem>
55+
<MenuItem feat="Save as png" clickHandler={saveAsPng}></MenuItem>
56+
<SponsorBtn></SponsorBtn>
57+
<Socials></Socials>
58+
</div>
59+
);
3760
}
3861

3962
export default Menu;

0 commit comments

Comments
 (0)