Skip to content

Commit

Permalink
feat: Change drawing of paint tool coordinates received from socket (#…
Browse files Browse the repository at this point in the history
…187)

* feat: Modification of maximum pixel amount setting

* feat: Modification of maximum pixel amount

* feat: Add applyFill method

* feat: Add logic to distinguish paint tool and pen tool
  • Loading branch information
dbjoung authored Dec 3, 2024
1 parent 3950456 commit 646cac2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions client/src/components/canvas/GameCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MouseEvent as ReactMouseEvent, TouchEvent as ReactTouchEvent, useCallback, useEffect, useRef } from 'react';
import { PlayerRole, RoomStatus } from '@troublepainter/core';
import { Canvas } from '@/components/canvas/CanvasUI';
import { COLORS_INFO, MAINCANVAS_RESOLUTION_WIDTH } from '@/constants/canvasConstants';
import { COLORS_INFO, DEFAULT_MAX_PIXELS, MAINCANVAS_RESOLUTION_WIDTH } from '@/constants/canvasConstants';
import { drawingSocketHandlers } from '@/handlers/socket/drawingSocket.handler';
import { gameSocketHandlers } from '@/handlers/socket/gameSocket.handler';
import { useDrawing } from '@/hooks/canvas/useDrawing';
Expand Down Expand Up @@ -48,7 +48,7 @@ interface GameCanvasProps {
*
* @category Components
*/
const GameCanvas = ({ role, maxPixels = 10000, currentRound, roomStatus, isHidden }: GameCanvasProps) => {
const GameCanvas = ({ role, maxPixels = DEFAULT_MAX_PIXELS, currentRound, roomStatus, isHidden }: GameCanvasProps) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const { convertCoordinate } = useCoordinateScale(MAINCANVAS_RESOLUTION_WIDTH, canvasRef);

Expand Down
3 changes: 2 additions & 1 deletion client/src/components/quiz/QuizStage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PlayerRole } from '@troublepainter/core';
import { GameCanvas } from '../canvas/GameCanvas';
import { QuizTitle } from '../ui/QuizTitle';
import sizzlingTimer from '@/assets/big-timer.gif';
import { DEFAULT_MAX_PIXELS } from '@/constants/canvasConstants';
import { useTimer } from '@/hooks/useTimer';
import { useGameSocketStore } from '@/stores/socket/gameSocket.store';
import { cn } from '@/utils/cn';
Expand Down Expand Up @@ -56,7 +57,7 @@ const QuizGameContent = () => {
currentRound={room.currentRound}
roomStatus={room.status}
role={roundAssignedRole || PlayerRole.GUESSER}
maxPixels={100000}
maxPixels={DEFAULT_MAX_PIXELS}
isHidden={shouldHideCanvas}
/>

Expand Down
6 changes: 3 additions & 3 deletions client/src/constants/canvasConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const LINEWIDTH_VARIABLE = {
STEP_WIDTH: 2,
};

export const MAINCANVAS_RESOLUTION_WIDTH = 1280;
export const MAINCANVAS_RESOLUTION_HEIGHT = 800;
export const MAINCANVAS_RESOLUTION_WIDTH = 1000;
export const MAINCANVAS_RESOLUTION_HEIGHT = 625;
//해상도 비율 변경 시 CanvasUI의 aspect-[16/10] 도 수정해야 정상적으로 렌더링됩니다.

export const COLORS_INFO = [
Expand All @@ -21,4 +21,4 @@ export const COLORS_INFO = [
{ color: '회색', backgroundColor: '#808080' },
];

export const DEFAULT_MAX_PIXELS = 1000; // 기본값 설정
export const DEFAULT_MAX_PIXELS = 50000; // 기본값 설정
5 changes: 3 additions & 2 deletions client/src/hooks/canvas/useDrawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const useDrawing = (

const strokeId = state.crdtRef.current.addStroke(drawingData);
state.currentStrokeIdsRef.current.push(strokeId);
operation.drawStroke(drawingData);
if (state.drawingMode === DRAWING_MODE.PEN) operation.drawStroke(drawingData);

return {
type: CRDTMessageTypes.UPDATE,
Expand Down Expand Up @@ -280,7 +280,8 @@ export const useDrawing = (
return;
}

operation.drawStroke(stroke);
if (stroke.points.length > 2) operation.applyFill(stroke);
else operation.drawStroke(stroke);

if (state.historyPointerRef.current < state.strokeHistoryRef.current.length - 1) {
state.strokeHistoryRef.current = state.strokeHistoryRef.current.slice(0, state.historyPointerRef.current + 1);
Expand Down
28 changes: 26 additions & 2 deletions client/src/hooks/canvas/useDrawingOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const checkColorisNotEqual = (pos: number, startColor: RGBA, pixelArray: Uint8Cl
* @property getCurrentStyle - 현재 상태를 기반으로 스트로크 스타일을 반환하는 함수
* @property drawStroke - 캔버스에 단일 스트로크를 그리는 함수
* @property redrawCanvas - 저장된 스트로크들을 기반으로 전체 캔버스를 다시 그리는 함수
* @property applyFill - 소켓에서 받아온 페인팅 좌표 배열을 그리는 함수
* @property floodFill - 지정된 좌표에서 영역 채우기를 수행하는 함수
*
* @category Hooks
Expand Down Expand Up @@ -104,9 +105,31 @@ export const useDrawingOperation = (

state.crdtRef.current.strokes
.filter((stroke) => stroke.stroke !== null)
.forEach(({ stroke }) => drawStroke(stroke));
.forEach(({ stroke }) => {
if (stroke.points.length > 2) applyFill(stroke);
else drawStroke(stroke);
});
}, [drawStroke]);

const applyFill = (drawingData: DrawingData) => {
const { canvas, ctx } = getCanvasContext(canvasRef);
const { points, style } = drawingData;

if (points.length === 0) return;

const color = hexToRGBA(style.color);

const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;

points.forEach(({ x, y }) => {
const pos = (y * canvas.width + x) * 4;
fillTargetColor(pos, color, data);
});

ctx.putImageData(imageData, 0, 0);
};

const floodFill = useCallback(
(startX: number, startY: number) => {
const { canvas, ctx } = getCanvasContext(canvasRef);
Expand All @@ -126,7 +149,7 @@ export const useDrawingOperation = (
let pixelCount = 0;
const filledPoints: Point[] = [];

while (pixelsToCheck.length > 0 && pixelCount < inkRemaining) {
while (pixelsToCheck.length > 0 && pixelCount <= inkRemaining) {
const [x, y] = pixelsToCheck.shift()!;
const pos = (y * canvas.width + x) * 4;

Expand Down Expand Up @@ -165,6 +188,7 @@ export const useDrawingOperation = (
getCurrentStyle,
drawStroke,
redrawCanvas,
applyFill,
floodFill,
clearCanvas,
};
Expand Down

0 comments on commit 646cac2

Please sign in to comment.