Eru Iluvatar
Eru Iluvatar

Reputation: 353

Animate RenderTransform in WPF

I've got a question regarding animations for WPF in C# code!
I have an handler function for the MouseWheel event. It just checks if you 'zoomed in' or 'zoomed out'. Just have a look at the code, the important line here is the 4th line, where the RenderTransform is set.

    private void ZoomPanCanvas_MouseWheel(object sender, MouseWheelEventArgs e) {
        var factor = (e.Delta > 0) ? (1.1) : (1 / 1.1);
        currrentScale = factor * currrentScale;
        mNetworkUI.RenderTransform = new ScaleTransform(currrentScale, currrentScale);
        var pos = e.GetPosition(mNetworkUI);
        mNetworkUI.Width = ZoomPanCanvas.ActualWidth / currrentScale;
        mNetworkUI.Height = ZoomPanCanvas.ActualHeight /currrentScale;
        var dummyTransform = new ScaleTransform(factor, factor, pos.X, pos.Y);
        var offSet = new Point(dummyTransform.Value.OffsetX, dummyTransform.Value.OffsetY);
        mNetworkUI.ViewModel.Network.SetTransformOffset(offSet);
    }

I kept the rest of the function in the code for completeness.
What I want to do, is to animate this change of the RenderTransform!

I already tried to use a Storyboard (with UIElement.RenderTransformProperty set). Best result was a not-animated change of the RenderTransform (but it was not the same result as this 4th line from the code achieves).

Maybe you can help me, I already tried a few suggested methods from the other questions here.

Edit:

Here's the non-working attempt and at first the chenged code from above:

    private void ZoomPanCanvas_MouseWheel(object sender, MouseWheelEventArgs e) {
        var factor = (e.Delta > 0) ? (1.1) : (1 / 1.1);
        currrentScale = factor * currrentScale;
        ///mNetworkUI.RenderTransform = new ScaleTransform(currrentScale, currrentScale);
        Helper.Animations.RenderTransformAnimation(mNetworkUI, new ScaleTransform(currrentScale, currrentScale));
        var pos = e.GetPosition(mNetworkUI);
        mNetworkUI.Width = ZoomPanCanvas.ActualWidth / currrentScale;
        mNetworkUI.Height = ZoomPanCanvas.ActualHeight /currrentScale;
        var dummyTransform = new ScaleTransform(factor, factor, pos.X, pos.Y);
        var offSet = new Point(dummyTransform.Value.OffsetX, dummyTransform.Value.OffsetY);
        mNetworkUI.ViewModel.Network.SetTransformOffset(offSet);
    }

And it follows the static helper function:

    public static void RenderTransformAnimation(FrameworkElement element, Transform newTransform) {
        MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
        var key1 = new DiscreteMatrixKeyFrame(element.RenderTransform.Value, KeyTime.FromPercent(0));
        var key2 = new DiscreteMatrixKeyFrame(newTransform.Value, KeyTime.FromPercent(1));

        Storyboard.SetTarget(anim, element.RenderTransform);
        Storyboard.SetTargetProperty(anim, new PropertyPath(UIElement.RenderTransformProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(anim);
        sb.Duration = AnimationDuration;
        sb.Begin();
    }

It always throws an exception on the sb.Begin() call, telling me, something was not ok with my 'PropertyPath'. I don't know how to do it :(. I mean, there is no way to directly create a "TransformAnimation", right? Only MatrixAnimations are available...

Upvotes: 1

Views: 2669

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

I've provided a simple animation of a ScaleTransform below. In the interests of providing a 'minimal' example, I only adjust the scale; I don't do any of the offset calculations that you're doing based on mouse position. You should be able to figure out where to go from here:

private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
    var factor = e.Delta > 0d ? 1.1d : 0.9d;

    var t = mNetworkUI.RenderTransform as ScaleTransform;
    if (t == null)
    {
        mNetworkUI.RenderTransform = t = new ScaleTransform(1d, 1d)
                                         {
                                             CenterX = 0.5d,
                                             CenterY = 0.5d
                                         };
    }

    var oldScale = (double)t.GetAnimationBaseValue(ScaleTransform.ScaleXProperty);
    var newScale = oldScale * factor;

    //
    // Make sure `GetAnimationBaseValue()` reflects the `To` value next time
    // (needed to calculate `oldScale`, and for the animation to infer `From`).
    //
    t.ScaleX = newScale;
    t.ScaleY = newScale;

    var animation = new DoubleAnimation
                    {
                        To = newScale,
                        Duration = TimeSpan.FromSeconds(0.5d),
                        DecelerationRatio = 0.5d,
                        FillBehavior = FillBehavior.Stop
                    };

    //
    // Use `HandoffBehavior.Compose` to transition more smoothly if an animation
    // is already in progress.
    //
    t.BeginAnimation(
        ScaleTransform.ScaleXProperty, 
        animation, 
        HandoffBehavior.Compose);

    t.BeginAnimation(
        ScaleTransform.ScaleYProperty,
        animation,
        HandoffBehavior.Compose);
}

Upvotes: 4

Related Questions