Kyle Abrahams, PMP
asked on
Zoom Calculations
Hi all,
I'm trying to implement zooming on my page. I would like the center to be wherever the mouse pointer currently is. Not seeing the solution, but I know it has to do with the ratios in terms of screen coords and real coords.
Explinations would be extremely helpful.
XAML below, code is code behind.
XAML:
<UserControl x:Class="FloorPlan.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MouseMove="Page_MouseMove" >
<UserControl.Resources>
<Storyboard x:Name="ZoomStoryboard">
<DoubleAnimation x:Name="ZoomAnimationX"
Storyboard.TargetName="Flo orScale"
Storyboard.TargetProperty= "ScaleX"
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="ZoomAnimationY"
Storyboard.TargetName="Flo orScale"
Storyboard.TargetProperty= "ScaleY"
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="CenterAnimationX"
Storyboard.TargetName="Flo orScale"
Storyboard.TargetProperty= "CenterX"
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="CenterAnimationY"
Storyboard.TargetName="Flo orScale"
Storyboard.TargetProperty= "CenterY"
Duration="0:0:0.2"/>
</Storyboard>
</UserControl.Resources>
<Canvas x:Name="Floor" MouseWheel="Floor_MouseWhe el">
<Canvas.RenderTransform>
<ScaleTransform x:Name="FloorScale" ScaleX="1" ScaleY="1"/>
</Canvas.RenderTransform>
<Grid ShowGridLines="false" x:Name="LayoutRoot" Background="White" >
</Grid>
<TextBlock x:Name="tbMouseX" Canvas.Left="100" Canvas.Top="100">
</TextBlock>
<TextBlock x:Name="tbMouseY" Canvas.Left="150" Canvas.Top="100">
</TextBlock>
</Canvas>
</UserControl>
I'm trying to implement zooming on my page. I would like the center to be wherever the mouse pointer currently is. Not seeing the solution, but I know it has to do with the ratios in terms of screen coords and real coords.
Explinations would be extremely helpful.
XAML below, code is code behind.
XAML:
<UserControl x:Class="FloorPlan.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MouseMove="Page_MouseMove"
<UserControl.Resources>
<Storyboard x:Name="ZoomStoryboard">
<DoubleAnimation x:Name="ZoomAnimationX"
Storyboard.TargetName="Flo
Storyboard.TargetProperty=
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="ZoomAnimationY"
Storyboard.TargetName="Flo
Storyboard.TargetProperty=
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="CenterAnimationX"
Storyboard.TargetName="Flo
Storyboard.TargetProperty=
Duration="0:0:0.2"/>
<DoubleAnimation x:Name="CenterAnimationY"
Storyboard.TargetName="Flo
Storyboard.TargetProperty=
Duration="0:0:0.2"/>
</Storyboard>
</UserControl.Resources>
<Canvas x:Name="Floor" MouseWheel="Floor_MouseWhe
<Canvas.RenderTransform>
<ScaleTransform x:Name="FloorScale" ScaleX="1" ScaleY="1"/>
</Canvas.RenderTransform>
<Grid ShowGridLines="false" x:Name="LayoutRoot" Background="White" >
</Grid>
<TextBlock x:Name="tbMouseX" Canvas.Left="100" Canvas.Top="100">
</TextBlock>
<TextBlock x:Name="tbMouseY" Canvas.Left="150" Canvas.Top="100">
</TextBlock>
</Canvas>
</UserControl>
Private Sub Floor_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseWheelEventArgs)
Static _ZoomSteps As Integer = 0
Static _Zoom As Double = 0
Static _CenterX As Double
Static _CenterY As Double
Dim deltaZoom As Double = e.Delta
_Zoom += deltaZoom / 150
If _Zoom <= 1 Then
_Zoom = 1
End If
If deltaZoom >= 0 Then
If _ZoomSteps = -1 Then
_CenterX = 0
_CenterY = 0
_ZoomSteps = 0
Else
_CenterX = (_CenterX * (Math.Abs(_ZoomSteps) + mouseX) / (Math.Abs(_ZoomSteps + 1)))
_CenterY = (_CenterY * (Math.Abs(_ZoomSteps) + mouseY) / (Math.Abs(_ZoomSteps + 1)))
_ZoomSteps += 1
End If
Else
If _ZoomSteps = 1 Then
_CenterX = 0
_CenterY = 0
_ZoomSteps = 0
Else
_CenterX = (_CenterX * Math.Abs(_ZoomSteps) - mouseX) / (Math.Abs(_ZoomSteps - 1))
_CenterY = (_CenterY * Math.Abs(_ZoomSteps) - mouseY) / (Math.Abs(_ZoomSteps - 1))
_ZoomSteps -= 1
End If
End If
ZoomAnimationX.[To] = _Zoom
ZoomAnimationY.[To] = _Zoom
CenterAnimationX.To = Math.Abs(_CenterX)
CenterAnimationY.To = Math.Abs(_CenterY)
ZoomStoryboard.Begin()
mouseX = CType(_CenterX, Integer)
mouseY = CType(_CenterY, Integer)
tbMouseX.Text = mouseX.ToString()
tbMouseY.Text = mouseY.ToString()
End Sub
Private Sub Page_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
mouseX = e.GetPosition(Nothing).X
mouseY = e.GetPosition(Nothing).Y
tbMouseX.Text = mouseX.ToString()
tbMouseY.Text = mouseY.ToString()
End Sub
What, exactly, is the question?
ASKER
How do you zoom to the mouse pointer correctly? (similiar to google maps)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.