zooming-ish

This commit is contained in:
Luca Conte 2025-02-17 05:17:39 +01:00
parent d30cfc2c59
commit eb0bc9d9b9
4 changed files with 36 additions and 9 deletions

View File

@ -4,7 +4,8 @@ Modular data conversion tools (for developers)
## TODO:
- [ ] zooming
- [X] zooming
- [ ] fix zooming font size
- [ ] controls explanation
- [ ] typed values
- [ ] implicit type conversion

View File

@ -57,6 +57,8 @@ document.addEventListener("mousemove", (e) => {
Mouse.updatePosition(e.clientX, e.clientY);
let delta = Mouse.getDelta();
delta.x *= 1/Camera.zoom;
delta.y *= 1/Camera.zoom;
if (draggingBox != null) {
draggingBox.x += delta.x;
@ -66,18 +68,24 @@ document.addEventListener("mousemove", (e) => {
}
if (draggingCamera) {
Camera.x -= delta.x;
Camera.y -= delta.y;
Box.updateAllPositions();
document.getElementById("canvas").style.backgroundPosition = (-Camera.x) + "px " + (-Camera.y) + "px";
Camera.movePosition(-delta.x, -delta.y);
e.preventDefault();
}
});
document.addEventListener("wheel", (e) => {
let lastZoom = Camera.zoom;
Camera.zoom = Math.min(1, Math.max(0.25, Camera.zoom - e.deltaY / 2000));
document.body.style.setProperty("--zoom", Camera.zoom);
Camera.movePosition(
(Mouse.position.x / lastZoom - Mouse.position.x / Camera.zoom),
(Mouse.position.y / lastZoom - Mouse.position.y / Camera.zoom),
);
});
function isPointInRect(px, py, rx, ry, rw, rh) {
return px >= rx && px <= rx + rw && py >= ry && py <= ry + rh;
}

View File

@ -1,4 +1,17 @@
class Camera {
static x = 0;
static y = 0;
static zoom = 1;
static setPosition(x, y) {
this.x = x;
this.y = y;
Box.updateAllPositions();
document.getElementById("canvas").style.backgroundPosition = (-Camera.x) + "px " + (-Camera.y) + "px";
}
static movePosition(x, y) {
this.setPosition(this.x + x, this.y + y);
}
}

View File

@ -27,6 +27,7 @@ body {
flex-direction: column;
align-items: stretch;
justify-content: flex-start;
--zoom: 1;
}
#canvas {
@ -38,6 +39,7 @@ body {
background-image: url("icons/background.svg");
background-size: 80px 80px;
z-index: 0;
zoom: var(--zoom);
}
#playground {
@ -48,6 +50,8 @@ body {
width: 100%;
height: 100%;
overflow: none;
zoom: var(--zoom);
font-size: calc(1em * var(--zoom));
}
@ -110,6 +114,7 @@ input, textarea, select, button {
background-color: #25252577;
padding: 5px;
border: 1px solid #111;
font-size: inherit;
}
button:hover, select:hover {