Ejemplo n.º 1
0
        private static CanvasGeometry CreateDountCore(CanvasGeometry outter, float holeRadius, Vector2 center)
        {
            // Donut
            Matrix3x2 holeMatrix = Matrix3x2.CreateTranslation(-center) * Matrix3x2.CreateScale(holeRadius) * Matrix3x2.CreateTranslation(center);

            return(outter.CombineWith(outter, holeMatrix, CanvasGeometryCombine.Exclude));
        }
        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);
        }
Ejemplo n.º 3
0
        static List <Data.Path> Op(IEnumerable <Data.Path> paths, IEnumerable <Data.Path> otherPaths, CanvasGeometryCombine operation)
        {
            using CanvasGeometry geom1 = MakeGeometry(paths),
                  geom2      = MakeGeometry(otherPaths);
            using var result = geom1.CombineWith(geom2, Matrix3x2.Identity, operation, float.Epsilon);

            var recv = new GeometrySink();

            result.SendPathTo(recv);

            return(recv.Paths);
        }
        private async void AnimatedCanvasCtrl_OnDraw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
        {
            if (_animatedCompositionMask == null)
            {
                return;
            }

            _angle = (float)((_angle + 1) % 360);
            var radians = (float)((_angle * Math.PI) / 180);
            // Calculate the new geometry based on the angle
            var updatedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.CreateRotation(radians, new Vector2(200, 200)),
                                                             CanvasGeometryCombine.Exclude);
            // Update the geometry in the Composition Mask
            await _animatedCompositionMask.RedrawAsync(updatedGeometry);
        }
Ejemplo n.º 5
0
        public override ICanvasImage GetRender(ICanvasResourceCreator resourceCreator, IList <Layerage> children)
        {
            Transformer transformer = base.Transform.Transformer;

            CanvasGeometry geometry = transformer.ToRectangle(resourceCreator);


            CanvasCommandList command = new CanvasCommandList(resourceCreator);

            using (CanvasDrawingSession drawingSession = command.CreateDrawingSession())
            {
                if (this.Transform.IsCrop)
                {
                    CanvasGeometry geometryCrop = this.Transform.CropTransformer.ToRectangle(resourceCreator);

                    CanvasGeometryRelation relation = geometry.CompareWith(geometryCrop);
                    switch (relation)
                    {
                    case CanvasGeometryRelation.Disjoint:
                        return(null);

                    case CanvasGeometryRelation.Contained:
                        this._patternRender(resourceCreator, drawingSession, geometry);
                        break;

                    case CanvasGeometryRelation.Contains:
                        this._patternRender(resourceCreator, drawingSession, geometryCrop);
                        break;

                    case CanvasGeometryRelation.Overlap:
                        Matrix3x2      zero    = Matrix3x2.CreateTranslation(0.0f, 0.0f);
                        CanvasGeometry combine = geometry.CombineWith(geometryCrop, zero, CanvasGeometryCombine.Intersect);
                        this._patternRender(resourceCreator, drawingSession, combine);
                        break;

                    default:
                        return(null);
                    }
                }
                else
                {
                    this._patternRender(resourceCreator, drawingSession, geometry);
                }
            }
            return(command);
        }
        private void AnimatedCanvasCtrl_OnDraw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
        {
            if (_animatedMaskSurface == null)
            {
                return;
            }

            _angle = (float)((_angle + 1) % 360);
            var angle = _angle * Scalar.DegreesToRadians;
            // Calculate the new geometry based on the angle
            var combinedGeometry = _ellipse1.CombineWith(_ellipse2, Matrix3x2.CreateRotation(angle, _center), CanvasGeometryCombine.Union);
            var updatedGeometry  = _outerGeometry.CombineWith(combinedGeometry,
                                                              Matrix3x2.CreateRotation(angle / 2, _center),
                                                              CanvasGeometryCombine.Xor);

            // Update the geometry in the Composition Mask
            _animatedMaskSurface.Redraw(updatedGeometry);
        }
Ejemplo n.º 7
0
        List <Rect> SplitGeometryIntoRectangles(CanvasGeometry geometry, float rectangleHeight, CanvasControl sender)
        {
            List <Rect> result = new List <Rect>();

            var    geometryBounds = geometry.ComputeBounds();
            double left           = geometryBounds.X;
            double top            = geometryBounds.Y;
            double y = top;

            while (y < geometryBounds.Bottom)
            {
                var lineRegion         = new Rect(left, y, geometryBounds.Width, rectangleHeight);
                var lineRegionGeometry = CanvasGeometry.CreateRectangle(sender, lineRegion);
                var compareResult      = geometry.CompareWith(lineRegionGeometry);

                if (compareResult == CanvasGeometryRelation.Contains)
                {
                    // The whole line fits.
                    result.Add(lineRegion);
                }
                else if (compareResult == CanvasGeometryRelation.Disjoint)
                {
                    // The line doesn't fit, so skip it.
                }
                else if (compareResult == CanvasGeometryRelation.Overlap)
                {
                    var intersection = geometry.CombineWith(lineRegionGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Intersect);

                    PathReader pathReader = new PathReader(lineRegion);
                    intersection.Simplify(CanvasGeometrySimplification.Lines).SendPathTo(pathReader);

                    var rectangles = pathReader.GetRectangles();
                    rectangles.Sort(new RectangleComparer(CurrentTextDirection == TextDirection.RightToLeft));
                    result.AddRange(rectangles);
                }

                y += rectangleHeight;
            }

            return(result);
        }
Ejemplo n.º 8
0
        private void DrawIdleBg(CanvasDrawingSession ds)
        {
            Easings.ParamTween(ref BgY_c, BgY_t, 0.85f, 0.15f);
            BgFillRect.Y = BgY_c;

            Easings.ParamTween(ref BgR_c, BgR_t, 0.75f, 0.25f);
            if (BgR_c < 0)
            {
                ds.DrawImage(BgBmp, StageRect, BgFillRect);
                ds.FillRectangle(StageRect, MaskBrush);
            }
            else
            {
                ds.DrawImage(BgBmp, StageRect, BgFillRect);

                CanvasGeometry MaskFill  = CanvasGeometry.CreateRectangle(ds, StageRect);
                CanvasGeometry DrillMask = CanvasGeometry.CreateCircle(ds, PCenter, BgR_c);
                CanvasGeometry Combined  = MaskFill.CombineWith(DrillMask, Matrix3x2.CreateTranslation(0, 0), CanvasGeometryCombine.Exclude);

                ds.FillGeometry(Combined, MaskBrush);
            }
        }
Ejemplo n.º 9
0
        private void RecreateGeometry(ICanvasResourceCreator resourceCreator)
        {
            leftGeometry  = CreateGeometry(resourceCreator, LeftGeometryType);
            rightGeometry = CreateGeometry(resourceCreator, RightGeometryType);

            if (enableTransform)
            {
                Matrix3x2 placeNearOrigin     = Matrix3x2.CreateTranslation(-200, -200);
                Matrix3x2 undoPlaceNearOrigin = Matrix3x2.CreateTranslation(200, 200);

                Matrix3x2 rotate0 = Matrix3x2.CreateRotation((float)Math.PI / 4.0f); // 45 degrees
                Matrix3x2 scale0  = Matrix3x2.CreateScale(1.5f);

                Matrix3x2 rotate1 = Matrix3x2.CreateRotation((float)Math.PI / 6.0f); // 30 degrees
                Matrix3x2 scale1  = Matrix3x2.CreateScale(2.0f);

                leftGeometry  = leftGeometry.Transform(placeNearOrigin * rotate0 * scale0 * undoPlaceNearOrigin);
                rightGeometry = rightGeometry.Transform(placeNearOrigin * rotate1 * scale1 * undoPlaceNearOrigin);
            }

            combinedGeometry = leftGeometry.CombineWith(rightGeometry, interGeometryTransform, WhichCombineType);

            if (UseFillOrStroke == FillOrStroke.Stroke)
            {
                CanvasStrokeStyle strokeStyle = new CanvasStrokeStyle();
                strokeStyle.DashStyle = CanvasDashStyle.Dash;
                combinedGeometry      = combinedGeometry.Stroke(15.0f, strokeStyle);
            }

            totalDistanceOnContourPath = combinedGeometry.ComputePathLength();

            if (showTessellation)
            {
                tessellation = combinedGeometry.Tessellate();
            }
        }
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _compositor    = ElementCompositionPreview.GetElementVisual(this).Compositor;
            _generator     = CompositionMaskFactory.GetCompositionMaskGenerator(_compositor);
            _backdropBrush = _compositor.CreateBackdropBrush();

            //Create the visual
            _visual        = _compositor.CreateSpriteVisual();
            _visual.Size   = new Vector2(400, 400);
            _visual.Offset = new Vector3(200, 0, 0);
            // Create the combined geometry
            var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, 200, 200, 150, 75);
            var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, 200, 200, 75, 150);

            _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union);
            // Create the CompositionMask
            _compositionMask = await _generator.CreateMaskAsync(_visual.Size.ToSize(), _combinedGeometry);

            // Create SurfaceBrush from CompositionMask
            var mask    = _compositor.CreateSurfaceBrush(_compositionMask.Surface);
            var source1 = _compositor.CreateColorBrush(Colors.Blue);
            // Create mask brush
            var maskBrush = _compositor.CreateMaskBrush();

            maskBrush.Mask   = mask;
            maskBrush.Source = source1;

            _visual.Brush = maskBrush;

            ElementCompositionPreview.SetElementChildVisual(CanvasCtrl, _visual);

            var container = _compositor.CreateContainerVisual();

            container.Size = new Vector2(1000, 1000);
            // Background visual
            var bgVisual = _compositor.CreateSpriteVisual();

            bgVisual.Size   = new Vector2(200, 200);
            bgVisual.Offset = new Vector3(300, 100, 0);
            bgVisual.Brush  = source1;
            container.Children.InsertAtTop(bgVisual);

            // Animated Visual
            _animatedVisual        = _compositor.CreateSpriteVisual();
            _animatedVisual.Size   = new Vector2(400, 400);
            _animatedVisual.Offset = new Vector3(200, 0, 0);
            // Create the Excluded geometry
            _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, 400, 400);
            var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity,
                                                              CanvasGeometryCombine.Exclude);

            // Create the CompositionMask
            _animatedCompositionMask = await _generator.CreateMaskAsync(_animatedVisual.Size.ToSize(), excludedGeometry);

            //// Create SurfaceBrush from CompositionMask
            //var animatedMask = _compositor.CreateSurfaceBrush(_animatedCompositionMask.Surface);
            //var source2 = _compositor.CreateColorBrush(Colors.Red);
            //// Create mask brush
            //var animatedMaskBrush = _compositor.CreateMaskBrush();
            //animatedMaskBrush.Mask = animatedMask;
            //animatedMaskBrush.Source = source2;

            _animatedVisual.Brush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Color.FromArgb(240, 232, 232, 232), 20f, _backdropBrush);
            container.Children.InsertAtTop(_animatedVisual);

            ElementCompositionPreview.SetElementChildVisual(AnimatedCanvasCtrl, container);
        }
Ejemplo n.º 11
0
        //Render Time to Drawing Session - Trio
        void DrawTimeOnTileTrio(CanvasDrawingSession ds, string TextTop, string TextMid, string TextBot)
        {
            try
            {
                CanvasTextLayout LayoutTop = new CanvasTextLayout(ds, TextTop, Win2DCanvasTextFormatTitle, LiveTileWidth, LiveTileHeight);
                CanvasTextLayout LayoutMid = new CanvasTextLayout(ds, TextMid, Win2DCanvasTextFormatBody, LiveTileWidth, LiveTileHeight);
                CanvasTextLayout LayoutBot = new CanvasTextLayout(ds, TextBot, Win2DCanvasTextFormatBody, LiveTileWidth, LiveTileHeight);

                //Live tile content - Time
                if (!setLiveTileTimeCutOut)
                {
                    //Live tile background photo or color
                    if (setDisplayBackgroundPhoto)
                    {
                        ds.Clear(Colors.Black); ds.DrawImage(Win2DCanvasBitmap, 0, 0, Win2DCanvasRenderTarget.Bounds, setDisplayBackgroundBrightnessFloat);
                    }
                    else
                    {
                        ds.Clear(Win2DCanvasColor);
                    }

                    //Live tile content - Center
                    if (setLiveTileFontDuoColor)
                    {
                        ds.DrawTextLayout(LayoutTop, 0, (TimeHeight1 - 66), Win2DFontColorWhite);
                    }
                    else
                    {
                        ds.DrawTextLayout(LayoutTop, 0, (TimeHeight1 - 66), Win2DFontColorCusto);
                    }
                    ds.DrawTextLayout(LayoutMid, 0, TimeHeight1, Win2DFontColorCusto);
                    ds.DrawTextLayout(LayoutBot, 0, (TimeHeight1 + 58), Win2DFontColorCusto);
                }
                else
                {
                    //Live tile background transparency
                    ds.Clear(Colors.Transparent);

                    CanvasGeometry GeometryBackground = CanvasGeometry.CreateRectangle(ds, 0, 0, LiveTileWidth, LiveTileHeight);
                    CanvasGeometry GeometryTop        = CanvasGeometry.CreateText(LayoutTop);
                    CanvasGeometry GeometryMid        = CanvasGeometry.CreateText(LayoutMid);
                    CanvasGeometry GeometryBot        = CanvasGeometry.CreateText(LayoutBot);

                    if (setDisplayBackgroundPhoto)
                    {
                        CanvasGeometry GeometryExclude1 = GeometryBackground.CombineWith(GeometryTop, Matrix3x2.CreateTranslation(0, (TimeHeight1 - 66)), CanvasGeometryCombine.Exclude);
                        GeometryExclude1 = GeometryExclude1.CombineWith(GeometryMid, Matrix3x2.CreateTranslation(0, TimeHeight1), CanvasGeometryCombine.Exclude);
                        GeometryExclude1 = GeometryExclude1.CombineWith(GeometryBot, Matrix3x2.CreateTranslation(0, (TimeHeight1 + 58)), CanvasGeometryCombine.Exclude);
                        ds.FillGeometry(GeometryExclude1, Colors.Black);
                        GeometryExclude1.Dispose();
                    }

                    CanvasGeometry GeometryExclude2 = GeometryBackground.CombineWith(GeometryTop, Matrix3x2.CreateTranslation(0, (TimeHeight1 - 66)), CanvasGeometryCombine.Exclude);
                    GeometryExclude2 = GeometryExclude2.CombineWith(GeometryMid, Matrix3x2.CreateTranslation(0, TimeHeight1), CanvasGeometryCombine.Exclude);
                    GeometryExclude2 = GeometryExclude2.CombineWith(GeometryBot, Matrix3x2.CreateTranslation(0, (TimeHeight1 + 58)), CanvasGeometryCombine.Exclude);
                    if (setDisplayBackgroundPhoto)
                    {
                        ds.FillGeometry(GeometryExclude2, Win2DCanvasImageBrush);
                    }
                    else
                    {
                        ds.FillGeometry(GeometryExclude2, Win2DCanvasColor);
                    }
                    GeometryExclude2.Dispose();

                    GeometryBackground.Dispose();
                    GeometryTop.Dispose();
                    GeometryMid.Dispose();
                    GeometryBot.Dispose();
                }

                LayoutTop.Dispose();
                LayoutMid.Dispose();
                LayoutBot.Dispose();
            }
            catch { }
        }
Ejemplo n.º 12
0
        //Render Time to Drawing Session - Solo
        void DrawTimeOnTileSolo(CanvasDrawingSession ds, int AmPmHeightMargin, bool AmPmCenter, bool HorizontalTime)
        {
            try
            {
                //Set the text render layout
                CanvasTextLayout LayoutTimeHour  = new CanvasTextLayout(ds, TextTimeHour, Win2DCanvasTextFormatTitle, LiveTileWidth, LiveTileHeight);
                CanvasTextLayout LayoutTimeSplit = new CanvasTextLayout(ds, TextTimeSplit, Win2DCanvasTextFormatBody, LiveTileWidth, LiveTileHeight);
                CanvasTextLayout LayoutTimeMin   = new CanvasTextLayout(ds, TextTimeMin, Win2DCanvasTextFormatBody, LiveTileWidth, LiveTileHeight);
                CanvasTextLayout LayoutTimeAmPm  = new CanvasTextLayout(ds, TextTimeAmPm, Win2DCanvasTextFormatSub, LiveTileWidth, LiveTileHeight);

                //Calculate the render positions
                int SplitWidthMargin = 15;
                int SplitHorizontal  = 0;
                int HourHorizontal   = 0;
                int MinHorizontal    = 0;
                int AmPmHorizontal   = 0;
                int AmPmVertical     = 0;

                if (Win2DCanvasTextFormatTitle.HorizontalAlignment == CanvasHorizontalAlignment.Center)
                {
                    HourHorizontal = -Convert.ToInt32((LayoutTimeHour.DrawBounds.Width / 2) + (LayoutTimeSplit.DrawBounds.Width / 2) + SplitWidthMargin);
                    MinHorizontal  = Convert.ToInt32((LayoutTimeMin.DrawBounds.Width / 2) + (LayoutTimeSplit.DrawBounds.Width / 2) + SplitWidthMargin);

                    if (!HorizontalTime)
                    {
                        if (setDisplayTimeCustomText)
                        {
                            //Calculate margin difference and center the text
                            if (setDisplayAMPMClock)
                            {
                                if (!AmPmCenter)
                                {
                                    AmPmHorizontal = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Right - LayoutTimeAmPm.DrawBounds.Width);
                                }
                                AmPmVertical = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                            }
                        }
                        else
                        {
                            //Calculate margin difference and center the text
                            Int32 MarginDifference = (Math.Abs(MinHorizontal) - Math.Abs(HourHorizontal));
                            SplitHorizontal = SplitHorizontal - MarginDifference;
                            HourHorizontal  = HourHorizontal - MarginDifference;
                            MinHorizontal   = MinHorizontal - MarginDifference;
                            if (setDisplayAMPMClock)
                            {
                                if (!AmPmCenter)
                                {
                                    AmPmHorizontal = Convert.ToInt32(LayoutTimeMin.DrawBounds.Right + MinHorizontal - LayoutTimeAmPm.DrawBounds.Width);
                                }
                                AmPmVertical = Convert.ToInt32(LayoutTimeMin.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                            }
                        }
                    }
                    else
                    {
                        if (setDisplayTimeCustomText)
                        {
                            //Calculate margin difference and center the text
                            Int32 MarginDifference = 0;
                            if (setDisplayAMPMClock)
                            {
                                MarginDifference = MarginDifference + Convert.ToInt32((LayoutTimeAmPm.DrawBounds.Width / 2) + (SplitWidthMargin / 2));
                            }
                            SplitHorizontal = SplitHorizontal - MarginDifference;

                            if (setDisplayAMPMClock)
                            {
                                AmPmHorizontal = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Right + SplitWidthMargin + SplitHorizontal);
                                int TimeAmPmHeightText = Convert.ToInt32(LayoutTimeAmPm.DrawBounds.Height + LayoutTimeAmPm.DrawBounds.Top);
                                AmPmVertical = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Bottom + TimeHeight1 - TimeAmPmHeightText);
                            }
                        }
                        else
                        {
                            //Calculate margin difference and center the text
                            Int32 MarginDifference = (Math.Abs(MinHorizontal) - Math.Abs(HourHorizontal));
                            if (setDisplayAMPMClock)
                            {
                                MarginDifference = MarginDifference + Convert.ToInt32((LayoutTimeAmPm.DrawBounds.Width / 2) + (SplitWidthMargin / 2));
                            }
                            SplitHorizontal = SplitHorizontal - MarginDifference;
                            HourHorizontal  = HourHorizontal - MarginDifference;
                            MinHorizontal   = MinHorizontal - MarginDifference;

                            if (setDisplayAMPMClock)
                            {
                                AmPmHorizontal = Convert.ToInt32(LayoutTimeMin.DrawBounds.Right + SplitWidthMargin + MinHorizontal);
                                int TimeAmPmHeightText = Convert.ToInt32(LayoutTimeAmPm.DrawBounds.Height + LayoutTimeAmPm.DrawBounds.Top);
                                AmPmVertical = Convert.ToInt32(LayoutTimeMin.DrawBounds.Bottom + TimeHeight1 - TimeAmPmHeightText);
                            }
                        }
                    }
                }
                else if (Win2DCanvasTextFormatTitle.HorizontalAlignment == CanvasHorizontalAlignment.Right)
                {
                    if (setDisplayTimeCustomText)
                    {
                        SplitHorizontal = -LiveTilePadding;
                        AmPmHorizontal  = -LiveTilePadding;
                        AmPmVertical    = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                    }
                    else
                    {
                        MinHorizontal   = -LiveTilePadding;
                        SplitHorizontal = -Convert.ToInt32(LiveTilePadding + LayoutTimeMin.DrawBounds.Width + SplitWidthMargin);
                        HourHorizontal  = -Convert.ToInt32(LiveTilePadding + LayoutTimeMin.DrawBounds.Width + SplitWidthMargin + LayoutTimeSplit.DrawBounds.Width + SplitWidthMargin);
                        AmPmHorizontal  = -LiveTilePadding;
                        AmPmVertical    = Convert.ToInt32(LayoutTimeMin.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                    }
                }
                else if (Win2DCanvasTextFormatTitle.HorizontalAlignment == CanvasHorizontalAlignment.Left)
                {
                    if (setDisplayTimeCustomText)
                    {
                        SplitHorizontal = LiveTilePadding;
                        AmPmHorizontal  = LiveTilePadding;
                        AmPmVertical    = Convert.ToInt32(LayoutTimeSplit.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                    }
                    else
                    {
                        HourHorizontal  = LiveTilePadding;
                        SplitHorizontal = Convert.ToInt32(LiveTilePadding + LayoutTimeHour.DrawBounds.Width + SplitWidthMargin);
                        MinHorizontal   = Convert.ToInt32(LiveTilePadding + LayoutTimeHour.DrawBounds.Width + SplitWidthMargin + LayoutTimeSplit.DrawBounds.Width + SplitWidthMargin);
                        AmPmHorizontal  = LiveTilePadding;
                        AmPmVertical    = Convert.ToInt32(LayoutTimeHour.DrawBounds.Bottom + TimeHeight1 - AmPmHeightMargin);
                    }
                }

                //Check the live tile text style
                if (!setLiveTileTimeCutOut)
                {
                    //Live tile background photo or color
                    if (setDisplayBackgroundPhoto)
                    {
                        ds.Clear(Colors.Black); ds.DrawImage(Win2DCanvasBitmap, 0, 0, Win2DCanvasRenderTarget.Bounds, setDisplayBackgroundBrightnessFloat);
                    }
                    else
                    {
                        ds.Clear(Win2DCanvasColor);
                    }

                    if (setLiveTileFontDuoColor)
                    {
                        ds.DrawTextLayout(LayoutTimeHour, HourHorizontal, TimeHeight1, Win2DFontColorWhite);
                        ds.DrawTextLayout(LayoutTimeSplit, SplitHorizontal, TimeHeight1, Win2DFontColorCusto);
                        ds.DrawTextLayout(LayoutTimeMin, MinHorizontal, TimeHeight1, Win2DFontColorCusto);
                        ds.DrawTextLayout(LayoutTimeAmPm, AmPmHorizontal, AmPmVertical, Win2DFontColorWhite);
                    }
                    else
                    {
                        ds.DrawTextLayout(LayoutTimeHour, HourHorizontal, TimeHeight1, Win2DFontColorCusto);
                        ds.DrawTextLayout(LayoutTimeSplit, SplitHorizontal, TimeHeight1, Win2DFontColorCusto);
                        ds.DrawTextLayout(LayoutTimeMin, MinHorizontal, TimeHeight1, Win2DFontColorCusto);
                        ds.DrawTextLayout(LayoutTimeAmPm, AmPmHorizontal, AmPmVertical, Win2DFontColorCusto);
                    }
                }
                else
                {
                    //Live tile background transparency
                    ds.Clear(Colors.Transparent);

                    CanvasGeometry GeometryBackground = CanvasGeometry.CreateRectangle(ds, 0, 0, LiveTileWidth, LiveTileHeight);
                    CanvasGeometry GeometryHour       = CanvasGeometry.CreateText(LayoutTimeHour);
                    CanvasGeometry GeometrySplit      = CanvasGeometry.CreateText(LayoutTimeSplit);
                    CanvasGeometry GeometryMin        = CanvasGeometry.CreateText(LayoutTimeMin);
                    CanvasGeometry GeometryAmPm       = CanvasGeometry.CreateText(LayoutTimeAmPm);

                    if (setDisplayBackgroundPhoto)
                    {
                        CanvasGeometry GeometryExclude1 = GeometryBackground.CombineWith(GeometryHour, Matrix3x2.CreateTranslation(HourHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                        GeometryExclude1 = GeometryExclude1.CombineWith(GeometrySplit, Matrix3x2.CreateTranslation(SplitHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                        GeometryExclude1 = GeometryExclude1.CombineWith(GeometryMin, Matrix3x2.CreateTranslation(MinHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                        GeometryExclude1 = GeometryExclude1.CombineWith(GeometryAmPm, Matrix3x2.CreateTranslation(AmPmHorizontal, AmPmVertical), CanvasGeometryCombine.Exclude);
                        ds.FillGeometry(GeometryExclude1, Colors.Black);
                        GeometryExclude1.Dispose();
                    }

                    CanvasGeometry GeometryExclude2 = GeometryBackground.CombineWith(GeometryHour, Matrix3x2.CreateTranslation(HourHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                    GeometryExclude2 = GeometryExclude2.CombineWith(GeometrySplit, Matrix3x2.CreateTranslation(SplitHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                    GeometryExclude2 = GeometryExclude2.CombineWith(GeometryMin, Matrix3x2.CreateTranslation(MinHorizontal, TimeHeight1), CanvasGeometryCombine.Exclude);
                    GeometryExclude2 = GeometryExclude2.CombineWith(GeometryAmPm, Matrix3x2.CreateTranslation(AmPmHorizontal, AmPmVertical), CanvasGeometryCombine.Exclude);
                    if (setDisplayBackgroundPhoto)
                    {
                        ds.FillGeometry(GeometryExclude2, Win2DCanvasImageBrush);
                    }
                    else
                    {
                        ds.FillGeometry(GeometryExclude2, Win2DCanvasColor);
                    }
                    GeometryExclude2.Dispose();

                    GeometryBackground.Dispose();
                    GeometryHour.Dispose();
                    GeometrySplit.Dispose();
                    GeometryMin.Dispose();
                    GeometryAmPm.Dispose();
                }

                LayoutTimeHour.Dispose();
                LayoutTimeSplit.Dispose();
                LayoutTimeMin.Dispose();
                LayoutTimeAmPm.Dispose();
            }
            catch { }
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        public void CreateLoadingWave(Microsoft.Graphics.Canvas.UI.Xaml.ICanvasAnimatedControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasAnimatedDrawEventArgs args, Vector2 position, Color color)
        {
            if (rate >= 10)
            {
                percent++;
                rate = 0;
            }
            if (percent > 100)
            {
                percent = 0;
            }

            CanvasTextLayout textLayout = new CanvasTextLayout(sender, $"{percent}", new CanvasTextFormat()
            {
                FontSize = RadiusValue
            }, RadiusValue * 2, RadiusValue * 2);

            CanvasGeometry orignalText = CanvasGeometry.CreateText(textLayout);
            var            rectText    = orignalText.ComputeBounds();
            var            textOffsetX = (RadiusValue * 2 - textLayout.LayoutBoundsIncludingTrailingWhitespace.Width) / 2;
            var            textOffsetY = (RadiusValue * 2 - textLayout.LayoutBoundsIncludingTrailingWhitespace.Height) / 2;

            orignalText = orignalText.Transform(Matrix3x2.CreateTranslation((float)textOffsetX + position.X, (float)textOffsetY + position.Y));

            CanvasPathBuilder builder = new CanvasPathBuilder(sender);

            var offsetY = 2 * rate / 10 + percent * 2;

            builder.BeginFigure(0 + offsetX + position.X, RadiusValue * 2 - offsetY + position.Y);

            builder.AddCubicBezier(new Vector2(RadiusValue * 1 + offsetX + position.X, RadiusValue * 2 + RadiusValue / 3 - offsetY + position.Y), new Vector2(RadiusValue * 1 + offsetX + position.X, RadiusValue * 2 - RadiusValue / 3 - offsetY + position.Y), new Vector2(RadiusValue * 2 + offsetX + position.X, RadiusValue * 2 - offsetY + position.Y));

            builder.AddCubicBezier(new Vector2(RadiusValue * 3 + offsetX + position.X, RadiusValue * 2 + RadiusValue / 3 - offsetY + position.Y), new Vector2(RadiusValue * 3 + offsetX + position.X, RadiusValue * 2 - RadiusValue / 3 - offsetY + position.Y), new Vector2(RadiusValue * 4 + offsetX + position.X, RadiusValue * 2 - offsetY + position.Y));

            builder.AddLine(RadiusValue * 4 + offsetX + position.X, RadiusValue * 4 + position.Y);
            builder.AddLine(0 + offsetX + position.X, RadiusValue * 4 + position.Y);

            builder.EndFigure(CanvasFigureLoop.Closed);

            var wavePath   = CanvasGeometry.CreatePath(builder);
            var circlePath = CanvasGeometry.CreateCircle(sender, new Vector2(RadiusValue, RadiusValue), RadiusValue);

            var backgroundPath = circlePath.CombineWith(wavePath, Matrix3x2.Identity, CanvasGeometryCombine.Intersect);

            var topText   = orignalText.CombineWith(backgroundPath, Matrix3x2.Identity, CanvasGeometryCombine.Exclude);
            var drawnText = orignalText.CombineWith(backgroundPath, Matrix3x2.Identity, CanvasGeometryCombine.Intersect);


            args.DrawingSession.FillGeometry(backgroundPath, position, color);
            args.DrawingSession.FillGeometry(topText, color);
            args.DrawingSession.FillGeometry(drawnText, Colors.White);

            var borderCircle = CanvasGeometry.CreateCircle(sender, new Vector2(RadiusValue, RadiusValue), RadiusValue - 1);

            args.DrawingSession.DrawGeometry(borderCircle, position, color);

            offsetX--;
            if (offsetX <= -RadiusValue * 2)
            {
                offsetX = 0;
            }
            rate++;
        }
        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);
        }
Ejemplo n.º 16
0
        private void RenderGeometory(CanvasDrawingSession session, CanvasGeometry geometry, SvgMatrix transform, CssStyleDeclaration style)
        {
            bool change    = false;
            var  geometry2 = geometry;
            CanvasSolidColorBrush opacityBrush = null;

            try
            {
                using (var t = TransformSession.CreateTransformSession(session, transform))
                {
                    var clipPath = style.ClipPath;
                    if (clipPath != null)
                    {
                        if (clipPath.Uri[0] != '#')
                        {
                            throw new ArgumentException();
                        }
                        var clipPathElement = (SvgClipPathElement)this.TargetDocument.GetElementById(clipPath.Uri.Substring(1));
                        var clipGeometory   = this.CreateClipPath(session, clipPathElement);
                        geometry2 = geometry.CombineWith(
                            clipGeometory,
                            new Matrix3x2 {
                            M11 = 1.0F, M12 = 0.0F, M21 = 0.0F, M22 = 1.0F, M31 = 0.0F, M32 = 0.0F
                        },
                            CanvasGeometryCombine.Intersect,
                            CanvasGeometry.ComputeFlatteningTolerance(session.Dpi, 1.0F, session.Transform));
                        change = true;
                    }

                    var area = geometry2.ComputeBounds();

                    var opacity = style.Opacity;
                    if (opacity != null)
                    {
                        opacityBrush = this.CreateOpacity(session, opacity.Value);
                    }

                    var fill = style.Fill;
                    if (fill == null || fill != null && fill.PaintType != SvgPaintType.None)
                    {
                        var pen = this.CreatePaint(session, area, fill, style.FillOpacity);
                        if (opacityBrush == null)
                        {
                            session.FillGeometry(geometry2, pen);
                        }
                        else
                        {
                            session.FillGeometry(geometry2, pen, opacityBrush);
                        }
                    }

                    var stroke = style.Stroke;
                    if (stroke != null && stroke.PaintType != SvgPaintType.None)
                    {
                        var pen         = this.CreatePaint(session, area, stroke, style.StrokeOpacity);
                        var strokeWidth = style.StrokeWidth;
                        session.DrawGeometry(geometry2, pen, strokeWidth.HasValue ? this.LengthConverter.Convert(strokeWidth.Value) : 1.0F);
                    }
                }
            }
            finally
            {
                if (change)
                {
                    geometry2.Dispose();
                }
                if (opacityBrush != null)
                {
                    opacityBrush.Dispose();
                }
            }
        }
        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);
        }
Ejemplo n.º 18
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 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);
        }