The way I would do it is with
- rW is width of the rectangle
- rH is height of the rectangle
- rL is the Canvas.Left of the rectangle
- rT is the Canvas.Top of the rectangle
- cW is the canvas width
- cH is the canvas height
First, Scale (S) is determined by the smaller of cW/rW and cH/rH to fit on screen, or larger to fill it.
Then you want the center of the rectangle to be in the center of the canvas after scaling+translating the canvas, so assuming the RenderTransformOrigin
of the Canvas is set to 0,0 for simplicity, after scaling without translation - the center of the rectangle would be in
- (rL + rW/2)*S,
- (rT + rH/2)*S
while you want it at
- cW/2,
- cH/2
so you need to translate by
- tX = cW/2 - (rL + rW/2)*S
- tY = cH/2 - (rT + rH/2)*S
These (S, tX, tY) are your To
values for ScaleX, ScaleY, TranslateX, TranslateY
.
Verified in Blend for
- rW:500
- rH:250
- rL:300
- rT:200
- cW:1366
cH:768
S:2.732
- tX:cW/2 - (rL + rW/2)*S = 683 - 550*2.732 = -819.6
- tY:cH/2 - (rT + rH/2)*S = 384 - 325*2.732 = -503.9
XAML
<Page
x:Class="App40.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App40"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Storyboard x:Name="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="canvas">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2.732"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Storyboard.TargetName="canvas">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2.732"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="canvas">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="-819.6"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="canvas">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="-503.9"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
<Grid
>
<Grid.Clip>
<RectangleGeometry
Rect="0,0,1366,768"/>
</Grid.Clip>
<Canvas x:Name="canvas" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RenderTransformOrigin="0,0" Height="768" Width="1366">
<Canvas.RenderTransform>
<CompositeTransform/>
</Canvas.RenderTransform>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="500" Canvas.Left="300" Canvas.Top="200"/>
</Canvas>
</Grid>
</Page>