Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from dcramer/feat/jank-image-editor
Add draft image editor
- Loading branch information
Showing
8 changed files
with
265 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Dialog } from "@headlessui/react"; | ||
import { ArrowPathRoundedSquareIcon } from "@heroicons/react/20/solid"; | ||
import { useRef, useState } from "react"; | ||
import AvatarEditor from "react-avatar-editor"; | ||
import Button from "./button"; | ||
|
||
export default ({ | ||
image, | ||
open, | ||
setOpen, | ||
onSave, | ||
width = 250, | ||
height = 250, | ||
}: { | ||
image: any; | ||
open: boolean; | ||
setOpen: (value: boolean) => void; | ||
onSave: (newImage: any) => void; | ||
width?: number; | ||
height?: number; | ||
}) => { | ||
const ref = useRef<AvatarEditor>(null); | ||
const [rotate, setRotate] = useState(0); | ||
const [scale, setScale] = useState(1); | ||
|
||
return ( | ||
<Dialog open={open} as="div" className="dialog" onClose={setOpen}> | ||
<Dialog.Overlay className="fixed inset-0" /> | ||
<Dialog.Panel className="dialog-panel flex flex-col items-center justify-center px-4 pb-4 pt-5 sm:p-6"> | ||
<AvatarEditor | ||
ref={ref} | ||
image={image} | ||
width={width} | ||
height={height} | ||
border={50} | ||
scale={scale} | ||
rotate={rotate} | ||
/> | ||
<div className="flex flex-col gap-x-2 gap-y-2"> | ||
<input | ||
type="range" | ||
min={1} | ||
max={3} | ||
step={0.01} | ||
value={scale} | ||
onInput={(e) => { | ||
setScale(parseFloat((e as any).target.value)); | ||
}} | ||
/> | ||
<div className="flex gap-x-2"> | ||
<Button | ||
onClick={() => { | ||
const newRotate = rotate >= 270 ? 0 : rotate + 90; | ||
setRotate(newRotate); | ||
}} | ||
icon={<ArrowPathRoundedSquareIcon className="h-6 w-6" />} | ||
></Button> | ||
<Button | ||
color="highlight" | ||
onClick={() => { | ||
if (ref.current) { | ||
onSave(ref.current.getImageScaledToCanvas()); | ||
setOpen(false); | ||
} | ||
}} | ||
> | ||
Save | ||
</Button> | ||
</div> | ||
</div> | ||
</Dialog.Panel> | ||
</Dialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const toBlob = (canvas: HTMLCanvasElement) => { | ||
return new Promise((resolve, reject) => { | ||
canvas.toBlob(async (blob) => { | ||
resolve(blob); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.