Tilt an image on mouse move in Silverlight 3 (3D Animation)

No.of Views2113
Bookmarked0 times
Downloads 
Votes0
By  Paru   On  16 Feb 2010 02:02:41
Tag : Silver Light and XAML , Skin And GUI
Tilt an image on mouse move in Silverlight 3 (3D Animation)
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Have you ever seen an image moving at angles on your mouse move. I applications like games etc.. one would have noticed and wondered how. Silverlight 3 has provided the Projection concept that gives a 3D effect for the 2D Images.

In this article let’s see how we can make an image tilt on mouse movement.PlaneProjection is the key tag to make it possible. 

XAML

1.    Place an image tag with source and stretch fill.
Please net that the same can be done for any control (I took image as it is more interesting for me)

<Image Source="images/Logo.png" Stretch="Fill"/>


2.    Add a Plane projection for the Image.
Specify the x:Name so that you can spot the same in the code behind

<Image.Projection>
<PlaneProjection x: Name="projImgLogo" RotationY="0″ RotationX="0″/>
</Image.Projection>


Now we are done with the XAML part and the whole code look like

<Image Source="images/Logo.png" Stretch="Fill">
<Image.Projection>
<PlaneProjection x: Name=" projImgLogo" RotationY="0" RotationX="0"/>
</Image.Projection>
</Image>

Code Behind (C#)

In all Silverlight applications LayoutRoot is the base object in the window (It can be a Grid, Canvas… any Panel which is the root of the controls.).
I have handled the mouse move of the root element, say LayoutRoot.

Every time the mouse is moved mouse co-ordinates are captured and the values are used in the equations to get the desired effect:

void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
{
//Get the mouse X and Y positions and store it in ‘pt’
mousePosition = e.GetPosition(LayoutRoot);
//Use the values stored in ‘pt’ to effect the value of ‘ProjectionFactor’
projImgLogo.RotationY = ((mousePosition.X – (LayoutRoot.ActualWidth / 2)) / LayoutRoot.ActualWidth) * 100;
projImgLogo.RotationX = ((mousePosition.Y – (LayoutRoot.ActualHeight / 2)) / LayoutRoot.ActualHeight) * 100;
}

Conclusion

This is a small article that provides an animation on mouse move, which looks really interesting in a game like application. Explore it.Happy Coding.

 
Sign Up to vote for this article
 
About Author
 
Paru
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-31 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    The color of the silverlight scroll bar was looking very ugly in one of my projects and it needs to be changed. Am so sure that the regular, default pale scroll bars suck most of the time. When the situation demands color tweaking for the scroll bar: Few simple steps and a few minutes of time, it’s done.
    Published Date : 15/Dec/2010
    in this article, i will describe,How to use styles in Silverlight
    Published Date : 16/Feb/2010
    Every time you could have imagined for any background image for your application and for some controls. In Silverlight 2 and Silverlight 3 Beta it was not so easy to put Images as Background and do some Tiling. Using Tile Brush in Silverlight 3 we will be able to use images as background. In this article we will see how can we achieve that.
    Published Date : 08/Apr/2010
    Themes are nothing but some predefined styles for each and every control. For example you want a Blue Theme for your Silverlight Application that is for all of your controls inside the application.
    Published Date : 08/Apr/2010
    We experienced styles in each and every control level. Now in Application Level we have some themes too.
    Published Date : 07/Apr/2010
Comments
By:RRaveenDate Of Posted:8/18/2010 7:17:34 AM
Please advise
Hi Paru, Please advise to him error , how he can solve? thank you for your support.
By:AshishDate Of Posted:8/18/2010 5:03:13 AM
Tilt an image on mouse move in Silverlight 3
void LayoutRoot_MouseMove(object sender, MouseEventArgs e) { //Get the mouse X and Y positions and store it in ‘pt’ mousePosition = e.GetPosition(LayoutRoot); //Use the values stored in ‘pt’ to effect the value of ‘ProjectionFactor’ projImgLogo.RotationY = ((mousePosition.X – (LayoutRoot.ActualWidth / 2)) / LayoutRoot.ActualWidth) * 100; projImgLogo.RotationX = ((mousePosition.Y – (LayoutRoot.ActualHeight / 2)) / LayoutRoot.ActualHeight) * 100; } This code is not working.given the error
By:AshishDate Of Posted:8/18/2010 5:00:33 AM
image moving at angles on your mouse move
void LayoutRoot_MouseMove(object sender, MouseEventArgs e) { //Get the mouse X and Y positions and store it in ‘pt’ mousePosition = e.GetPosition(LayoutRoot); //Use the values stored in ‘pt’ to effect the value of ‘ProjectionFactor’ projImgLogo.RotationY = ((mousePosition.X – (LayoutRoot.ActualWidth / 2)) / LayoutRoot.ActualWidth) * 100; projImgLogo.RotationX = ((mousePosition.Y – (LayoutRoot.ActualHeight / 2)) / LayoutRoot.ActualHeight) * 100; } This Code not working.given the error.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top