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);
        }
        private void OnRadiusChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            _radius = (float)RadiusSlider.Value;

            if (_compositor == null)
            {
                return;
            }

            var roundRectGeometry =
                CanvasGeometry.CreateRoundedRectangle(_generator.Device, 0, 0, _width, _height, _radius, _radius);
            var squircleGeometry =
                CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _radius, _radius);
            var differenceGeometry =
                roundRectGeometry.CombineWith(squircleGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude);
            var xorGeometry =
                roundRectGeometry.CombineWith(squircleGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Xor);

            _roundedRectangleSurface.Redraw(roundRectGeometry);
            _squircleSurface.Redraw(squircleGeometry);
            _differenceSurface.Redraw(differenceGeometry);
            _xorSurface.Redraw(xorGeometry);
        }
Пример #3
0
        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);
        }
        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);
        }