Merge pull request #1318 from Mnem42/main

zoom.js ui improvements
This commit is contained in:
slweeb 2025-12-07 14:06:47 -05:00 committed by GitHub
commit ddbdfbe07e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 264 additions and 253 deletions

View File

@ -1,19 +1,17 @@
// IIFE because paranoid const zoom_levels = [
(() => {
const zoom_levels = [
0.5, 0.5,
1, 1,
2, 2,
3, 3,
6, 6,
12 12
] ]
window.zoom_data_div = null window.zoom_data_div = null
window.zoom_level = 1 window.zoom_level = 1
window.zoom_panning = [0,0] window.zoom_panning = [0,0]
function handle_zoom(direction){ function handle_zoom(direction){
switch (direction){ switch (direction){
case "in": case "in":
if (!(zoom_level+1 in zoom_levels)) { break; } if (!(zoom_level+1 in zoom_levels)) { break; }
@ -25,9 +23,9 @@
break; break;
} }
rescale() rescale()
} }
function handle_pan(direction, speed){ function handle_pan(direction, speed){
switch (direction){ switch (direction){
case "right": case "right":
zoom_panning[0] -= speed zoom_panning[0] -= speed
@ -43,9 +41,9 @@
break; break;
} }
rescale() rescale()
} }
function gen_button(row, col, html, click, nopos, id){ function gen_button(row, col, html, click, nopos, id){
const elem = document.createElement("button") const elem = document.createElement("button")
@ -70,16 +68,31 @@
} }
return elem return elem
}
function add_css(){
const CSS = `
#zm_data_div { margin-bottom: 10px; }
#canvasDiv { overflow: hidden }
@media(pointer=coarse){
#zm_floater_container#zm_floater_container {
width: 40%;
height: auto;
}
#zm_floater_container:has(#zm_collapse[data-collapsed="true"]){
width: calc(40% / 3);
}
} }
function add_css(){ @media(pointer:coarse) and (orientation:landscape){
const FLOATER_CSS = ` #zm_floater_container#zm_floater_container {
#zm_data_div { width: auto;
margin-bottom: 10px; top: 5px;
}
#zm_floater_container:has(#zm_collapse[data-collapsed="true"]){
width: calc(40% / 3);
} }
#canvasDiv {
overflow: hidden
} }
#zm_floater_container { #zm_floater_container {
@ -88,12 +101,15 @@
right: 5px; right: 5px;
bottom: 5px; bottom: 5px;
height: 24%; height: 100px;
aspect-ratio: 1; aspect-ratio: 1;
max-width: 200px;
max-height: 200px;
border: 2px solid white; border: 2px solid white;
background-color: black; background-color: black;
font-size: 1.2em; font-size: 120%;
button { text-align: center; border: 0px solid white } button { text-align: center; border: 0px solid white }
@ -103,16 +119,12 @@
button:where([data-pos="br"]) { border-width: 2px 0px 0px 2px }; button:where([data-pos="br"]) { border-width: 2px 0px 0px 2px };
} }
#zm_floater_container:has(#zm_collapse[data-collapsed="true"]) { #zm_floater_container:has(#zm_collapse[data-collapsed="true"]) {
height: calc(8% - 1px);
button:not(#zm_collapse) { button:not(#zm_collapse) {
display: none display: none
} }
} }
.zm_corner { .zm_corner { border: 2px solid white; }
border: 2px solid white;
}
#zm_collapse { #zm_collapse {
grid-row: 3; grid-row: 3;
@ -126,12 +138,12 @@
` `
const style_div = document.createElement("style") const style_div = document.createElement("style")
style_div.innerHTML = FLOATER_CSS style_div.innerHTML = CSS
document.head.appendChild(style_div) document.head.appendChild(style_div)
} }
function add_zoom_floaters(){ function add_zoom_floaters(){
const container = document.createElement("div") const container = document.createElement("div")
container.id = "zm_floater_container" container.id = "zm_floater_container"
@ -177,9 +189,9 @@
const canvas_div = document.getElementById("canvasDiv") const canvas_div = document.getElementById("canvasDiv")
canvas_div.appendChild(container) canvas_div.appendChild(container)
} }
function rescale(){ function rescale(){
log_info() log_info()
const scale = zoom_levels[zoom_level] const scale = zoom_levels[zoom_level]
@ -187,9 +199,9 @@
const y = zoom_panning[1] * (pixelSize * scale) const y = zoom_panning[1] * (pixelSize * scale)
gameCanvas.style.transform = `translate(${x}px,${y}px) scale(${scale})` gameCanvas.style.transform = `translate(${x}px,${y}px) scale(${scale})`
} }
function log_info(){ function log_info(){
// Values are negated to make them more intuitive // Values are negated to make them more intuitive
const x_pan = (-zoom_panning[0]).toString().padEnd(4) const x_pan = (-zoom_panning[0]).toString().padEnd(4)
const y_pan = (-zoom_panning[1]).toString().padEnd(4) const y_pan = (-zoom_panning[1]).toString().padEnd(4)
@ -199,9 +211,9 @@
zoom_data_div.innerText = "" zoom_data_div.innerText = ""
zoom_data_div.innerText += `Scale: ${zoom_levels[zoom_level]}x\n` zoom_data_div.innerText += `Scale: ${zoom_levels[zoom_level]}x\n`
zoom_data_div.innerText += `Pan : ${x_pan}, ${y_pan}` zoom_data_div.innerText += `Pan : ${x_pan}, ${y_pan}`
} }
function patch_keybinds(){ function patch_keybinds(){
// Be more granular at higher zoom levels // Be more granular at higher zoom levels
const speed_a = () => zoom_level > 3 ? 5 : 10 const speed_a = () => zoom_level > 3 ? 5 : 10
const speed_b = () => zoom_level > 3 ? 10 : 20 const speed_b = () => zoom_level > 3 ? 10 : 20
@ -218,9 +230,9 @@
keybinds["A"] = () => handle_pan("left", speed_b()) keybinds["A"] = () => handle_pan("left", speed_b())
keybinds["S"] = () => handle_pan("down", speed_b()) keybinds["S"] = () => handle_pan("down", speed_b())
keybinds["D"] = () => handle_pan("right", speed_b()) keybinds["D"] = () => handle_pan("right", speed_b())
} }
function patch_ui(){ function patch_ui(){
add_css() add_css()
add_zoom_floaters() add_zoom_floaters()
@ -257,10 +269,10 @@
</td> </td>
</tr> </tr>
`) `)
} }
// Redefine to give correct numbers when zoomed // Redefine to give correct numbers when zoomed
window.getMousePos = (canvas, evt) => { window.getMousePos = (canvas, evt) => {
if (evt.touches) { if (evt.touches) {
evt.preventDefault(); evt.preventDefault();
evt = evt.touches[0]; evt = evt.touches[0];
@ -275,15 +287,14 @@
y = Math.floor((y / canvas.clientHeight) * (height+1)); y = Math.floor((y / canvas.clientHeight) * (height+1));
return {x:x, y:y}; return {x:x, y:y};
} }
runAfterReset(() => { runAfterReset(() => {
window.zoom_level = 1 window.zoom_level = 1
rescale() rescale()
}) })
runAfterLoad(() => { runAfterLoad(() => {
patch_keybinds() patch_keybinds()
patch_ui() patch_ui()
}) })
})()