Skip to content

Commit 16b41a8

Browse files
committed
feat: add touch event support for mobile canvas interaction
1 parent 3e64b00 commit 16b41a8

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

apps/collabydraw/canvas-engine/CanvasEngine.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ export class CanvasEngine {
296296
this.canvas.addEventListener("wheel", this.mouseWheelHandler, {
297297
passive: false,
298298
});
299+
this.canvas.addEventListener("touchstart", this.touchStartHandler, {
300+
passive: false,
301+
});
302+
this.canvas.addEventListener("touchmove", this.touchMoveHandler, {
303+
passive: false,
304+
});
305+
this.canvas.addEventListener("touchend", this.touchEndHandler, {
306+
passive: false,
307+
});
299308
}
300309

301310
setTool(tool: ToolType) {
@@ -974,6 +983,38 @@ export class CanvasEngine {
974983
}
975984
};
976985

986+
touchStartHandler = (e: TouchEvent) => {
987+
e.preventDefault();
988+
const touch = e.touches[0];
989+
if (!touch) return;
990+
991+
const simulatedMouse = new MouseEvent("mousedown", {
992+
clientX: touch.clientX,
993+
clientY: touch.clientY,
994+
});
995+
996+
this.mouseDownHandler(simulatedMouse);
997+
};
998+
999+
touchMoveHandler = (e: TouchEvent) => {
1000+
e.preventDefault();
1001+
const touch = e.touches[0];
1002+
if (!touch) return;
1003+
1004+
const simulatedMouse = new MouseEvent("mousemove", {
1005+
clientX: touch.clientX,
1006+
clientY: touch.clientY,
1007+
});
1008+
1009+
this.mouseMoveHandler(simulatedMouse);
1010+
};
1011+
1012+
touchEndHandler = (e: TouchEvent) => {
1013+
e.preventDefault();
1014+
const simulatedMouse = new MouseEvent("mouseup", {});
1015+
this.mouseUpHandler(simulatedMouse);
1016+
};
1017+
9771018
private handleTexty(e: MouseEvent) {
9781019
const { x, y } = this.transformPanScale(e.clientX, e.clientY);
9791020

@@ -1767,6 +1808,9 @@ export class CanvasEngine {
17671808
this.canvas.removeEventListener("mousemove", this.mouseMoveHandler);
17681809
this.canvas.removeEventListener("mouseup", this.mouseUpHandler);
17691810
this.canvas.removeEventListener("wheel", this.mouseWheelHandler);
1811+
this.canvas.removeEventListener("touchstart", this.touchStartHandler);
1812+
this.canvas.removeEventListener("touchmove", this.touchMoveHandler);
1813+
this.canvas.removeEventListener("touchend", this.touchEndHandler);
17701814

17711815
if (this.socket?.readyState === WebSocket.OPEN) {
17721816
this.socket.send(

apps/collabydraw/components/canvas/CanvasBoard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ export default function CanvasBoard() {
229229
const handleResize = () => {
230230
if (canvasRef.current) {
231231
const canvas = canvasRef.current;
232-
canvas.width = window.innerWidth;
233-
canvas.height = window.innerHeight;
232+
canvas.width = window.innerWidth || document.documentElement.clientWidth;
233+
canvas.height = window.innerHeight || document.documentElement.clientHeight;
234234
engine.handleResize(window.innerWidth, window.innerHeight);
235235
}
236236
};
@@ -452,7 +452,7 @@ export default function CanvasBoard() {
452452
<ScreenLoading />
453453
)}
454454

455-
<canvas className={cn("collabydraw collabydraw-canvas", theme === 'dark' ? 'collabydraw-canvas-dark' : '')} ref={canvasRef} />
455+
<canvas className={cn("collabydraw collabydraw-canvas touch-none", theme === 'dark' ? 'collabydraw-canvas-dark' : '')} ref={canvasRef} />
456456
</div >
457457
)
458458
};

0 commit comments

Comments
 (0)