-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcustom-cursor.ts
74 lines (65 loc) · 1.46 KB
/
custom-cursor.ts
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
import {
type CanvasRenderingContext2D,
mainloop,
WindowCanvas,
} from "../ext/canvas.ts";
import { decode } from "https://deno.land/x/[email protected]/mod.ts";
const canvasW = new WindowCanvas({
title: "Deno Window Manager",
width: 800,
height: 600,
resizable: true,
});
const file = await Deno.readFile("examples/assets/cursor.png");
const png = decode(file);
//This bellow is a Custom RGBA Cursor of 16x16 pixels, each pixel is 4 bytes (RGBA)
const cursor = new Uint8Array(16 * 16 * 4);
for (let i = 0; i < 16; i++) {
cursor[i * 16 * 4 + i * 4 + 3] = 255;
cursor[i * 16 * 4 + (15 - i) * 4 + 3] = 255;
}
let current = 0;
canvasW.window.setCustomCursor(png.image, {
width: 32,
height: 32,
}, {
x: 0,
y: 0,
});
addEventListener("click", (evt) => {
if (current == 0) {
canvasW.window.setCustomCursor(png.image, {
width: 32,
height: 32,
}, {
x: 0,
y: 0,
});
current = 1;
} else {
canvasW.window.setCustomCursor(cursor, {
width: 16,
height: 16,
}, {
x: 0,
y: 0,
});
current = 0;
}
});
canvasW.onDraw = (ctx: CanvasRenderingContext2D) => {
ctx.clearRect(
0,
0,
canvasW.canvas.width,
canvasW.canvas.height,
);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvasW.canvas.width, canvasW.canvas.height);
ctx.fillStyle = "black";
ctx.font = "20px Arial";
ctx.fillText("Click to change the cursor", 10, 50);
};
await mainloop(() => {
canvasW.draw();
});