UI Toolbox logoUI Toolbox
Support

PanZoomView

PanZoomView in UIToolbox is a viewport whose content pans and zooms. Use it for an image viewer, a large world map with pins, a skill tree, a level select or a canvas of notes on a grid. Put any elements in it: they move and scale together, and still take clicks.

A PanZoomView that shows a large map zoomed in on two of its pins, the castle and the mineA PanZoomView that shows a large map zoomed in on two of its pins, the castle and the mine

Video tutorial: Pan and zoom. All videos: Video tutorials.

<tb:PanZoomView name="map" min-zoom="0.2" max-zoom="3">
  <ui:VisualElement
      class="world-map"
      style="width: 1536px; height: 1536px;">
    <ui:Button
        name="castle"
        text="Castle"
        style="position: absolute; left: 690px; top: 520px;" />
  </ui:VisualElement>
</tb:PanZoomView>
var view = root.Q<PanZoomView>("map");
// fits the map once the view has a size
view.FrameAll();

// A pin zooms the map to its place
var castle = root.Q<Button>("castle");
castle.clicked += () =>
    view.FrameRect(
        new Rect(castle.layout.center - new Vector2(120, 120),
            new Vector2(240, 240)));

// The map point under a click
view.RegisterCallback<ClickEvent>(evt =>
    Debug.Log(view.ViewToContent(view.WorldToLocal(evt.position))));
view.ViewChanged += () => zoomLabel.text = $"{view.Zoom:P0}";

With no setting, the view pans with a middle or right drag, zooms with the wheel, keeps the content in view, and shows the zoom in the bottom right corner.

Settings

Setting (UXML) Type Default Does
min-zoom float 0.25 The smallest zoom.
max-zoom float 4 The largest zoom.
zoom float 1 The zoom: 1 shows a content pixel as one pixel. Clamped to the limits. A change keeps the content point at the center of the view in place.
pan-button PanZoomButton MiddleOrRight The mouse buttons that pan with a drag: MiddleOrRight, Middle, Right, Left, Any or None. A right click with no drag still opens a context menu. Left pans from the background and from content that does not take the press. Space or Alt with a left drag and a touch drag pan with any choice.
wheel-zoom bool true The wheel zooms around the pointer. Off lets the wheel reach the elements around the view.
clamp-to-content bool true The content cannot leave the view, and content smaller than the view stays centered. Off lets the view pan anywhere, as on an endless canvas.
show-grid bool false Draws a grid under the content that pans and zooms with it. A major line shows every 5 lines.
grid-size float 20 The distance between grid lines, in content pixels.
show-scroll-bars bool false Shows scroll bars along the bottom and the right edges, for the content outside the view.
frame-margin float 0 The space that FrameAll and FrameRect leave around the content, in content pixels.
show-zoom-indicator bool true Shows the zoom indicator in the bottom right corner. See Zoom indicator.

The content is the box of the content element and its children, so children placed with position: absolute count too.

Zoom indicator

The zoom indicator in the bottom right corner shows the zoom in percent between a minus and a plus button. The buttons zoom out and in around the center of the view, and a click on the percent goes back to 100 %. The indicator is faint (35 % opacity) until the pointer is over it. It shows in full for 1.5 seconds after the user zooms with the wheel, a pinch, its buttons or the keys, and while one of its buttons has the focus; then it fades back. A zoom by code shows the new percent and leaves the indicator faint.

A click on a button leaves the focus on the view, so the keys of the view go on working. Tab reaches the buttons, and Enter, Space or a gamepad button presses them. Each button has a tooltip that names it. Turn the indicator off with show-zoom-indicator="false", for example when the game shows the zoom its own way.

Methods

Method Does
ZoomAt(zoom, viewPoint) Zooms and keeps the content point at a view point, such as the pointer, in place.
FrameAll() Fits the whole content in the view and centers it, zoomed in at most to 1. Before the view has a size, it fits once it gets one, unless a later frame, pan or zoom comes first.
FrameRect(contentRect) Fits a content rectangle in the view and centers it, within the zoom limits. Before the view has a size, it fits once it gets one, unless a later frame, pan or zoom comes first.
ViewToContent(point), ContentToView(point) Convert between the pixels of the view and the content.
TryGetContentBounds(out bounds) The box of the content, in content pixels. False while the content has no size. The fits, the clamp and the scroll bars use it, so a subclass can override it to measure its own content.

Pan is the content point at the top left corner of the view. Content is the element that moves and scales; it is the contentContainer, so Add puts children there. IsDragging is true while the user pans or pinches.

Events

Event When
ViewChanged The pan or the zoom changes, by the user or by code.

Mouse, touch and keyboard

Input Does
Middle or right drag Pans. A right press pans only once it moves a few pixels.
Space or Alt + left drag Pans.
Mouse wheel Zooms around the pointer.
One-finger drag Pans.
Two-finger pinch Zooms around the fingers, and pans with them.
Arrow keys or gamepad D-pad Pan by 40 pixels.
Gamepad right stick Pans while the view has the focus.
Gamepad triggers The right trigger zooms in and the left trigger zooms out around the center, while the view has the focus.
+ or =, - Zoom in or out around the center.
Home or F Fits the content.
Minus, plus or the percent of the zoom indicator Zoom out or in around the center, or go back to 100 %.
Escape Cancels a drag and puts the view back. With no drag, the press goes on to the elements around the view, for example to close a dialog.

The view takes the focus with Tab. The keys act only while the view itself has the focus, so a field inside the content keeps its keys.

Performance

Styling

Class Element
tb-pan-zoom-view The view (grows; clips; background, border). With --panning during a drag and --scroll-bars with scroll bars.
tb-pan-zoom-view__grid The grid layer under the content.
tb-pan-zoom-view__content The element that pans and zooms.
tb-pan-zoom-view__scroller A scroll bar, with --horizontal or --vertical.
tb-pan-zoom-view__zoom The zoom indicator (bottom right; a row; faint until the pointer is over it). With --active while it shows in full.
tb-pan-zoom-view__zoom-button A button of the zoom indicator, with tb-pan-zoom-view__zoom-out, tb-pan-zoom-view__zoom-percent or tb-pan-zoom-view__zoom-in.
Variable On Description
--tb-pan-zoom-view-grid-color tb-pan-zoom-view The color of the minor grid lines.
--tb-pan-zoom-view-grid-major-color tb-pan-zoom-view The color of the major grid lines.
/* A blueprint canvas */
.planner .tb-pan-zoom-view {
    background-color: rgb(20, 40, 80);
    --tb-pan-zoom-view-grid-color: rgba(255, 255, 255, 0.08);
    --tb-pan-zoom-view-grid-major-color: rgba(255, 255, 255, 0.2);
}

/* The zoom indicator in the top left corner, always in full */
.planner .tb-pan-zoom-view__zoom {
    right: auto;
    bottom: auto;
    left: 8px;
    top: 8px;
    opacity: 1;
}

The PanZoomDemo scene shows an image viewer with scroll bars, a large map with pins and a canvas of notes on a grid.

The view of a NodeGraph is a PanZoomView, so a graph pans, zooms and fits the same way.

For a top-down map of the world around the player, see the Minimap and its FreePan mode.