Skip to content

rune-kit: a zoom/pan/fit canvas for arbitrary content, not only images #720

Done

Description

@edwardmanhattan

What

rune-kit has the right zoom/pan math in image_zoom, but it is only reachable through image previews. A tree, diagram, chart or map-like canvas that wants the same interaction has to reimplement it.

Stage::new(lens, natural, image: El) already accepts an arbitrary El, but the surrounding API is image-specific: natural pixels, "never grown past its pixels", 48 pt padding, zoom-bar labels, Preview/backdrop, and StageZoom is private. At 04389ed there is no public ZoomPan/Canvas state and no issue for one.

What I built, and the two non-obvious parts

A chain-of-command pane: a 784 pt forest in a 320 pt pane, drawn with El::scale/origin/offset. The app side is about 200 lines of state: fit on open until a hand touches it, wheel zoom about the pointer, drag pan, double-click fit, keyboard +/-/0, hit-testing through the transform. The math is exactly StageZoom's. The two things that are not obvious:

  • Min zoom from legibility. 12 pt labels at 0.35 are 4 pt; I clamp at 0.35 and accept that the whole forest does not fit. A generic canvas should expose the effective scale (and maybe a min_scale/on_scale hook) so an app can drop detail instead of drawing unreadable text.
  • Fit and "untouched". Fit again on resize only while the user has not panned or zoomed; after a hand touches it, a resize must not throw the view away.

Proposal

One of:

  1. Make StageZoom/ZoomView public and content-agnostic (content size instead of natural, no controls required); or
  2. A rune-kit::Canvas/ZoomPan container:
let mut zoom = ZoomPan::new();
Canvas::new(&mut zoom, [content_w, content_h], tree_el())
    .fit_on_open(true)
    .min_scale(0.25)
    .build(&kit, size)

with drag/wheel/double-click/keyboard already wired, and a documented hit-test story (El::scale keeps layout size, so pointer coordinates need the inverse transform).

Happy to test; the app implementation is in a public repo (arcons-cc #127) if the shape is useful as a reference.

Activity

  1. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed write-up and the reference implementation.

    You're right about the gap. One correction: StageZoom is exported (rune_kit::StageZoom), but only zoomed(), reset() and zoom_by() are public. Its scale, pan and bounds are private, and its gesture handling runs only inside Stage. Stage itself is built for images:

    • it sizes the element absolutely instead of scaling it, so a tree or diagram would re-lay out at every zoom;
    • it fits with a fixed 48 pt margin and never past 1:1 unless grows is set;
    • it always draws its zoom controls.

    rune_kit::image_zoom::math (ZoomView, zoom_at, pan_by, clamp_view) is public, but it's written around images too.

    I'll look at a content-agnostic zoom/pan container along the lines of your option 2:

    • the content's size instead of natural pixels;
    • optional controls;
    • the effective scale readable from app state, plus a minimum scale;
    • fit on resize only until the user pans or zooms;
    • hit testing through the transform documented, with a helper for it.

    This is related to #997, which covers mounting only the visible part of a large canvas, but it's a separate request.

  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I've opened a pull request for this, along the lines of your option 2: rune_kit::Canvas with a ZoomPan state.

    • Fit until touched: it fits on open and refits on resize until the user pans or zooms. After that, a resize keeps their view. A double-click or 0 fits again.
    • Legibility: .min_scale(..) bounds the fit as well, and below it the content pans. ZoomPan::scale() gives the effective scale, and Canvas::scaled(lens, size, |scale| ..) builds the content for the scale of that frame, so labels can drop out before they become unreadable.
    • Input: a background drag pans, Ctrl/⌘ + wheel (or a pinch on macOS) zooms about the pointer, a plain wheel pans when the content overflows, and +/-/0 work while the pane has focus.
    • Hit testing: controls inside the content take their own presses as drawn. For pane coordinates you handle yourself, ZoomPan::to_content / to_viewport apply the inverse transform.

    I couldn't compile or run the tests where I made this change, so it still needs a build. If you try it on the chain-of-command pane, it would help to hear whether the API fits.

    Fixed in #2180.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions