From e1e4dfb6507fa90d18c24f7fcd8ec7d2b93415f1 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 25 Oct 2024 18:03:44 +0200 Subject: [PATCH] center zooming on mouse --- showimage.odin | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/showimage.odin b/showimage.odin index b3c5463..16c8c35 100644 --- a/showimage.odin +++ b/showimage.odin @@ -26,9 +26,9 @@ main :: proc() { for !rl.WindowShouldClose() { scroll := rl.GetMouseWheelMove() if scroll > 0 { - zoom *= 2 + change_zoom(zoom) } else if scroll < 0 { - zoom /= 2 + change_zoom(-zoom / 2) } if rl.IsMouseButtonDown(rl.MouseButton.LEFT) { offset += rl.GetMouseDelta() @@ -47,6 +47,13 @@ main :: proc() { rl.CloseWindow() } +change_zoom :: proc(delta: f32) { + mouse_pos := rl.GetMousePosition() + zoom_pos := (mouse_pos - offset) / zoom + zoom += delta + offset = mouse_pos - zoom_pos * zoom +} + zoom_to_power :: proc(size: i32, max: i32) -> i32 { fit: i32 = 2 for fit * size < max {