- commit
- 215d0770d1e22df4456e2048a7a39748a6bfe4e4
- parent
- e1c9d308ad8817b287aacb9611f4d6ccd49abae1
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-26 15:35
resize to fill screen
Diffstat
| M | static/main.js | 49 | ++++++++++++++++++++++++++++++++++++++----------- |
1 files changed, 38 insertions, 11 deletions
diff --git a/static/main.js b/static/main.js
@@ -8,6 +8,8 @@ var gameId = params.get('game');
8 8 var socketProtocol = location.protocol.replace('http', 'ws');
9 9 var socket = new WebSocket(`${socketProtocol}//${location.host}/ws/${gameId}`);
10 10 var pointer = null;
-1 11 var rows = null;
-1 12 var cols = null;
11 13
12 14 var send = function(data) {
13 15 socket.send(JSON.stringify(data));
@@ -28,6 +30,34 @@ var inRect = function(pos, rect, withWalls) {
28 30 }
29 31 };
30 32
-1 33 var binSearch = function(key) {
-1 34 var v2 = 2;
-1 35 while (key(v2) <= 0) {
-1 36 v2 <<= 1;
-1 37 }
-1 38 var v1 = v2 >> 1;
-1 39 while (v2 - v1 > 1) {
-1 40 var v = Math.round((v2 + v1) / 2);
-1 41 if (key(v) > 0) {
-1 42 v2 = v;
-1 43 } else {
-1 44 v1 = v;
-1 45 }
-1 46 }
-1 47 return v1;
-1 48 };
-1 49
-1 50 var updateSize = function() {
-1 51 rows = binSearch(v => {
-1 52 $pre.textContent = '\n'.repeat(v);
-1 53 return document.documentElement.scrollHeight - document.documentElement.clientHeight;
-1 54 });
-1 55 cols = binSearch(v => {
-1 56 $pre.textContent = ' '.repeat(v);
-1 57 return document.body.scrollWidth - document.body.clientWidth;
-1 58 });
-1 59 };
-1 60
31 61 var game = {
32 62 id: -1,
33 63 rects: [],
@@ -140,17 +170,10 @@ var game = {
140 170 },
141 171 };
142 172
143 -1 var getSize = function() {
144 -1 // minimum is 10x10
145 -1 // maximum is 100x30
146 -1 // consider aspect ratio
147 -1 // find font size and rows/columns for best match
148 -1 // probably have to experiment
149 -1 return [10, 20, 100];
150 -1 };
151 -1
152 173 var render = function() {
153 -1 var [fontSize, rows, cols] = getSize();
-1 174 if (!rows || !cols) {
-1 175 updateSize();
-1 176 }
154 177
155 178 var xOffset = -(cols >> 1);
156 179 var yOffset = -(rows >> 1);
@@ -159,7 +182,6 @@ var render = function() {
159 182 yOffset += game.objects[game.id].pos.y;
160 183 }
161 184
162 -1 $pre.style.fontSize = fontSize;
163 185 $pre.innerHTML = '';
164 186
165 187 var commitSpan = (text, color) => {
@@ -304,3 +326,8 @@ var pointerup = function(event) {
304 326
305 327 $dpad.addEventListener('pointerup', pointerup);
306 328 $dpad.addEventListener('pointercancel', pointerup);
-1 329
-1 330 window.addEventListener('resize', () => {
-1 331 rows = null;
-1 332 cols = null;
-1 333 });