diff --git a/modules/canvas/components/Canvas.tsx b/modules/canvas/components/Canvas.tsx index e69de29..565a3bd 100644 --- a/modules/canvas/components/Canvas.tsx +++ b/modules/canvas/components/Canvas.tsx @@ -0,0 +1,143 @@ +import { CANVAS_SIZE } from "@/common/constants/canvasSize"; +import { useViewportSize } from "@/common/hooks/useViewportSize"; +import { useMotionValue, motion } from "framer-motion"; +import { useRef, useState, useEffect } from "react"; +import { useKeyPressEvent } from "react-use"; +import { useDraw } from "../hooks/Canvas.hooks"; +import { socket } from "@/common/lib/socket"; +import { drawFromSocket } from "../helpers/Canvas.helpers"; +import Minimap from "./Minimap"; + +const Canvas = () => { + const canvasRef = useRef(null); + const smallCanvasRef = useRef(null); + + const [ctx, setCtx] = useState(); + const [dragging, setDragging] = useState(false); + const [, setMovedMiniMap] = useState(false); + + const { width, height } = useViewportSize(); + + useKeyPressEvent("Control", () => setDragging(true), () => setDragging(false)); + + const x = useMotionValue(0); + const y = useMotionValue(0); + + const copyCanvasToSmall = () => { + if (canvasRef.current) { + smallCanvasRef.current + ?.getContext("2d") + ?.drawImage( + canvasRef.current, + 0, + 0, + CANVAS_SIZE.width, + CANVAS_SIZE.height + ); + } + }; + + const { handleDraw, handleEndDrawing, handleStartDrawing, drawing } = useDraw( + ctx, + dragging, + -x.get(), + -y.get(), + copyCanvasToSmall + ); + + useEffect(() => { + const newCtx = canvasRef.current?.getContext("2d"); + if (newCtx) setCtx(newCtx); + + const handleKeyUp = (e: KeyboardEvent) => { + if (!e.ctrlKey && dragging) { + setDragging(false); + } + } + window.addEventListener("keyup", handleKeyUp); + return () => { + window.removeEventListener("keyup", handleKeyUp); + } + }, [dragging]); + + useEffect(() => { + let movesToDrawLater: [number, number][] = []; + let optionsToUseLater: CtxOptions = { + lineColor: "", + lineWidth: 0, + }; + socket.on("socket_draw", (movesToDraw, socketOptions) => { + if (ctx && !drawing) { + drawFromSocket(movesToDraw, socketOptions, ctx, copyCanvasToSmall); + } else { + movesToDrawLater = movesToDraw; + optionsToUseLater = socketOptions; + } + }); + + return () => { + socket.off("socket_draw"); + if (movesToDrawLater.length && ctx) { + drawFromSocket(movesToDrawLater, optionsToUseLater, ctx, copyCanvasToSmall); + } + }; + }, [drawing, ctx]); + + return ( +
+ { + handleStartDrawing( + e.clientX, + e.clientY + ) + }} + onMouseUp={handleEndDrawing} + onMouseMove={(e) => { + handleDraw( + e.clientX, + e.clientY + ); + }} + + onTouchStart={(e) => { + handleStartDrawing( + e.changedTouches[0].clientX, + e.changedTouches[0].clientY + ); + }} + onTouchEnd={handleEndDrawing} + onTouchMove={(e) => { + handleDraw( + e.changedTouches[0].clientX, + e.changedTouches[0].clientY + ); + }} + /> + +
+ ) +}; + +export default Canvas; diff --git a/modules/canvas/components/Minimap.tsx b/modules/canvas/components/Minimap.tsx index 6732f84..7d96bd3 100644 --- a/modules/canvas/components/Minimap.tsx +++ b/modules/canvas/components/Minimap.tsx @@ -3,12 +3,16 @@ import { useViewportSize } from "@/common/hooks/useViewportSize"; import { MotionValue, useMotionValue } from "framer-motion"; import {Dispatch, SetStateAction, forwardRef, useEffect, useRef} from "react"; import {motion} from "framer-motion"; -const Minimap = forwardRef<{ + + +const Minimap = forwardRef< +HTMLCanvasElement,{ x: MotionValue; y: MotionValue; dragging: boolean; - setMovedMinimap: Dispatch>; -}>(({x, y, dragging, setMovedMinimap}, ref)=>{ + setMovedMiniMap: Dispatch>; +} +>(({x, y, dragging, setMovedMiniMap}, ref)=>{ const containerRef = useRef(null); const {width, height} = useViewportSize(); @@ -36,7 +40,7 @@ return ( height: CANVAS_SIZE.height / 10, }}> setMovedMinimap((prev:boolean)=> !prev)} + onDragEnd={()=> setMovedMiniMap((prev:boolean)=> !prev)} className="absolute top-0 left-0 cursor-grab border-2 border-red-500" style={{ width: width / 10, diff --git a/modules/canvas/helpers/Canvas.helpers.ts b/modules/canvas/helpers/Canvas.helpers.ts index 46e10d9..4303383 100644 --- a/modules/canvas/helpers/Canvas.helpers.ts +++ b/modules/canvas/helpers/Canvas.helpers.ts @@ -1,4 +1,4 @@ -const drawFromSocket = ( +export const drawFromSocket = ( socketMoves: [number, number][], socketOptions: CtxOptions, ctx: CanvasRenderingContext2D,