SpriteHand
Module Border
  Using TransformToVisual for Custom XAML Animation
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/7/2009 10:37 PM

I recently had the need to dynamically draw a shape in XAML from a specific X,Y offset within another control. This is straightforward... unless the control is Rotated, Scaled, or otherwise transformed. Since the original X,Y position is no longer in the same spot after a transform, we need to use some method to calculate the new X,Y position within the control.

Luckily, all UIElement's contain a TransformToVisual method, which can calculate a point's location within a control after all transforms (such as Scale, Rotate, etc) are applied.

Consider the following sample: we have two separate Canvas elements (the wheels) which are Rotating as part of an animation. And we want to draw a line from an offset point within each Wheel Canvas, and update that line as the wheels rotate:

[VIEW DEMO] | [DOWNLOAD SOURCE]

In order to pull this effect off, we can use TransformToVisual on each Wheel Canvas to calculate the new X,Y offset relative to the Parent Canvas (LayoutRoot) like so:

Point pt1 = new Point(50, 25);

GeneralTransform genTransform1 = cnvWheel1.TransformToVisual(LayoutRoot);

Point pt1Transformed = genTransform1.Transform(pt1);

 

Point pt2 = new Point(50, 25);

GeneralTransform genTransform2 = cnvWheel2.TransformToVisual(LayoutRoot);

Point pt2Transformed = genTransform2.Transform(pt2);

 

lineJoin.X1 = pt1Transformed.X;

lineJoin.Y1 = pt1Transformed.Y;

lineJoin.X2 = pt2Transformed.X;

lineJoin.Y2 = pt2Transformed.Y;

That's it! TransformToVisual is a handy method which I'll be keeping in my little bag of XAML tricks.

Permalink |  Trackback

Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2010 andy.beaulieu.com - Login