-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
015bf75
commit a24171a
Showing
5 changed files
with
209 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { FormEvent, useState } from "react"; | ||
import { useRecoilState, useSetRecoilState } from "recoil"; | ||
import { canvasHeight, canvasWidth, diaryText } from "../../recoil/diary"; | ||
|
||
interface ICanvasLimit { | ||
maxWidth: number | undefined; | ||
maxHeight: number | undefined; | ||
} | ||
const CanvasInput = ({ maxWidth, maxHeight }: ICanvasLimit) => { | ||
const [width, setWidth] = useRecoilState<number>(canvasWidth); | ||
const [height, setHeight] = useRecoilState<number>(canvasHeight); | ||
const handleWidthChange = (event: FormEvent<HTMLInputElement>) => { | ||
setWidth(+event.currentTarget.value); | ||
}; | ||
const handleHeightChange = (event: FormEvent<HTMLInputElement>) => { | ||
setHeight(+event.currentTarget.value); | ||
}; | ||
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
event.currentTarget.reset(); | ||
}; | ||
return ( | ||
<div> | ||
<p> | ||
{maxWidth} {maxHeight} | ||
</p> | ||
<form onSubmit={handleSubmit} className="flex justify-between"> | ||
<input | ||
onChange={handleWidthChange} | ||
className="w-[80%] rounded-md shadow-md" | ||
type="number" | ||
min={1} | ||
max={maxWidth} | ||
/> | ||
<input | ||
onChange={handleHeightChange} | ||
className="w-[80%] rounded-md shadow-md" | ||
type="number" | ||
min={1} | ||
max={maxHeight} | ||
/> | ||
<button | ||
type="submit" | ||
className="border-[0.5px] bg-white w-10 h-10 justify-center items-center rounded-md shadow-md" | ||
> | ||
+ | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CanvasInput; |
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,91 @@ | ||
import { RefObject, useEffect, useRef, useState } from "react"; | ||
import { createTextChangeRange } from "typescript"; | ||
|
||
interface ILCanvas { | ||
leftWidth: number; | ||
leftHeight: number; | ||
rightWidth: number; | ||
rightHeight: number; | ||
} | ||
const LeftCanvas = ({ | ||
leftWidth, | ||
leftHeight, | ||
rightWidth, | ||
rightHeight, | ||
}: ILCanvas) => { | ||
const canvasRef: RefObject<HTMLCanvasElement> = | ||
useRef<HTMLCanvasElement>(null); | ||
const canvasRef2: RefObject<HTMLCanvasElement> = | ||
useRef<HTMLCanvasElement>(null); | ||
const [context, setContext] = useState<CanvasRenderingContext2D>(); | ||
const [context2, setContext2] = useState<CanvasRenderingContext2D>(); | ||
|
||
const [isDrawing, setIsDrawing] = useState<boolean>(false); | ||
const [leftToRight, setLeftToRight] = useState(false); | ||
useEffect(() => { | ||
const canvas = canvasRef.current; | ||
const ctx = canvas?.getContext("2d"); | ||
|
||
if (ctx) { | ||
ctx.strokeStyle = "black"; // 선 색 | ||
ctx.lineWidth = 2.5; // 선 굵기 | ||
setContext(ctx); | ||
} | ||
}); | ||
const toRight = () => { | ||
setLeftToRight(true); | ||
}; | ||
useEffect(() => { | ||
const canvas2 = canvasRef2.current; | ||
const ctx2 = canvas2?.getContext("2d"); | ||
|
||
if (ctx2 && leftWidth > 0 && leftHeight > 0) { | ||
const content2 = context?.getImageData(0, 0, leftWidth, leftHeight); | ||
content2 && ctx2.putImageData(content2, 0, 0); | ||
setLeftToRight(false); | ||
} | ||
}, [leftToRight]); | ||
const drawing = (nativeEvent: any) => { | ||
const { offsetX, offsetY } = nativeEvent.nativeEvent; | ||
if (isDrawing) { | ||
context?.beginPath(); | ||
context?.moveTo(offsetX, offsetY); | ||
} else { | ||
context?.lineTo(offsetX, offsetY); | ||
context?.stroke(); | ||
} | ||
}; | ||
const startDrawing = () => { | ||
setIsDrawing(true); | ||
}; | ||
const stopDrawing = () => { | ||
setIsDrawing(false); | ||
}; | ||
|
||
return ( | ||
<div className="grid grid-cols-2"> | ||
<div> | ||
<canvas | ||
ref={canvasRef} | ||
width={leftWidth} | ||
height={leftHeight} | ||
onMouseEnter={startDrawing} | ||
onMouseDown={stopDrawing} | ||
onMouseUp={startDrawing} | ||
onMouseMove={drawing} | ||
className="border-2 border-gray-400 rounded-md" | ||
></canvas> | ||
<button onClick={toRight}> 옆으로! </button> | ||
</div> | ||
|
||
<canvas | ||
ref={canvasRef2} | ||
width={rightWidth} | ||
height={rightHeight} | ||
className="border-2 border-gray-400 rounded-md" | ||
></canvas> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LeftCanvas; |
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,20 @@ | ||
import { RefObject, useEffect, useState } from "react"; | ||
|
||
export const useClientWidthHeight = (ref: RefObject<HTMLDivElement>) => { | ||
const [canvasProps, setCanvasProps] = useState({ | ||
width: 0, | ||
height: 0, | ||
}); | ||
useEffect(() => { | ||
const setClientWidthHeight = () => { | ||
ref.current && | ||
setCanvasProps({ | ||
width: ref.current.clientWidth, | ||
height: ref.current.clientHeight, | ||
}); | ||
}; | ||
setClientWidthHeight(); | ||
}, []); | ||
|
||
return canvasProps; | ||
}; |
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