-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathappendixMapmaker.qmd
297 lines (235 loc) · 8.61 KB
/
appendixMapmaker.qmd
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Dungeon Maker
**How to use**
- Input the size of the square grid to create the drawing canvas (note this erases anything you may have already drawn)
- Click or drag to create rooms and hallways. The click toggles between white and blue squares.
- Click on a square and then type a character on your keyboard to annotate a square with a letter or number (multiple characters are possible).
- Right click on a square to erase existing text.
```{ojs}
viewof gridsize = Inputs.text({placeholder:"Grid Size"})
viewof grid1 = Inputs.button("Create Grid", {reduce: () => draw(Number(gridsize))})
```
```{ojs}
size = 30
viewof form = Inputs.form(
{
button1: " ",
button2: Inputs.button("Delete Grid", {reduce: () => resetGrid()})
},
{
template: (formParts) => htl.html`
<div>
<h3></h3>
<div style="
width: 400px;
display: flex;
">
${Object.values(formParts)}
</div>
</div>`
}
)
```
:::{.column-screen-inset}
```{=html}
<div style="width:95%; height:600px; border: 1px solid black; resize:both; overflow:auto; margin:10px; align:center;">
<style>
#grid {
display: grid;
gap: 0px;
}
.square {
width: 30px;
height: 30px;
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.3);
background-color: white;
color:rgba(218, 118, 118, 1);
text-align: center;
vertical-align: center;
}
</style>
<div id="grid"></div>
<canvas></canvas>
<script>
function resetGrid() {
var grid = document.getElementById('grid');
while (grid.firstChild) {
grid.removeChild(grid.firstChild);
}
}
function draw (nums) {
// Get the grid element and export button
resetGrid();
var grid = document.getElementById('grid');
// var exportBtn = document.getElementById('exportBtn');
var squareSize = 30
var num = nums
// Create an array to store the colors of each square
var colors = []
var characters = []
// Create the grid of squares
function createGrid(numSquares) {
colors = Array(numSquares * numSquares).fill('white');
characters = Array(numSquares * numSquares).fill('');
for (var i = 0; i < numSquares; i++) {
for (var j = 0; j < numSquares; j++) {
var square = document.createElement('div');
// Initialize the square's string array
square.className = 'square';
square.dataset.squareStrings = JSON.stringify([]);
square.setAttribute('tabindex', '0'); // Make the square focusable
square.addEventListener('mousedown', startDrag);
square.addEventListener('mouseover', changeColor);
square.addEventListener('mouseup', stopDrag);
square.addEventListener('click', changeColor);
square.addEventListener('keydown', function (event) {
console.log ("event", event)
var index = Array.prototype.indexOf.call(grid.children, this);
var squareStrings = JSON.parse(this.dataset.squareStrings);
var char = event.key;
if (
event.code === 'ShiftLeft' ||
event.code === 'ShiftRight'
) {
// Ignore Shift key press
return;
}
if (event.shiftKey && char.length === 1) {
// Adjust the character if Shift key is pressed
char = char.toUpperCase();
}
console.log(char)
squareStrings.push(char);
this.textContent = squareStrings.join('');
this.dataset.squareStrings = JSON.stringify(squareStrings);
characters[index] = this.textContent;
});
square.addEventListener('contextmenu', function (event) {
event.preventDefault();
var index = Array.prototype.indexOf.call(grid.children, this);
this.textContent = '';
this.dataset.squareStrings = JSON.stringify([]);
characters[index] = ""
});
grid.appendChild(square);
grid.style.width = `${numSquares * squareSize}px`
grid.style.gridTemplateColumns = `repeat(${numSquares}, minmax(0, 1fr))`
grid.style.gridTemplateRows = `repeat(${numSquares}, minmax(0, 1fr))`
}
}
}
// Variables to track dragging
var isDragging = false;
// Start dragging the mouse
function startDrag() {
isDragging = true;
changeColor.call(this); // Change color of the square immediately when mouse is pressed down
}
// Change the color of a square
function changeColor() {
var index = Array.prototype.indexOf.call(grid.children, this);
// event.preventDefault();
console.log(event.type)
if (isDragging || (!isDragging && event.type === 'mousedown')) {
var char = String.fromCharCode(event.which);
console.log ("inside first if", char)
if (colors[index] === 'white') {
colors[index] = 'blue';
this.style.backgroundColor = 'blue';
} else {
colors[index] = 'white';
this.style.backgroundColor = 'white';
}
}
/* if (event.type === 'keydown') {
var char = String.fromCharCode(event.which); // Get the character from the pressed key
console.log(char)
this.textContent = char;
characters[index] = char; // Set the text content of the square
} */
}
// Stop dragging the mouse
function stopDrag() {
isDragging = false;
}
// Export the grid as an SVG image
// Export the grid as an SVG image
function exportToSVG() {
var svgString = '<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400">';
var squareSize = 400 / Math.sqrt(colors.length);
for (var i = 0; i < colors.length; i++) {
var x = (i % Math.sqrt(colors.length)) * squareSize;
var y = Math.floor(i / Math.sqrt(colors.length)) * squareSize;
svgString += '<rect x="' + x + '" y="' + y + '" width="' + squareSize + '" height="' + squareSize + '" fill="' + colors[i] + '"/>';
}
svgString += '</svg>';
var blob = new Blob([svgString], { type: 'image/svg+xml' });
var url = URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = url;
link.download = 'grid.svg';
link.click();
URL.revokeObjectURL(url);
}
// Call the functions to create the grid and handle the export button click
createGrid(num); // Specify the number of squares in the grid
return {col:colors, chars: characters}
}
// Export the grid as an image (PNG or JPEG)
// Export the grid as an image (PNG or JPEG)
function drawGridLines(num, ctx, width, height) {
ctx.strokeStyle = "rgba(0,0,0,0.3)";
ctx.lineWidth = 1;
var gridSize = num;
var squareSize = Math.floor(Math.min(width, height) / gridSize);
for (var x = 0; x <= gridSize; x++) {
ctx.beginPath();
ctx.moveTo(x * squareSize, 0);
ctx.lineTo(x * squareSize, height);
ctx.stroke();
}
for (var y = 0; y <= gridSize; y++) {
ctx.beginPath();
ctx.moveTo(0, y * squareSize);
ctx.lineTo(width, y * squareSize);
ctx.stroke();
}
}
function exportToImage(sqSize, format, numsquares, cols, chars) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = numsquares*sqSize;
canvas.height = numsquares*sqSize;
cols.forEach(function(color, index) {
var x = (index % Math.sqrt(cols.length)) * sqSize;
var y = Math.floor(index / Math.sqrt(cols.length)) * sqSize;
ctx.fillStyle = color;
ctx.fillRect(x, y, sqSize, sqSize);
ctx.fillStyle = 'red';
ctx.font = "20px serif";
ctx.textAlign = "center";
ctx.fillText(chars[index], x+15, y+20 );
});
drawGridLines(numsquares, ctx, canvas.width, canvas.height);
var link = document.createElement('a');
if (format === 'png') {
link.href = canvas.toDataURL('image/png');
link.download = 'grid.png';
} else if (format === 'jpeg') {
link.href = canvas.toDataURL('image/jpeg', 0.8);
link.download = 'grid.jpg';
} else {
console.log('Invalid format specified.');
return;
}
link.click();
}
exportBtn.addEventListener('click', function() {
exportToImage('png', num);});
</script>
</div>
```
:::
```{ojs}
Inputs.button("Export to PNG", {reduce: () => exportToImage(size, "png", Number(gridsize), grid1.col, grid1.chars)})
```