Visual effects while displaying an image. Part II

by gleblanc 21. September 2008 18:13

In this second (but not last) post, we will discuss different techniques for animating the clipping area.
The image is a painting ("The persistence of memory") from Salvador Dali (1904-1989), the famous Spanish (Catalan) surrealist painter.

In this post, we'll animate the clipping area using an old animation technique, ie the timer.


Project / source code

We have two identical images : one, in the background, with an opacity of 0.3 and the other, on top, with a clipping area limited to a growing ellipse that will appear (at random location) at regular interval :
 <Image Source="Dali.jpg" Width="500" Height="363" Opacity="0.3" />
 <Image Source="Dali.jpg" Width="500" Height="363" >
  <Image.Clip>
   <EllipseGeometry x:Name="ellGeo" />
  </Image.Clip>
 </Image>

The animation is here based on the timer, a technique used by veterans of animations in computer programs. Though animation techniques introduced in WPF and Silverlight have to be privileged (see next post), the technique based on the timer is still there (do not throw it away too fast). To use the timer :
 using System.Windows.Threading;
 .....
 DispatcherTimer timer;  // outside of function

In the function that handles the Loaded event :
 private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
 {
  timer = new DispatcherTimer();
  timer.Interval = new TimeSpan(0, 0, 0, 0, 50); // 50 millisec
  timer.Tick += new EventHandler(timer_Tick);
  timer.Start();
 }
The following function is executed at regular interval :   
  void timer_Tick(object sender, EventArgs e)
  {
   .....  
  }

Our timer_Tick function is simple :

  • we reinitialize the EllipseGeometry properties every 100 ticks : new Center (at random location) and reduced radius (also a random small value), 
  • we make this ellipse bigger every tick.

  Random rndm;
  .....
  rndm = new Random();
  .....
  int N;
  void timer_Tick(object sender, EventArgs e)
  {
   if (N % 100==0)
   {
    X = rndm.Next(500); Y = rndm.Next(380);
    W = rndm.Next(10);
    ellGeo.Center = new Point(X, Y);
   }
   W += 5; ellGeo.RadiusX = ellGeo.RadiusY = W;
   N++;
  }

Now, let's go a step further : the clipping area is now made from several basic figures, here several growing ellipses, that are dynamically added to the clipping area :


Project / source code

Since the clipping area is build dynamically (in the timer_Tick function), the XAML is just :
  <Image Source="Dali.jpg" Width="500" Height="363" Opacity="0.3" />
  <Image Source="Dali.jpg" Width="500" Height="363" >
   <Image.Clip>
    <GeometryGroup x:Name="geoGroup" FillRule="Nonzero"  />
   </Image.Clip>
  </Image>

It's important to specify NonZero in the FillRule property. Otherwise, the intersection of two or several ellipses is not part of the clipping area.

The timer_Tick function becomes :
   void timer_Tick(object sender, EventArgs e)
   {
    if (N % 100 == 0) geoGroup.Children.Clear(); // reset clipping area
    if (N % 2 == 0)
    {
     // create a new hole in the clipping area
     EllipseGeometry eg = new EllipseGeometry();
     eg.Center = new Point(rndm.Next(0, 500), rndm.Next(0, 360)); 
     eg.RadiusX = eg.RadiusY = rndm.Next(5, 20);
     geoGroup.Children.Add(eg);
    }
    // grow and move slightly each ellipse
    foreach (EllipseGeometry g in geoGroup.Children)
    {
     g.RadiusX += 2; g.RadiusY += 2;
     g.Center = new Point(g.Center.X, g.Center.Y - 1);
    }
    N++;
   }

In the next post, we will use the Silverlight animation technique to animate the clipping area.

Tags:

Comments

9/23/2008 5:03:56 PM #

Trackback from Community Blogs

Silverlight Cream for September 22, 2008 -- #374

Community Blogs

9/23/2008 10:37:37 PM #

Pingback from mgalinks.wordpress.com

2008 September 23 - Links for today « My (almost) Daily Links

mgalinks.wordpress.com

9/24/2008 10:31:39 PM #

Also wonderful
Thanks again
mike

mike greenway United States

1/20/2009 5:49:16 PM #

thanks for the source to make that.

Busby Seo Test United States

7/14/2009 3:05:33 PM #

Hey I love your style I will subscribe for your feed please keep posting!

Audemars Piguet United States

7/24/2009 9:27:06 PM #

I think you have to improve a bit the design and usability of your blog.

Roberto Cavalli United States

8/11/2009 3:41:31 AM #

I keep listening to the news talk about getting free online grant applications so I have been searching around for the best site to get one.

cash loans United States

8/11/2009 3:41:35 AM #

You are a very smart person!

cash loans United States

8/13/2009 12:08:19 PM #

Where can I get free downloads of educational animations?

SEO United States

8/17/2009 9:42:04 AM #

Admiring the time and effort you put into your blog and detailed information you offer!

My Website On Google United States

8/17/2009 9:42:07 AM #

Great insights. I loved to read your article. You must be putting a lot of time into your blog!

My Website On Google United States

8/19/2009 12:22:42 PM #

Did you know any companies or website’s that act as an mediator for outsourcing 3D Animation and Visual effect?

seo company United States

9/22/2009 11:01:47 AM #

Did you know any companies or website’s that act as an mediator for outsourcing 3D Animation and Visual effect?

cheap laptop United States

9/24/2009 9:44:41 AM #

Hi,
   Which are the best institutes for diploma in visual effects in mumbai ?

Social network marketing United States

9/30/2009 11:35:31 AM #

Trackback from Community Blogs

Silverlight Cream for September 22, 2008 -- #374

Tiffany Rings People's Republic of China

10/2/2009 4:15:03 PM #

ohh�nice post but really?/? Tong

personal loan United States

10/2/2009 4:15:11 PM #

I wish getting over a broken heart can be so easy as following a few steps.. but its not� Frown

personal loan United States

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Gerard Leblanc is the author of several books (in french) on C++, C#, .NET and Silverlight (Eyrolles, Paris as publisher). See www.gleblanc.eu as the companion web site for these books (included sample programs).
He is Microsoft MVP for Silverlight.

MVP logo