-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid.html
More file actions
137 lines (122 loc) · 3.07 KB
/
grid.html
File metadata and controls
137 lines (122 loc) · 3.07 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gerg</title>
<style>
body {
background: #000;
display: grid;
place-items: center;
height: 100vh;
}
canvas {
height: 400px;
width: 400px;
box-shadow: #9007 10px 10px 80px, #009a -10px -10px 50px,
#009a 0px 0px 500px;
border-radius: 0.5em;
}
pre {
position: absolute;
top: 0;
left: 0;
width: 500px;
color: #ccc;
max-height: 90vh;
overflow: hidden;
}
</style>
</head>
<body>
<canvas></canvas>
<pre>
Foo
Bar
</pre
>
<script>
let pre = document.querySelector("pre");
let msgs = [];
function log(m) {
msgs.unshift(m);
pre.innerText = msgs.join("\n");
if (msgs.length > 30) {
msgs.pop();
}
}
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
// Define the grid size
const rows = 10;
const cols = 10;
const cellWidth = canvas.width / cols;
const cellHeight = canvas.height / rows;
function grid() {
ctx.strokeStyle = "#101";
// Draw vertical lines98
for (let i = 0; i <= cols; i++) {
let x = i * cellWidth;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
}
// Draw horizontal lines
for (let i = 0; i <= rows; i++) {
let y = i * cellHeight;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
}
}
const char = { x: 2, y: 2 };
document.addEventListener("keydown", (e) => {
log("Key " + e.key)
if (e.key === "w") {
if (char.y > 1) char.y--;
}
if (e.key === "a") {
if (char.x > 1) char.x--;
}
if (e.key === "s") {
if (char.y < 10) char.y++;
}
if (e.key === "d") {
if (char.x < 10) char.x++;
}
});
const halfx = cellWidth / 2;
function coordX(x) {
return x * cellWidth - halfx;
}
const halfy = cellHeight / 2;
function coordY(x) {
return x * cellHeight - halfy;
}
function drawChar() {
ctx.fillStyle = "#fee";
ctx.beginPath();
ctx.arc(coordX(char.x), coordY(char.y), 5, 0, Math.PI * 2); // x, y, radius, start angle, end angle
ctx.fill(); // Or use ctx.stroke() if you want only the outline
}
function clear() {
ctx.fillStyle = "#000";
ctx.beginPath();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fill();
}
log("Move with WASD");
function tick() {
clear();
drawChar();
grid();
// requestAnimationFrame()
setTimeout(tick, 1000 / 25);
}
tick();
</script>
</body>
</html>