Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refactoring Start Page #23

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ lerna-debug.log*
# Tests
/coverage
/.nyc_output
/client/test_results

# IDEs and editors
/.idea
Expand Down
4 changes: 3 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
"tailwindcss-animate": "^1.0.7",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
"vite": "^5.4.10",
"puppeteer": "^24.1.0"

},
"eslintConfig": {
"extends": [
Expand Down
15 changes: 7 additions & 8 deletions client/src/components/ui/BackgroundCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, useRef, MouseEvent } from 'react';
import { useEffect, useRef, PointerEvent } from 'react';
import { Point } from '@troublepainter/core';
import { CURSOR_LENGTH, CURSOR_WIDTH, DELETE_INTERVAL } from '@/constants/backgroundConstants';
import { getCanvasContext } from '@/utils/getCanvasContext';
import { getDrawPoint } from '@/utils/getDrawPoint';
import { getDrawPoint_Pointer } from '@/utils/getDrawPoint';

const Background = ({ className }: { className: string }) => {
const cursorCanvasRef = useRef<HTMLCanvasElement>(null);
Expand Down Expand Up @@ -74,17 +74,16 @@ const Background = ({ className }: { className: string }) => {
};
}, []);

const handleMouseMove = (e: MouseEvent<HTMLCanvasElement>) => {
const handlePointerMove = (e: PointerEvent<HTMLCanvasElement>) => {
currentTimestamp.current = performance.now();
if (currentTimestamp.current - lastTimestamp.current < 16) return;
lastTimestamp.current = currentTimestamp.current;

const { canvas } = getCanvasContext(cursorCanvasRef);
const point = getDrawPoint(e, canvas);
const point = getDrawPoint_Pointer(e);
pointsRef.current.push(point);
};

const handleMouseLeave = () => {
const handlePointerLeave = () => {
const { canvas, ctx } = getCanvasContext(cursorCanvasRef);
pointsRef.current.length = 0;
ctx.clearRect(0, 0, canvas.width, canvas.height);
Expand All @@ -95,8 +94,8 @@ const Background = ({ className }: { className: string }) => {
<canvas
ref={cursorCanvasRef}
className="absolute h-full w-full cursor-none"
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
onPointerMove={handlePointerMove}
onPointerLeave={handlePointerLeave}
/>
</div>
);
Expand Down
21 changes: 20 additions & 1 deletion client/src/utils/getDrawPoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TouchEvent as ReactTouchEvent, MouseEvent as ReactMouseEvent } from 'react';
import { TouchEvent as ReactTouchEvent, MouseEvent as ReactMouseEvent, PointerEvent } from 'react';
import { Point } from '@troublepainter/core';

/**
Expand Down Expand Up @@ -48,3 +48,22 @@ export const getDrawPoint = (
else if (e.nativeEvent instanceof TouchEvent) return getTouchPoint(canvas, e.nativeEvent);
else throw new Error('mouse ํ˜น์€ touch ์ด๋ฒคํŠธ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.');
};

/**
* PointerEvent ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ๋งˆ์šฐ์Šค ์ขŒํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” Util์ž…๋‹ˆ๋‹ค.
* - ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ offsetX, offsetY ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
* - ์ขŒํ‘œ๋Š” ์บ”๋ฒ„์Šค ์ขŒ์ธก ์ƒ๋‹จ์ด (0,0) ์ž…๋‹ˆ๋‹ค.
*
* @param e - PointerEvent ๊ฐ์ฒด
* @returns ์‚ฌ์šฉ์ž ์ •์˜ Point ํƒ€์ž… ๊ฐ์ฒด
*
* @example
* ```typescript
* const {x, y} = getDrawPoint_Pointer(e);
* ```
*
* @category Utils
*/
export const getDrawPoint_Pointer = (e: PointerEvent<HTMLCanvasElement>): Point => {
return { x: e.nativeEvent.offsetX, y: e.nativeEvent.offsetY };
};
Loading
Loading