Пример #1
0
        public void ParsePathReturnsValidPath()
        {
            // based on ParsePath

            foreach (var test in parsePathTestCases)
            {
                var path = SKPath.ParseSvgPathData(test.Item1);

                Assert.IsNotNull(path);
                Assert.AreEqual(test.Item2, path.Bounds);

                TestToFromSvgPath(path);
            }

            var r = SKRect.Create(0, 0, 10, 10.5f);

            using (var p = new SKPath()) {
                p.AddRect(r);
                TestToFromSvgPath(p);

                p.AddOval(r);
                TestToFromSvgPath(p);

                p.AddRoundedRect(r, 4, 4.5f);
                TestToFromSvgPath(p);
            }
        }
        public ConveyorBeltPage()
        {
            Title = "Conveyor Belt";

            canvasView = new SKCanvasView();
            canvasView.PaintSurface += OnCanvasViewPaintSurface;
            Content = canvasView;

            // Create the path for the bucket starting with the handle
            bucketPath.AddRect(new SKRect(-5, -3, 25, 3));

            // Sides
            bucketPath.AddRoundedRect(new SKRect(25, -19, 27, 18), 10, 10,
                                      SKPathDirection.CounterClockwise);
            bucketPath.AddRoundedRect(new SKRect(63, -19, 65, 18), 10, 10,
                                      SKPathDirection.CounterClockwise);

            // Five slats
            for (int i = 0; i < 5; i++)
            {
                bucketPath.MoveTo(25, -19 + 8 * i);
                bucketPath.LineTo(25, -13 + 8 * i);
                bucketPath.ArcTo(50, 50, 0, SKPathArcSize.Small,
                                 SKPathDirection.CounterClockwise, 65, -13 + 8 * i);
                bucketPath.LineTo(65, -19 + 8 * i);
                bucketPath.ArcTo(50, 50, 0, SKPathArcSize.Small,
                                 SKPathDirection.Clockwise, 25, -19 + 8 * i);
                bucketPath.Close();
            }

            // Arc to suggest the hidden side
            bucketPath.MoveTo(25, -17);
            bucketPath.ArcTo(50, 50, 0, SKPathArcSize.Small,
                             SKPathDirection.Clockwise, 65, -17);
            bucketPath.LineTo(65, -19);
            bucketPath.ArcTo(50, 50, 0, SKPathArcSize.Small,
                             SKPathDirection.CounterClockwise, 25, -19);
            bucketPath.Close();

            // Make it a little bigger and correct the orientation
            bucketPath.Transform(SKMatrix.MakeScale(-2, 2));
            bucketPath.Transform(SKMatrix.MakeRotationDegrees(90));
        }
Пример #3
0
        /// <summary>
        /// Converts the current shape to a path.
        /// </summary>
        /// <returns>The shape as a <c>SKPath</c>.</returns>
        public override SKPath ToPath()
        {
            // Update any attached properties
            EvaluateConnectedProperties();

            // Construct new path
            var path = new SKPath();

            // Add round rect to path
            path.AddRoundedRect(Rect, CornerRadius, CornerRadius, SKPathDirection.Clockwise);

            // Return path
            return(path);
        }
Пример #4
0
        public override void Awake(NSObject context)
        {
            base.Awake(context);

            var scale = WKInterfaceDevice.CurrentDevice.ScreenScale;

            var bitmap = new SKBitmap((int)(ContentFrame.Width * scale), (int)(ContentFrame.Height * scale));

            var colors = new[] { SKColors.Cyan, SKColors.Magenta, SKColors.Yellow, SKColors.Cyan };
            var center = new SKPoint(bitmap.Width / 2, bitmap.Height / 2);

            using (var canvas = new SKCanvas(bitmap))
            {
                canvas.Clear(SKColors.Transparent);

                using (var path = new SKPath())
                {
                    path.AddRoundedRect(new SKRect(5, 5, bitmap.Width - 5, bitmap.Height - 5), 5, 5);
                    canvas.ClipPath(path);
                }

                using (var paint = new SKPaint())
                    using (var gradient = SKShader.CreateSweepGradient(center, colors, null))
                    {
                        paint.IsAntialias = true;
                        paint.Shader      = gradient;
                        canvas.DrawPaint(paint);
                        paint.Shader = null;
                    }

                using (var paint = new SKPaint())
                    using (var tf = SKTypeface.FromFamilyName("San Fransisco"))
                    {
                        paint.IsAntialias = true;
                        paint.Color       = SKColors.DarkBlue;
                        paint.TextSize    = (float)(20 * scale);
                        paint.TextAlign   = SKTextAlign.Center;
                        paint.Typeface    = tf;

                        canvas.DrawText("SkiaSharp", center.X, (center.Y / 2) + (paint.TextSize / 2), paint);
                    }
            }

            var image = bitmap.ToUIImage(scale, UIKit.UIImageOrientation.Up);

            imageView.SetImage(image);
        }
Пример #5
0
        public override SKPath Render(SKCanvas g, Rectangle r, NodeStyle o)
        {
            var skPath = new SKPath();

            skPath.AddRoundedRect(r.ToSKRect(), r.Height / 2, r.Height / 2);
            using (var p = new SKPaint()
            {
                IsAntialias = true
            })
            {
                p.Color = new Pen(o.BackBrush).Color.ToSKColor();
                g.DrawPath(skPath, p);
                p.IsStroke    = true;
                p.Color       = o.BorderPen.Color.ToSKColor();
                p.StrokeWidth = o.BorderPen.Width;
                g.DrawPath(skPath, p);
            }
            return(skPath);
        }
Пример #6
0
        private void DrawInnerBlurRectangle(SKCanvas canvas, SKRect rect)
        {
            // create the rounded rectangle
            var roundedRect = new SKPath();

            roundedRect.AddRoundedRect(rect, 10, 10);

            // draw the white background
            var p = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = SKColors.White
            };

            canvas.DrawPath(roundedRect, p);

            using (new SKAutoCanvasRestore(canvas))
            {
                // clip the canvas to stop the blur from appearing outside
                canvas.ClipPath(roundedRect, SKClipOperation.Intersect, true);

                // draw the wide blur all around
                p.Color       = SKColors.Black;
                p.Style       = SKPaintStyle.Stroke;
                p.StrokeWidth = 2;
                p.MaskFilter  = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 2, SKBlurMaskFilterFlags.HighQuality);
                canvas.Translate(0.5f, 1.5f);
                canvas.DrawPath(roundedRect, p);

                // draw the narrow blur at the top
                p.StrokeWidth = 1;
                p.MaskFilter  = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 1, SKBlurMaskFilterFlags.HighQuality);
                canvas.DrawPath(roundedRect, p);
            }

            // draw the border
            p.StrokeWidth = 2;
            p.MaskFilter  = null;
            p.Color       = SampleMedia.Colors.XamarinGreen;
            canvas.DrawPath(roundedRect, p);
        }
Пример #7
0
        public override SKPath GetPath()
        {
            if (path != null)
            {
                return(path);
            }

            path = new SKPath();
            var rect = SKRect.Create(0, 0, (float)Width, (float)Height);

            if (RadiusX > 0 || RadiusY > 0)
            {
                path.AddRoundedRect(rect, (float)RadiusX, (float)RadiusY);
            }
            else
            {
                path.AddRect(rect);
            }

            return(path);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            using (SKPath roundRectPath = new SKPath())
            {
                // Create a path
                roundRectPath.AddRoundedRect(
                    new SKRect(50, 50, info.Width - 50, info.Height - 50), 100, 100);

                // Horizontal hatch marks
                fillPaint.PathEffect = horzLinesPath;
                fillPaint.Color      = SKColors.Red;
                canvas.DrawPath(roundRectPath, fillPaint);

                // Vertical hatch marks
                fillPaint.PathEffect = vertLinesPath;
                fillPaint.Color      = SKColors.Blue;
                canvas.DrawPath(roundRectPath, fillPaint);

                // Diagonal hatch marks -- use clipping
                fillPaint.PathEffect = diagLinesPath;
                fillPaint.Color      = SKColors.Green;

                canvas.Save();
                canvas.ClipPath(roundRectPath);
                canvas.DrawRect(new SKRect(0, 0, info.Width, info.Height), fillPaint);
                canvas.Restore();

                // Outline the path
                canvas.DrawPath(roundRectPath, strokePaint);
            }
        }
Пример #9
0
        public override void Render(SKCanvas canvas)
        {
            var boundsAligned = BoundsStrokeAligned(Bounds);
            var boundsInner   = BoundsMinusThickness(Bounds);
            var radiuses      = FixedCornerRadius;

            if (radiuses.IsEmpty && Thickness.IsSymmetric)
            {
                // Simple rect
                canvas.DrawRect(Bounds, Background);
                if (!Thickness.IsEmpty)
                {
                    var sw = BorderBrush.StrokeWidth;
                    BorderBrush.StrokeWidth = Thickness.Top;
                    canvas.DrawRect(BorderBrush.StrokeWidth % 2 > 0 ? boundsAligned : Bounds, BorderBrush);
                    BorderBrush.StrokeWidth = sw;
                }
            }
            else if (radiuses.IsSymetric && Thickness.IsSymmetric)
            {
                // Simple round rect
                canvas.DrawRoundRect(Bounds, radiuses.TopLeft, radiuses.TopLeft, Background);
                if (!Thickness.IsEmpty)
                {
                    var sw = BorderBrush.StrokeWidth;
                    BorderBrush.StrokeWidth = Thickness.Top;
                    canvas.DrawRoundRect(BorderBrush.StrokeWidth % 2 > 0 ? boundsAligned : Bounds, radiuses.TopLeft, radiuses.TopLeft, BorderBrush);
                    BorderBrush.StrokeWidth = sw;
                }
            }
            else
            {
                BorderBrush.IsStroke = false;
                // Custom rect
                var outerPath = new SKPath();
                if (!radiuses.IsSymetric)
                {
                    if (radiuses.TopLeft > 0)
                    {
                        outerPath.AddArc(new SKRect(Bounds.Left, Bounds.Top, Bounds.Left + radiuses.TopLeft * 2, Bounds.Top + radiuses.TopLeft * 2), 180, 90);
                    }

                    outerPath.RLineTo(Width - radiuses.TopLeft - radiuses.TopRight, 0);
                    if (radiuses.TopRight > 0)
                    {
                        outerPath.RArcTo(radiuses.TopRight, radiuses.TopRight, 90, SKPathArcSize.Small,
                                         SKPathDirection.Clockwise, radiuses.TopRight, radiuses.TopRight);
                    }

                    outerPath.RLineTo(0, Height - radiuses.TopRight - radiuses.BottomRight);
                    if (radiuses.BottomRight > 0)
                    {
                        outerPath.RArcTo(-radiuses.BottomRight, radiuses.BottomRight, 90, SKPathArcSize.Small,
                                         SKPathDirection.Clockwise, -radiuses.BottomRight, radiuses.BottomRight);
                    }

                    outerPath.RLineTo(-Width + radiuses.BottomLeft + radiuses.BottomRight, 0);
                    if (radiuses.BottomLeft > 0)
                    {
                        outerPath.RArcTo(-radiuses.BottomLeft, -radiuses.BottomLeft, 90, SKPathArcSize.Small,
                                         SKPathDirection.Clockwise, -radiuses.BottomLeft, -radiuses.BottomLeft);
                    }

                    outerPath.Close();
                }
                else
                {
                    if (!radiuses.IsEmpty)
                    {
                        outerPath.AddRoundedRect(Bounds, radiuses.TopLeft, radiuses.TopLeft);
                    }
                    else
                    {
                        outerPath.AddRect(Bounds);
                    }
                }

                var innerPath = new SKPath();

                var rx = radiuses.TopLeft - Thickness.Left;
                var ry = radiuses.TopLeft - Thickness.Top;
                if (rx > 0 && ry > 0)
                {
                    // Use arc
                    innerPath.MoveTo(boundsInner.Left, Bounds.Top + radiuses.TopLeft);
                    innerPath.RArcTo(rx, ry, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, rx, -ry);
                }
                else
                {
                    // Use point
                    innerPath.MoveTo(boundsInner.Location);
                }

                rx = radiuses.TopRight - Thickness.Right;
                ry = radiuses.TopRight - Thickness.Top;
                if (rx > 0 && ry > 0)
                {
                    innerPath.LineTo(Bounds.Right - radiuses.TopRight, boundsInner.Top);
                    innerPath.RArcTo(rx, ry, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, rx, ry);
                }
                else
                {
                    innerPath.LineTo(boundsInner.Right, boundsInner.Top);
                }

                rx = radiuses.BottomRight - Thickness.Right;
                ry = radiuses.BottomRight - Thickness.Bottom;
                if (rx > 0 && ry > 0)
                {
                    innerPath.LineTo(boundsInner.Right, Bounds.Bottom - radiuses.BottomRight);
                    innerPath.RArcTo(rx, ry, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, -rx, ry);
                }
                else
                {
                    innerPath.LineTo(boundsInner.Right, boundsInner.Bottom);
                }


                rx = radiuses.BottomLeft - Thickness.Left;
                ry = radiuses.BottomLeft - Thickness.Bottom;
                if (rx > 0 && ry > 0)
                {
                    innerPath.LineTo(Bounds.Left + radiuses.BottomLeft, boundsInner.Bottom);
                    innerPath.RArcTo(rx, ry, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, -rx, -ry);
                }
                else
                {
                    innerPath.LineTo(boundsInner.Left, boundsInner.Bottom);
                }

                innerPath.Close();

                var borderPath = outerPath.Op(innerPath, SKPathOp.Difference);
                canvas.DrawPath(borderPath, BorderBrush);
                canvas.DrawPath(innerPath, Background);

                base.Render(canvas);
            }
        }
Пример #10
0
        private SKPath ReadElement(XElement e)
        {
            var path = new SKPath();

            var elementName = e.Name.LocalName;

            switch (elementName)
            {
            case "rect":
                var rect = ReadRoundedRect(e);
                if (rect.IsRounded)
                {
                    path.AddRoundedRect(rect.Rect, rect.RadiusX, rect.RadiusY);
                }
                else
                {
                    path.AddRect(rect.Rect);
                }
                break;

            case "ellipse":
                var oval = ReadOval(e);
                path.AddOval(oval.BoundingRect);
                break;

            case "circle":
                var circle = ReadCircle(e);
                path.AddCircle(circle.Center.X, circle.Center.Y, circle.Radius);
                break;

            case "path":
                var d = e.Attribute("d")?.Value;
                if (!string.IsNullOrWhiteSpace(d))
                {
                    path.Dispose();
                    path = SKPath.ParseSvgPathData(d);
                }
                break;

            case "polygon":
            case "polyline":
                var close = elementName == "polygon";
                var p     = e.Attribute("points")?.Value;
                if (!string.IsNullOrWhiteSpace(p))
                {
                    p = "M" + p;
                    if (close)
                    {
                        p += " Z";
                    }
                    path.Dispose();
                    path = SKPath.ParseSvgPathData(p);
                }
                break;

            case "line":
                var line = ReadLine(e);
                path.MoveTo(line.P1);
                path.LineTo(line.P2);
                break;

            default:
                path.Dispose();
                path = null;
                break;
            }

            return(path);
        }