public void MakeCircleAnimation(FrameworkElement animatedElement, double width, double height, TimeSpan timeSpan) { EllipseGeometry ellipseGeometry = new EllipseGeometry(); ellipseGeometry.RadiusX = 0; ellipseGeometry.RadiusY = 0; double centrex = width / 2; double centrey = height / 2; ellipseGeometry.Center = new Point(centrex, centrey); animatedElement.Clip = ellipseGeometry; //The most important line double halfWidth = width / 2; double halfheight = height / 2; DoubleAnimation a = new DoubleAnimation(); a.Completed += new EventHandler(a_Completed); a.From = 0; a.To = Math.Sqrt(halfWidth * halfWidth + halfheight * halfheight); a.Duration = new Duration(timeSpan); ellipseGeometry.BeginAnimation(EllipseGeometry.RadiusXProperty, a); ellipseGeometry.BeginAnimation(EllipseGeometry.RadiusYProperty, a); }
/// <summary> /// Funkja przygotowywująca animację na podstawie rozmiaru obiektu canvas /// </summary> /// <param name="geometry">Aktualny obiekt geometry</param> private void PrepareAnimation(EllipseGeometry geometry) { double space = playField.ActualWidth / baloons; j += (int)space + 50; if (j > playField.ActualWidth) { j = Convert.ToInt32(j % playField.ActualWidth); } geometry.Center = new Point(j, -50); up = new PointAnimationUsingPath(); pBezierSegment = new PolyBezierSegment(); animationPath = new PathGeometry(); pFigure = new PathFigure(); pFigure.StartPoint = new Point(j, -50); for (int i = 0; (i < playField.ActualHeight); i += 25) { int randomSign = random.Next(1); if (randomSign == 0) { pBezierSegment.Points.Add(new Point(j - random.Next(30), i)); } else if (randomSign == 1) { pBezierSegment.Points.Add(new Point(j + random.Next(30), i)); } } pBezierSegment.Points.Add(new Point(j, playField.ActualHeight + 50)); pFigure.Segments.Add(pBezierSegment); animationPath.Figures.Add(pFigure); up.PathGeometry = animationPath; if (timesIndex != times.Count) { up.BeginTime = TimeSpan.FromSeconds(Convert.ToInt32(times[timesIndex])); timesIndex++; } up.Duration = TimeSpan.FromSeconds(10); up.SpeedRatio = 0.5; up.RepeatBehavior = RepeatBehavior.Forever; animationPath.Freeze(); geometry.BeginAnimation(EllipseGeometry.CenterProperty, up); }