In the previous article about UWP Inking platform as input for advanced scenarios, we have been able to
We are about to see how we can easily integrate a zoom functionality in our inking application.
You can start from the code explained in the previous blog post or from a blank UWP application.
We use the InkCanvas, but this time, we incorporate it inside a ScrollViewer:
<ScrollViewer x:Name="rootScrollViewer"
ZoomMode="Enabled" MaxZoomFactor="5" MinZoomFactor="0.5"
HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto">
<InkCanvas x:Name="inkCanvas" />
</ScrollViewer>
The important properties there are:
You can use the mouse wheel to make vary the zoom level, but the touch is much more friendly and the best interaction to use for tablets or Surface devices. The C# code provided for the sample of the previous article was also using touch for drawing; For this scenario, we have to allow only the mouse and the pen to dedicate the touch for the zoom gesture:
public MainPage()
{
this.InitializeComponent();
// Initialize the InkCanvas
inkCanvas.InkPresenter.InputDeviceTypes =
Windows.UI.Core.CoreInputDeviceTypes.Mouse |
Windows.UI.Core.CoreInputDeviceTypes.Pen;
}
Look at the following interactions about our sample. Do you notice the two limitations?
This not really usable for professional drawing or content creation apps, no? Let's use an extremely small magic joker: No code, no work! Just using a Viewbox control :party_popper: :
<Grid>
<ScrollViewer x:Name="rootScrollViewer"
ZoomMode="Enabled" MaxZoomFactor="5" MinZoomFactor="0.5"
HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto">
<Viewbox x:Name="imageViewbox">
<Grid>
<Image x:Name="floorplanImage"
Source="assets/SplashScreen.scale-200.png"/>
<InkCanvas x:Name="inkCanvas" />
</Grid>
</Viewbox>
</ScrollViewer>
</Grid>
With this code, you can zoom/unzoom focusing on a specific area and move freely inside the drawing:
This sample XAML code is the shortest possible for being able to read it with simplicity. Feel free to use some RowDefinition/ColumnDefinition with fixed Height/Width to really control the surface on which you allow the user to draw. Here, the Viewbox provides all the functionalities we need to adapt the content to the available space in the Grid.
You have seen in this sample that we do not accept touch for drawing on the InkCanvas. We use the pen or the mouse for inking. You understand that you will have to make a choice for input types. For example, you can consider the following options.
Just remember that at any time, you can change the inputs allowed. Here is an example to enable or disable touch:
private void EnableTouchDrawing(bool enable)
{
if (enable)
{
inkCanvas.InkPresenter.InputDeviceTypes =
CoreInputDeviceTypes.Mouse |
CoreInputDeviceTypes.Touch |
CoreInputDeviceTypes.Pen;
}
else
{
inkCanvas.InkPresenter.InputDeviceTypes =
CoreInputDeviceTypes.Mouse |
CoreInputDeviceTypes.Pen;
}
}
Ink strokes or XAML Shapes that you are displaying on Canvas are vectorial shapes. Implementing functionalities to be able to move the 'sheet' on which you draw or zoom for a better visualization/precision is almost mandatory for inking applications. Define a strategy for handling and switching inputs is also key to provide the best user experience.
--
As always, the source code of the sample is on GitHub - https://github.com/microsoft/Windows-AppConsult-Samples-UWP/
@sbovo for the AppConsult team.
This article is part of a series exploring concepts about inking and XAML Shapes. Here are all links:
Handling zoom in Inking applications ⇐ You are here
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.