-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmouse-capture.ts
More file actions
107 lines (96 loc) · 2.1 KB
/
mouse-capture.ts
File metadata and controls
107 lines (96 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
type CanvasRenderingContext2D,
mainloop,
WindowCanvas,
} from "../ext/canvas.ts";
const canvasW = new WindowCanvas({
title: "Deno Window Manager",
width: 800,
height: 600,
resizable: true,
});
/// Define the Input mode of the cursor to disabled, this gives us the ability to
/// capture the cursor exclusively to the window.
canvasW.window.setInputMode("cursor", "disabled");
addEventListener("focus", (evt) => {
/// When unfocused, set the cursor to normal, when focused, set the cursor to disabled
canvasW.window.setInputMode("cursor", evt.focused ? "disabled" : "normal");
});
/// When the escape key is pressed, set the cursor to normal
addEventListener("keydown", (evt) => {
if (evt.code === "Escape") {
canvasW.window.setInputMode("cursor", "normal");
}
});
const Cursor = {
x: 0,
y: 0,
};
addEventListener("mousemove", (evt) => {
if (!canvasW.window.focused) {
evt.preventDefault();
return;
}
Cursor.x = evt.x;
Cursor.y = evt.y;
});
/// On click to the window capture the cursor if its not already captured
addEventListener("click", (evt) => {
if (canvasW.window.getInputMode("cursor") === "disabled") {
return;
}
canvasW.window.setInputMode("cursor", "disabled");
});
canvasW.onDraw = (ctx: CanvasRenderingContext2D) => {
ctx.clearRect(
0,
0,
canvasW.canvas.width,
canvasW.canvas.height,
);
//draw cursor
ctx.fillStyle = "black";
ctx.fillRect(
0,
0,
canvasW.canvas.width,
canvasW.canvas.height,
);
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(
Cursor.x,
Cursor.y,
5,
0,
Math.PI * 2,
true,
);
ctx.fill();
ctx.fillStyle = "white";
ctx.font = "20px Arial";
ctx.fillText(
`Cursor: ${Cursor.x} ${Cursor.y}`,
10,
20,
);
ctx.fillStyle = "white";
ctx.font = "20px Arial";
ctx.fillText(
`Pressing escape will release the cursor`,
10,
40,
);
ctx.fillText(
`Cursor is ${
canvasW.window.getInputMode("cursor") === "disabled"
? "Captured"
: "Released"
}`,
10,
60,
);
};
await mainloop(() => {
canvasW.draw();
});