Visual effects while displaying an image. Part I

by gleblanc 17. September 2008 13:45

Let's review different visual effects while displaying an image, from simple to more sophisticated. In this serie, we'll discuss animation, clipping area and opacity mask. Here, a first post dedicated to clipping. 

The image used in this post is a painting from Alfred Sisley (1839-1899), a French impressionnist painter, though British citizen (he asked for French citizenship but died before the Administration took a decision, due to missing papers - nothing is never new in this world).


Project / source code

The effect is based on a clipping area. Nothing is displayed outside the clipping area, here an ellipse. We give a name (ellGeo) to this ellipse and animate the RadiusX and RadiusY properties. When stb1.Begin() is executed, RadiusX jumps from 0 to 400 in two seconds. Simultaneously, RadiusY jumps from 0 to 300 (to be sure that the 400x300 rectangle is fully inside the ellipse).

 <Image x:Name="img" Source="Sisley.jpg" Width="400" Height="300" >
  <Image.Clip>
   <EllipseGeometry x:Name="ellGeo" Center="200, 150" RadiusX="400" RadiusY="300" />
  </Image.Clip>
  <Image.Resources>
   <Storyboard x:Name="stb1">
    <DoubleAnimation Storyboard.TargetName="ellGeo" Storyboard.TargetProperty="RadiusX" 
                     From="0" To="400" Duration="0:0:2" />
    <DoubleAnimation Storyboard.TargetName="ellGeo" Storyboard.TargetProperty="RadiusY" 
                     From="0" To="300" Duration="0:0:2" />
   </Storyboard>
  </Image.Resources>
 </Image>

Instead of giving a name to EllipseGeometry, we could write :

 <Image x:Name="img" Source="Sisley.jpg" Width="400" Height="300" >
  <Image.Clip>
   <EllipseGeometry Center="200, 150" RadiusX="400" RadiusY="300" />
  </Image.Clip>
  <Image.Resources>
   <Storyboard x:Name="stb1">
    <DoubleAnimation Storyboard.TargetName="img"
                     Storyboard.TargetProperty="(UIElement.Clip).(EllipseGeometry.RadiusX)"
                     From="0" To="400" Duration="0:0:2" />
    <DoubleAnimation Storyboard.TargetName="img"
                     Storyboard.TargetProperty="(UIElement.Clip).(EllipseGeometry.RadiusY)"
                     From="0" To="300" Duration="0:0:2" />      
   </Storyboard>
  </Image.Resources>
 </Image> 

Fine for the EllipseGeometry, thanks to its two properties (RadiusX and RadiusY) that are of double type. Not so easy with RectangleGeometry that has a property (Rect) with four values ! It's easier to use the PathGeometry.


Project / source code

As PathGeometry, we define one PathFigure (closed figure starting at upper-left corner) and made of three line segments (remember, it's a closed figure). Each line ending point is specified in the Point property. of course of type Point. Our animation will now be based on PointAnimation :    

  <Image x:Name="img" Source="Sisley.jpg" Width="400" Height="300" >
   <Image.Clip>
    <PathGeometry>
      <PathFigure StartPoint="0,0">
       <LineSegment x:Name="ls1" Point="400, 0" />
       <LineSegment x:Name="ls2" Point="400, 300" />
       <LineSegment Point="0, 300" />
      </PathFigure>
    </PathGeometry>
   </Image.Clip>
   <Image.Resources>
    <Storyboard x:Name="stb2">
     <PointAnimation Storyboard.TargetName="ls1" Storyboard.TargetProperty="Point"
                     From="0, 0" To="400, 0" Duration="0:0:2" />
     <PointAnimation Storyboard.TargetName="ls2" Storyboard.TargetProperty="Point"
                     From="0, 300" To="400, 300" Duration="0:0:2" />
    </Storyboard>
   </Image.Resources>
  </Image>

Since a path can be made of a succession of paths, we can do more complex animations. For instance :


Project / source code

The clipping path is made of five triangles, all having in common the image's center :
The five clipping areas
The animation is now made of five animations in succession, each elementary triangle being drawn in one second. The initial Point values in the lsxB LineSegment are such that the image is initially invisible. The values are changed in the corresponding PointAnimation :

  <Image x:Name="img" Source="Sisley.jpg" Width="400" Height="300" >
   <Image.Clip>
    <PathGeometry>
     <PathGeometry.Figures>
      <PathFigure StartPoint="200,150">
       <LineSegment x:Name="ls1A" Point="200, 0" />
       <LineSegment x:Name="ls1B" Point="200, 0" />
      </PathFigure>
      <PathFigure StartPoint="200,150" >
       <LineSegment x:Name="ls2A" Point="400, 0" />
       <LineSegment x:Name="ls2B" Point="400, 0" />
      </PathFigure>
      <PathFigure StartPoint="200,150" >
       <LineSegment x:Name="ls3A" Point="400, 300" />
       <LineSegment x:Name="ls3B" Point="400, 300" />
      </PathFigure>
       <PathFigure StartPoint="200,150" >
       <LineSegment x:Name="ls4A" Point="0, 300" />
      <LineSegment x:Name="ls4B" Point="0, 300" />
      </PathFigure>
      <PathFigure StartPoint="200,150" >
       <LineSegment x:Name="ls5A" Point="0, 0" />
       <LineSegment x:Name="ls5B" Point="0, 0" />
      </PathFigure>
     </PathGeometry.Figures>
    </PathGeometry>
   </Image.Clip>
   <Image.Resources>
    <Storyboard x:Name="stb3">
     <PointAnimation Storyboard.TargetName="ls1B" Storyboard.TargetProperty="Point"
                     From="200, 0" To="400, 0" Duration="0:0:1" />
     <PointAnimation BeginTime="0:0:1" Storyboard.TargetName="ls2B"
                     Storyboard.TargetProperty="Point" From="400, 0" To="400, 300"
                     Duration="0:0:1" />
     <PointAnimation BeginTime="0:0:2" Storyboard.TargetName="ls3B"
                     Storyboard.TargetProperty="Point" From="400, 300" To="0, 300"
                     Duration="0:0:1" />
     <PointAnimation BeginTime="0:0:3" Storyboard.TargetName="ls4B"
                     Storyboard.TargetProperty="Point" From="0, 300" To="0, 0"
                     Duration="0:0:1" />
     <PointAnimation BeginTime="0:0:4" Storyboard.TargetName="ls5B"
                     Storyboard.TargetProperty="Point" From="0, 0" To="200, 0" 
                     Duration="0:0:1" />
    </Storyboard>
   </Image.Resources>
  </Image>

To restart an animation (just before executing st3.Begin), we need to reinitialize the clipping path, to have the image disappearing :
 ls1A.Point = ls1B.Point = new Point(200, 0);
 ls2A.Point = ls2B.Point = new Point(400, 0);
 ls3A.Point = ls3B.Point = new Point(400, 300);
 ls4A.Point = ls4B.Point = new Point(0, 300);
 ls5A.Point = ls5B.Point = new Point(0, 0);

Instead of writing 
 <PointAnimation BeginTime="0:0:3" Storyboard.TargetName="ls4B"
                 Storyboard.TargetProperty="Point" From="0, 300" To="0, 0"
                 Duration="0:0:1" /> >

it's possible to write (remember, it's the fourth PointAnimation and we animate the second LineSegment) :
 <PointAnimation BeginTime="0:0:3" Storyboard.TargetName="img"
                 Storyboard.TargetProperty="(UIElement.Clip)
                                            .(PathGeometry.Figures)[3]
                                            .(PathFigure.Segments)[1]
                                            .(LineSegment.Point)"
                 From="0, 300" To="0, 0" Duration="0:0:1" />

As said Lewis Carroll in "Alice in Wonderland" : "I could have thought of a much more complicated way of doing it, said the Red Queen, immensely proud". 

In a forthcoming post, we will show more sophisticated animations, still based on the clipping area.

 

Tags:

Comments

9/19/2008 6:35:25 AM #

Trackback from Community Blogs

Silverlight Cream for September 18, 2008 -- #372

Community Blogs

9/23/2008 6:51:15 AM #

Cool!
Animating a Path Point on a clip path. fucking amazing. Way to go!
I never even thought about this. very cool sutff.

Justin-Josef Angel Canada

9/24/2008 10:29:48 PM #


Hi Gerard
The code is great, sharing it is greater!
Thank you for helping me.
mike

mike greenway United States

9/26/2008 6:42:35 PM #

Thanks for the article. Anyway I just upgraded RC0 and cannot view your Silverlight examples. It were loaded but showed only white screen. Anybody got the same problem?

meekob Thailand

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

oke, thanks

Busby Seo Test United States

4/30/2009 3:50:11 PM #

I googled it and came across this great post: .... Thank you so much for posting this!

Criminal Background Check United States

7/9/2009 7:38:26 AM #

Animating a Path Point on a clip path. fucking amazing. Way to go!
I never even thought about this.

club penguin cheats United States

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

Hmm strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

Baume Mercier United States

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

I would like to add your blog to my blogroll please tell me what anchor should I use?

Roberto Cavalli United States

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

Tried to autotranslate you site not understand the writing any hope deutsch version?

Adee Kaye United States

8/5/2009 6:46:20 AM #

I think your blog need a new wordpress template. Downalod it from http://templates.wordpressguru.in, The site has nice and unique wordpress templates.

payday loans United States

8/5/2009 6:46:24 AM #

Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.

payday loans United States

9/5/2009 12:52:03 AM #

I think you made a great point. One that I have been thinking about as well.

Learn & Master Guitar United States

9/5/2009 12:52:07 AM #

You made me late for work reading this. Nice blog, just bookmarked it to come back later.

Harley Davidson Motorcycles United States

9/23/2009 6:21:52 PM #

Great blog.It is really informative article. Thank you for sharing your experience and teaching us.

best cam chat software United States

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

Thanks for the article. Anyway I just upgraded RC0 and cannot view your Silverlight examples. It were loaded but showed only white screen. Anybody got the same problem?

Tiffany Rings People's Republic of China

9/30/2009 6:47:56 PM #

Never taught of such effects while displaying images

Nice Post!!!

ecommerce website design United States

10/10/2009 10:59:28 PM #

I like the last idea, because it was something very new for me

Ares download 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