public void Selected(SpriteVisual hostVisual) { // Set ColorBrush as Source to NineGridBrush with HollowCenter and insert child Content visual hostVisual.Brush = _nineGridBrush; hostVisual.Children.InsertAtTop(_borderedContent); // Run expression animations to manage the size of borderedContent child visual var hostNode = hostVisual.GetReference(); var nineBrush = _nineGridBrush.GetReference(); var xSizeExpression = hostNode.Size.X - (nineBrush.LeftInset * nineBrush.LeftInsetScale + nineBrush.RightInset * nineBrush.RightInsetScale); var ySizeExpression = hostNode.Size.Y - (nineBrush.TopInset * nineBrush.TopInsetScale + nineBrush.BottomInset + nineBrush.BottomInsetScale); var xOffsetExpression = nineBrush.LeftInset * nineBrush.LeftInsetScale; var yOffsetExpression = nineBrush.TopInset * nineBrush.TopInsetScale; _borderedContent.StartAnimation("Size.X", xSizeExpression); _borderedContent.StartAnimation("Size.Y", ySizeExpression); _borderedContent.StartAnimation("Offset.X", xOffsetExpression); _borderedContent.StartAnimation("Offset.Y", yOffsetExpression); }
private void SamplePage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { Compositor compositor = ElementCompositionPreview.GetElementVisual(MyGrid).Compositor; ContainerVisual container = compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(MyGrid, container); // // Create a couple of SurfaceBrushes for the orbiters and center // _redBallSurface = ImageLoader.Instance.LoadFromUri(new Uri("ms-appx:///Samples/SDK 10586/PropertySets/RedBall.png")); _blueBallSurface = ImageLoader.Instance.LoadFromUri(new Uri("ms-appx:///Samples/SDK 10586/PropertySets/BlueBall.png")); // // Create the center and orbiting sprites // SpriteVisual redSprite = compositor.CreateSpriteVisual(); redSprite.Brush = _redBallSurface.Brush; redSprite.Size = new Vector2(100f, 100f); redSprite.Offset = new Vector3((float)Window.Current.Bounds.Width / 2 - redSprite.Size.X / 2, 150f, 0f); container.Children.InsertAtTop(redSprite); SpriteVisual blueSprite = compositor.CreateSpriteVisual(); blueSprite.Brush = _blueBallSurface.Brush; blueSprite.Size = new Vector2(25f, 25f); blueSprite.Offset = new Vector3((float)Window.Current.Bounds.Width / 2 - redSprite.Size.X / 2, 50f, 0f); container.Children.InsertAtTop(blueSprite); // // Create the PropertySet that contains all the value referenced in the expression. We can also // animate these properties, leading to the expression being re-evaluated per frame. // _propertySet = compositor.CreatePropertySet(); _propertySet.InsertScalar("Rotation", 0f); _propertySet.InsertVector3("CenterPointOffset", new Vector3(redSprite.Size.X / 2 - blueSprite.Size.X / 2, redSprite.Size.Y / 2 - blueSprite.Size.Y / 2, 0)); // // Create the expression. This expression positions the orbiting sprite relative to the center of // of the red sprite's center. As we animate the red sprite's position, the expression will read // the current value of it's offset and keep the blue sprite locked in orbit. // var propSetCenterPoint = _propertySet.GetReference().GetVector3Property("CenterPointOffset"); var propSetRotation = _propertySet.GetReference().GetScalarProperty("Rotation"); var orbitExpression = redSprite.GetReference().Offset + propSetCenterPoint + EF.Vector3( EF.Cos(EF.ToRadians(propSetRotation)) * 150, EF.Sin(EF.ToRadians(propSetRotation)) * 75, 0); // Start the expression animation! blueSprite.StartAnimation("Offset", orbitExpression); // Now animate the rotation property in the property bag, this generates the orbitting motion. var linear = compositor.CreateLinearEasingFunction(); var rotAnimation = compositor.CreateScalarKeyFrameAnimation(); rotAnimation.InsertKeyFrame(1.0f, 360f, linear); rotAnimation.Duration = TimeSpan.FromMilliseconds(4000); rotAnimation.IterationBehavior = AnimationIterationBehavior.Forever; _propertySet.StartAnimation("Rotation", rotAnimation); // Lastly, animation the Offset of the red sprite to see the expression track appropriately var offsetAnimation = compositor.CreateScalarKeyFrameAnimation(); offsetAnimation.InsertKeyFrame(0f, 50f); offsetAnimation.InsertKeyFrame(.5f, 150f); offsetAnimation.InsertKeyFrame(1f, 50f); offsetAnimation.Duration = TimeSpan.FromMilliseconds(4000); offsetAnimation.IterationBehavior = AnimationIterationBehavior.Forever; redSprite.StartAnimation("Offset.Y", offsetAnimation); }