Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
1
Indexable
Never
private void Button1Click(object sender, RoutedEventArgs e)
{
    Point relativePoint = buttonAnimation.TransformToVisual(CanvasAnimation).Transform(new Point(0, 0));

    var initialLeft = Canvas.GetLeft(rectAnimation);
    var initialTop = Canvas.GetTop(rectAnimation);
    
    // Calculate the distance from the center of the circle to the initial position of the rectangle
    double distanceFromCenterX = initialLeft + (rectAnimation.Width / 2) - circle.ActualWidth / 2;
    double distanceFromCenterY = initialTop + (rectAnimation.Height / 2) - circle.ActualHeight / 2;

    // Calculate the final position based on the distance from the center and the relative point
    var finalLeft = relativePoint.X - distanceFromCenterX;
    var finalTop = relativePoint.Y - distanceFromCenterY;

    var moveAnimX = new DoubleAnimation(initialLeft, finalLeft, new Duration(TimeSpan.FromSeconds(1)));
    var moveAnimY = new DoubleAnimation(initialTop, finalTop, new Duration(TimeSpan.FromSeconds(1)));

    rectAnimation.BeginAnimation(Canvas.LeftProperty, moveAnimX);
    rectAnimation.BeginAnimation(Canvas.TopProperty, moveAnimY);
}