• Stars
    star
    285
  • Rank 145,115 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pan and zoom control for Avalonia.

PanAndZoom

Gitter

Build Status CI

NuGet NuGet MyGet

PanAndZoom control for Avalonia

NuGet

PanAndZoom is delivered as a NuGet package.

You can find the NuGet packages here for Avalonia or by using nightly build feed:

  • Add https://www.myget.org/F/panandzoom-nightly/api/v2 to your package sources
  • Alternative nightly build feed https://pkgs.dev.azure.com/wieslawsoltes/GitHub/_packaging/Nightly/nuget/v3/index.json
  • Update your package using PanAndZoom feed

You can install the package for Avalonia based projects like this:

Install-Package Avalonia.Controls.PanAndZoom -Pre

Package Sources

Resources

Using PanAndZoom

MainWindow.xaml

<Window x:Class="AvaloniaDemo.MainWindow"
        xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:paz="using:Avalonia.Controls.PanAndZoom"
        WindowStartupLocation="CenterScreen" UseLayoutRounding="True"
        Title="PanAndZoom" Height="640" Width="640">
    <Grid RowDefinitions="Auto,12,Auto,12,*,12" ColumnDefinitions="50,*,50">
        <StackPanel Orientation="Vertical"
                    HorizontalAlignment="Center" Grid.Row="0" Grid.Column="1">
            <TextBlock Text="F - Fill"/>
            <TextBlock Text="U - Uniform"/>
            <TextBlock Text="R - Reset"/>
            <TextBlock Text="T - Toggle Stretch Mode"/>
            <TextBlock Text="Mouse Wheel - Zoom to Point"/>
            <TextBlock Text="Mouse Left Button Down - Pan"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center" Grid.Row="2" Grid.Column="1">
            <TextBlock Text="PanButton:" VerticalAlignment="Center"/>
            <ComboBox Items="{x:Static paz:ZoomBorder.ButtonNames}"
                      SelectedItem="{Binding #ZoomBorder.PanButton, Mode=TwoWay}"
                      Margin="2">
            </ComboBox>
            <TextBlock Text="Stretch:" VerticalAlignment="Center"/>
            <ComboBox Items="{x:Static paz:ZoomBorder.StretchModes}"
                      SelectedItem="{Binding #ZoomBorder.Stretch, Mode=TwoWay}"
                      Margin="2">
            </ComboBox>
            <TextBlock Text="ZoomSpeed:" VerticalAlignment="Center"/>
            <TextBox Text="{Binding #ZoomBorder.ZoomSpeed, Mode=TwoWay}"
                     TextAlignment="Center" Width="50" Margin="2"/>
            <CheckBox IsChecked="{Binding #ZoomBorder.EnablePan}"
                      Content="EnablePan" VerticalAlignment="Center"/>
            <CheckBox IsChecked="{Binding #ZoomBorder.EnableZoom}"
                      Content="EnableZoom" VerticalAlignment="Center"/>
        </StackPanel>
        <ScrollViewer Grid.Row="4" Grid.Column="1"
                      VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Auto">
            <paz:ZoomBorder Name="ZoomBorder" Stretch="None" ZoomSpeed="1.2"
                            Background="SlateBlue" ClipToBounds="True" Focusable="True"
                            VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <Canvas Background="LightGray" Width="300" Height="300">
                    <Rectangle Canvas.Left="100" Canvas.Top="100" Width="50" Height="50" Fill="Red"/>
                    <StackPanel Canvas.Left="100" Canvas.Top="200">
                        <TextBlock Text="Text1" Width="100" Background="Red" Foreground="WhiteSmoke"/>
                        <TextBlock Text="Text2" Width="100" Background="Red" Foreground="WhiteSmoke"/>
                    </StackPanel>
                </Canvas>
            </paz:ZoomBorder>  
        </ScrollViewer>
    </Grid> 
</Window>

MainWindow.xaml.cs

using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.PanAndZoom;
using Avalonia.Input;
using Avalonia.Markup.Xaml;

namespace AvaloniaDemo
{
    public class MainWindow : Window
    {
        private readonly ZoomBorder? _zoomBorder;

        public MainWindow()
        {
            this.InitializeComponent();
            this.AttachDevTools();

            _zoomBorder = this.Find<ZoomBorder>("ZoomBorder");
            if (_zoomBorder != null)
            {
                _zoomBorder.KeyDown += ZoomBorder_KeyDown;
                
                _zoomBorder.ZoomChanged += ZoomBorder_ZoomChanged;
            }
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        private void ZoomBorder_KeyDown(object? sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.F:
                    _zoomBorder?.Fill();
                    break;
                case Key.U:
                    _zoomBorder?.Uniform();
                    break;
                case Key.R:
                    _zoomBorder?.ResetMatrix();
                    break;
                case Key.T:
                    _zoomBorder?.ToggleStretchMode();
                    _zoomBorder?.AutoFit();
                    break;
            }
        }

        private void ZoomBorder_ZoomChanged(object sender, ZoomChangedEventArgs e)
        {
            Debug.WriteLine($"[ZoomChanged] {e.ZoomX} {e.ZoomY} {e.OffsetX} {e.OffsetY}");
        }
    }
}

Getting zoom ratio

To get current zoom ratio use ZoomX and ZoomY properties.

Getting pan offset

To get current pan offset use OffsetX and OffsetY properties.

Constrain zoom ratio

To constrain zoom ratio use MinZoomX, MaxZoomX, MinZoomY and MaxZoomY properties.

Constrain pan offset

To constrain pan offset use MinOffsetX, MaxOffsetX, MinOffsetY and MaxOffsetY properties.

Enable or disable constrains

To enable or disable constrains use EnableConstrains flag.

License

PanAndZoom is licensed under the MIT license.

More Repositories

1

ChatGPT

A ChatGPT C# client for MacOS, Windows, Linux, Android, iOS and Browser. Powered by Avalonia UI framework.
C#
1,376
star
2

Core2D

A multi-platform data driven 2D diagram editor.
C#
835
star
3

Dock

A docking layout system.
C#
771
star
4

Svg.Skia

An SVG rendering library.
C#
382
star
5

ColorPicker

Avalonia ColorPicker control
C#
174
star
6

BatchEncoder

BatchEncoder is an audio files conversion software.
C++
145
star
7

NodeEditor

A node editor control for Avalonia.
C#
144
star
8

NXUI

NXUI (nex-ui), next-gen UI - Create minimal Avalonia applications using C# 10 and .NET 6, 7 & 8
C#
136
star
9

ReactiveHistory

Reactive undo/redo framework for .NET.
C#
130
star
10

Avalonia.ThemeManager

Theme manager for Avalonia applications.
C#
87
star
11

AvaloniaDockApplication

Sample Dock application.
C#
60
star
12

wavtoac3encoder

WAV to AC3 Encoder an audio encoder which generates compressed audio streams based on ATSC A/52 specification
C++
35
star
13

HackerNews

HackerNews reader.
C#
34
star
14

DataBox

A DataGrid control based on ListBox control
C#
31
star
15

TreeDataGridEx

TreeDataGridEx is an experimental version of TreeDataGrid for Avalonia with added XAML syntax.
C#
30
star
16

SvgToXaml

Svg to xaml conveter.
C#
23
star
17

Avalonia.Skia.Lottie

An lottie animation player control for Avalonia.
C#
22
star
18

SpiroNet

The .NET C# port of libspiro - conversion between spiro control points and bezier's.
C#
19
star
19

SimpleWavSplitter

Split multi-channel WAV files into single channel WAV files.
C#
18
star
20

PackageReferenceEditor

MSBuild, csproj and props package reference editor.
C#
18
star
21

AwesomeWebAvalonia

Awesome Web Avalonia
17
star
22

EffectsDemo

Effect Demo for Avalonia using SkiaSharp
C#
16
star
23

HexView

HexView control for Avalonia.
C#
13
star
24

Draw2D

C#
11
star
25

WebDemos

C#
11
star
26

WpfUnitTests

Unit tests for WPF controls
C#
9
star
27

Spreadsheet

An experimental virtualized rows and columns DataGrid control created using ItemsRepeater/ListBox.
C#
9
star
28

VariableSizedWrapGrid

VariableSizedWrapGrid is a port of Windows UWP version of VariableSizedWrapGrid control for Avalonia based on WPF implemantation.
C#
8
star
29

MicroStationTagExplorer

MicroStation Tag Explorer
C#
7
star
30

RivePlayer

A rive animation player control for Avalonia.
C#
7
star
31

Trace

Trace is an application for transforming bitmaps into vector graphics using BitmapToVector library.
C#
7
star
32

YawPitchRollTransform

Yaw, Pitch and Roll transform for Avalonia
C#
6
star
33

AvaloniaDialog

C#
6
star
34

FastDraw2D

C#
6
star
35

Avalonia.Xaml.Behaviors

C#
6
star
36

AvaloniaWhatKey

Press any key to see Key enum value for Avalonia UI framework.
C#
5
star
37

DotNetCompress

An .NET compression tool. Supported file formats are Brotli and GZip.
C#
5
star
38

AvaloniaTutorialSamples

C#
5
star
39

wrpc

A Graphical User Interface for using the Wasabi Wallet RPC
C#
5
star
40

CustomControlDemo

Custom control demo for AvaloniaUI
C#
5
star
41

AvsDec

Decode avisynth audio stream to raw audio file and split multi-channel WAV files into single channel WAV files.
C++
5
star
42

MfcToolkit

MFC toolkit.
C++
4
star
43

AftenWindowsBuilds

๏ปฟAften Windows Win32 and Win64 builds.
C
4
star
44

LayoutDemo

Experimental layout controls for AvaloniaUI
C#
4
star
45

LanguageEditor

A dedicated xml language translation files editor.
C#
4
star
46

PaletteGenerator

Palette generator using K-Means clustering algorithm for getting colors from images
C#
4
star
47

CompositorDemos

Avalonia compositor demos
C#
4
star
48

BasicDemo

C#
4
star
49

lols

Performance test: LOLs per second
C#
3
star
50

ImplicitCanvasAnimations

C#
3
star
51

SourceGenerators

Svg to C# Source Generators
C#
3
star
52

TemplatedDataGrid

A DataGrid control based on ListBox and Grid panels.
C#
3
star
53

GesturesDemo

Avalonia gesture recognizers generated by ChatGPT
C#
3
star
54

TypeArgumentsDemo

C#
2
star
55

TilePanelDemo

Experimental tile panel control.
C#
2
star
56

XamlAdorner

Set control adorner from xaml in Avalonia
C#
2
star
57

AttachedPropertyDemo

C#
2
star
58

VectorPaint

C#
2
star
59

SmoothPanelSample

C#
2
star
60

XmlParser

Experimental Xml parser for .NET
C#
2
star
61

CustomVirtualizingLayout

C#
2
star
62

KnobControlDemo

C#
2
star
63

DraggableControls

Draggable controls demo.
C#
2
star
64

SimpleDraw

C#
2
star
65

ReactiveNavigation

An navigation control for Avalonia
C#
2
star
66

GridDemo

Experimental tile control for Avalonia based on Grid panel.
C#
2
star
67

TestCustomDrawing

C#
2
star
68

CatML

A CatML is an alternative Xaml controls toolkit for Avalonia.
C#
2
star
69

SqlExtensions

C#
2
star
70

NonAffineImageTransform2

Port of Non-Affine Transforms in 2D by Charles Petzold to WPF
C#
2
star
71

SvgML_Demo

SvgML
C#
2
star
72

MorphingDemo

Geometry morphing demo for AvaloniaUI
C#
1
star
73

SvgGradientsDemo

C#
1
star
74

TreeDemo

TreeDataGrid node tree demo
C#
1
star
75

StylingParentRepro

C#
1
star
76

TypefaceUtil

An OpenType typeface utilities.
C#
1
star
77

BatchEncoderWx

BatchEncoder is an audio files conversion software (wxWidgets Demo).
C++
1
star
78

AvaloniaMinimized

C#
1
star
79

DataGridDemo

C#
1
star
80

LineEditor

Line Editor
C#
1
star
81

DbDemo

C#
1
star
82

RenderLoopTaskDemo

C#
1
star
83

HelloAvaloniaNdc

C#
1
star
84

AvaloniaResponsiveLayout

C#
1
star
85

Svg.Maui

Svg rendering support for Microsoft.Maui.Graphics
C#
1
star
86

WalletWasabiSandbox

C#
1
star
87

LogicSimulator

Asynchronous event-driven digital logic simulator
C#
1
star
88

InertiaDemo

C#
1
star
89

SpringAnimator

C#
1
star
90

PerspectiveDemo

C#
1
star
91

TicTacToeAI

C#
1
star
92

Painter

C#
1
star
93

merge

C#
1
star
94

DirectorySize

C#
1
star
95

Pong

Pong console and Avalonia game generated by ChatGPT
C#
1
star
96

FuzzyXaml

C#
1
star
97

PriorityChart

C#
1
star
98

Breakout

Atari Breakout game made by ChatGPT using Avalonia
C#
1
star
99

Spacewar

Spacewar! game made by ChatGPT using Avalonia
C#
1
star
100

ColorBlender

A .NET library for color matching and palette design.
C#
1
star