-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.html
More file actions
196 lines (154 loc) · 6.58 KB
/
grid.html
File metadata and controls
196 lines (154 loc) · 6.58 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!--
To change this template use Tools | Templates.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Keyboard Events - keypress</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<style type="text/css">
canvas.default { border: 1px solid black; }
canvas.active { border: 1px solid red; }
div.feedback { color: red; font-size: 1.0em; }
body {
/*background: lightblue;*/
}
canvas {
background: #fff;
margin: 20px;
}
</style>
<!--SCRIPTS-->
<script type="text/javascript">
var x = 0;
var y = 0;
var kBoardWidth = 9;
var kBoardHeight= 9;
var kPieceWidth = 50;
var kPieceHeight = 50;
var kPixelWidth = 1 + (kBoardWidth * kPieceWidth);
var kPixelHeight= 1 + (kBoardHeight * kPieceHeight);
var fontXOffset = -15;
var fontYOffset = 12;
var currentPosition = { column:0, row:0 };
var gContext;
if (window.addEventListener) { // Mozilla, Netscape, Firefox
window.addEventListener('load', WindowLoad, false);
}
else if (window.attachEvent) { // IE
window.attachEvent('onload', WindowLoad);
}
function isInsideGrid(position) {
return ( position.row >= 0 && position.row < kBoardWidth ) && ( position.column >= 0 && position.column < kBoardHeight );
}
function drawCharacter(position) {
var column = position.column;
var row = position.row;
// transform row / column to grid coords
var x = (column * kPieceWidth) + (kPieceWidth/2);
var y = (row * kPieceHeight) + (kPieceHeight/2);
// Radius based of the piece size
var radius = (kPieceWidth/2) - (kPieceWidth/10);
// draw
gContext.strokeStyle = "#000";
gContext.stroke();
// Set the font
gContext.font = "italic 200 36px/2 Unknown Font, sans-serif";
// Draw the @ symbol in the center of the circle
//
gContext.fillStyle = 'black';
gContext.fillText('@', x + fontXOffset, y + fontYOffset);
//
var up = { column: position.column, row: position.row - 1 };
var down = { column: position.column, row: position.row + 1 };
var left = { column: position.column - 1, row: position.row };
var right = { column: position.column + 1, row: position.row };
// Set the fill pattern
gContext.fillStyle = '#ccc';
if(isInsideGrid(up)){
var x = (up.column * kPieceWidth) + (kPieceWidth/2);
var y = (up.row * kPieceHeight) + (kPieceHeight/2);
gContext.fillText('W', x + fontXOffset, y + fontYOffset);
}
if(isInsideGrid(down)){
var x = (down.column * kPieceWidth) + (kPieceWidth/2);
var y = (down.row * kPieceHeight) + (kPieceHeight/2);
gContext.fillText('S', x + fontXOffset, y + fontYOffset);
}
if(isInsideGrid(left)){
var x = (left.column * kPieceWidth) + (kPieceWidth/2);
var y = (left.row * kPieceHeight) + (kPieceHeight/2);
gContext.fillText('A', x + fontXOffset, y + fontYOffset);
}
if(isInsideGrid(right)){
var x = (right.column * kPieceWidth) + (kPieceWidth/2);
var y = (right.row * kPieceHeight) + (kPieceHeight/2);
gContext.fillText('D', x + fontXOffset, y + fontYOffset);
}
}
// This function was taken from an example of the book HTML5: Up and Running by Mark Pilgrim
// http://www.amazon.com/HTML5-Up-Running-Mark-Pilgrim/dp/0596806027
//
function drawBoard() {
gContext.clearRect(0, 0, kPixelWidth, kPixelHeight);
gContext.beginPath();
/* vertical lines */
for (var x = 0; x <= kPixelWidth; x += kPieceWidth) {
gContext.moveTo(0.5 + x, 0);
gContext.lineTo(0.5 + x, kPixelHeight);
}
/* horizontal lines */
for (var y = 0; y <= kPixelHeight; y += kPieceHeight) {
gContext.moveTo(0, 0.5 + y);
gContext.lineTo(kPixelWidth, 0.5 + y);
}
/* draw it! */
gContext.strokeStyle = "#ccc";
gContext.stroke();
drawCharacter(currentPosition);
}
document.addEventListener("keydown", function (e) {
var newPosition = { column:0, row:0 };
newPosition.column = currentPosition.column;
newPosition.row = currentPosition.row;
// A
if( e.keyCode == 65 ){
newPosition.column -= 1;
}
// D
if( e.keyCode == 68 ){
newPosition.column += 1;
}
// W
if( e.keyCode == 87 ){
newPosition.row -= 1;
}
// S
if( e.keyCode == 83 ){
newPosition.row += 1;
}
if(isInsideGrid(newPosition)){
currentPosition = newPosition;
}
drawBoard();
}, false);
function WindowLoad(event) {
var canvas = document.createElement('canvas');
canvas.id = 'canvas';
canvas.width = kPixelWidth;
canvas.height = kPixelHeight;
document.body.appendChild(canvas);
var context = canvas.getContext("2d");
gContext = context;
drawBoard();
}
</script>
</head>
<body>
<p>
<a href="index.html">Back to index</a>
</p>
</body>
</html>