var panel = sender as Panel;
if (panel != null)
{
var projectionMax = GetProjectionMax(panel);
var translateMax = GetTranslateMax(panel);
foreach (UIElement child in panel.Children)
{
var storyboard = new Storyboard();
PlaneProjection planeProjection;
child.EnsureProjection(out planeProjection);
DoubleAnimation projectionX = AnimationFactory.CreateProjectionXAnimation(planeProjection, random.Next(-projectionMax, projectionMax), seconds);
DoubleAnimation projectionY = AnimationFactory.CreateProjectionYAnimation(planeProjection, random.Next(-projectionMax, projectionMax), seconds);
DoubleAnimation projectionZ = AnimationFactory.CreateProjectionZAnimation(planeProjection, random.Next(-projectionMax, projectionMax), seconds);
storyboard.Children.Add(projectionX);
storyboard.Children.Add(projectionY);
storyboard.Children.Add(projectionZ);
ScaleTransform scale;
TranslateTransform translate;
RotateTransform rotate;
SkewTransform skew;
child.EnsureTransforms(out scale, out rotate, out skew, out translate);
DoubleAnimation translateX = AnimationFactory.CreateTranslateXAnimation(translate, random.Next(-translateMax, translateMax), seconds);
DoubleAnimation translateY = AnimationFactory.CreateTranslateYAnimation(translate, random.Next(-translateMax, translateMax), seconds);
storyboard.Children.Add(translateX);
storyboard.Children.Add(translateY);
storyboard.Completed += (s, e1) =>
{
projectionX.To = random.Next(-projectionMax, projectionMax);
projectionY.To = random.Next(-projectionMax, projectionMax);
projectionZ.To = random.Next(-projectionMax, projectionMax);
translateX.To = random.Next(-translateMax, translateMax);
translateY.To = random.Next(-translateMax, translateMax);
storyboard.Begin();
};
storyboard.Begin();
}
}