private void AddPathSegmentToSkiaPath(SKPath skPath, IPathSegment pathSegment) { if (pathSegment is IBezierSegment bezierSegment) { skPath.CubicTo( (float)bezierSegment.Point1.X, (float)bezierSegment.Point1.Y, (float)bezierSegment.Point2.X, (float)bezierSegment.Point2.Y, (float)bezierSegment.Point3.X, (float)bezierSegment.Point3.Y); } else if (pathSegment is IPolyBezierSegment polyBezierSegment) { List <Point> points = new List <Point>(); foreach (Point point in polyBezierSegment.Points) { points.Add(point); } if (points.Count % 3 != 0) { throw new InvalidOperationException($"IPolyBezerSegment contains {points.Count} points, which isn't a multiple of 3"); } for (int i = 0; i < points.Count;) { var point1 = points[i + 0]; var point2 = points[i + 1]; var point3 = points[i + 2]; skPath.CubicTo( (float)point1.X, (float)point1.Y, (float)point2.X, (float)point2.Y, (float)point3.X, (float)point3.Y); i += 3; } } else if (pathSegment is ILineSegment lineSegment) { skPath.LineTo((float)lineSegment.Point.X, (float)lineSegment.Point.Y); } else if (pathSegment is IQuadraticBezierSegment quadraticBezierSegment) { skPath.QuadTo( (float)quadraticBezierSegment.Point1.X, (float)quadraticBezierSegment.Point1.Y, (float)quadraticBezierSegment.Point2.X, (float)quadraticBezierSegment.Point2.Y); } else if (pathSegment is IPolyLineSegment polyLineSegment) { var skiaPoints = new List <SKPoint>(); AddSkiaPoints(polyLineSegment.Points, skiaPoints); skPath.AddPoly(skiaPoints.ToArray()); } else { throw new InvalidOperationException($"IPathSegment type {pathSegment.GetType()} not yet implemented"); } }
private void SkiaCanvas_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) { // Draw the four corners of the "card outline". SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); SKPaint paint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Stroke, Color = SKColors.Blue, StrokeWidth = 5, StrokeCap = SKStrokeCap.Square, StrokeJoin = SKStrokeJoin.Round }; SKPath path = new SKPath(); const float cornerOffset = 50f; // Top left SKPoint tlBottomLeft = new SKPoint(info.Width * 0.1f, info.Height * 0.1f + cornerOffset); SKPoint tlTopLeft = new SKPoint(info.Width * 0.1f, info.Height * 0.1f); SKPoint tlTopRight = new SKPoint(info.Width * 0.1f + cornerOffset, info.Height * 0.1f); path.MoveTo(tlBottomLeft); path.CubicTo(tlTopLeft, tlTopLeft, tlTopRight); // Top right SKPoint trTopLeft = new SKPoint(info.Width * 0.9f - cornerOffset, info.Height * 0.1f); SKPoint trTopRight = new SKPoint(info.Width * 0.9f, info.Height * 0.1f); SKPoint trBottomRight = new SKPoint(info.Width * 0.9f, info.Height * 0.1f + cornerOffset); path.MoveTo(trTopLeft); path.CubicTo(trTopRight, trTopRight, trBottomRight); // Bottom right SKPoint brTopRight = new SKPoint(info.Width * 0.9f, info.Height * 0.9f - cornerOffset); SKPoint brBottomRight = new SKPoint(info.Width * 0.9f, info.Height * 0.9f); SKPoint brBottomLeft = new SKPoint(info.Width * 0.9f - cornerOffset, info.Height * 0.9f); path.MoveTo(brTopRight); path.CubicTo(brBottomRight, brBottomRight, brBottomLeft); // Bottom left SKPoint blBottomRight = new SKPoint(info.Width * 0.1f + cornerOffset, info.Height * 0.9f); SKPoint blBottomLeft = new SKPoint(info.Width * 0.1f, info.Height * 0.9f); SKPoint blTopLeft = new SKPoint(info.Width * 0.1f, info.Height * 0.9f - cornerOffset); path.MoveTo(blBottomRight); path.CubicTo(blBottomLeft, blBottomLeft, blTopLeft); canvas.DrawPath(path, paint); }
internal static SKPath BuildRoundedRectangleGeometry(Vector2 offset, Vector2 size, Vector2 cornerRadius) { float radiusX = Clamp(cornerRadius.X, 0, size.X * 0.5f); float radiusY = Clamp(cornerRadius.Y, 0, size.Y * 0.5f); float bezierX = (float)((1.0 - CIRCLE_BEZIER_KAPPA) * radiusX); float bezierY = (float)((1.0 - CIRCLE_BEZIER_KAPPA) * radiusY); SKPath path = new SKPath(); var lastPoint = new SKPoint(offset.X + radiusX, offset.Y); path.MoveTo(lastPoint); // Top line path.LineTo(lastPoint + new SKPoint(size.X - 2 * radiusX, 0)); lastPoint += new SKPoint(size.X - 2 * radiusX, 0); // Top-right Arc path.CubicTo( lastPoint + new SKPoint(radiusX - bezierX, 0), // 1st control point lastPoint + new SKPoint(radiusX, bezierY), // 2nd control point lastPoint + new SKPoint(radiusX, radiusY)); // End point lastPoint += new SKPoint(radiusX, radiusY); // Right line path.LineTo(lastPoint + new SKPoint(0, size.Y - 2 * radiusY)); lastPoint += new SKPoint(0, size.Y - 2 * radiusY); // Bottom-right Arc path.CubicTo( lastPoint + new SKPoint(0, bezierY), // 1st control point lastPoint + new SKPoint(-bezierX, radiusY), // 2nd control point lastPoint + new SKPoint(-radiusX, radiusY)); // End point lastPoint += new SKPoint(-radiusX, radiusY); // Bottom line path.LineTo(lastPoint + new SKPoint(-(size.X - 2 * radiusX), 0)); lastPoint = lastPoint + new SKPoint(-(size.X - 2 * radiusX), 0); // Bottom-left Arc path.CubicTo( lastPoint + new SKPoint(-radiusX + bezierX, 0), // 1st control point lastPoint + new SKPoint(-radiusX, -bezierY), // 2nd control point lastPoint + new SKPoint(-radiusX, -radiusY)); // End point lastPoint += new SKPoint(-radiusX, -radiusY); // Left line path.LineTo(lastPoint + new SKPoint(0, -(size.Y - 2 * radiusY))); lastPoint += new SKPoint(0, -(size.Y - 2 * radiusY)); // Top-left Arc path.CubicTo( lastPoint + new SKPoint(0, -radiusY + bezierY), // 1st control point lastPoint + new SKPoint(bezierX, -radiusY), // 2nd control point lastPoint + new SKPoint(radiusX, -radiusY)); // End point path.Close(); return(path); }
private static void AddSegment(this SKPath path, PathSegment segment) { if (segment is LineSegment) { var s = segment as LineSegment; path.LineTo(s.Point.ToSKPoint()); } else if (segment is PolyLineSegment) { var s = segment as PolyLineSegment; path.AddPoly(s.Points.Select(x => x.ToSKPoint()).Reverse().ToArray(), false); } else if (segment is ArcSegment) { var s = segment as ArcSegment; path.AddArc(new Rect(s.Point, s.Size).ToSKRect(), 180 + -s.RotationAngle.ToFloat(), -180); } else if (segment is BezierSegment) { var s = segment as BezierSegment; path.CubicTo(s.Point1.ToSKPoint(), s.Point2.ToSKPoint(), s.Point3.ToSKPoint()); } else if (segment is PolyBezierSegment) { var s = segment as PolyBezierSegment; if (s.Points.Count % 3 != 0) { throw new ArgumentException("Number of PolyBezierSegment points must be divisable by 3."); } for (int i = 0; i < s.Points.Count; i += 3) { var p1 = s.Points[i]; var p2 = s.Points[i + 1]; var p3 = s.Points[i + 2]; path.CubicTo(p1.ToSKPoint(), p2.ToSKPoint(), p3.ToSKPoint()); } } else if (segment is PolyQuadraticBezierSegment) { var s = segment as PolyQuadraticBezierSegment; for (int i = 0; i < s.Points.Count; i += 2) { var p1 = s.Points[i]; var p2 = s.Points[i + 1]; path.CubicTo(p1.ToSKPoint(), p2.ToSKPoint(), p2.ToSKPoint()); } } }
public void TightBoundsForEnclosedPathIsNotZero() { var path = new SKPath(); path.MoveTo(10, 20); path.CubicTo(10, 20, 30, 40, 30, 40); path.CubicTo(50, 60, 30, 40, 30, 40); var bounds = path.TightBounds; Assert.AreEqual(SKRect.Create(10, 20, 20, 20), bounds); }
public InfinityColorsPage() { Title = "Infinity Colors"; // Create path for infinity sign infinityPath = new SKPath(); infinityPath.MoveTo(0, 0); // Center infinityPath.CubicTo(50, -50, 95, -100, 150, -100); // To top of right loop infinityPath.CubicTo(205, -100, 250, -55, 250, 0); // To far right of right loop infinityPath.CubicTo(250, 55, 205, 100, 150, 100); // To bottom of right loop infinityPath.CubicTo(95, 100, 50, 50, 0, 0); // Back to center infinityPath.CubicTo(-50, -50, -95, -100, -150, -100); // To top of left loop infinityPath.CubicTo(-205, -100, -250, -55, -250, 0); // To far left of left loop infinityPath.CubicTo(-250, 55, -205, 100, -150, 100); // To bottom of left loop infinityPath.CubicTo(-95, 100, -50, 50, 0, 0); // Back to center infinityPath.Close(); // Calculate path information pathBounds = infinityPath.Bounds; gradientCycleLength = pathBounds.Width + pathBounds.Height * pathBounds.Height / pathBounds.Width; // Create SKColor array for gradient for (int i = 0; i < colors.Length; i++) { colors[i] = SKColor.FromHsl(i * 360f / (colors.Length - 1), 100, 50); } canvasView = new SKCanvasView(); canvasView.PaintSurface += OnCanvasViewPaintSurface; Content = canvasView; }
private void DrawOrganicInside(SKRect rect, SKCanvas canvas) { var quarterWidth = rect.Width / 4f; var cMultiplier = new SKPoint(quarterWidth * 0.5f, 0); var p0 = new SKPoint(rect.Left, rect.MidY); var p1 = new SKPoint(rect.Left + quarterWidth, rect.Top) + DeltaPoint(0); var p2 = new SKPoint(rect.Right - quarterWidth, rect.Top) + DeltaPoint(.5f); var p3 = new SKPoint(rect.Right, rect.MidY) + DeltaPoint(.9f); var p4 = new SKPoint(rect.Right - quarterWidth, rect.Bottom) + DeltaPoint(.65f); var p5 = new SKPoint(rect.Left + quarterWidth, rect.Bottom) + DeltaPoint(.4f); var qp1 = new SKPoint(rect.Left, rect.Top); var qp2 = new SKPoint(rect.Right, rect.Top); var qp3 = new SKPoint(rect.Right, rect.Bottom); var qp4 = new SKPoint(rect.Left, rect.Bottom); var m1 = new SKPoint(rect.MidX, rect.Top) + Multiply(DeltaPoint(.25f), new SKPoint(-1.5f, 2.4f)); var m2 = new SKPoint(rect.MidX, rect.Bottom) + Multiply(DeltaPoint(.65f), new SKPoint(0.5f, 1.8f)); var cp1 = p1 + cMultiplier; var cm1 = m1 - cMultiplier; var cp2 = p2 - cMultiplier; var cm2 = m1 + cMultiplier; var cp3 = p4 - cMultiplier; var cm3 = m2 + cMultiplier; var cp4 = p5 + cMultiplier; var cm4 = m2 - cMultiplier; // Draw path with quadratic Bezier using (var path = new SKPath()) { path.MoveTo(p0); path.QuadTo(qp1, p1); path.CubicTo(cp1, cm1, m1); path.CubicTo(cm2, cp2, p2); path.QuadTo(qp2, p3); path.QuadTo(qp3, p4); path.CubicTo(cp3, cm3, m2); path.CubicTo(cm4, cp4, p5); path.QuadTo(qp4, p0); canvas.DrawPath(path, _backgroundPaint); } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); // Draw path with cubic Bezier curve using (SKPath path = new SKPath()) { path.MoveTo(touchPoints[0].Center); path.CubicTo(touchPoints[1].Center, touchPoints[2].Center, touchPoints[3].Center); canvas.DrawPath(path, strokePaint); } // Draw tangent lines canvas.DrawLine(touchPoints[0].Center.X, touchPoints[0].Center.Y, touchPoints[1].Center.X, touchPoints[1].Center.Y, dottedStrokePaint); canvas.DrawLine(touchPoints[2].Center.X, touchPoints[2].Center.Y, touchPoints[3].Center.X, touchPoints[3].Center.Y, dottedStrokePaint); foreach (TouchPoint touchPoint in touchPoints) { touchPoint.Paint(canvas); } }
public ClockPage() { InitializeComponent(); catEar.MoveTo(0, 0); catEar.LineTo(0, 75); catEar.LineTo(100, 75); catEar.Close(); catEye.MoveTo(0, 0); catEye.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 50, 0); catEye.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 0, 0); catEye.Close(); catPupil.MoveTo(25, -5); catPupil.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, 5); catPupil.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, -5); catPupil.Close(); catTail.MoveTo(0, 100); catTail.CubicTo(50, 200, 0, 250, -50, 200); Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () => { canvasView.InvalidateSurface(); return(true); }); }
internal static void GetPathFromData(ShapeData shapeData, SKPath outPath) { outPath.Reset(); var initialPoint = shapeData.InitialPoint; outPath.MoveTo(initialPoint.X, initialPoint.Y); var currentPoint = initialPoint; for (var i = 0; i < shapeData.Curves.Count; i++) { var curveData = shapeData.Curves[i]; var cp1 = curveData.ControlPoint1; var cp2 = curveData.ControlPoint2; var vertex = curveData.Vertex; if (cp1.Equals(currentPoint) && cp2.Equals(vertex)) { outPath.LineTo(vertex.X, vertex.Y); } else { outPath.CubicTo(cp1.X, cp1.Y, cp2.X, cp2.Y, vertex.X, vertex.Y); } currentPoint.X = vertex.X; currentPoint.Y = vertex.Y; } if (shapeData.Closed) { outPath.Close(); } }
public SKPath CreateWaveMaskPath(float width, float height, float controlOffset, float transX, float transY, int waveCount = 1) { var min = Math.Min(width, height); var startX = (width - min) / 2; SKPath sinePath = new SKPath(); sinePath.MoveTo(new SKPoint(startX + transX, transY)); var waveWidth = min / waveCount; for (int i = 0; i < waveCount * 2; i++) { sinePath.CubicTo(new SKPoint(startX + transX + waveWidth / 2 + waveWidth * i, transY - controlOffset), new SKPoint(startX + transX + waveWidth / 2 + waveWidth * i, transY + controlOffset), new SKPoint(startX + transX + waveWidth + waveWidth * i, transY)); } //sinePath.CubicTo(new SKPoint(startX + transX + min / 2, transY - controlOffset), new SKPoint(startX + transX + min / 2, transY + controlOffset), new SKPoint(startX + transX + min, transY)); //sinePath.CubicTo(new SKPoint(startX + transX + min / 2 + min, transY - controlOffset), new SKPoint(startX + transX + min / 2 + min, transY + controlOffset), new SKPoint(startX + transX + min * 2, transY)); sinePath.LineTo(new SKPoint(startX + transX + min * 2, height + transY)); sinePath.LineTo(new SKPoint(startX + transX, height + transY)); sinePath.LineTo(new SKPoint(startX + transX, transY)); sinePath.Close(); return(sinePath); }
private void DrawPath(SKCanvas canvas, SKPaint paint, SKPoint[] points) { var path = new SKPath(); path.MoveTo(points.First()); var last = LineMode == LineMode.Straight ? points.Length : points.Length - 1; for (int i = 0; i < last; i++) { if (LineMode == LineMode.Spline) { var point = points[i]; var nextPoint = points[i + 1]; var offsetPoint = new SKPoint((nextPoint.X - point.X) * 0.5f, 0); var currentPoint = point + offsetPoint; var next = nextPoint - offsetPoint; path.CubicTo(currentPoint, next, nextPoint); } else { path.LineTo(points[i]); } } canvas.DrawPath(path, paint); }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); using (SKPath path = new SKPath()) { path.MoveTo(new SKPoint(0, 0)); path.CubicTo(new SKPoint(2 * info.Width, info.Height), new SKPoint(-info.Width, info.Height), new SKPoint(info.Width, 0)); switch ((string)effectStylePicker.SelectedItem) { case "Translate": pathPaint.PathEffect = translatePathEffect; break; case "Rotate": pathPaint.PathEffect = rotatePathEffect; break; case "Morph": pathPaint.PathEffect = morphPathEffect; break; } canvas.DrawPath(path, pathPaint); } }
public MainPage() { InitializeComponent(); // Make cat ear path catEarPath.MoveTo(7, -10); catEarPath.LineTo(0, 75); catEarPath.LineTo(80, 75); catEarPath.Close(); // Make Cat eye path catEyePath.MoveTo(0, 0); catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 50, 0); catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 0, 0); catEyePath.Close(); // Make eye pupil path catPupilPath.MoveTo(25, -5); catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, 5); catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, -5); catPupilPath.Close(); // Make cat tail path catTailPath.MoveTo(0, 100); catTailPath.CubicTo(50, 200, 0, 250, -50, 200); Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () => { canvasView.InvalidateSurface(); return(true); }); }
public void Draw(SKCanvas canvas, SKPaint paint, int posH, int PosV) { foreach (IFragments fragment in items) { IShape item = fragment.GetFragment(); item.UpdatePoints(posH, PosV); int type = item.GetType(); if (type == 1) { Bezier bezier = item as Bezier; SKPath path = new SKPath(); path.MoveTo(bezier.x); path.CubicTo(bezier.x, bezier.y, bezier.z); canvas.DrawPath(path, paint); } else if (type == 0) { Line line = item as Line; canvas.DrawLine(line.x, line.y, paint); } else { Oval oval = item as Oval; canvas.DrawOval(oval.x.X, oval.x.Y, oval.rx, oval.ry, paint); } } }
protected override void OnPaintSurface(SKPaintGLSurfaceEventArgs e) { base.OnPaintSurface(e); var paint = new SKPaint { Color = Color2.ToSKColor(), Style = SKPaintStyle.Fill, IsAntialias = true }; var bounds = e.Surface.Canvas.LocalClipBounds; var canvas = e.Surface.Canvas; canvas.Clear(Color1.ToSKColor()); var point1 = new SKPoint(0, bounds.Height); var point2 = new SKPoint(bounds.Width / 2, bounds.Height); var point3 = new SKPoint(bounds.Width / 2, 0); var point4 = new SKPoint(bounds.Width, 0); var point5 = new SKPoint(bounds.Width, bounds.Height); using (var path = new SKPath()) { path.MoveTo(point1); path.CubicTo(point2, point3, point4); path.LineTo(point5); path.Close(); canvas.DrawPath(path, paint); } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); // Draw path with cubic Bezier curve using (SKPath path = new SKPath()) { path.MoveTo(touchPoints[0].Center); path.CubicTo(touchPoints[1].Center, touchPoints[2].Center, touchPoints[3].Center); canvas.DrawPath(path, strokePaint); // Get path length SKPathMeasure pathMeasure = new SKPathMeasure(path, false, 1); // Find new text size textPaint.TextSize = pathMeasure.Length / baseTextWidth * 10; // Draw text on path canvas.DrawTextOnPath(text, path, 0, 0, textPaint); } foreach (TouchPoint touchPoint in touchPoints) { touchPoint.Paint(canvas); } }
/********************************************************************** *********************************************************************/ void DrawConnectionsNetwork3() { bool[,] visitedEgdes = DijkstraAlgorithm.GetVisistedEdges(); DrawConnection(routerX, routerU, visitedEgdes[currentStep, 1]); DrawConnection(routerX, routerW, visitedEgdes[currentStep, 11]); DrawConnection(routerX, routerY, visitedEgdes[currentStep, 12]); DrawConnection(routerU, routerV, visitedEgdes[currentStep, 0]); DrawConnection(routerV, routerX, visitedEgdes[currentStep, 8]); DrawConnection(routerV, routerW, visitedEgdes[currentStep, 10]); //DrawConnections(canvas, routerV, routerY); DrawConnection(routerW, routerY, visitedEgdes[currentStep, 13]); DrawConnection(routerW, routerZ, visitedEgdes[currentStep, 4]); DrawConnection(routerZ, routerY, visitedEgdes[currentStep, 5]); SKPoint p = new SKPoint(xPercent(0.15f), yPercent(0.6f)); SKPath curveUY = new SKPath(); curveUY.MoveTo(routerU); curveUY.CubicTo(routerU, p, routerY); if (visitedEgdes[currentStep, 3]) { canvas.DrawPath(curveUY, sk_Visited); } else { if (currentStep != maxStep) { canvas.DrawPath(curveUY, sk_RouterContour); } } }
private void SKCanvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) { SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); int cornerRadius = info.Width / 14; using (SKPath path = new SKPath()) { path.MoveTo(0, 0); path.LineTo(info.Width - cornerRadius, 0); path.CubicTo(info.Width, 0, info.Width, info.Height, info.Width - cornerRadius, info.Height); path.LineTo(cornerRadius, info.Height); path.QuadTo(0, info.Height, 0, info.Height - cornerRadius); path.LineTo(0, 0); SKPaint paint = new SKPaint { Style = SKPaintStyle.StrokeAndFill, Color = ((Color)App.Current.Resources["secondMessage"]).ToSKColor(), StrokeWidth = 1, IsAntialias = true }; canvas.DrawPath(path, paint); } }
public void DrawDroplet(SKCanvas canvas, string label, bool biggie, SKPoint center, float padRadius, float radius, float textStrokeWidth, SKPaint fillPaint, float strokeWidth, float accentStrokeWidth, Swipe swipe) { float ratio = radius / padRadius; //strokeWidth = strokeWidth * ratio; textStrokeWidth = textStrokeWidth * ratio; accentStrokeWidth = accentStrokeWidth * ratio; float pull = 0.75f; float accentRadius = radius - (strokeWidth + accentStrokeWidth) / 2.0f; var path = new SKPath(); path.MoveTo(swipe % new SKPoint(center.X, center.Y - radius)); path.CubicTo(swipe % new SKPoint(center.X + pull * radius, center.Y - radius), swipe % new SKPoint(center.X + radius, center.Y - pull * radius), swipe % new SKPoint(center.X + radius, center.Y)); path.CubicTo(swipe % new SKPoint(center.X + radius, center.Y + pull * radius), swipe % new SKPoint(center.X + pull * radius, center.Y + radius), swipe % new SKPoint(center.X, center.Y + radius)); path.CubicTo(swipe % new SKPoint(center.X - pull * radius, center.Y + radius), swipe % new SKPoint(center.X - radius, center.Y + pull * radius), swipe % new SKPoint(center.X - radius, center.Y)); path.CubicTo(swipe % new SKPoint(center.X - radius, center.Y - pull * radius), swipe % new SKPoint(center.X - pull * radius, center.Y - radius), swipe % new SKPoint(center.X, center.Y - radius)); path.Close(); var darkPath = new SKPath(); darkPath.MoveTo(swipe % new SKPoint(center.X + accentRadius, center.Y)); darkPath.CubicTo(swipe % new SKPoint(center.X + accentRadius, center.Y + pull * accentRadius), swipe % new SKPoint(center.X + pull * accentRadius, center.Y + accentRadius), swipe % new SKPoint(center.X, center.Y + accentRadius)); var lightPath = new SKPath(); lightPath.MoveTo(swipe % new SKPoint(center.X - accentRadius, center.Y)); lightPath.CubicTo(swipe % new SKPoint(center.X - accentRadius, center.Y - pull * accentRadius), swipe % new SKPoint(center.X - pull * accentRadius, center.Y - accentRadius), swipe % new SKPoint(center.X, center.Y - accentRadius)); using (var strokePaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = new SKColor(0, 0, 0, 191), StrokeWidth = swipe % strokeWidth, IsAntialias = true }) using (var accentLightPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = new SKColor(255, 255, 255, 191), StrokeWidth = swipe % accentStrokeWidth, StrokeCap = SKStrokeCap.Round, IsAntialias = true }) using (var accentDarkPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = new SKColor(0, 0, 0, 95), StrokeWidth = swipe % accentStrokeWidth, StrokeCap = SKStrokeCap.Round, IsAntialias = true }) { canvas.DrawPath(path, fillPaint); canvas.DrawPath(darkPath, accentDarkPaint); canvas.DrawPath(lightPath, accentLightPaint); canvas.DrawPath(path, strokePaint); } DrawDropletLabel(canvas, label, center, radius, textStrokeWidth, biggie, swipe); }
internal static void AddCubic(IToolContext context, CubicBezierShape cubicBezier, double dx, double dy, SKPath geometry) { geometry.MoveTo(ToSKPoint(cubicBezier.StartPoint, dx, dy)); geometry.CubicTo( ToSKPoint(cubicBezier.Point1, dx, dy), ToSKPoint(cubicBezier.Point2, dx, dy), ToSKPoint(cubicBezier.Point3, dx, dy)); }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); // Translate to center canvas.Translate(info.Width / 2, info.Height / 2); // Draw the circle float radius = Math.Min(info.Width, info.Height) / 3; canvas.DrawCircle(0, 0, radius, blackStroke); // Get the value of the Slider float angle = (float)angleSlider.Value; // Calculate length of control point line float length = radius * 4 * (float)Math.Tan(Math.PI * angle / 180 / 4) / 3; // Calculate sin and cosine for half that angle float sin = (float)Math.Sin(Math.PI * angle / 180 / 2); float cos = (float)Math.Cos(Math.PI * angle / 180 / 2); // Find the end points SKPoint point0 = new SKPoint(-radius * sin, radius * cos); SKPoint point3 = new SKPoint(radius * sin, radius * cos); // Find the control points SKPoint point0Normalized = Normalize(point0); SKPoint point1 = point0 + new SKPoint(length * point0Normalized.Y, -length * point0Normalized.X); SKPoint point3Normalized = Normalize(point3); SKPoint point2 = point3 + new SKPoint(-length * point3Normalized.Y, length * point3Normalized.X); // Draw the points canvas.DrawCircle(point0.X, point0.Y, 10, blackFill); canvas.DrawCircle(point1.X, point1.Y, 10, blackFill); canvas.DrawCircle(point2.X, point2.Y, 10, blackFill); canvas.DrawCircle(point3.X, point3.Y, 10, blackFill); // Draw the tangent lines canvas.DrawLine(point0.X, point0.Y, point1.X, point1.Y, dottedStroke); canvas.DrawLine(point3.X, point3.Y, point2.X, point2.Y, dottedStroke); // Draw the Bezier curve using (SKPath path = new SKPath()) { path.MoveTo(point0); path.CubicTo(point1, point2, point3); canvas.DrawPath(path, redStroke); } }
internal static SKPath BuildEllipseGeometry(Vector2 center, Vector2 radius) { SKRect rect = SKRect.Create(center.X - radius.X, center.Y - radius.Y, radius.X * 2, radius.Y * 2); float bezierX = (float)((1.0 - CIRCLE_BEZIER_KAPPA) * radius.X); float bezierY = (float)((1.0 - CIRCLE_BEZIER_KAPPA) * radius.Y); // IMPORTANT: // - The order of following operations is important for dashed strokes. // - Stroke might get merged in the end. // - WPF starts with bottom right ellipse arc. // - TODO: Verify UWP behavior SKPath path = new SKPath(); path.MoveTo(new SKPoint(rect.Right, rect.Top + radius.Y)); // Bottom-right Arc path.CubicTo( new SKPoint(rect.Right, rect.Bottom - bezierY), // 1st control point new SKPoint(rect.Right - bezierX, rect.Bottom), // 2nd control point new SKPoint(rect.Right - radius.X, rect.Bottom)); // End point // Bottom-left Arc path.CubicTo( new SKPoint(rect.Left + bezierX, rect.Bottom), // 1st control point new SKPoint(rect.Left, rect.Bottom - bezierY), // 2nd control point new SKPoint(rect.Left, rect.Bottom - radius.Y)); // End point // Top-left Arc path.CubicTo( new SKPoint(rect.Left, rect.Top + bezierY), // 1st control point new SKPoint(rect.Left + bezierX, rect.Top), // 2nd control point new SKPoint(rect.Left + radius.X, rect.Top)); // End point // Top-right Arc path.CubicTo( new SKPoint(rect.Right - bezierX, rect.Top), // 1st control point new SKPoint(rect.Right, rect.Top + bezierY), // 2nd control point new SKPoint(rect.Right, rect.Top + radius.Y)); // End point path.Close(); return(path); }
private void SkCanvas_OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e) { SKImageInfo info = new SKImageInfo(e.BackendRenderTarget.Width, e.BackendRenderTarget.Height, e.ColorType); SKSurface surface = e.Surface; SKCanvas surfaceCanvas = surface.Canvas; surfaceCanvas.Clear(); surfaceCanvas.SetMatrix(_canvasMatrix); if (_picture == null) { using (SKPictureRecorder pictureRecorder = new SKPictureRecorder()) using (SKCanvas canvas = pictureRecorder.BeginRecording(info.Rect)) using (SKPath path = new SKPath()) { /* * Draw path with multiple segments(>=3). * Then every time you translate the surfaceCanvas, it consumes more GPU memory. */ path.CubicTo(150, 50, 200, 125, 300, 25); // path.LineTo(80, 125); // path.LineTo(130, 75); // path.LineTo(200, 205); canvas.DrawPath(path, _paint); // Fine with DrawRoundRect or DrawText // canvas.DrawText("TEXT", new SKPoint(50, 50), _paint); // canvas.DrawRoundRect(120,120,300,220,12,12,paint); _picture = pictureRecorder.EndRecording(); } } surfaceCanvas.DrawPicture(_picture); /* * Directly drawing on surfaceCanvas is fine. */ // using (SKPath path = new SKPath()) // { // path.CubicTo(150, 50, 200, 125, 300, 25); // // path.LineTo(80, 125); // path.LineTo(130, 75); // path.LineTo(200, 205); // // surfaceCanvas.DrawPath(path, _paint); // } }
public void TightBoundsForEnclosedPathIsNotZero() { const int Precision = 3; var rect = SKRect.Create(10, 20, 28.889f, 28.889f); var path = new SKPath(); path.MoveTo(10, 20); path.CubicTo(10, 20, 30, 40, 30, 40); path.CubicTo(50, 60, 30, 40, 30, 40); var bounds = path.ComputeTightBounds(); Assert.Equal(rect.Left, bounds.Left); Assert.Equal(rect.Top, bounds.Top); Assert.Equal(rect.Right, bounds.Right, Precision); Assert.Equal(rect.Bottom, bounds.Bottom, Precision); }
public MainPage() { InitializeComponent(); // Make cat ear path catEarPath.MoveTo(0, 0); catEarPath.LineTo(0, 75); catEarPath.LineTo(100, 75); catEarPath.Close(); // Make cat eye path catEyePath.MoveTo(0, 0); catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 50, 0); catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 0, 0); catEyePath.Close(); // Make eye pupil path catPupilPath.MoveTo(25, -5); catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, 5); catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, -5); catPupilPath.Close(); // Make cat tail path catTailPath.MoveTo(0, 100); catTailPath.CubicTo(50, 200, 0, 250, -50, 200); // Create Shader Assembly assembly = GetType().GetTypeInfo().Assembly; using (Stream stream = assembly.GetManifestResourceStream("SkiaWithXamarin.WoodGrain.png")) using (SKManagedStream skStream = new SKManagedStream(stream)) using (SKBitmap bitmap = SKBitmap.Decode(skStream)) using (SKShader shader = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Mirror, SKShaderTileMode.Mirror)) { backgroundFillPaint.Shader = shader; } Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () => { canvasView.InvalidateSurface(); return(true); }); }
public void TightBoundsForEnclosedPathIsNotZero() { var delta = 0.001f; var rect = SKRect.Create(10, 20, 28.889f, 28.889f); var path = new SKPath(); path.MoveTo(10, 20); path.CubicTo(10, 20, 30, 40, 30, 40); path.CubicTo(50, 60, 30, 40, 30, 40); var bounds = path.ComputeTightBounds(); Assert.AreEqual(rect.Left, bounds.Left, "Left"); Assert.AreEqual(rect.Top, bounds.Top, "Top"); Assert.AreEqual(rect.Right, bounds.Right, delta, "Right"); Assert.AreEqual(rect.Bottom, bounds.Bottom, delta, "Bottom"); }
public void bezier(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { // Draw path with cubic Bezier curve. using (SKPath path = new SKPath()) { path.MoveTo((float)x1, (float)y1); path.CubicTo((float)x2, (float)y2, (float)x3, (float)y3, (float)x4, (float)y4); _canvas.DrawPath(path, _pen); } }
private static void DrawCard(float xOffset, float yOffset, SKCanvas canvas) { SKPaint paintCard = new SKPaint { Style = SKPaintStyle.Stroke, Color = Color.Black.ToSKColor(), StrokeWidth = 2, }; var path = new SKPath(); var cardWith = 120.0f; var cardHeight = 180.0f; path.MoveTo(0.0f, 0.0f); path.LineTo(0.0f, 1.0f); path.LineTo(0.7f, 1.0f); path.LineTo(0.7f, 0.0f); path.Close(); path.Transform(SKMatrix.MakeRotation((float)(45.0f * Math.PI / 180), 0.7f, 1.0f)); path.Transform(SKMatrix.MakeScale(cardWith, cardHeight)); path.Offset(xOffset, yOffset); canvas.DrawPath(path, paintCard); var pathHeart = new SKPath(); pathHeart.MoveTo(0.5f, 1.0f); pathHeart.LineTo(0.0f, 0.4f); pathHeart.CubicTo(0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.4f); pathHeart.CubicTo(0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.4f); pathHeart.LineTo(0.5f, 1.0f); pathHeart.Close(); pathHeart.Transform(SKMatrix.MakeScale(cardWith, cardHeight)); pathHeart.Offset(xOffset, yOffset); canvas.DrawPath(pathHeart, paintCard); }
/// <inheritdoc /> public override void CubicCurveTo(double x1, double y1, double x2, double y2, double x, double y) { x1 *= _coordinateScale; y1 *= -_coordinateScale; x2 *= _coordinateScale; y2 *= -_coordinateScale; x *= _coordinateScale; y *= -_coordinateScale; _path.CubicTo((float)x1, (float)y1, (float)x2, (float)y2, (float)x, (float)y); }
public void PathPointsAreCorrect() { using (var path = new SKPath ()) { // set up the xamagon path.MoveTo (71.4311121f, 56f); path.CubicTo (68.6763107f, 56.0058575f, 65.9796704f, 57.5737917f, 64.5928855f, 59.965729f); path.LineTo (43.0238921f, 97.5342563f); path.CubicTo (41.6587026f, 99.9325978f, 41.6587026f, 103.067402f, 43.0238921f, 105.465744f); path.LineTo (64.5928855f, 143.034271f); path.CubicTo (65.9798162f, 145.426228f, 68.6763107f, 146.994582f, 71.4311121f, 147f); path.LineTo (114.568946f, 147f); path.CubicTo (117.323748f, 146.994143f, 120.020241f, 145.426228f, 121.407172f, 143.034271f); path.LineTo (142.976161f, 105.465744f); path.CubicTo (144.34135f, 103.067402f, 144.341209f, 99.9325978f, 142.976161f, 97.5342563f); path.LineTo (121.407172f, 59.965729f); path.CubicTo (120.020241f, 57.5737917f, 117.323748f, 56.0054182f, 114.568946f, 56f); path.LineTo (71.4311121f, 56f); path.Close (); // the right number/count Assert.AreEqual (25, path.PointCount); Assert.AreEqual (25, path.Points.Length); // the right value Assert.AreEqual (new SKPoint (68.6763107f, 56.0058575f), path.GetPoint (1)); Assert.AreEqual (new SKPoint (68.6763107f, 56.0058575f), path.Points [1]); } }