Showing
3 changed files
with
116 additions
and
75 deletions
1 | +import dynamic from "next/dynamic"; | ||
1 | import { useState } from "react"; | 2 | import { useState } from "react"; |
2 | import styled from "styled-components"; | 3 | import styled from "styled-components"; |
3 | 4 | ||
5 | +const ToastEditor = dynamic(() => import("components/ToastEditor"), { | ||
6 | + ssr: false, | ||
7 | +}); | ||
4 | const Image = () => { | 8 | const Image = () => { |
5 | const [file, setFile] = useState(undefined); | 9 | const [file, setFile] = useState(undefined); |
6 | const [previewURL, setPreviewURL] = useState<string | ArrayBuffer>(""); | 10 | const [previewURL, setPreviewURL] = useState<string | ArrayBuffer>(""); |
... | @@ -24,47 +28,57 @@ const Image = () => { | ... | @@ -24,47 +28,57 @@ const Image = () => { |
24 | reader.readAsDataURL(targetFile); | 28 | reader.readAsDataURL(targetFile); |
25 | }; | 29 | }; |
26 | 30 | ||
31 | + const [isEditorOpened, setIsEditorOpened] = useState(false); | ||
32 | + const handleEditor = () => { | ||
33 | + setIsEditorOpened(true); | ||
34 | + }; | ||
35 | + | ||
27 | return ( | 36 | return ( |
28 | - <Container> | 37 | + <> |
29 | - <ImgBox> | 38 | + <Container> |
30 | - {file === undefined ? ( | 39 | + <ImgBox> |
31 | - <> | 40 | + <div onClick={handleEditor}>asdf</div> |
32 | - <div className="sub-flex"> | 41 | + {file === undefined ? ( |
33 | - <BlankBox /> | 42 | + <> |
34 | - <div>Click to add a photo</div> | 43 | + <div className="sub-flex"> |
35 | - <input | 44 | + <BlankBox /> |
36 | - type="file" | 45 | + <div>Click to add a photo</div> |
37 | - style={{ | 46 | + <input |
38 | - position: "absolute", | 47 | + type="file" |
39 | - top: 0, | 48 | + style={{ |
40 | - paddingLeft: 0, | 49 | + position: "absolute", |
41 | - zIndex: 0, | 50 | + top: 0, |
42 | - width: "90%", | 51 | + paddingLeft: 0, |
43 | - height: "100%", | 52 | + zIndex: 0, |
44 | - border: "none", | 53 | + width: "90%", |
45 | - cursor: "pointer", | 54 | + height: "100%", |
46 | - outline: "none", | 55 | + border: "none", |
47 | - }} | 56 | + cursor: "pointer", |
48 | - onChange={selectImg} | 57 | + outline: "none", |
49 | - /> | 58 | + }} |
50 | - </div> | 59 | + onChange={selectImg} |
51 | - <div className="sub-flex">Open Image Editor</div> | 60 | + /> |
52 | - </> | 61 | + </div> |
53 | - ) : ( | 62 | + <div className="sub-flex">Open Image Editor</div> |
54 | - <img | 63 | + </> |
55 | - alt={""} | 64 | + ) : ( |
56 | - style={{ | 65 | + <img |
57 | - objectFit: "cover", | 66 | + id="image" |
58 | - display: "flex", | 67 | + alt={""} |
59 | - margin: "0 auto", | 68 | + style={{ |
60 | - width: "50rem", | 69 | + objectFit: "cover", |
61 | - }} | 70 | + display: "flex", |
62 | - src={previewURL as string} | 71 | + margin: "0 auto", |
63 | - /> | 72 | + width: "30rem", |
64 | - )} | 73 | + }} |
65 | - </ImgBox> | 74 | + src={previewURL as string} |
66 | - {/* <Menu /> */} | 75 | + /> |
67 | - </Container> | 76 | + )} |
77 | + </ImgBox> | ||
78 | + {/* <Menu /> */} | ||
79 | + </Container> | ||
80 | + {isEditorOpened && <ToastEditor {...{ setPreviewURL }} />} | ||
81 | + </> | ||
68 | ); | 82 | ); |
69 | }; | 83 | }; |
70 | 84 | ... | ... |
... | @@ -3,12 +3,13 @@ import ImageEditor from "@toast-ui/react-image-editor"; | ... | @@ -3,12 +3,13 @@ import ImageEditor from "@toast-ui/react-image-editor"; |
3 | import { useEffect, useState } from "react"; | 3 | import { useEffect, useState } from "react"; |
4 | import "tui-image-editor/dist/tui-image-editor.css"; | 4 | import "tui-image-editor/dist/tui-image-editor.css"; |
5 | 5 | ||
6 | -const ToastEditor = () => { | 6 | +const ToastEditor = ({ setPreviewURL }) => { |
7 | const [lowerCanvas, setLowerCanvas] = useState<HTMLCanvasElement>(); | 7 | const [lowerCanvas, setLowerCanvas] = useState<HTMLCanvasElement>(); |
8 | + const [upperCanvas, setUpperCanvas] = useState<HTMLCanvasElement>(); | ||
8 | // console.log( | 9 | // console.log( |
9 | // document.getElementsByClassName("lower-canvas")[0]?.toDataURL("image/png") | 10 | // document.getElementsByClassName("lower-canvas")[0]?.toDataURL("image/png") |
10 | // ); | 11 | // ); |
11 | - console.log("c"); | 12 | + console.log("s"); |
12 | 13 | ||
13 | // const [upperCanvas, setUpperCanvas] = useState( | 14 | // const [upperCanvas, setUpperCanvas] = useState( |
14 | // document.getElementsByClassName("upper-canvas ")[0] | 15 | // document.getElementsByClassName("upper-canvas ")[0] |
... | @@ -19,44 +20,70 @@ const ToastEditor = () => { | ... | @@ -19,44 +20,70 @@ const ToastEditor = () => { |
19 | setLowerCanvas( | 20 | setLowerCanvas( |
20 | document.getElementsByClassName("lower-canvas")[0] as HTMLCanvasElement | 21 | document.getElementsByClassName("lower-canvas")[0] as HTMLCanvasElement |
21 | ); | 22 | ); |
23 | + setUpperCanvas( | ||
24 | + document.getElementsByClassName("upper-canvas")[0] as HTMLCanvasElement | ||
25 | + ); | ||
22 | }); | 26 | }); |
23 | }, []); | 27 | }, []); |
24 | 28 | ||
25 | - useEffect(() => { | 29 | + // useEffect(() => { |
26 | - const img = lowerCanvas?.toDataURL("image/png"); | 30 | + // const img = lowerCanvas?.toDataURL("image/png"); |
27 | - let w = window.open(); | 31 | + // const uploaded = document.getElementById("image"); |
28 | - if (w?.window) w.document.body.innerHTML = "<img src='" + img + "'>"; | 32 | + // console.log(uploaded); |
33 | + // // let w = window.open(); | ||
34 | + // // if (w?.window) w.document.body.innerHTML = "<img src='" + img + "'>"; | ||
35 | + // const image = new Image(); | ||
36 | + // // image.onload = function () { | ||
37 | + // // lowerCanvas.width = uploaded.clientWidth; | ||
38 | + // // lowerCanvas.height = uploaded.clientHeight; | ||
39 | + // // lowerCanvas?.getContext("2d").drawImage(image, 0, 0); | ||
40 | + // // }; | ||
41 | + // image.src = previewURL; | ||
42 | + // console.log("b"); | ||
43 | + // if (lowerCanvas?.getContext&&upperCanvas?.getContext) { | ||
44 | + // image.onload = function () { | ||
45 | + | ||
46 | + // lowerCanvas.width = 1000; | ||
47 | + // lowerCanvas.height = 572; | ||
48 | + // upperCanvas.width = 1000; | ||
49 | + // upperCanvas.height = 572; | ||
50 | + // lowerCanvas?.getContext("2d").drawImage(image, 0, 0); | ||
51 | + // }; | ||
52 | + // console.log(lowerCanvas.getContext("2d")); | ||
53 | + // } | ||
54 | + // }, [lowerCanvas?.toDataURL("image/png")]); | ||
29 | 55 | ||
30 | - console.log("b"); | 56 | + const handleEnd = () => { |
31 | - if (lowerCanvas?.getContext) { | 57 | + setPreviewURL(lowerCanvas.toDataURL("image/png")); |
32 | - console.log(lowerCanvas.getContext("2d")); | 58 | + }; |
33 | - } | ||
34 | - }, [lowerCanvas?.toDataURL("image/png")]); | ||
35 | 59 | ||
36 | return ( | 60 | return ( |
37 | - <ImageEditor | 61 | + <> |
38 | - includeUI={{ | 62 | + <div onClick={handleEnd}>asdf</div> |
39 | - loadImage: { | 63 | + <ImageEditor |
40 | - // path: 'img/sampleImage.jpg', | 64 | + includeUI={{ |
41 | - name: "SampleImage", | 65 | + loadImage: { |
42 | - }, | 66 | + // path: 'img/sampleImage.jpg', |
43 | - // theme: myTheme, | 67 | + name: "SampleImage", |
44 | - menu: ["shape", "filter"], | 68 | + }, |
45 | - initMenu: "filter", | 69 | + // theme: myTheme, |
46 | - uiSize: { | 70 | + menu: ["shape", "filter"], |
47 | - width: "1000px", | 71 | + initMenu: "filter", |
48 | - height: "700px", | 72 | + uiSize: { |
49 | - }, | 73 | + width: "1000px", |
50 | - menuBarPosition: "bottom", | 74 | + height: "700px", |
51 | - }} | 75 | + }, |
52 | - cssMaxHeight={500} | 76 | + menuBarPosition: "bottom", |
53 | - cssMaxWidth={700} | 77 | + }} |
54 | - selectionStyle={{ | 78 | + cssMaxHeight={500} |
55 | - cornerSize: 20, | 79 | + cssMaxWidth={700} |
56 | - rotatingPointOffset: 70, | 80 | + selectionStyle={{ |
57 | - }} | 81 | + cornerSize: 20, |
58 | - usageStatistics={true} | 82 | + rotatingPointOffset: 70, |
59 | - /> | 83 | + }} |
84 | + usageStatistics={true} | ||
85 | + /> | ||
86 | + </> | ||
60 | ); | 87 | ); |
61 | }; | 88 | }; |
62 | 89 | ... | ... |
-
Please register or login to post a comment