private void SetYAnimations(Ellipse g, int offset, LengthAnimationCollection centerYAnimCopy, LengthAnimationCollection centerXAnimCopy, int fallRate, Boolean isShadow) { LengthAnimation anim = centerXAnimCopy[0] as LengthAnimation; anim.By = new Length(windowWidth - 2 * largestRadius); anim.Duration = xAnimDuration; // The following 3 animations simulate a bouncing motion. anim = centerYAnimCopy[0] as LengthAnimation; anim.By = new Length((isShadow ? 1 : -1) * windowHeight - largestRadius); anim = centerYAnimCopy[1] as LengthAnimation; anim.To = g.CenterY; anim = centerYAnimCopy[2] as LengthAnimation; anim.To = g.CenterY; // Animation that causes general motion in the Y direction. anim = centerYAnimCopy[3] as LengthAnimation; anim.By = new Length(-windowHeight * 2 / 3); Double lastValue = anim.By.Value; // Animation that perturbs motion a bit in Y direction to keep it from being regular. anim = centerYAnimCopy[4] as LengthAnimation; anim.By = new Length(lastValue / 10.0); // Animation that makes the balls fall off the screen at different rates. Later balls fall at a later time that earlier balls. anim = centerYAnimCopy[5] as LengthAnimation; anim.Begin = new TimeSyncValue(bounceDuration + new Time(1000 + offset * 100)); anim.By = new Length(windowHeight * fallRate / 100.0); anim.RepeatCount = 1; }
// note: the hard-coded numbers within the code were chosen for visual and optimization reasons. private void Start(object sender, System.EventArgs e) { // Get size of window NavigationApplication NavApp = MSAvalon.Windows.Application.Current as NavigationApplication; NavigationWindow NavWin; NavWin = NavApp.Windows[0] as NavigationWindow; windowWidth = Convert.ToInt32(NavWin.Width.Value); windowHeight = Convert.ToInt32(NavWin.Height.Value); InitColors(); rng = new Random(); // Set time for balls to move across screen/explode xAnimDuration = new Time(4000); bounceDuration = new Time(25200); // Grab the resources from the XAML file. LengthAnimationCollection centerXAnim = rootCanvas.FindResource("myCenterXAnim") as LengthAnimationCollection; LengthAnimationCollection centerYAnim = rootCanvas.FindResource("myCenterYAnim") as LengthAnimationCollection; LengthAnimationCollection radiusAnim = rootCanvas.FindResource("myRadiusAnim") as LengthAnimationCollection; // Create a master timeline that drives the ball animations. timeline = new Timeline(); timeline.Begin = new Time(1000); // Pause to allow window to load timeline.Duration = bounceDuration + new Time(3000); // Added pause after balls fall off screen timeline.Repeated += (new EventHandler(OnRepeated)); timeline.RepeatDuration = Time.Indefinite; // Create a number of balls with animations. balls = new Ellipse[maxBalls]; shadows = new Ellipse[maxBalls]; numBalls = rng.Next(minBalls, maxBalls); for (int i = 0; i < balls.Length; i++) { balls[i] = CreateGlobe(i, false); shadows[i] = CreateGlobe(i, true); // For bursting/falling: int randomGrowth = rng.Next(8, 13); // Growth rate will be between +8 and +13 int fallRate = rng.Next(150, 200); // Fall rate will between 150% and 200% SetGlobeAnimations(balls[i], i, randomGrowth, fallRate, centerXAnim.Copy(), centerYAnim.Copy(), radiusAnim.Copy(), false); SetGlobeAnimations(shadows[i], i, randomGrowth, fallRate, centerXAnim.Copy(), centerYAnim.Copy(), radiusAnim.Copy(), true); // Add the ball to the canvas and to our ball collection. if (i < numBalls) { rootCanvas.Children.Add(balls[i]); rootCanvas.Children.Add(shadows[i]); } } int randomColorMethod = rng.Next(0, 100); // Used to choose how to generate random colors. int randomColorOffset = rng.Next(0, 4); // Used to choose base color for sequenced colors. // Assign colors to the balls ColorGlobes(balls, randomColorOffset, randomColorMethod, false); ColorGlobes(shadows, randomColorOffset, randomColorMethod, true); }
private void SetDelayedStart(LengthAnimationCollection centerAnimCopy, int offset) { foreach (LengthAnimation la in centerAnimCopy) { int delta = 75 * offset; la.Begin = new TimeSyncValue(delta); la.RepeatDuration = bounceDuration - new Time(delta); } }
private void SetGlobeAnimations(Ellipse g, int offset, int randomGrowth, int fallRate, LengthAnimationCollection centerXAnimCopy, LengthAnimationCollection centerYAnimCopy, LengthAnimationCollection radiusAnimCopy, Boolean isShadow) { // Set the animations so that they start staggered in time. SetDelayedStart(centerXAnimCopy, offset); SetDelayedStart(centerYAnimCopy, offset); foreach (LengthAnimation laR in radiusAnimCopy) { laR.Begin = new TimeSyncValue(bounceDuration + new Time(10 * offset)); laR.By = new Length(laR.By.Value * randomGrowth / 10.0); // Have each ball explode to a random size. } SetYAnimations(g, offset, centerYAnimCopy, centerXAnimCopy, fallRate, isShadow); // Bind the animations to the ball's center position. g.SetAnimations(Ellipse.CenterXProperty, centerXAnimCopy); g.SetAnimations(Ellipse.CenterYProperty, centerYAnimCopy); g.SetAnimations(Ellipse.RadiusXProperty, radiusAnimCopy); g.SetAnimations(Ellipse.RadiusYProperty, radiusAnimCopy); }