Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Created November 21, 2024 21:07
Show Gist options
  • Save asinghvi17/f18ed85881fafc7c44f4d106d82f2e23 to your computer and use it in GitHub Desktop.
Save asinghvi17/f18ed85881fafc7c44f4d106d82f2e23 to your computer and use it in GitHub Desktop.
a way to zoom in to a rectangle in 2d
using Makie
zoom_to_extent!(ax, target::Rect, factor = 1.0) = zoom_to_extent!(ax, ax.finallimits[], target, factor)
function zoom_to_extent!(ax, current_lims::Rect, target_lims::Rect2, factor = 1.0)
# Get current limits
current_xlims = current_lims.origin[1], current_lims.origin[1] + current_lims.widths[1]
current_ylims = current_lims.origin[2], current_lims.origin[2] + current_lims.widths[2]
# Calculate target limits
target_xlims = target_lims.origin[1], target_lims.origin[1] + target_lims.widths[1]
target_ylims = target_lims.origin[2], target_lims.origin[2] + target_lims.widths[2]
# Interpolate between current and target
new_xmin = current_xlims[1] + (target_xlims[1] - current_xlims[1]) * factor
new_xmax = current_xlims[2] + (target_xlims[2] - current_xlims[2]) * factor
new_ymin = current_ylims[1] + (target_ylims[1] - current_ylims[1]) * factor
new_ymax = current_ylims[2] + (target_ylims[2] - current_ylims[2]) * factor
# Set new limits
xlims!(ax, new_xmin, new_xmax)
ylims!(ax, new_ymin, new_ymax)
end
fig = Figure()
ax = Axis(fig[1, 1]; aspect = DataAspect())
using CairoMakie
record(fig, "zoom.mp4", LinRange(0, 1, 120)) do i
zoom_to_extent!(ax, Rect2((0,0), (10,10)),Rect2((0,1), (1,1)), i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment