-
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.
feat: send user input to server (#12)
- fix pnpm workspace error -
- Loading branch information
1 parent
45875ec
commit e94277a
Showing
14 changed files
with
2,967 additions
and
285 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use client" | ||
|
||
import { useEffect, useRef, useState } from "react"; | ||
import { ServerWSResponse } from "./WebSocketExample"; | ||
|
||
const validCommands : Record<string, string> = { | ||
"W": "UP", | ||
"A": "LEFT", | ||
"S": "DOWN", | ||
"D": "RIGHT", | ||
"ARROWUP": "ZOOM-IN", | ||
"ARROWDOWN": "ZOOM-OUT" | ||
} | ||
|
||
export const Canvas=({img_metadata, socket}:{img_metadata: ServerWSResponse, socket: WebSocket })=>{ | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const [cursorPosition, setCursorPosition] = useState<[number, number]>() | ||
const aspectRatio = 16/9; | ||
|
||
useEffect(() => { | ||
if (img_metadata){ | ||
const ctx = canvasRef.current?.getContext('2d'); | ||
const img = new Image(); | ||
img.src = img_metadata.image; | ||
[img.width, img.height] = img_metadata.dimension | ||
img.onload = () => { | ||
ctx?.drawImage(img, 0, 0) | ||
} | ||
} | ||
}, [img_metadata]); | ||
|
||
const handle_keyboard_input = (e: KeyboardEvent) =>{ | ||
const key = e.key.toUpperCase() | ||
if(key in validCommands){ | ||
e.preventDefault(); | ||
socket.send(validCommands[key]) | ||
console.log("key down -> ", key) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
document.addEventListener("keydown", handle_keyboard_input) | ||
return () => document.removeEventListener("keydown", handle_keyboard_input) | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<canvas | ||
className="w-screen h-screen" | ||
ref={canvasRef} | ||
width={1000} | ||
height={1000} | ||
onPointerMove={(event) => { | ||
const [x,y] = [event.clientX, event.clientY] | ||
setCursorPosition([Math.round(x),Math.round(y)]) | ||
console.log(event.clientX, event.clientY) | ||
}} | ||
onPointerLeave={(_)=>{ | ||
setCursorPosition(undefined) | ||
}} | ||
/> | ||
</> | ||
) | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.