private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = _compositor.CreateCompositionGenerator(); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; _center = new Vector2(150f); // Create the combined geometry _ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); _ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); var combinedGeometry = _ellipse1.CombineWith(_ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); _outerGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((RenderGrid1.ActualWidth - _width) / 2).ToSingle(), ((RenderGrid1.ActualHeight - _height) / 2).ToSingle(), 0); _animatedMaskSurface = _generator.CreateMaskSurface(_visual1.Size.ToSize(), excludedGeometry); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(_animatedMaskSurface); var source = _compositor.CreateColorBrush(Color.FromArgb(255, 192, 0, 0)); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _visual1.Brush = maskBrush; ElementCompositionPreview.SetElementChildVisual(RenderGrid1, _visual1); // Create custom shaped visual using CompositionMaskBrush _visual2 = _compositor.CreateSpriteVisual(); _visual2.Size = new Vector2(_width, _height); _visual2.Brush = _compositor.CreateMaskedBackdropBrush(_animatedMaskSurface, Colors.AntiqueWhite, 10, _backdropBrush); _container = _compositor.CreateContainerVisual(); _container.Size = new Vector2(_width, _height); _container.Offset = new Vector3(((RenderGrid2.ActualWidth - _width) / 2).ToSingle(), ((RenderGrid2.ActualHeight - _height) / 2).ToSingle(), 0); var bgVisual = _compositor.CreateSpriteVisual(); bgVisual.Size = _container.Size * 0.6f; bgVisual.Brush = _compositor.CreateColorBrush(Colors.Maroon); bgVisual.Offset = new Vector3(_container.Size * 0.2f, 0); _container.Children.InsertAtTop(bgVisual); _container.Children.InsertAtTop(_visual2); ElementCompositionPreview.SetElementChildVisual(RenderGrid2, _container); }
/// <summary> /// Creates a SpriteVisual which contains SpriteVisuals representing each of the /// render layers in the CanvasElement /// </summary> /// <param name="generator">ICompositionGenerator</param> /// <param name="width">Target width of the rendered geometry</param> /// <param name="height">Target height of the rendered geometry</param> /// <param name="offset">Offset of the rendered geometry</param> /// <param name="padding">Padding of the surface on which the geometry /// is rendered.</param> /// <param name="rotation">Rotation angle (in radians) about the center of the /// geometry</param> /// <returns>SpriteVisual</returns> public SpriteVisual CreateVisual(ICompositionGenerator generator, float width, float height, Vector2 offset, Vector4 padding, float rotation) { var rootVisual = generator.Compositor.CreateSpriteVisual(); rootVisual.Size = new Vector2(width, height); for (var i = 0; i < Layers.Count(); i++) { var geometry = GetGeometry(i, width, height, offset, padding, rotation); if (geometry == null) { continue; } var fill = GetFill(i, width, height, offset, padding, rotation); var stroke = GetStroke(i, width, height, offset, padding, rotation); var geometrySurface = generator.CreateGeometrySurface(rootVisual.Size.ToSize(), geometry, stroke, fill); var surfaceBrush = generator.Compositor.CreateSurfaceBrush(geometrySurface.Surface); var visual = generator.Compositor.CreateSpriteVisual(); visual.Size = rootVisual.Size; visual.Brush = surfaceBrush; rootVisual.Children.InsertAtTop(visual); } return(rootVisual); }
private IEnumerable <InteractiveLayeredQuiverGeneratorOutput> DoWork( InteractiveLayeredQuiverGenerator interactiveQuiverGenerator, CompositionParameters nextCompositionParameters, ICompositionGenerator compositionGenerator) { if (nextCompositionParameters is null) { yield return(interactiveQuiverGenerator.EndGeneration()); yield break; } foreach (var composition in compositionGenerator.GenerateCompositions(nextCompositionParameters)) { if (!interactiveQuiverGenerator.TrySupplyComposition(composition, out nextCompositionParameters)) { continue; } foreach (var output in DoWork(interactiveQuiverGenerator, nextCompositionParameters, compositionGenerator)) { yield return(output); } interactiveQuiverGenerator.UnsupplyLastComposition(); } }
private void OnCreateResources(CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) { _c = Window.Current.Compositor; _g = _c.CreateCompositionGenerator(); _rootShape = _c.CreateShapeVisual(); _rootShape.Size = new Vector2((float)RenderGrid.ActualWidth, (float)RenderGrid.ActualHeight); _rootSize = _rootShape.Size.ToSize(); var rectGeom = _c.CreatePathGeometry(new CompositionPath(CanvasGeometry.CreateRectangle(_g.Device, new Rect(new Point(), _rootSize)))); var bgShape = _c.CreateSpriteShape(rectGeom); bgShape.FillBrush = _c.CreateColorBrush(CanvasObject.CreateColor("#161616")); _rootShape.Shapes.Add(bgShape); for (var i = 0; i < MaxLayerCount; i++) { var shape = _c.CreateSpriteShape(_c.CreatePathGeometry(GetClip(_rootSize, 0f))); // Offset each of the shape to the right to hide the bump of lower layers shape.Offset = HideOffset; shape.FillBrush = _c.CreateColorBrush(_colors[i]); _rootShape.Shapes.Add(shape); } _selectedIndex = -1; _swipeRightIndex = -1; _swipeLeftIndex = MaxLayerCount; _nextShapeIndex = -1; // Reset offset of top most shape _rootShape.Shapes[MaxLayerCount].Offset = Vector2.Zero; ElementCompositionPreview.SetElementChildVisual(RenderGrid, _rootShape); }
/// <summary> /// Ctor /// </summary> public FluidProgressRing() { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _isAnimationStarted = false; // Default Values Width = 70; Height = 70; }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); var size = new Vector2(GridWidth, GridHeight); _topOffset = -GridHeight / 2f; // Create the left image visual var leftImageVisual = _compositor.CreateSpriteVisual(); leftImageVisual.Size = size; // Left Image Surface var leftImageSurface = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/Image1.jpg"), size.ToSize(), ImageSurfaceOptions.Default); // Left Mask Geometry _leftGeometry = CanvasGeometry.CreateRectangle(_generator.Device, new Rect(0f, 0f, 2f * (GridWidth * _splitValue) / 100f, 2f * GridHeight)); var leftOffset = -(GridWidth * _splitValue) / 100f; // Left Masked Brush var leftMaskedBrush = _compositor.CreateMaskBrush(); leftMaskedBrush.Source = _compositor.CreateSurfaceBrush(leftImageSurface); _leftGaussianSurface = _generator.CreateGaussianMaskSurface(size.ToSize(), _leftGeometry, new Vector2(leftOffset, _topOffset), _blurRadius); leftMaskedBrush.Mask = _compositor.CreateSurfaceBrush(_leftGaussianSurface); leftImageVisual.Brush = leftMaskedBrush; // Create the right image visual var rightImageVisual = _compositor.CreateSpriteVisual(); rightImageVisual.Size = size; // Right Image Surface var rightImageSurface = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/Image2.jpg"), size.ToSize(), ImageSurfaceOptions.Default); // Right Mask Geometry _rightGeometry = CanvasGeometry.CreateRectangle(_generator.Device, new Rect(0, 0, 2f * (GridWidth * (100f - _splitValue)) / 100, 2f * GridHeight)); // Right Masked Brush var rightMaskedBrush = _compositor.CreateMaskBrush(); rightMaskedBrush.Source = _compositor.CreateSurfaceBrush(rightImageSurface); _rightGaussianSurface = _generator.CreateGaussianMaskSurface(size.ToSize(), _rightGeometry, new Vector2(-leftOffset, _topOffset), _blurRadius); rightMaskedBrush.Mask = _compositor.CreateSurfaceBrush(_rightGaussianSurface); rightImageVisual.Brush = rightMaskedBrush; var visualContainer = _compositor.CreateContainerVisual(); visualContainer.Size = size; visualContainer.Children.InsertAtTop(leftImageVisual); visualContainer.Children.InsertAtTop(rightImageVisual); ElementCompositionPreview.SetElementChildVisual(ImageGrid, visualContainer); }
private void SetupCompositor() { if (compositor != null && generator != null) { return; } compositor = ElementCompositionPreview.GetElementVisual(Control).Compositor; generator = compositor.CreateCompositionGenerator(); }
void SetupCompositor() { if (compositor != null && generator != null) { return; } Debug.WriteLine("Setting up Compositor"); compositor = ElementCompositionPreview.GetElementVisual(Control).Compositor; generator = CompositionGeneratorFactory.GetCompositionGenerator(compositor); }
private async void OnPageLoaded(object sender, RoutedEventArgs e) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); var size = new Vector2(GridWidth, GridWidth); // Create the image visual var imageVisual = _compositor.CreateSpriteVisual(); imageVisual.Size = size; var imageSurface = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/cat.png"), size.ToSize(), ImageSurfaceOptions.Default); imageVisual.Brush = _compositor.CreateSurfaceBrush(imageSurface); ElementCompositionPreview.SetElementChildVisual(ImageGrid, imageVisual); //Create the mask visual var maskVisual = _compositor.CreateSpriteVisual(); maskVisual.Size = size; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, GridWidth / 2, GridWidth / 2, GridWidth * 0.375f, GridWidth * 0.1875f); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, GridWidth / 2, GridWidth / 2, GridWidth * 0.1875f, GridWidth * 0.375f); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create SurfaceBrush from MaskSurface ElementCompositionPreview.SetElementChildVisual(MaskGrid, maskVisual); var outputVisual = _compositor.CreateSpriteVisual(); outputVisual.Size = size; var maskedBrush = _compositor.CreateMaskBrush(); maskedBrush.Source = imageVisual.Brush; _gaussianSurface = _generator.CreateGaussianMaskSurface(size.ToSize(), _combinedGeometry, Vector2.Zero, 0); maskedBrush.Mask = _compositor.CreateSurfaceBrush(_gaussianSurface); maskVisual.Brush = _compositor.CreateSurfaceBrush(_gaussianSurface);; outputVisual.Brush = maskedBrush; ElementCompositionPreview.SetElementChildVisual(OutputGrid, outputVisual); }
private void OnCreateResources(CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) { _c = Window.Current.Compositor; _g = _c.CreateCompositionGenerator(); _rootShape = _c.CreateShapeVisual(); _rootShape.Size = new Vector2((float)RenderGrid.ActualWidth, (float)RenderGrid.ActualHeight); _rootSize = _rootShape.Size.ToSize(); var rectGeom = _c.CreatePathGeometry(new CompositionPath(CanvasGeometry.CreateRectangle(_g.Device, new Rect(new Point(), _rootSize)))); var bgShape = _c.CreateSpriteShape(rectGeom); bgShape.FillBrush = _c.CreateColorBrush(CanvasObject.CreateColor("#161616")); _rootShape.Shapes.Add(bgShape); _slideLeftImplicitAnimationCollection = _c.CreateImplicitAnimationCollection(); var slideLeftAnim = _c.GenerateVector2KeyFrameAnimation() .HavingDuration(700) .ForTarget(() => _c.CreateSpriteShape().Offset); slideLeftAnim.InsertFinalValueKeyFrame(1f, _c.CreateEaseInOutBackEasingFunction()); _slideLeftImplicitAnimationCollection["Offset"] = slideLeftAnim.Animation; _slideRightImplicitAnimationCollection = _c.CreateImplicitAnimationCollection(); var slideRightAnim = _c.GenerateVector2KeyFrameAnimation() .HavingDuration(2000) .ForTarget(() => _c.CreateSpriteShape().Offset); slideRightAnim.InsertFinalValueKeyFrame(1f, _c.CreateEaseInOutBackEasingFunction()); _slideRightImplicitAnimationCollection["Offset"] = slideRightAnim.Animation; for (var i = 0; i < MaxShapeCount; i++) { var shape = _c.CreateSpriteShape(_c.CreatePathGeometry(GetClip(_rootSize))); shape.Offset = HideOffset; shape.FillBrush = _c.CreateColorBrush(_colors[i]); _rootShape.Shapes.Add(shape); } _selIndex = MaxShapeCount; _rootShape.Shapes[_selIndex].Offset = Vector2.Zero; ElementCompositionPreview.SetElementChildVisual(RenderGrid, _rootShape); PrevBtn.IsEnabled = false; }
/// <summary> /// Generates all layered quivers of the specified layer type using the specified composition /// generator. /// </summary> /// <param name="layerType">The layer type of the layered quivers to generate.</param> /// <param name="firstVertex">The first vertex of the quivers to generate.</param> /// <param name="compositionGenerator">The composition generator whose compositions to use.</param> /// <returns></returns> /// <exception cref="ArgumentNullException"><paramref name="layerType"/> is /// <see langword="null"/>, or <paramref name="compositionGenerator"/> is /// <see langword="null"/>.</exception> public IEnumerable <InteractiveLayeredQuiverGeneratorOutput> GenerateForFixedLayerType( LayerType layerType, ICompositionGenerator compositionGenerator) { if (layerType is null) { throw new ArgumentNullException(nameof(layerType)); } if (compositionGenerator is null) { throw new ArgumentNullException(nameof(compositionGenerator)); } var interactiveGenerator = new InteractiveLayeredQuiverGenerator(); var nextCompositionParameters = interactiveGenerator.StartGeneration(layerType, firstVertex); return(DoWork(interactiveGenerator, nextCompositionParameters, compositionGenerator)); }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); _imageVisual = _compositor.CreateSpriteVisual(); _imageVisual.Size = new Vector2(RenderGrid.Width.ToSingle(), RenderGrid.Height.ToSingle()); _imageOptions = ImageSurfaceOptions.DefaultOptimized; _imageOptions.SurfaceBackgroundColor = Colors.Black; _imageSurface = _generator.CreateImageSurface(null, _imageVisual.Size.ToSize(), _imageOptions); _imageVisual.Brush = _compositor.CreateSurfaceBrush(_imageSurface); ElementCompositionPreview.SetElementChildVisual(RenderGrid, _imageVisual); ImageCB.SelectedIndex = 0; StretchCB.SelectedIndex = 0; AlignXCB.SelectedIndex = 1; AlignYCB.SelectedIndex = 1; }
private void InitComposition() { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); _angle = 0f; _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _geometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((RenderGrid.ActualWidth - _width) / 2).ToSingle(), ((RenderGrid.ActualHeight - _height) / 2).ToSingle(), 0); _geometrySurface = _generator.CreateGeometrySurface(_visual1.Size.ToSize(), _geometry, Color.FromArgb(255, 192, 0, 0)); _visual1.Brush = _compositor.CreateSurfaceBrush(_geometrySurface); ElementCompositionPreview.SetElementChildVisual(RenderGrid, _visual1); _visual2 = _compositor.CreateSpriteVisual(); _visual2.Size = new Vector2(_width, _height); _visual2.Offset = new Vector3(((AnimatedGrid.ActualWidth - _width) / 2).ToSingle(), ((AnimatedGrid.ActualHeight - _height) / 2).ToSingle(), 0); var ellipse3 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse4 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _animatedGeometry = ellipse3.CombineWith(ellipse4, Matrix3x2.Identity, CanvasGeometryCombine.Xor); _animatedGeometrySurface = _generator.CreateGeometrySurface(_visual1.Size.ToSize(), _animatedGeometry, Color.FromArgb(255, 192, 0, 0)); _visual2.Brush = _compositor.CreateSurfaceBrush(_animatedGeometrySurface); ElementCompositionPreview.SetElementChildVisual(AnimatedGrid, _visual2); }
public IEnumerable <InteractiveLayeredQuiverGeneratorOutput> GenerateFromBaseForFixedLayerType( QuiverInPlane <int> quiverInPlane, Potential <int> potential, IEnumerable <int> boundaryLayer, LayerType layerType, ICompositionGenerator compositionGenerator, int nextVertex) { if (quiverInPlane is null) { throw new ArgumentNullException(nameof(quiverInPlane)); } if (potential is null) { throw new ArgumentNullException(nameof(potential)); } if (boundaryLayer is null) { throw new ArgumentNullException(nameof(boundaryLayer)); } if (layerType is null) { throw new ArgumentNullException(nameof(layerType)); } if (compositionGenerator is null) { throw new ArgumentNullException(nameof(compositionGenerator)); } var interactiveGenerator = new InteractiveLayeredQuiverGenerator(); if (!interactiveGenerator.TryStartGenerationFromBase(quiverInPlane, potential, boundaryLayer, layerType, nextVertex, out var nextCompositionParameters)) { return(new InteractiveLayeredQuiverGeneratorOutput[0]); } return(DoWork(interactiveGenerator, nextCompositionParameters, compositionGenerator)); }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); var visualChild = _compositor.CreateSpriteVisual(); visualChild.Size = new Vector2(_width * 0.75f, _height * 0.75f); visualChild.Offset = new Vector3(_width * 0.125f, _height * 0.125f, 0); _visual1.Children.InsertAtTop(visualChild); // Create the CompositionMask var compositionMask = _generator.CreateMaskSurface(_visual1.Size.ToSize(), _combinedGeometry); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(compositionMask.Surface); var source = _compositor.CreateColorBrush(Color.FromArgb(255, 0, 173, 239)); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _visual1.Brush = maskBrush; ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, _visual1); // Create custom shaped visual using CompositionMaskBrush _visual2 = _compositor.CreateSpriteVisual(); _visual2.Size = new Vector2(_width, _height); _visual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask filled with color var compositionMask2 = _generator.CreateGeometrySurface(_visual2.Size.ToSize(), _combinedGeometry, Color.FromArgb(192, 192, 0, 0)); // Create SurfaceBrush from CompositionMask var surfaceBrush = _compositor.CreateSurfaceBrush(compositionMask2.Surface); _visual2.Brush = surfaceBrush; ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, _visual2); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container = _compositor.CreateContainerVisual(); container.Size = new Vector2(CompositionGrid3.ActualWidth.Single(), CompositionGrid3.ActualHeight.Single()); // Background Visual _bgVisual = _compositor.CreateSpriteVisual(); _bgVisual.Size = new Vector2(_width, _height); _bgVisual.Offset = new Vector3(((CompositionGrid3.ActualWidth - _width) / 2).Single(), ((CompositionGrid3.ActualHeight - _height) / 2).Single(), 0); var radians = ((45f * Math.PI) / 180).Single(); var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); var bgMask = _generator.CreateGeometrySurface(_bgVisual.Size.ToSize(), bgGeometry, Color.FromArgb(255, 0, 173, 239)); var bgBrush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual.Brush = bgBrush; container.Children.InsertAtBottom(_bgVisual); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid3.ActualWidth - _width) / 2).Single(), ((CompositionGrid3.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedMaskSurface = _generator.CreateGeometrySurface(_animatedVisual.Size.ToSize(), excludedGeometry, Color.FromArgb(192, 192, 0, 0)); var animBrush = _compositor.CreateSurfaceBrush(_animatedMaskSurface.Surface); _animatedVisual.Brush = animBrush; container.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid3, container); }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.Single(), CompositionGrid1.ActualHeight.Single()); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.45f, _height * 0.45f); _bgVisual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); _bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); container1.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var compositionMask = _generator.CreateMask(_visual1.Size.ToSize(), ellipseGeometry); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateMaskedBackdropBrush(compositionMask, Colors.AntiqueWhite, 30f, _backdropBrush); container1.Children.InsertAtTop(_visual1); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, container1); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.Single(), CompositionGrid2.ActualHeight.Single()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).Single(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).Single(), 0); //var radians = ((45f * Math.PI) / 180).Single(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMask(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); var distance = 4f; var visualSize = new Vector2(225, 150); var vRefHeight = visualSize.Y + (visualSize.Y * 0.7f) + distance; var hRefWidth = visualSize.X + (visualSize.X * 0.7f) + distance; var vRefLeft = (float)(CompositionGrid1.ActualWidth - visualSize.X) / 2f; var vRefTop = (float)(CompositionGrid1.ActualHeight - vRefHeight) / 2f; var hRefLeft = (float)(CompositionGrid1.ActualWidth - hRefWidth) / 2f; var hRefTop = (float)(CompositionGrid1.ActualHeight - visualSize.Y) / 2f; var surfaceImage = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/Car.jpg"), visualSize.ToSize(), ImageSurfaceOptions.Default); var surfaceBrush = _compositor.CreateSurfaceBrush(surfaceImage.Surface); var visual1 = _compositor.CreateSpriteVisual(); visual1.Size = visualSize; visual1.Offset = new Vector3(vRefLeft, vRefTop, 0); visual1.Brush = surfaceBrush; _generator.CreateReflection(visual1, distance); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, visual1); var invertEffect = new InvertEffect() { Source = new CompositionEffectSourceParameter("source") }; var invertEffectFactory = _compositor.CreateEffectFactory(invertEffect); var invertBrush = invertEffectFactory.CreateBrush(); invertBrush.SetSourceParameter("source", surfaceBrush); var visual2 = _compositor.CreateSpriteVisual(); visual2.Size = visualSize; visual2.Offset = new Vector3(hRefLeft, hRefTop, 0); visual2.Brush = invertBrush; _generator.CreateReflection(visual2, distance, 0.7f, ReflectionLocation.Right); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, visual2); var visual3 = _compositor.CreateSpriteVisual(); visual3.Size = visualSize; visual3.Offset = new Vector3(vRefLeft, vRefTop, 0); visual3.Brush = surfaceBrush; var inVisual = _compositor.CreateSpriteVisual(); inVisual.Size = new Vector2(80, 80); inVisual.Offset = new Vector3(20, 10, 0); inVisual.Brush = _compositor.CreateColorBrush(Colors.Yellow); var inVisual2 = _compositor.CreateSpriteVisual(); inVisual2.Size = new Vector2(80, 80); inVisual2.Offset = new Vector3(60, 60, 0); inVisual2.Brush = _compositor.CreateColorBrush(Colors.Blue); visual3.Children.InsertAtTop(inVisual2); visual3.Children.InsertAtTop(inVisual); _generator.CreateReflection(visual3, distance, 0.7f, ReflectionLocation.Bottom); ElementCompositionPreview.SetElementChildVisual(CompositionGrid3, visual3); var visual4 = _compositor.CreateSpriteVisual(); visual4.Size = visualSize; visual4.Offset = new Vector3(hRefLeft, hRefTop, 0); visual4.Brush = surfaceBrush; var inVisual3 = _compositor.CreateSpriteVisual(); inVisual3.Size = new Vector2(80, 80); inVisual3.Offset = new Vector3(20, 10, 0); inVisual3.Brush = _compositor.CreateColorBrush(Colors.Yellow); var inVisual4 = _compositor.CreateSpriteVisual(); inVisual4.Size = new Vector2(80, 80); inVisual4.Offset = new Vector3(60, 40, 0); inVisual4.Brush = _compositor.CreateColorBrush(Colors.Blue); visual4.Children.InsertAtTop(inVisual3); visual4.Children.InsertAtTop(inVisual4); _generator.CreateReflection(visual4, distance, 0.7f, ReflectionLocation.Right); ElementCompositionPreview.SetElementChildVisual(CompositionGrid4, visual4); }
/// <summary> /// Initialize all Composition related stuff here (Compositor, Animations etc) /// </summary> private void InitializeComposition() { var rootVisual = ElementCompositionPreview.GetElementVisual(this); // Compositor _compositor = rootVisual.Compositor; // Composition Generator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Final Value Expressions var vector3Expr = _compositor.CreateFinalValueExpression<Vector3>(); var scalarExpr = _compositor.CreateFinalValueExpression<float>(); // Opacity Animation var opacityAnimation = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(DefaultOpacityAnimationDuration) .ForTarget(() => rootVisual.Opacity); opacityAnimation.InsertExpressionKeyFrame(1f, scalarExpr); // Scale Animation var scaleAnimation = _compositor.CreateKeyFrameAnimation<Vector3>() .HavingDuration(DefaultScaleAnimationDuration) .ForTarget(() => rootVisual.Scale); scaleAnimation.InsertExpressionKeyFrame(1f, vector3Expr); // ImplicitAnimation _implicitAnimationCollection = _compositor.CreateImplicitAnimationCollection(); _implicitAnimationCollection["Opacity"] = opacityAnimation.Animation; _implicitAnimationCollection["Scale"] = scaleAnimation.Animation; // Expand Animations _expandLeftInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandLeftInset.InsertKeyFrame(1f, 0); _expandRightInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandInsetClip = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetClipAnimationDuration); _expandInsetClip.InsertKeyFrame(1f, 0); // Collapse Animations _collapseLeftInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration); _collapseRightInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration); _collapseInsetClip = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetClipAnimationDuration); // Root Container _rootContainer = _compositor.CreateContainerVisual(); // Background Layer _bgLayer = _compositor.CreateLayerVisual(); _bgLayer.Size = _rootContainer.Size; _bgLayer.CenterPoint = new Vector3(_bgLayer.Size * 0.5f, 0); // Top Layer _topLayer = _compositor.CreateLayerVisual(); _topLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtBottom(_bgLayer); _rootContainer.Children.InsertAtTop(_topLayer); // Add the rootContainer to the visual tree ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
private void SquirclePage_Loaded(object sender, RoutedEventArgs e) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); // Create Squircle shaped visual using CompositionSurfaceBrush _roundedRectangleVisual = _compositor.CreateSpriteVisual(); _roundedRectangleVisual.Size = new Vector2(_width, _height); _roundedRectangleVisual.Offset = new Vector3(((RoundedRectangleGrid.ActualWidth - _width) / 2).ToSingle(), ((RoundedRectangleGrid.ActualHeight - _height) / 2).ToSingle(), 0); // Create the geometry surface for squircle filled with red color var roundedRectGeometry = CanvasGeometry.CreateRoundedRectangle(_generator.Device, 0, 0, _width, _height, _radius, _radius); _roundedRectangleSurface = _generator.CreateGeometrySurface(_roundedRectangleVisual.Size.ToSize(), roundedRectGeometry, Color.FromArgb(255, 192, 0, 0)); // Create SurfaceBrush from CompositionMask var rectSurfaceBrush = _compositor.CreateSurfaceBrush(_roundedRectangleSurface.Surface); _roundedRectangleVisual.Brush = rectSurfaceBrush; ElementCompositionPreview.SetElementChildVisual(RoundedRectangleGrid, _roundedRectangleVisual); // Create Squircle shaped visual using CompositionSurfaceBrush _squircleVisual = _compositor.CreateSpriteVisual(); _squircleVisual.Size = new Vector2(_width, _height); _squircleVisual.Offset = new Vector3(((SquircleGrid.ActualWidth - _width) / 2).ToSingle(), ((SquircleGrid.ActualHeight - _height) / 2).ToSingle(), 0); // Create the geometry surface for squircle filled with red color var squircleGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _radius, _radius); _squircleSurface = _generator.CreateGeometrySurface(_squircleVisual.Size.ToSize(), squircleGeometry, Color.FromArgb(255, 192, 0, 0)); // Create SurfaceBrush from CompositionMask var surfaceBrush = _compositor.CreateSurfaceBrush(_squircleSurface); _squircleVisual.Brush = surfaceBrush; ElementCompositionPreview.SetElementChildVisual(SquircleGrid, _squircleVisual); // Create visual showing difference between the two geometries _differenceVisual = _compositor.CreateSpriteVisual(); _differenceVisual.Size = new Vector2(_width, _height); // Create the geometry surface for squircle filled with red color var differenceGeometry = roundedRectGeometry.CombineWith(squircleGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); _differenceSurface = _generator.CreateGeometrySurface(_differenceVisual.Size.ToSize(), differenceGeometry, Color.FromArgb(255, 192, 192, 0)); // Create SurfaceBrush from CompositionMask _differenceVisual.Brush = _compositor.CreateSurfaceBrush(_differenceSurface); _xorVisual = _compositor.CreateSpriteVisual(); _xorVisual.Size = _differenceVisual.Size; _xorVisual.Offset = new Vector3(((DifferenceGrid.ActualWidth - _width) / 2).ToSingle(), ((DifferenceGrid.ActualHeight - _height) / 2).ToSingle(), 0); var xorGeometry = roundedRectGeometry.CombineWith(squircleGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Xor); _xorSurface = _generator.CreateGeometrySurface(_xorVisual.Size.ToSize(), xorGeometry, Color.FromArgb(255, 192, 0, 0)); // Create SurfaceBrush from CompositionMask _xorVisual.Brush = _compositor.CreateSurfaceBrush(_xorSurface); _xorVisual.Children.InsertAtTop(_differenceVisual); ElementCompositionPreview.SetElementChildVisual(DifferenceGrid, _xorVisual); }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = _compositor.CreateCompositionGenerator(); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.ToSingle(), CompositionGrid1.ActualHeight.ToSingle()); var parentVisual = _compositor.CreateSpriteVisual(); parentVisual.Size = new Vector2(_width, _height); parentVisual.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).ToSingle(), ((CompositionGrid1.ActualHeight - _height) / 2).ToSingle(), 0); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.5f, _height * 0.5f); _bgVisual1.Offset = new Vector3(((parentVisual.Size.X - _width * 0.5f) / 2f), ((parentVisual.Size.Y - _height * 0.5f) / 2f), 0f); //_bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); // Create the CompositionMask var gaussianMask = _generator.CreateGaussianMaskSurface(); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(gaussianMask.Surface); //mask = ((CompositionBrush)_compositor.CreateGaussianMaskedBackdropBrush(gaussianMask, Colors.AntiqueWhite, 10, _backdropBrush)); var source = _compositor.CreateColorBrush(Colors.DarkOrange); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _bgVisual1.Brush = maskBrush; parentVisual.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); //_visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).ToSingle(), // ((CompositionGrid1.ActualHeight - _height) / 2).ToSingle(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var rectGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 50, 50, _width - 50, _height - 50); var compositionMask = _generator.CreateGaussianMaskSurface(_visual1.Size.ToSize(), ellipseGeometry, new Vector2(20), 20); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateGaussianMaskedBackdropBrush(compositionMask, Colors.Transparent, 20f, _backdropBrush); parentVisual.Children.InsertAtTop(_visual1); var visual = _compositor.CreateSpriteVisual(); visual.Size = new Vector2(400, 400); visual.Offset = new Vector3(((3 * CompositionGrid1.ActualWidth / 4f) - (_width / 2)).ToSingle(), ((3 * CompositionGrid1.ActualHeight / 4f) - (_height / 2)).ToSingle(), 0); var roundRectGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width / 8f, _height / 8f); var maskSurface = _generator.CreateMaskSurface(visual.Size.ToSize(), roundRectGeometry); var frostedBrush = _compositor.CreateFrostedGlassBrush(maskSurface, Colors.DarkGray, 30f, _backdropBrush); var shadow = _compositor.CreateDropShadow(); shadow.Opacity = 0.5f; shadow.Color = Colors.Black; shadow.Offset = new Vector3(10, 10, 0); shadow.BlurRadius = 15; shadow.Mask = frostedBrush.GetSourceParameter("mask"); visual.Brush = frostedBrush; visual.Shadow = shadow; container1.Children.InsertAtTop(visual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, parentVisual); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.ToSingle(), CompositionGrid2.ActualHeight.ToSingle()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).ToSingle(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).ToSingle(), 0); //var radians = ((45f * Math.PI) / 180).ToSingle(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).ToSingle(), ((CompositionGrid2.ActualHeight - _height) / 2).ToSingle(), 0); // Create the Excluded geometry _outerGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width / 8f, _height / 8f); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMaskSurface(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }
/// <summary> /// Initializes the Composition elements /// </summary> private void InitComposition() { // Compositor _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; // CompositionGenerator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Fade Out Animation _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeOutAnimation.InsertKeyFrame(1f, 0); _fadeOutAnimation.Duration = TransitionDuration; // Fade In Animation _fadeInAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeInAnimation.InsertKeyFrame(1f, 1); _fadeInAnimation.Duration = TransitionDuration; // Color Animation _colorAnimation = _compositor.CreateColorKeyFrameAnimation(); _colorAnimation.Duration = TransitionDuration; // Offset Animation _offsetAnimation = _compositor.CreateVector3KeyFrameAnimation(); _offsetAnimation.Target = "Offset"; _offsetAnimation.Duration = TransitionDuration; _offsetAnimation.InsertKeyFrame(1f, Vector3.Zero); // Alignment animations _alignXAnimation = _compositor.CreateScalarKeyFrameAnimation(); _alignXAnimation.Duration = AlignmentTransitionDuration; _alignYAnimation = _compositor.CreateScalarKeyFrameAnimation(); _alignYAnimation.Duration = AlignmentTransitionDuration; // ZoomIn Animation Group _scaleAnimation = _compositor.CreateVector3KeyFrameAnimation(); _scaleAnimation.Target = "Scale"; _scaleAnimation.InsertKeyFrame(1f, Vector3.One); _scaleAnimation.Duration = TransitionDuration; _zoomInAnimationGroup = _compositor.CreateAnimationGroup(); _zoomInAnimationGroup.Add(_scaleAnimation); _zoomInAnimationGroup.Add(_offsetAnimation); // Visuals _rootContainer = _compositor.CreateContainerVisual(); _frameLayer = _compositor.CreateLayerVisual(); _frameBackgroundVisual = _compositor.CreateSpriteVisual(); _frameContentVisual = _compositor.CreateSpriteVisual(); _placeholderContentVisual = _compositor.CreateSpriteVisual(); _placeholderBackgroundVisual = _compositor.CreateSpriteVisual(); _nextVisualContent = _compositor.CreateSpriteVisual(); _frameLayer.Children.InsertAtTop(_frameBackgroundVisual); _frameLayer.Children.InsertAtTop(_frameContentVisual); _frameLayer.Children.InsertAtTop(_placeholderBackgroundVisual); _frameLayer.Children.InsertAtTop(_placeholderContentVisual); _frameLayer.Children.InsertAtTop(_nextVisualContent); // Placeholder content _placeholderContentMask = _generator.CreateGeometrySurface(PlaceholderSize, GetPlaceHolderGeometry(), PlaceholderColor, PlaceholderBackground); _placeholderContentBrush = _compositor.CreateSurfaceBrush(_placeholderContentMask.Surface); _placeholderContentVisual.Brush = _placeholderContentBrush; // Placeholder background _placeholderBackgroundVisual.Brush = _compositor.CreateColorBrush(PlaceholderBackground); // By default placeholder visual will not be visible HidePlaceholder(); // Shadow visual _shadowVisual = _compositor.CreateSpriteVisual(); _rootContainer.Children.InsertAtBottom(_shadowVisual); _rootContainer.Children.InsertAtTop(_frameLayer); _frameBackgroundVisual.Brush = _compositor.CreateColorBrush(FrameBackground); // Create the effect to create the opacity mask var layerEffect = new CompositeEffect { // CanvasComposite.DestinationIn - Intersection of source and mask. // Equation: O = MA * S // where O - Output pixel, MA - Mask Alpha, S - Source pixel. Mode = CanvasComposite.DestinationIn, Sources = { new CompositionEffectSourceParameter("source"), new CompositionEffectSourceParameter("mask") } }; var layerEffectFactory = _compositor.CreateEffectFactory(layerEffect); _layerEffectBrush = layerEffectFactory.CreateBrush(); // The mask for the imageFrame _frameLayerMask = _generator.CreateMaskSurface(new Size(0, 0), null); _layerEffectBrush.SetSourceParameter("mask", _compositor.CreateSurfaceBrush(_frameLayerMask.Surface)); // Apply the mask effect to the frameLayer _frameLayer.Effect = _layerEffectBrush; ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
async void Redraw(bool recreateSurface = true) { if (_compositor == null) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); _defaultSize = new Vector2(GridWidth, GridWidth); // Create the original output image visual _originalImageVisual = _compositor.CreateSpriteVisual(); _originalImageVisual.Size = _defaultSize; _imageSurface = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/cat.png"), _defaultSize.ToSize(), ImageSurfaceOptions.Default); _originalImageVisual.Brush = _compositor.CreateSurfaceBrush(_imageSurface); ElementCompositionPreview.SetElementChildVisual(OriginalOutputGrid, _originalImageVisual); // Create the source image visual _sourceImageVisual = _compositor.CreateSpriteVisual(); _sourceImageVisual.Size = _defaultSize; ElementCompositionPreview.SetElementChildVisual(ImageGrid, _sourceImageVisual); //Create the mask visual _maskVisual = _compositor.CreateSpriteVisual(); _maskVisual.Size = _defaultSize; ElementCompositionPreview.SetElementChildVisual(MaskGrid, _maskVisual); // Create the output visual _outputVisual = _compositor.CreateSpriteVisual(); _outputVisual.Size = _defaultSize; ElementCompositionPreview.SetElementChildVisual(OutputGrid, _outputVisual); } var selIndex = ImageList.SelectedIndex; if (selIndex == -1) { return; } var uri = _images.Values.ElementAt(selIndex); if (uri == null) { return; } var offset = OffsetSlider.Value.ToSingle(); var padding = new Thickness(offset); if (recreateSurface) { _sourceImageSurface = await _generator.CreateImageSurfaceAsync(uri, _defaultSize.ToSize(), ImageSurfaceOptions.Default); _imageMaskSurface = _generator.CreateImageMaskSurface(_sourceImageSurface, _defaultSize.ToSize(), padding, ImageSurfaceOptions.GetDefaultImageMaskOptionsForBlur(_blurRadius)); } else { _imageMaskSurface.Resize(_defaultSize.ToSize(), padding, ImageSurfaceOptions.GetDefaultImageMaskOptionsForBlur(_blurRadius)); } _sourceImageVisual.Brush = _compositor.CreateSurfaceBrush(_sourceImageSurface); _maskVisual.Brush = _compositor.CreateSurfaceBrush(_imageMaskSurface); var maskedBrush = _compositor.CreateMaskBrush(); maskedBrush.Source = _originalImageVisual.Brush; maskedBrush.Mask = _compositor.CreateSurfaceBrush(_imageMaskSurface); _outputVisual.Brush = maskedBrush; }
/// <summary> /// Disposes the resources /// </summary> public void Dispose() { // Clean up resources _compositor = null; Source = null; DataContext = null; Foreground = null; Background = null; _scheduledObject = null; _currentObject = null; // Clean up Composition Objects _imageSurface?.Dispose(); _imageSurface = null; _nextImageSurface?.Dispose(); _nextImageSurface = null; _frameLayerMask?.Dispose(); _frameLayerMask = null; _placeholderContentMask?.Dispose(); _placeholderContentMask = null; _placeholderContentBrush?.Dispose(); _placeholderContentBrush = null; _nextSurfaceBrush?.Dispose(); _nextSurfaceBrush = null; _rootContainer?.Dispose(); _rootContainer = null; _shadowVisual?.Dispose(); _shadowVisual = null; _frameLayer?.Dispose(); _frameLayer = null; _frameBackgroundVisual?.Dispose(); _frameBackgroundVisual = null; _placeholderBackgroundVisual?.Dispose(); _placeholderBackgroundVisual = null; _placeholderContentVisual?.Dispose(); _placeholderContentVisual = null; _frameContentVisual?.Dispose(); _frameContentVisual = null; _nextVisualContent?.Dispose(); _nextVisualContent = null; _shadow?.Dispose(); _shadow = null; _layerEffectBrush?.Dispose(); _layerEffectBrush = null; _imageOptions = null; _zoomInAnimationGroup?.Dispose(); _zoomInAnimationGroup = null; _fadeOutAnimation?.Dispose(); _fadeOutAnimation = null; _fadeInAnimation?.Dispose(); _fadeInAnimation = null; _colorAnimation?.Dispose(); _colorAnimation = null; _alignXAnimation?.Dispose(); _alignXAnimation = null; _alignYAnimation?.Dispose(); _alignYAnimation = null; _offsetAnimation?.Dispose(); _offsetAnimation = null; _scaleAnimation?.Dispose(); _scaleAnimation = null; // Dispose the generator at the end to allow the // dependant composition objects to unsubscribe from // generator events _generator?.Dispose(); _generator = null; }
private async void OnPageLoaded(object sender, RoutedEventArgs e) { _compositor = Window.Current.Compositor; _generator = _compositor.CreateCompositionGenerator(); var gridSize = new Vector2((float)RootGrid.ActualWidth, (float)RootGrid.ActualHeight); var anim = _compositor.CreatePathKeyFrameAnimation(); _rootVisual = _compositor.CreateSpriteVisual(); _rootVisual.Size = gridSize; // Create the surface brush from the image var imageSurface = await _generator.CreateImageSurfaceAsync( new Uri("ms-appx:///Assets/Images/Cat.png"), new Size(400, 400), ImageSurfaceOptions.Default); var imageBrush = _compositor.CreateSurfaceBrush(imageSurface); // Create the clipped visuals for (var i = 0; i < 145; i++) { var visual = _compositor.CreateSpriteVisual(); visual.Offset = new Vector3(400, 400, 0); visual.Size = new Vector2(400, 400); visual.Brush = imageBrush; visual.AnchorPoint = new Vector2(0.5f); var radius = 290 - (i * 2); // Create the GeometricClip for this visual var clipGeometry = CanvasGeometry.CreateCircle(null, new Vector2(200, 200), radius); visual.Clip = _compositor.CreateGeometricClip(clipGeometry); _rootVisual.Children.InsertAtTop(visual); _visuals.Add(visual); } // Display the rootVisual ElementCompositionPreview.SetElementChildVisual(RootGrid, _rootVisual); // Reverse the visuals list so that the items in the list are now sorted // in z-order from top to bottom _visuals.Reverse(); // The topmost visual would track the pointer position _dragVisual = _visuals.First(); // Get the CompositionPropertySet which tracks the pointer position on the RootGrid _pointerTrackerSet = ElementCompositionPreview.GetPointerPositionPropertySet(RootGrid); // Animate the topmost visual so that it tracks and follows the pointer position _pointerTrackerAnimation = _compositor.GenerateVector3KeyFrameAnimation() .HavingDuration(PointerTrackerAnimationDuration) .RepeatsForever(); _pointerTrackerAnimation.InsertExpressionKeyFrame(0f, c => new VisualTarget().Offset); _pointerTrackerAnimation.InsertExpressionKeyFrame( 1f, c => c.Lerp(new VisualTarget().Offset, _pointerTrackerSet.Get <Vector3>("Position"), DefaultLerpAmount), _compositor.CreateEaseOutQuinticEasingFunction()); // Animate the remaining visuals in such a way that each visual tracks and follows the // position of the visual above it. var prevChild = _dragVisual; foreach (var child in _visuals.Skip(1)) { var offsetAnimation = _compositor.GenerateVector3KeyFrameAnimation() .HavingDuration(ChildOffsetAnimationDuration) .RepeatsForever(); offsetAnimation.InsertExpressionKeyFrame(0f, c => new VisualTarget().Offset); offsetAnimation.InsertExpressionKeyFrame( 1f, c => c.Lerp(new VisualTarget().Offset, prevChild.Offset, DefaultLerpAmount), _compositor.CreateEaseOutQuinticEasingFunction()); child.StartAnimation(() => child.Offset, offsetAnimation); prevChild = child; } }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); var visualChild = _compositor.CreateSpriteVisual(); visualChild.Size = new Vector2(_width * 0.75f, _height * 0.75f); visualChild.Offset = new Vector3(_width*0.125f, _height*0.125f, 0); _visual1.Children.InsertAtTop(visualChild); // Create the CompositionMask var compositionMask = _generator.CreateMaskSurface(_visual1.Size.ToSize(), _combinedGeometry); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(compositionMask.Surface); var source = _compositor.CreateColorBrush(Color.FromArgb(255, 0, 173, 239)); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _visual1.Brush = maskBrush; ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, _visual1); // Create custom shaped visual using CompositionMaskBrush _visual2 = _compositor.CreateSpriteVisual(); _visual2.Size = new Vector2(_width, _height); _visual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask filled with color var compositionMask2 = _generator.CreateGeometrySurface(_visual2.Size.ToSize(), _combinedGeometry, Color.FromArgb(192, 192, 0, 0)); // Create SurfaceBrush from CompositionMask var surfaceBrush = _compositor.CreateSurfaceBrush(compositionMask2.Surface); _visual2.Brush = surfaceBrush; ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, _visual2); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container = _compositor.CreateContainerVisual(); container.Size = new Vector2(CompositionGrid3.ActualWidth.Single(), CompositionGrid3.ActualHeight.Single()); // Background Visual _bgVisual = _compositor.CreateSpriteVisual(); _bgVisual.Size = new Vector2(_width, _height); _bgVisual.Offset = new Vector3(((CompositionGrid3.ActualWidth - _width) / 2).Single(), ((CompositionGrid3.ActualHeight - _height) / 2).Single(), 0); var radians = ((45f*Math.PI)/180).Single(); var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width/2, _height/2))); var bgMask = _generator.CreateGeometrySurface(_bgVisual.Size.ToSize(), bgGeometry, Color.FromArgb(255, 0, 173, 239)); var bgBrush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual.Brush = bgBrush; container.Children.InsertAtBottom(_bgVisual); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid3.ActualWidth - _width) / 2).Single(), ((CompositionGrid3.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedMaskSurface = _generator.CreateGeometrySurface(_animatedVisual.Size.ToSize(), excludedGeometry, Color.FromArgb(192, 192, 0, 0)); var animBrush = _compositor.CreateSurfaceBrush(_animatedMaskSurface.Surface); _animatedVisual.Brush = animBrush; container.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid3, container); }
/// <summary> /// Initialize all Composition related stuff here (Compositor, Animations etc) /// </summary> private void InitializeComposition() { var rootVisual = ElementCompositionPreview.GetElementVisual(this); // Compositor _compositor = rootVisual.Compositor; // Composition Generator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Final Value Expressions var vector3Expr = _compositor.CreateFinalValueExpression <Vector3>(); var scalarExpr = _compositor.CreateFinalValueExpression <float>(); // Opacity Animation var opacityAnimation = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(DefaultOpacityAnimationDuration) .ForTarget(() => rootVisual.Opacity); opacityAnimation.InsertExpressionKeyFrame(1f, scalarExpr); // Scale Animation var scaleAnimation = _compositor.CreateKeyFrameAnimation <Vector3>() .HavingDuration(DefaultScaleAnimationDuration) .ForTarget(() => rootVisual.Scale); scaleAnimation.InsertExpressionKeyFrame(1f, vector3Expr); // ImplicitAnimation _implicitAnimationCollection = _compositor.CreateImplicitAnimationCollection(); _implicitAnimationCollection["Opacity"] = opacityAnimation.Animation; _implicitAnimationCollection["Scale"] = scaleAnimation.Animation; // Expand Animations _expandLeftInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandLeftInset.InsertKeyFrame(1f, 0); _expandRightInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandInsetClip = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetClipAnimationDuration); _expandInsetClip.InsertKeyFrame(1f, 0); // Collapse Animations _collapseLeftInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration); _collapseRightInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration); _collapseInsetClip = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetClipAnimationDuration); // Root Container _rootContainer = _compositor.CreateContainerVisual(); // Background Layer _bgLayer = _compositor.CreateLayerVisual(); _bgLayer.Size = _rootContainer.Size; _bgLayer.CenterPoint = new Vector3(_bgLayer.Size * 0.5f, 0); // Top Layer _topLayer = _compositor.CreateLayerVisual(); _topLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtBottom(_bgLayer); _rootContainer.Children.InsertAtTop(_topLayer); // Add the rootContainer to the visual tree ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.Single(), CompositionGrid1.ActualHeight.Single()); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.45f, _height * 0.45f); _bgVisual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); _bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); container1.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var compositionMask = _generator.CreateMaskSurface(_visual1.Size.ToSize(), ellipseGeometry); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateMaskedBackdropBrush(compositionMask, Colors.AntiqueWhite, 30f, _backdropBrush); container1.Children.InsertAtTop(_visual1); var visual = _compositor.CreateSpriteVisual(); visual.Size = new Vector2(400, 400); visual.Offset = new Vector3(((3 * CompositionGrid1.ActualWidth / 4f) - (_width / 2)).Single(), ((3 * CompositionGrid1.ActualHeight / 4f) - (_height / 2)).Single(), 0); var roundRectGeometry = CanvasGeometry.CreateRoundedRectangle(_generator.Device, 0, 0, _width, _height, 25, 25); var maskSurface = _generator.CreateMaskSurface(visual.Size.ToSize(), roundRectGeometry); var frostedBrush = _compositor.CreateFrostedGlassBrush(maskSurface, Colors.AntiqueWhite, 30f, _backdropBrush); var shadow = _compositor.CreateDropShadow(); shadow.Opacity = 0.5f; shadow.Color = Colors.Black; shadow.Offset = new Vector3(10, 10, 0); shadow.BlurRadius = 15; shadow.Mask = frostedBrush.GetSourceParameter("mask"); visual.Brush = frostedBrush; visual.Shadow = shadow; container1.Children.InsertAtTop(visual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, container1); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.Single(), CompositionGrid2.ActualHeight.Single()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).Single(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).Single(), 0); //var radians = ((45f * Math.PI) / 180).Single(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMaskSurface(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }