Пример #1
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);

            var blockSize = 30;

            // create the path
            var path = new SKPath();
            // the rect must be offset as the path uses the center
            var rect = SKRect.Create(blockSize / -2, blockSize / -2, blockSize, blockSize);

            path.AddRect(rect);

            // move the path around: across 1 block
            var offsetMatrix = SKMatrix.MakeScale(2 * blockSize, blockSize);

            // each row, move across a bit / offset
            SKMatrix.PreConcat(ref offsetMatrix, SKMatrix.MakeSkew(0.5f, 0));

            // create the paint
            var paint = new SKPaint();

            paint.PathEffect = SKPathEffect.Create2DPath(offsetMatrix, path);
            paint.Color      = SKColors.LightGray;

            // draw a rectangle
            canvas.DrawRect(SKRect.Create(width, height), paint);
        }
Пример #2
0
        private void SKCanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
        {
            SKSurface surface = e.Surface;
            SKCanvas  canvas  = surface.Canvas;

            var    scale = ((SKCanvasView)sender).CanvasSize.Width / 20;
            SKPath path  = new SKPath();

            path.MoveTo(-1 * scale, -1 * scale);
            path.LineTo(0 * scale, -1 * scale);
            path.LineTo(0 * scale, 0 * scale);
            path.LineTo(1 * scale, 0 * scale);
            path.LineTo(1 * scale, 1 * scale);
            path.LineTo(0 * scale, 1 * scale);
            path.LineTo(0 * scale, 0 * scale);
            path.LineTo(-1 * scale, 0 * scale);
            path.LineTo(-1 * scale, -1 * scale);

            SKMatrix matrix = SKMatrix.MakeScale(2 * scale, 2 * scale);
            SKPaint  paint  = new SKPaint();

            paint.PathEffect  = SKPathEffect.Create2DPath(matrix, path);
            paint.Color       = Color.LightGray.ToSKColor();
            paint.IsAntialias = true;

            var patternRect = new SKRect(0, 0, ((SKCanvasView)sender).CanvasSize.Width, ((SKCanvasView)sender).CanvasSize.Height);

            canvas.Save();
            canvas.DrawRect(patternRect, paint);
            canvas.Restore();
        }
Пример #3
0
        private void PaintChessPattern(SKCanvas canvas, SliderLocation slider, SKSize canvasSize)
        {
            var    pickerRadiusPixels = GetPickerRadiusPixels();
            var    sliderTop          = slider.GetSliderOffset(pickerRadiusPixels);
            var    scale = pickerRadiusPixels / 3;
            SKPath path  = new SKPath();

            path.MoveTo(-1 * scale, -1 * scale);
            path.LineTo(0 * scale, -1 * scale);
            path.LineTo(0 * scale, 0 * scale);
            path.LineTo(1 * scale, 0 * scale);
            path.LineTo(1 * scale, 1 * scale);
            path.LineTo(0 * scale, 1 * scale);
            path.LineTo(0 * scale, 0 * scale);
            path.LineTo(-1 * scale, 0 * scale);
            path.LineTo(-1 * scale, -1 * scale);

            SKMatrix matrix = SKMatrix.MakeScale(2 * scale, 2 * scale);
            SKPaint  paint  = new SKPaint
            {
                PathEffect  = SKPathEffect.Create2DPath(matrix, path),
                Color       = Color.LightGray.ToSKColor(),
                IsAntialias = true
            };

            SKRect      patternRect;
            SKRect      clipRect;
            SKRoundRect clipRoundRect;

            if (Vertical)
            {
                patternRect = new SKRect(sliderTop - pickerRadiusPixels, pickerRadiusPixels
                                         , sliderTop + pickerRadiusPixels, canvasSize.Height - pickerRadiusPixels);
                clipRect = new SKRect(sliderTop - (pickerRadiusPixels * 0.65f), pickerRadiusPixels * 1.35f
                                      , sliderTop + (pickerRadiusPixels * 0.65f), canvasSize.Height - (pickerRadiusPixels * 1.35f));
                clipRoundRect = new SKRoundRect(clipRect, pickerRadiusPixels * 0.65f, pickerRadiusPixels * 0.65f);
            }
            else
            {
                patternRect = new SKRect(pickerRadiusPixels, sliderTop - pickerRadiusPixels
                                         , canvasSize.Width - pickerRadiusPixels, sliderTop + pickerRadiusPixels);
                clipRect = new SKRect(pickerRadiusPixels * 1.35f, sliderTop - (pickerRadiusPixels * 0.65f)
                                      , canvasSize.Width - (pickerRadiusPixels * 1.35f), sliderTop + (pickerRadiusPixels * 0.65f));
                clipRoundRect = new SKRoundRect(clipRect, pickerRadiusPixels * 0.65f, pickerRadiusPixels * 0.65f);
            }

            canvas.Save();
            canvas.ClipRoundRect(clipRoundRect);
            canvas.DrawRect(patternRect, paint);
            canvas.Restore();
        }
Пример #4
0
        private void DrawTransparencyBackground(SKCanvas canvas, int width, int height, float scale)
        {
            var blockSize = BaseBlockSize * scale;

            var offsetMatrix = SKMatrix.MakeScale(2 * blockSize, blockSize);
            var skewMatrix   = SKMatrix.MakeSkew(0.5f, 0);

            SKMatrix.PreConcat(ref offsetMatrix, ref skewMatrix);

            using (var path = new SKPath())
                using (var paint = new SKPaint())
                {
                    path.AddRect(SKRect.Create(blockSize / -2, blockSize / -2, blockSize, blockSize));

                    paint.PathEffect = SKPathEffect.Create2DPath(offsetMatrix, path);
                    paint.Color      = AlternatePaneColor;

                    canvas.DrawRect(SKRect.Create(width + blockSize, height + blockSize), paint);
                }
        }
Пример #5
0
        private void OnPaintBackground(object sender, SKPaintSurfaceEventArgs e)
        {
            var scale     = e.Info.Width / (float)((View)sender).Width;
            var blockSize = BaseBlockSize * scale;

            var offsetMatrix = SKMatrix.CreateScale(2 * blockSize, blockSize);
            var skewMatrix   = SKMatrix.CreateSkew(0.5f, 0);
            var matrix       = offsetMatrix.PreConcat(skewMatrix);

            using var path = new SKPath();
            path.AddRect(SKRect.Create(blockSize / -2, blockSize / -2, blockSize, blockSize));

            using var paint = new SKPaint
                  {
                      PathEffect = SKPathEffect.Create2DPath(matrix, path),
                      Color      = 0xFFF0F0F0
                  };

            var canvas = e.Surface.Canvas;
            var area   = SKRect.Create(e.Info.Width + blockSize, e.Info.Height + blockSize);

            canvas.DrawRect(area, paint);
        }
Пример #6
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            using (SKPaint paint = new SKPaint())
            {
                paint.Color = SKColors.Red;

                using (SKPathEffect pathEffect =
                           SKPathEffect.Create2DPath(SKMatrix.MakeScale(64, 64), tilePath))
                {
                    paint.PathEffect = pathEffect;

                    canvas.DrawRoundRect(
                        new SKRect(50, 50, info.Width - 50, info.Height - 50),
                        100, 100, paint);
                }
            }
        }
        public CatsInFramePage()
        {
            Title = "Cats in Frame";

            SKCanvasView canvasView = new SKCanvasView();

            canvasView.PaintSurface += OnCanvasViewPaintSurface;
            Content = canvasView;

            // Move (0, 0) point to center of cat path
            catPath.Transform(SKMatrix.MakeTranslation(-240, -175));

            // Now catPath is 400 by 250
            // Scale it down to 160 by 100
            catPath.Transform(SKMatrix.MakeScale(0.40f, 0.40f));

            // Get the outlines of the contours of the cat path
            SKPath outlinedCatPath = new SKPath();

            catStroke.GetFillPath(catPath, outlinedCatPath);

            // Create a 2D path effect from those outlines
            SKPathEffect fillEffect = SKPathEffect.Create2DPath(
                new SKMatrix {
                ScaleX = 170, ScaleY = 110,
                TransX = 75, TransY = 80,
                Persp2 = 1
            },
                outlinedCatPath);

            // Create a 1D path effect from the scallop path
            SKPathEffect strokeEffect =
                SKPathEffect.Create1DPath(scallopPath, 75, 0, SKPath1DPathEffectStyle.Rotate);

            // Set the sum the effects to frame paint
            framePaint.PathEffect = SKPathEffect.CreateSum(fillEffect, strokeEffect);
        }
Пример #8
0
        public static void Draw(SKCanvas canvas, IReadOnlyViewport viewport, IStyle style, IFeature feature, IGeometry geometry,
                                float opacity, SymbolCache symbolCache = null)
        {
            if (style is LabelStyle labelStyle)
            {
                var worldCenter = geometry.BoundingBox.Centroid;
                var center      = viewport.WorldToScreen(worldCenter);
                LabelRenderer.Draw(canvas, labelStyle, feature, (float)center.X, (float)center.Y, opacity);
            }
            else if (style is StyleCollection styleCollection)
            {
                foreach (var s in styleCollection)
                {
                    Draw(canvas, viewport, s, feature, geometry, opacity, symbolCache);
                }
            }
            else if (style is VectorStyle vectorStyle)
            {
                var polygon = (Polygon)geometry;

                float   lineWidth        = 1;
                var     lineColor        = Color.Black;       // default
                var     fillColor        = Color.Gray;        // default
                var     strokeCap        = PenStrokeCap.Butt; // default
                var     strokeJoin       = StrokeJoin.Miter;  // default
                var     strokeMiterLimit = 4f;                // default
                var     strokeStyle      = PenStyle.Solid;    // default
                float[] dashArray        = null;              // default

                if (vectorStyle.Outline != null)
                {
                    lineWidth        = (float)vectorStyle.Outline.Width;
                    lineColor        = vectorStyle.Outline.Color;
                    strokeCap        = vectorStyle.Outline.PenStrokeCap;
                    strokeJoin       = vectorStyle.Outline.StrokeJoin;
                    strokeMiterLimit = vectorStyle.Outline.StrokeMiterLimit;
                    strokeStyle      = vectorStyle.Outline.PenStyle;
                    dashArray        = vectorStyle.Outline.DashArray;
                }

                if (vectorStyle.Fill != null)
                {
                    fillColor = vectorStyle.Fill?.Color;
                }

                using (var path = polygon.ToSkiaPath(viewport, canvas.LocalClipBounds, lineWidth))
                    using (var paintFill = new SKPaint {
                        IsAntialias = true
                    })
                    {
                        // Is there a FillStyle?
                        if (vectorStyle.Fill?.FillStyle == FillStyle.Solid)
                        {
                            paintFill.StrokeWidth = 0;
                            paintFill.Style       = SKPaintStyle.Fill;
                            paintFill.PathEffect  = null;
                            paintFill.Shader      = null;
                            paintFill.Color       = fillColor.ToSkia(opacity);
                            canvas.DrawPath(path, paintFill);
                        }
                        else
                        {
                            paintFill.StrokeWidth = 1;
                            paintFill.Style       = SKPaintStyle.Stroke;
                            paintFill.Shader      = null;
                            paintFill.Color       = fillColor.ToSkia(opacity);
                            var scale    = 10.0f;
                            var fillPath = new SKPath();
                            var matrix   = SKMatrix.MakeScale(scale, scale);

                            switch (vectorStyle.Fill?.FillStyle)
                            {
                            case FillStyle.Cross:
                                fillPath.MoveTo(scale * 0.8f, scale * 0.8f);
                                fillPath.LineTo(0, 0);
                                fillPath.MoveTo(0, scale * 0.8f);
                                fillPath.LineTo(scale * 0.8f, 0);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.DiagonalCross:
                                fillPath.MoveTo(scale, scale);
                                fillPath.LineTo(0, 0);
                                fillPath.MoveTo(0, scale);
                                fillPath.LineTo(scale, 0);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.BackwardDiagonal:
                                fillPath.MoveTo(0, scale);
                                fillPath.LineTo(scale, 0);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.ForwardDiagonal:
                                fillPath.MoveTo(scale, scale);
                                fillPath.LineTo(0, 0);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.Dotted:
                                paintFill.Style = SKPaintStyle.StrokeAndFill;
                                fillPath.AddCircle(scale * 0.5f, scale * 0.5f, scale * 0.35f);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.Horizontal:
                                fillPath.MoveTo(0, scale * 0.5f);
                                fillPath.LineTo(scale, scale * 0.5f);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.Vertical:
                                fillPath.MoveTo(scale * 0.5f, 0);
                                fillPath.LineTo(scale * 0.5f, scale);
                                paintFill.PathEffect = SKPathEffect.Create2DPath(matrix, fillPath);
                                break;

                            case FillStyle.Bitmap:
                                paintFill.Style = SKPaintStyle.Fill;
                                var image = GetImage(symbolCache, vectorStyle.Fill.BitmapId);
                                if (image != null)
                                {
                                    paintFill.Shader = image.ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat);
                                }
                                break;

                            case FillStyle.BitmapRotated:
                                paintFill.Style = SKPaintStyle.Fill;
                                image           = GetImage(symbolCache, vectorStyle.Fill.BitmapId);
                                if (image != null)
                                {
                                    paintFill.Shader = image.ToShader(SKShaderTileMode.Repeat,
                                                                      SKShaderTileMode.Repeat,
                                                                      SKMatrix.MakeRotation((float)(viewport.Rotation * System.Math.PI / 180.0f), image.Width >> 1, image.Height >> 1));
                                }
                                break;

                            default:
                                paintFill.PathEffect = null;
                                break;
                            }

                            // Do this, because if not, path isn't filled complete
                            using (new SKAutoCanvasRestore(canvas))
                            {
                                canvas.ClipPath(path);
                                var bounds = path.Bounds;
                                // Make sure, that the brush starts with the correct position
                                var inflate = ((int)path.Bounds.Width * 0.3f / scale) * scale;
                                bounds.Inflate(inflate, inflate);
                                // Draw rect with bigger size, which is clipped by path
                                canvas.DrawRect(bounds, paintFill);
                            }
                        }

                        if (vectorStyle.Outline != null)
                        {
                            using (var paintStroke = new SKPaint {
                                IsAntialias = true
                            })
                            {
                                paintStroke.Style       = SKPaintStyle.Stroke;
                                paintStroke.StrokeWidth = lineWidth;
                                paintStroke.Color       = lineColor.ToSkia(opacity);
                                paintStroke.StrokeCap   = strokeCap.ToSkia();
                                paintStroke.StrokeJoin  = strokeJoin.ToSkia();
                                paintStroke.StrokeMiter = strokeMiterLimit;
                                if (strokeStyle != PenStyle.Solid)
                                {
                                    paintStroke.PathEffect = strokeStyle.ToSkia(lineWidth, dashArray);
                                }
                                else
                                {
                                    paintStroke.PathEffect = null;
                                }
                                canvas.DrawPath(path, paintStroke);
                            }
                        }
                    }
            }
        }