Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jpg, png export types now available #139

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions src/components/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
import { PiPencilSimpleFill } from "react-icons/pi";
import { FaFeatherPointed } from "react-icons/fa6";
import { RiScreenshot2Fill } from "react-icons/ri";
import {FaFilePdf} from 'react-icons/fa'
import { FaFilePdf } from "react-icons/fa";
import { TbFileTypeSvg } from "react-icons/tb";
import { useState } from "react";
import { takeSnapshot,convertToPDF,convertToSVG } from "../utils/canvas.js";
import {
convertToPDF,
convertToSVG,
convertToJPG,
convertToPng,
} from "../utils/canvas.js";
import { PiPlus } from "react-icons/pi";
import { PiMinus } from "react-icons/pi";
import { increaseHeight } from "../utils/canvas.js";
Expand Down Expand Up @@ -68,7 +73,6 @@ const Menu = ({
onChange={(e) => {
setThickness(e.target.value);
}}

className="cursor-pointer"
/>
)}
Expand All @@ -93,36 +97,65 @@ const Menu = ({
onMouseLeave={handleMouseLeave}
>
Save As
<svg className="w-2.5 h-2.5 ms-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="m1 1 4 4 4-4"/>
<svg
className="w-2.5 h-2.5 ms-3"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 10 6"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="m1 1 4 4 4-4"
/>
</svg>
</button>

<div
id="dropdownHover"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
className={`absolute z-10 ${isOpen ? '' : 'hidden'} divide-y bg-[#CBCCCF] rounded-lg shadow w-48 top-[3.1rem]`}
className={`absolute z-10 ${
isOpen ? "" : "hidden"
} divide-y bg-[#CBCCCF] rounded-lg shadow w-59 top-[3.1rem]`}
>
<ul className="text-sm text-gray-700 flex space-x-3 p-3 justify-center" aria-labelledby="dropdownHoverButton">
<ul
className="text-sm text-gray-700 flex space-x-5 p-5 justify-center"
aria-labelledby="dropdownHoverButton"
>
<li>
<RiScreenshot2Fill
className={`text-[2rem] md:text-[3rem] p-[0.5rem] md:p-[0.8rem] shadow-mdm rounded-[0.5rem] cursor-pointer hover:bg-[#B7BABF]`}
onClick={() => takeSnapshot(canvasRef.current, color)}
title="Snapshot"
/>
<button
className={`text-[1rem] md:text-[1rem] p-[0.5rem] md:p-[0.8rem] shadow-mdm rounded-[0.5rem] cursor-pointer hover:bg-[#B7BABF]`}
onClick={() => convertToPng(canvasRef.current)}
title="ToPNG"
>
<p>PNG</p>
</button>
</li>

<li>
<button
className={`text-[1rem] md:text-[1rem] p-[0.5rem] md:p-[0.8rem] shadow-mdm rounded-[0.5rem] cursor-pointer hover:bg-[#B7BABF]`}
onClick={() => convertToJPG(canvasRef.current)}
title="ToJPG"
>
<p>JPG</p>
</button>
</li>
<li>
<FaFilePdf
<FaFilePdf
className={`text-[2rem] md:text-[3rem] p-[0.5rem] md:p-[0.8rem] shadow-mdm rounded-[0.5rem] cursor-pointer hover:bg-[#B7BABF]`}
onClick={()=>convertToPDF(canvasRef.current)}
onClick={() => convertToPDF(canvasRef.current)}
title="PDF"
/>
</li>
<li>
<TbFileTypeSvg
className={`text-[2rem] md:text-[3rem] p-[0.5rem] md:p-[0.8rem] shadow-mdm rounded-[0.5rem] cursor-pointer hover:bg-[#B7BABF]`}
onClick={()=>convertToSVG(canvasRef.current)}
onClick={() => convertToSVG(canvasRef.current)}
title="SVG"
/>
</li>
Expand Down
18 changes: 14 additions & 4 deletions src/utils/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ export function clearCanvas(canvas, bgColor) {
ctx.fillRect(0, 0, canvas.width, canvas.height);
}

// Function to handle taking a snapshot
export const takeSnapshot = (canvas) => {
const snapshot = canvas.toDataURL();
// Snapshot by default saves files as png
// here we broke its functionality to convertToPng for readability
// and consistency with other export option functions
export const convertToPng = (canvas) => {
let snapshot = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = snapshot;
link.download = "snapshot.png";
link.download = `snapshot.png`;
link.click();
};

export const convertToJPG = (canvas) => {
let snapshot = canvas.toDataURL("image/jpeg");
const link = document.createElement("a");
link.href = snapshot;
link.download = `snapshot.jpg`;
link.click();
};

Expand Down