private void OnPaintCanvas(object sender, SKPaintSurfaceEventArgs e) { SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; int surfaceWidth = info.Width; int surfaceHeight = info.Height; SKPoint surfaceSize = new SKPoint(surfaceWidth, surfaceHeight); //絵画開始 //背景色を塗る canvas.Clear(PaintColors.DefaultBackground); SKBitmap bitmap = PaintPatterns.CheckeredPattern(PaintColors.Black); //pathList絵画 foreach (var aPath in pathList) { if (!aPath.IsVisibility) { continue; } if (aPath.Points.Count >= 2) { canvas.DrawPath(aPath.ToSKPath(), aPath.FillPaint); canvas.DrawPath(aPath.ToSKPath(), aPath.StrokePaint); } else if (aPath.Points.Count == 1) { canvas.DrawPoint(aPath.GetFirst(), aPath.StrokePaint); } } //最前面 //Lasso色で暫定Pathを絵画 if (!inProgressPath.IsEmpty()) { if (inProgressPath.Points.Count >= 2) { canvas.DrawPath(inProgressPath.ToSKPath(), inProgressPath.FillPaint); canvas.DrawPath(inProgressPath.ToSKPath(), inProgressPath.StrokePaint); } else if (inProgressPath.Points.Count == 1) { canvas.DrawPoint(inProgressPath.GetFirst(), inProgressPath.StrokePaint); } } }
public void PaintSurface(SkiaManager skiaManager, SKCanvas canvas) { foreach (var skiaObject in SketchEngine.SketchObjectList) { if (skiaObject is IRectTransformable) { var rect = skiaManager.SketchSpaceToCanvasSpaceMatrix.MapRect((skiaObject as IRectTransformable).RectTransform.Rect.ToSKRect()); canvas.DrawRect(rect, FramePaint); canvas.DrawPoint(skiaManager.SketchSpaceToCanvasSpaceMatrix.MapPoint((skiaObject as IRectTransformable).RectTransform.Rect.Min.ToSKPoint()), MinPointPaint); canvas.DrawPoint(skiaManager.SketchSpaceToCanvasSpaceMatrix.MapPoint((skiaObject as IRectTransformable).RectTransform.Rect.Max.ToSKPoint()), MaxPointPaint); } } }
// // Takes a list of points and draws lines between them // private void drawRoute(List <MapPoint> points, SKCanvas canvas) { for (int i = 0; i < points.Count - 1; i++) // count produces higher value than max index { if (i == 0) { canvas.DrawPoint(points[i].getPointLocation(), greenStroke); } else if (i < points.Count - 1) { canvas.DrawPoint(points[i].getPointLocation(), blackStroke); // not super necessary, illustrating where points were found } canvas.DrawLine(points[i].getPointLocation(), points[i + 1].getPointLocation(), routeColour); } }
/// handles / calls all the drawing /// if you need to refresh / reset, call invalidate in /// NavigationPageDetail() private void canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) { SKSurface surface = e.Surface; // screen SKCanvas canvas = surface.Canvas; // drawable screen int width = e.Info.Width; // screen dimensions int height = e.Info.Height; canvas.Scale(1, 1); setFloorPlan(width, height); canvas.DrawBitmap(image, new SKRect(0, 0, width, height)); canvas.Save(); // unnecessary at this moment, but leave in // Calls routing if (currentLocation != null) { LoadPoints pointLoader = new LoadPoints(); List <MapPoint> points = pointLoader.loadPoints(width, height); points = calculateRoute(points); drawRoute(points, canvas); canvas.DrawPoint(points[points.Count - 1].pointLocation, redStroke); } }
public void Render(SKCanvas canvas, IGeometryVisual geometry, double scale) { var moist = geometry as IMoist; if (moist == null || !moist.IsValid) { return; } using (var paint = new SKPaint()) { // Setup common paint parameters paint.Color = moist.ToolSettings.SelectedColor.ToSkiaColor(); paint.IsAntialias = true; paint.IsStroke = false; var text = moist.ToolSettings.SelectedText; if (string.IsNullOrEmpty(text)) { paint.StrokeWidth = (float)(moist.Size * scale); paint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint((float)(moist.Point.X * scale), (float)(moist.Point.Y * scale), paint); } else { paint.TextSize = (float)(moist.Size * scale); // Get the path of the text to calibrate the touch point to the middle of the text area var originalTextPath = paint.GetTextPath(text, (float)(moist.Point.X * scale), (float)(moist.Point.Y * scale)); // Set the mid of the text area to the touch point var calibratedX = originalTextPath.Bounds.Left - originalTextPath.Bounds.Width / 2; var calibratedY = originalTextPath.Bounds.Bottom + originalTextPath.Bounds.Height / 2; // Set the right text color var textColor = Converter.ContrastColor(moist.ToolSettings.SelectedColor); // Get the path of the calibrated text area and add some padding to the rect var calibratedTextPath = paint.GetTextPath(text, calibratedX, calibratedY); // Rect parameters float left; float top; float right; float bottom; float cornerRadius; var textSizeDependentPadding = paint.TextSize / 4; left = calibratedTextPath.Bounds.Left - textSizeDependentPadding; top = calibratedTextPath.Bounds.Top - textSizeDependentPadding; right = calibratedTextPath.Bounds.Right + textSizeDependentPadding; bottom = calibratedTextPath.Bounds.Bottom + textSizeDependentPadding; cornerRadius = paint.TextSize / 2; var rect = new SKRect(left, top, right, bottom); // Draw background rect canvas.DrawRoundRect(rect, cornerRadius, cornerRadius, paint); // Draw text paint.Color = textColor.ToSkiaColor(); canvas.DrawText(text, calibratedX, calibratedY, paint); // Draw the background border in same color as text paint.IsStroke = true; canvas.DrawRoundRect(rect, cornerRadius, cornerRadius, paint); } } }
/// <summary>Draws the test layout area</summary> /// <param name="info"></param> /// <param name="canvas"></param> private void DrawLayout(SKImageInfo info, SKCanvas canvas) { SKPaint debugPaint = new SKPaint() { Color = _test, StrokeWidth = 1, Style = SKPaintStyle.Stroke }; // draw origin and control area frame canvas.DrawPoint(this.Origin, new SKPaint() { Color = _test, StrokeWidth = 20, Style = SKPaintStyle.Stroke, StrokeCap = SKStrokeCap.Round }); canvas.DrawRect(this.OuterRect, debugPaint); canvas.DrawRect(this.InscribedRect, debugPaint); // frame the entire canvas canvas.DrawRect(0, 0, info.Width - 1, info.Height - 1, new SKPaint() { Color = _test, StrokeWidth = 1, Style = SKPaintStyle.Stroke }); // draw cross hairs canvas.DrawLine(this.Origin.X, 0, this.Origin.X, info.Height, debugPaint); canvas.DrawLine(0, this.Origin.Y, info.Width, this.Origin.Y, debugPaint); }
/// <inheritdoc /> protected override void DrawBar(ChartSerie serie, SKCanvas canvas, float headerHeight, float itemX, SKSize itemSize, SKSize barSize, float origin, float barX, float barY, SKColor color) { if (PointMode != PointMode.None) { var point = new SKPoint(barX - (itemSize.Width / 2) + (barSize.Width / 2), barY); canvas.DrawPoint(point, color, PointSize, PointMode); } }
public override void Draw(SKCanvas canvas) { foreach (var point in Points) { canvas.DrawPoint(point, Paint); } //canvas.DrawPath(Path, Paint); }
public void DrawPoint(float x, float y, SKPaint paint) { canvas.DrawPoint(x, y, paint); if (calculateBounds) { displayObject.addBoundingPoint(x, y); } }
//public static void DrawPoint(SKCanvas canvas, SKPaint paint, SKPoint point, float raidus) { // canvas.DrawCircle( point, raidus, paint ); //} public static void DrawPoints(SKCanvas canvas, SKPaint paint, List <SKPoint> points) { if (points == null || points.Count == 0)// < 4 { return; } for (int i = 0; i < points.Count; i++) { canvas.DrawPoint(points[i], paint); } }
protected void DrawPoints(SKCanvas canvas, SKPoint[] points) { if (points.Length > 0 && PointMode != PointMode.None) { for (int i = 0; i < points.Length; i++) { var entry = this.Entries.ElementAt(i); var point = points[i]; canvas.DrawPoint(point, entry.Color, this.PointSize, this.PointMode); } } }
/// <summary></summary> /// <param name="info"></param> /// <param name="canvas"></param> private void DrawGraph(SKImageInfo info, SKCanvas canvas) { if (this.Series == null) { return; } float xf = (this.GraphFrame.Width / (this.XMax - this.XMin)); float yf = (this.GraphFrame.Height / (this.YMax - this.YMin)); // iterate each line series foreach (LineSeries ls in this.Series) { using (SKPaint axisPaint = new SKPaint() { Color = ls.LineColor, StrokeWidth = ls.StrokeWidth, Style = SKPaintStyle.Stroke, IsAntialias = true }) { // normalize the points to the graph frame SKPoint[] tps = new SKPoint[ls.Values.Length]; // calculate all the points for (int i = 0; i < ls.Values.Length; ++i) { tps[i] = new SKPoint(this.GraphFrame.Left + ((ls.Values[i].X - this.XMin) * xf), this.TitleHeight + this.GraphFrame.Height - ((ls.Values[i].Y - this.YMin) * yf)); } if (tps.Length < 2) { continue; } // draw the lines for (int i = 0; i < tps.Length - 1; ++i) { canvas.DrawLine(tps[i], tps[i + 1], axisPaint); } // draw the dots using (SKPaint ptPaint = new SKPaint() { Color = ls.LineColor, StrokeWidth = ls.StrokeWidth * 5.0F, StrokeCap = SKStrokeCap.Round, IsAntialias = true }) { for (int i = 0; i < tps.Length; ++i) { canvas.DrawPoint(tps[i], ptPaint); } } } } }
private void DrawPoints(SKCanvas canvas) { if (PointMode != PointMode.None) { foreach (var pps in pointsPerSerie) { var entries = pps.Key.Entries.ToArray(); for (int i = 0; i < pps.Value.Count; i++) { var entry = entries[i]; var point = pps.Value.ElementAt(i); canvas.DrawPoint(point, pps.Key.Color ?? entry.Color, PointSize, PointMode); } } } }
/// <summary> /// Onload loads map of roskilde and /// draws path that was calculated by Wificonnection /// </summary> private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); topOffset = info.Height / 5; canvas.DrawBitmap(resourceBitmap, new SKRect(0, info.Height / 5f, info.Width, 2 * info.Height / 2.5f)); for (int i = 0; i < WifiConnection.xValues.Length; i++) { canvas.DrawPoint(WifiConnection.xValues[i] + 3, WifiConnection.yValues[i] + topOffset + 20, SKColor.Parse("#ff0000")); } }
/// <summary> /// Draws path on canvas when event is triggers /// not currently in use /// </summary> private void DrawingOnCanvas(object senderr, EventArgs ee) { CanvasView.InvalidateSurface(); CanvasView.PaintSurface += (sender, e) => { SKSurface surface = e.Surface; int surfaceWidth = e.Info.Width; int surfaceHeight = e.Info.Height; SKCanvas canvas = surface.Canvas; for (int i = 0; i < WifiConnection.xValues.Length; i++) { canvas.DrawPoint(WifiConnection.xValues[i] + 3, WifiConnection.yValues[i] + topOffset + 20, SKColor.Parse("#ff0000")); } canvas.Flush(); }; }
protected void DrawPoints(IEnumerable <Entry> serie, SKCanvas canvas, SKPoint[] points, SKPoint selectedPoint) { if (points.Length > 0 && PointMode != PointMode.None) { var entries = serie as Entry[] ?? serie.ToArray(); for (int i = 0; i < points.Length; i++) { var entry = entries.ElementAt(i); var point = points[i]; var size = PointSize; if (point == selectedPoint) { size = SelectedPointSize; } canvas.DrawPoint(point, entry.Color, size, PointMode); } } }
private static SKBitmap DrawArrayImage(BlockColors basis, PT[][] ArraySrc) { SKBitmap drawimage = new SKBitmap(blockinfo, SKBitmapAllocFlags.ZeroPixels); SKCanvas skc = new SKCanvas(drawimage); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { var PixelType = ArraySrc[y][x]; BCColor ChosenColor = SelectColor(PixelType, basis); SKColor useChosen = ChosenColor; skc.DrawPoint(new SKPoint(x, y), ChosenColor); //drawimage.SetPixel(x, y, ChosenColor); } } skc.Flush(); return(drawimage); }
public void Render(SKCanvas canvas, IGeometryVisual gemoetry, double scale) { var mark = gemoetry as IMark; if (mark == null || !mark.IsValid) { return; } using (var paint = new SKPaint()) { paint.Color = mark.ToolSettings.SelectedColor.ToSkiaColor(); paint.IsAntialias = true; paint.IsStroke = false; paint.StrokeWidth = (float)(mark.Size * scale); paint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint((float)(mark.Point.X * scale), (float)(mark.Point.Y * scale), paint); //TODO: Maybe fix real fill } }
/// <summary> /// Рисует ФЧХ для параметра. /// </summary> /// <param name="plot"></param> /// <param name="parameters"></param> /// <param name="canvas"></param> private void PaintSParameter(ParameterType type, SKPaint paint, Plot plot, Parameters parameters, SKCanvas canvas) { // цикл для расчета коэффициента масштабирования координат X RestrictiveFrame frame = plot.Frame; float scalingFactor = base.GetXScalingFactor(parameters, frame); double i = plot.Frame.GetFirstPointX(); float j = (float)parameters.Fmin; while (j * scalingFactor <= parameters.Fmax * scalingFactor) { float x = j; float y = plot.GetCenterPointOfYAxis(); // Встречаем отрицательную бесконечность на 1 шаге. if (parameters.GetMagnitude(ParameterType.S12, x) == Double.NegativeInfinity) { i += 0.04f; j += 0.04f; continue; } // Инвертирование значений. float currentPhase = (float)parameters.GetPhase(type, x); y += -(currentPhase); if (plot.FirstPointY + y >= plot.SecondPointY) { i += 0.04f; j += 0.04f; continue; } canvas.DrawPoint(plot.FirstPointX + x * scalingFactor, y, paint); i += 0.04f; j += 0.04f; } }
private static SKImage DrawPath(IEnumerable <int[][]> points, float strokeWidth) { var info = new SKImageInfo(255, 255); using (var surface = SKSurface.Create(info)) { SKCanvas canvas = surface.Canvas; canvas.Clear(SKColors.Transparent); var paint = new SKPaint { StrokeWidth = strokeWidth, Style = SKPaintStyle.Stroke, Color = new SKColor(0x00, 0x00, 0x00), StrokeCap = SKStrokeCap.Round, }; foreach (var pathData in points) { if (pathData[0].Length == 1) { canvas.DrawPoint(new SKPoint(pathData[0][0], pathData[1][0]), paint); } else if (pathData[0].Length > 1) { var path = new SKPath(); path.MoveTo(new SKPoint(pathData[0][0], pathData[1][0])); for (int i = 1; i < pathData[0].Length; i++) { path.LineTo(new SKPoint(pathData[0][i], pathData[1][i])); } canvas.DrawPath(path, paint); } } return(surface.Snapshot()); } }
public override void DrawContent(SKCanvas canvas, int width, int height) { var total = Series.Count(); if (total > 0) { var captionHeight = Series.First().Max(x => { var result = 0.0f; var hasLabel = !string.IsNullOrEmpty(x.Label); var hasValueLabel = !string.IsNullOrEmpty(x.ValueLabel); if (hasLabel || hasValueLabel) { var hasOffset = hasLabel && hasValueLabel; var captionMargin = LabelTextSize * 0.60f; var space = hasOffset ? captionMargin : 0; if (hasLabel) { result += LabelTextSize; } if (hasValueLabel) { result += LabelTextSize; } } return(result); }); var center = new SKPoint(width / 2, height / 2); var radius = ((Math.Min(width, height) - (2 * Margin)) / 2) - captionHeight; var rangeAngle = (float)((Math.PI * 2) / total); var startAngle = (float)Math.PI; var nextEntry = Series.First().First(); var nextAngle = startAngle; var nextPoint = GetPoint(nextEntry.Value, center, nextAngle, radius); DrawBorder(canvas, center, radius); for (int i = 0; i < total; i++) { var angle = nextAngle; var entry = nextEntry; var point = nextPoint; var nextIndex = (i + 1) % total; nextAngle = startAngle + (rangeAngle * nextIndex); nextEntry = Series.First().ElementAt(nextIndex); nextPoint = GetPoint(nextEntry.Value, center, nextAngle, radius); // Border center bars using (var paint = new SKPaint() { Style = SKPaintStyle.Stroke, StrokeWidth = BorderLineSize, Color = BorderLineColor, IsAntialias = true, }) { var borderPoint = GetPoint(MaxValue, center, angle, radius); canvas.DrawLine(point.X, point.Y, borderPoint.X, borderPoint.Y, paint); } // Values points and lines using (var paint = new SKPaint() { Style = SKPaintStyle.Stroke, StrokeWidth = BorderLineSize, Color = entry.Color.WithAlpha((byte)(entry.Color.Alpha * 0.75f)), PathEffect = SKPathEffect.CreateDash(new[] { BorderLineSize, BorderLineSize * 2 }, 0), IsAntialias = true, }) { var amount = Math.Abs(entry.Value - AbsoluteMinimum) / ValueRange; canvas.DrawCircle(center.X, center.Y, radius * amount, paint); } canvas.DrawGradientLine(center, entry.Color.WithAlpha(0), point, entry.Color.WithAlpha((byte)(entry.Color.Alpha * 0.75f)), LineSize); canvas.DrawGradientLine(point, entry.Color, nextPoint, nextEntry.Color, LineSize); canvas.DrawPoint(point, entry.Color, PointSize, PointMode); // Labels var labelPoint = GetPoint(MaxValue, center, angle, radius + LabelTextSize + (PointSize / 2)); var alignment = SKTextAlign.Left; if ((Math.Abs(angle - (startAngle + Math.PI)) < Epsilon) || (Math.Abs(angle - Math.PI) < Epsilon)) { alignment = SKTextAlign.Center; } else if (angle > (float)(startAngle + Math.PI)) { alignment = SKTextAlign.Right; } canvas.DrawCaptionLabels(entry.Label, entry.TextColor, entry.ValueLabel, entry.Color, LabelTextSize, labelPoint, alignment); } } }
/// <summary>Draw a progress meter</summary> /// <remarks> /// Value 1 is the part and Value 2 is the total possible /// Value 1 can exceed value 2 for mutiple revolutions /// </remarks> private void PaintProgress(SKImageInfo info, SKCanvas canvas) { // set up the gradient colors SKColor[] colors = new SKColor[2] { this.ArcColor1.ToSKColor(), this.ArcColor2.ToSKColor() }; // sweep is in DEG -> normalize the sweep angle between 0 and 360 //float sweep = QuickCalc.Revolution( ( this.Value1 / this.Value2 ) * 360.0F ); //sweep = ( sweep > 360.0 ) ? 360.0F : sweep; float sweep = (this.Value1 % this.Value2 / this.Value2) * 360.0F; // we have to roate the drawing canvas 90 degrees CCW canvas.RotateDegrees(-90, info.Width / 2, info.Height / 2); // no value if (this.Value1 <= 0.0) { // draw background ring using (SKPath path = new SKPath()) { using (SKPaint bkgPaint = new SKPaint() { Color = _disabled, StrokeWidth = this.ArcLineWidth, Style = SKPaintStyle.Stroke, IsAntialias = true }) { path.AddArc(this.OuterRect, 0, 360.0F); canvas.DrawPath(path, bkgPaint); } } } // less than 1 revolution else if (this.Value1 < this.Value2) { // draw background ring using (SKPath path = new SKPath()) { using (SKPaint bkgPaint = new SKPaint() { Color = _disabled, StrokeWidth = this.ArcLineWidth, Style = SKPaintStyle.Stroke, IsAntialias = true }) { path.AddArc(this.OuterRect, 0, 360.0F); canvas.DrawPath(path, bkgPaint); } } // draw the partial arc using (SKPath path = new SKPath()) { using (SKPaint arcPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = this.ArcColor1.ToSKColor() }) { arcPaint.StrokeWidth = this.ArcLineWidth; arcPaint.StrokeCap = SKStrokeCap.Butt; arcPaint.IsAntialias = true; arcPaint.Shader = SKShader.CreateSweepGradient(this.Origin, colors, new Single[] { 0, 1 }, SKShaderTileMode.Clamp, 0, sweep); // create an arc to sweep along path.AddArc(this.OuterRect, 0, sweep); canvas.DrawPath(path, arcPaint); } } } else // 1 or more revolution { // draw background ring using (SKPath path = new SKPath()) { using (SKPaint bkgPaint = new SKPaint() { Color = this.ArcColor1.ToSKColor(), StrokeWidth = this.ArcLineWidth }) { bkgPaint.Style = SKPaintStyle.Stroke; bkgPaint.IsAntialias = true; path.AddArc(this.OuterRect, 0, 360.0F); canvas.DrawPath(path, bkgPaint); } } // rotate the canvas by the sweep angle so we always start at 0 canvas.RotateDegrees(sweep - 180, info.Width / 2, info.Height / 2); // draw the partial gradiant arc using (SKPath path = new SKPath()) { using (SKPaint arcPaint = new SKPaint { Style = SKPaintStyle.Stroke }) { arcPaint.Color = this.ArcColor2.ToSKColor(); arcPaint.StrokeWidth = this.ArcLineWidth; arcPaint.StrokeCap = SKStrokeCap.Butt; arcPaint.IsAntialias = true; // sweep gradient uses start angle to end angle arcPaint.Shader = SKShader.CreateSweepGradient(this.Origin, colors, new Single[] { 0, 1 }, SKShaderTileMode.Clamp, 0, 180); // create an arc to sweep along - uses start angle and then how many degrees to rotate from start path.AddArc(this.OuterRect, 0, 180); canvas.DrawPath(path, arcPaint); } } canvas.RotateDegrees(-(sweep - 180), info.Width / 2, info.Height / 2); } // calc pts for the trailing handle Tuple <float, float> pt1 = QuickCalc.Transform(QuickCalc.Deg2Rad(sweep + (this.ArcLineWidth * 0.075F)), this.OuterRect.Width / 2.0F); Tuple <float, float> pt2 = QuickCalc.Transform(QuickCalc.Deg2Rad(sweep), this.OuterRect.Width / 2.0F); // draw the trailing point with shadow using (SKPaint handlePaint = new SKPaint() { Color = this.BackgroundColor.ToSKColor() }) { handlePaint.StrokeCap = SKStrokeCap.Round; handlePaint.StrokeWidth = this.ArcLineWidth * 2; handlePaint.IsAntialias = true; // shadow canvas.DrawPoint(pt1.Item1 + info.Rect.MidX, info.Rect.MidY + pt1.Item2, handlePaint); // change color handlePaint.Color = this.ArcColor2.ToSKColor(); // handle canvas.DrawPoint(pt2.Item1 + info.Rect.MidX, info.Rect.MidY + pt2.Item2, handlePaint); } // rotate it all back canvas.RotateDegrees(90, info.Width / 2, info.Height / 2); }
/// <summary></summary> /// <param name="info"></param> /// <param name="canvas"></param> private void DrawGraph(SKImageInfo info, SKCanvas canvas) { // max height of any bar float height = this.BarFrame.Bottom - this.BarFrame.Top; // find the max value float maxValue = this.Frames.Max(e => e.Entry.Value); float maxThreshold = this.Frames.Max(e => e.Entry.Threshold); float max = new float[] { maxValue, maxThreshold }.Max(); // draw the horizontal scale if (this.HorizontalScale) { // draw horizontal line every float yline = this.BarFrame.Bottom; float n = 10F; using (SKPaint pen = new SKPaint() { Color = this.AxisColor.ToSKColor(), StrokeWidth = 1.0F, Style = SKPaintStyle.Stroke }) { while (yline > 0) { canvas.DrawLine(this.BarFrame.Left, yline, this.BarFrame.Right, yline, pen); yline -= height / n; } } } // draw each bar foreach (ColumnEntry entry in this.Frames) { // the bar drawing frame SKRect rect = entry.Frame; // calc margin from thickness - use denominator from [3,15] float denom = 3.0F + ((15.0F - 3.0F) * (this.BarThickness / 100.0F)); float margin = rect.Width / denom; // calculate threshold & value percentage of height float pt = (entry.Entry.Threshold / max) * height; float pv = (entry.Entry.Value / max) * height; // calculate threshold rect and value rect SKRect bart = new SKRect(rect.Left + margin, rect.Bottom - pt, rect.Right - margin, rect.Bottom); SKRect barv = new SKRect(rect.Left + margin, rect.Bottom - pv, rect.Right - margin, rect.Bottom); // draw threshold bar //canvas.DrawRect(bart, new SKPaint() { Color = entry.Entry.BackgroundColor, StrokeWidth = 0, Style = SKPaintStyle.StrokeAndFill }); // epmty entries are ignored if (entry.Entry == null) { continue; } // no data if (entry.Entry.IsEmpty) { float rad = 10.0F; using (SKPaint ptPaint = new SKPaint() { Color = this.AxisColor.ToSKColor(), StrokeWidth = rad, StrokeCap = SKStrokeCap.Round }) { canvas.DrawPoint(rect.MidX, rect.Bottom - rad, ptPaint); } } // reached goal then paint threshold on top of value else if (entry.Entry.Value >= entry.Entry.Threshold) { using (SKPaint barPaint = new SKPaint() { Color = entry.Entry.ValueColor, StrokeWidth = 0, Style = SKPaintStyle.StrokeAndFill }) { canvas.DrawRect(barv, barPaint); barPaint.Color = entry.Entry.ThresholdColor; canvas.DrawRect(bart, barPaint); } //// draw threshold line //if (entry.Entry.Value > entry.Entry.Threshold) // canvas.DrawLine(bart.Left, bart.Top, bart.Right, bart.Top, // new SKPaint() { Color = entry.Entry.BackgroundColor, StrokeWidth = 3.0F, Style = SKPaintStyle.Stroke, // PathEffect = SKPathEffect.CreateDash(new float[] { 10, 10 }, 20) // }); } else { using (SKPaint barPaint = new SKPaint() { Color = entry.Entry.ThresholdColor, StrokeWidth = 0, Style = SKPaintStyle.StrokeAndFill }) { canvas.DrawRect(bart, barPaint); barPaint.Color = entry.Entry.ValueColor; canvas.DrawRect(barv, barPaint); } } } }
public override void DrawContent(SKCanvas canvas, int width, int height) { var total = this.Entries?.Count() ?? 0; if (total > 0) { var captionHeight = this.Entries.Max(x => { var result = 0.0f; var hasLabel = !string.IsNullOrEmpty(x.Label); var hasValueLabel = !string.IsNullOrEmpty(x.ValueLabel); if (hasLabel || hasValueLabel) { var hasOffset = hasLabel && hasValueLabel; var captionMargin = this.LabelTextSize * 0.60f; var space = hasOffset ? captionMargin : 0; if (hasLabel) { result += this.LabelTextSize; } if (hasValueLabel) { result += this.LabelTextSize; } } return(result); }); var center = new SKPoint(width / 2, height / 2); var radius = ((Math.Min(width, height) - (2 * Margin)) / 2) - captionHeight; var rangeAngle = (float)((Math.PI * 2) / total); var startAngle = (float)Math.PI; var nextEntry = this.Entries.First(); var nextAngle = startAngle; var nextPoint = this.GetPoint(nextEntry.Value * this.AnimationProgress, center, nextAngle, radius); this.DrawBorder(canvas, center, radius); using (var clip = new SKPath()) { clip.AddCircle(center.X, center.Y, radius); for (int i = 0; i < total; i++) { var angle = nextAngle; var entry = nextEntry; var point = nextPoint; var nextIndex = (i + 1) % total; nextAngle = startAngle + (rangeAngle * nextIndex); nextEntry = this.Entries.ElementAt(nextIndex); nextPoint = this.GetPoint(nextEntry.Value * this.AnimationProgress, center, nextAngle, radius); canvas.Save(); canvas.ClipPath(clip); // Border center bars using (var paint = new SKPaint() { Style = SKPaintStyle.Stroke, StrokeWidth = this.BorderLineSize, Color = this.BorderLineColor, IsAntialias = true, }) { var borderPoint = this.GetPoint(this.MaxValue, center, angle, radius); canvas.DrawLine(point.X, point.Y, borderPoint.X, borderPoint.Y, paint); } // Values points and lines using (var paint = new SKPaint() { Style = SKPaintStyle.Stroke, StrokeWidth = this.BorderLineSize, Color = entry.Color.WithAlpha((byte)(entry.Color.Alpha * 0.75f * this.AnimationProgress)), PathEffect = SKPathEffect.CreateDash(new[] { this.BorderLineSize, this.BorderLineSize * 2 }, 0), IsAntialias = true, }) { var amount = Math.Abs(entry.Value - this.AbsoluteMinimum) / this.ValueRange; canvas.DrawCircle(center.X, center.Y, radius * amount, paint); } canvas.DrawGradientLine(center, entry.Color.WithAlpha(0), point, entry.Color.WithAlpha((byte)(entry.Color.Alpha * 0.75f)), this.LineSize); canvas.DrawGradientLine(point, entry.Color, nextPoint, nextEntry.Color, this.LineSize); canvas.DrawPoint(point, entry.Color, this.PointSize, this.PointMode); canvas.Restore(); // Labels var labelPoint = new SKPoint(0, radius + this.LabelTextSize + (this.PointSize / 2)); var rotation = SKMatrix.MakeRotation(angle); labelPoint = center + rotation.MapPoint(labelPoint); var alignment = SKTextAlign.Left; if ((Math.Abs(angle - (startAngle + Math.PI)) < Epsilon) || (Math.Abs(angle - Math.PI) < Epsilon)) { alignment = SKTextAlign.Center; } else if (angle > (float)(startAngle + Math.PI)) { alignment = SKTextAlign.Right; } canvas.DrawCaptionLabels(entry.Label, entry.TextColor, entry.ValueLabel, entry.Color.WithAlpha((byte)(255 * this.AnimationProgress)), this.LabelTextSize, labelPoint, alignment, base.Typeface, out var _); } } } }
/// <summary>Draw a pie meter</summary> private void PaintPie(SKImageInfo info, SKCanvas canvas, bool handles = false) { float arcWidth = (info.Width / 15.0F) * 1.0F; // always draw a background ring using (SKPath path = new SKPath()) { path.AddArc(this.OuterRect, 0, 360); canvas.DrawPath(path, new SKPaint() { Color = _disabled, StrokeWidth = this.ArcLineWidth, Style = SKPaintStyle.Stroke, IsAntialias = true }); } // calc the sum float total = this.Value1 + this.Value2; // bail if both are 0 if (total <= 0.0) { return; } // calc the sweep angle float sweep = (this.Value1 / total) * 360.0F; // calc the angles, 0 is to the right, start from overhead float rewardSweep = (sweep > 360.0) ? 360.0F : sweep; float bonusStartAngle = QuickCalc.Revolution(270.0F + rewardSweep); // draw arc 1 using (SKPath path = new SKPath()) { using (SKPaint arcPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = this.ArcColor1.ToSKColor(), StrokeWidth = this.ArcLineWidth, IsAntialias = true }) { path.AddArc(this.OuterRect, 270, rewardSweep); canvas.DrawPath(path, arcPaint); } } // draw arc 2 using (SKPath path = new SKPath()) { using (SKPaint arcPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = this.ArcColor2.ToSKColor(), StrokeWidth = this.ArcLineWidth, IsAntialias = true }) { path.AddArc(this.OuterRect, bonusStartAngle, 360 - rewardSweep); canvas.DrawPath(path, arcPaint); } } if (!handles) { return; } // draw handles using (SKPaint handlePaint = new SKPaint() { Color = this.ArcColor1.ToSKColor(), StrokeCap = SKStrokeCap.Round, StrokeWidth = this.ArcLineWidth * 2, IsAntialias = true }) { canvas.DrawPoint(this.VerticalCenter, this.OuterRect.Top, handlePaint); // where's the other handle? float rad = QuickCalc.Deg2Rad(QuickCalc.Revolution(360 - rewardSweep + 90)); Tuple <float, float> pt = QuickCalc.Transform(rad, this.OuterRect.Width / 2.0F); handlePaint.Color = this.ArcColor2.ToSKColor(); canvas.DrawPoint(pt.Item1 + info.Rect.MidX, info.Rect.MidY - pt.Item2, handlePaint); } }
public override void Draw(SKCanvas canvas) { canvas.DrawPoint(DrawingPoint, Paint); }
private void Canvas_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e) { if (BindingContext is MarsRoverModel model) { if (model.CurrentPlateau != null && model.CurrentRover != null) { SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); SKPaint gridLinePaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = SKColors.LightGray, StrokeWidth = 2 }; SKPaint startPointPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = SKColors.Green, StrokeWidth = 8 }; SKPaint endPointPaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = SKColors.Red, StrokeWidth = 8 }; SKPaint pathLinePaint = new SKPaint { Style = SKPaintStyle.Stroke, Color = SKColors.Black, StrokeWidth = 2 }; float xBreak = (float)info.Width / model.CurrentPlateau.UpperX; for (int i = 0; i <= model.CurrentPlateau.UpperX; i++) { canvas.DrawLine(xBreak * i, 0, xBreak * i, info.Width, gridLinePaint); } float yBreak = (float)info.Height / model.CurrentPlateau.UpperY; for (int i = 0; i <= model.CurrentPlateau.UpperY; i++) { canvas.DrawLine(0, yBreak * i, info.Height, yBreak * i, gridLinePaint); } if (model.Path != null) { Tuple <float, float> lastPos = null; foreach (var pos in model.Path) { float posX = pos.Item1 * xBreak; float posY = pos.Item2 * yBreak; if (lastPos == null) { canvas.DrawPoint(posX, posY, startPointPaint); } else { canvas.DrawLine(lastPos.Item1, lastPos.Item2, posX, posY, pathLinePaint); } lastPos = new Tuple <float, float>(posX, posY); } canvas.DrawPoint(lastPos.Item1, lastPos.Item2, endPointPaint); using (var snapshot = surface.Snapshot()) using (var data = snapshot.Encode(SKEncodedImageFormat.Png, 80)) using (var ms = new MemoryStream()) { data.SaveTo(ms); byte[] imageBytes = ms.ToArray(); string base64String = Convert.ToBase64String(imageBytes); model.SendImage(base64String); } } } } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); // Make image into bitmap SKBitmap bitmap = SKBitmap.Decode(ViewModel.Image.GetStreamWithImageRotatedForExternalStorage()); // Find rectangle to fit bitmap float scale = Math.Min((float)info.Width / bitmap.Width, (float)info.Height / bitmap.Height); SKRect rect = SKRect.Create(scale * bitmap.Width, scale * bitmap.Height); float x = (info.Width - rect.Width) / 2; float y = (info.Height - rect.Height) / 2; rect.Offset(x, y); // Draw the image canvas.DrawBitmap(bitmap, rect); // Prep rectangle paint var rectPaint = new SKPaint { Color = new SKColor(255, 0, 0), IsStroke = true, StrokeWidth = 2 }; // Prep text paint var textPaint = new SKPaint { Color = new SKColor(255, 0, 0), TextSize = 48 }; // Draw the face boxes ViewModel.Faces.ToList().ForEach(face => { var faceRect = SKRect.Create(scale * face.FaceRectangle.Left + x, scale * face.FaceRectangle.Top + y, scale * face.FaceRectangle.Width, scale * face.FaceRectangle.Height); canvas.DrawRect(faceRect, rectPaint); canvas.DrawText(ViewModel.Faces.IndexOf(face).ToString(), (scale * face.FaceRectangle.Left + x) + (scale * face.FaceRectangle.Width / 2), (scale * face.FaceRectangle.Top + y), textPaint); // If landmarks are present, draw them too! if (face.FaceLandmarks != null) { var landmarkPaint = new SKPaint { Color = new SKColor(255, 0, 0), StrokeWidth = 6 }; face.FaceLandmarks.GetType().GetProperties().ToList().ForEach(landmark => { if (landmark.PropertyType == typeof(Coordinate)) { var coord = (Coordinate)landmark.GetValue(face.FaceLandmarks); canvas.DrawPoint((float)(scale * coord.X + x), (float)(scale * coord.Y + y), landmarkPaint); } }); } }); }
protected void DrawPoints(SKCanvas canvas, SKPoint[] points, float origin = 0) { if (points.Length > 0 && PointMode != PointMode.None) { for (int i = 0; i < points.Length; i++) { var entry = this.Entries.ElementAt(i); var point = points[i]; var size = entry.Selected ? 20 : this.PointSize; if (entry.Selected) { // Line from Point to bottom canvas.DrawLine(point.X, point.Y, point.X, origin, new SKPaint() { Color = new SKColor(255, 255, 255) }); // Draw Annotation var path = new SKPath(); if (i == 0) // Inverse { canvas.DrawRoundRect(new SKRect(point.X + 90, point.Y - 70, point.X, point.Y - 20), 5, 5, new SKPaint() { Color = new SKColor(255, 255, 255) }); path.MoveTo(point.X, point.Y - 15); path.RLineTo(0, -10); path.RLineTo(20, 0); path.LineTo(point.X, point.Y - 15); canvas.DrawText(entry.AnnotationLabel ?? "$790", point.X + 45, point.Y - 30, new SKPaint() { IsAntialias = true, Color = new SKColor(0, 0, 0), TextAlign = SKTextAlign.Center, TextSize = 20 }); canvas.DrawText(entry.AnnotationHeadingLabel ?? "3/14 - 7/14", point.X + 45, point.Y - 55, new SKPaint() { IsAntialias = true, Color = new SKColor(61, 61, 61), TextAlign = SKTextAlign.Center, TextSize = 11 }); // TODO: SkTypeface.FromFile("path/to/typeface.ttf"); } else { canvas.DrawRoundRect(new SKRect(point.X - 90, point.Y - 70, point.X, point.Y - 20), 5, 5, new SKPaint() { Color = new SKColor(255, 255, 255) }); path.MoveTo(point.X, point.Y - 15); path.RLineTo(0, -10); path.RLineTo(-20, 0); path.LineTo(point.X, point.Y - 15); canvas.DrawText(entry.AnnotationLabel ?? "$790", point.X - 45, point.Y - 30, new SKPaint() { IsAntialias = true, Color = new SKColor(0, 0, 0), TextAlign = SKTextAlign.Center, TextSize = 20 }); canvas.DrawText(entry.AnnotationHeadingLabel ?? "3/14 - 7/14", point.X - 45, point.Y - 55, new SKPaint() { IsAntialias = true, Color = new SKColor(61, 61, 61), TextAlign = SKTextAlign.Center, TextSize = 11 }); // TODO: SkTypeface.FromFile("path/to/typeface.ttf"); } canvas.DrawPath(path, new SKPaint() { IsAntialias = true, Color = new SKColor(255, 255, 255), StrokeWidth = 1, Style = SKPaintStyle.StrokeAndFill }); canvas.DrawPoint(point, entry.Color, size, this.PointMode); canvas.DrawPoint(point, new SKColor(0, 0, 0), size - 12, this.PointMode); } else { // Line from Point to bottom canvas.DrawLine(point.X, point.Y, point.X, origin, new SKPaint() { Color = new SKColor(255, 255, 255, 127), PathEffect = SKPathEffect.CreateDash(new float[] { 10, 2 }, 20) }); canvas.DrawPoint(point, entry.Color, size, this.PointMode); } AddTouchHandler(new Rectangle(new Point(point.X - 20, point.Y - 20), new Size(40, 40)), () => { foreach (var item in Entries) { if (item == entry) { entry.Selected = !entry.Selected; } else { item.Selected = false; } } }); } } }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) { base.OnPaintSurface(e); SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); using (var primaryPaint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeCap = SKStrokeCap.Butt, StrokeWidth = (float)Thickness, }) using (var path = new SKPath()) using (var backgroundPath = new SKPath()) { var margin = (float)Thickness / 2.0f; if (RadialBackgroundColor != Color.Default && RadialBackgroundColor != Color.Transparent) { primaryPaint.Color = RadialBackgroundColor.ToSKColor(); backgroundPath.AddArc(new SKRect(margin, margin, info.Width - margin, info.Height - margin), -90 + (float)StartAngle, (float)SweepAngle); canvas.DrawPath(backgroundPath, primaryPaint); if (backgroundPath.PointCount > 0) { if (StartStrokeCap == RadialProgressStrokeCap.Round) { primaryPaint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint(backgroundPath.Points[0], primaryPaint); } if (EndStrokeCap == RadialProgressStrokeCap.Round) { primaryPaint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint(backgroundPath.Points[backgroundPath.PointCount - 1], primaryPaint); } } } SKColor[] colors = new SKColor[] { RadialStartColor.ToSKColor(), RadialStartColor.ToSKColor(), RadialMiddleColor.ToSKColor(), RadialMiddleColor.ToSKColor(), RadialEndColor.ToSKColor(), RadialEndColor.ToSKColor(), RadialStartColor.ToSKColor(), }; float[] colorPos = new float[] { 0, // start (1 / 6.0f) * 1, // start (1 / 6.0f) * 2, // mid (1 / 6.0f) * 3, // mid (1 / 6.0f) * 4, // end (1 / 6.0f) * 5.7f, // end 0.95f // start }; primaryPaint.StrokeCap = SKStrokeCap.Butt; primaryPaint.Shader = SKShader.CreateSweepGradient(new SKPoint(info.Rect.MidX, info.Rect.MidY), colors, colorPos, SKShaderTileMode.Repeat, -90 + (float)StartAngle, -90 + (float)StartAngle + (float)SweepAngle); path.AddArc(new SKRect(margin, margin, info.Width - margin, info.Height - margin), -90 + (float)StartAngle, (float)SweepAngle * (float)Progress); canvas.DrawPath(path, primaryPaint); if (path.PointCount > 2) { if (StartStrokeCap == RadialProgressStrokeCap.Round) { primaryPaint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint(path.Points[0], primaryPaint); } if (EndStrokeCap == RadialProgressStrokeCap.Round) { primaryPaint.StrokeCap = SKStrokeCap.Round; canvas.DrawPoint(path.Points[path.PointCount - 1], primaryPaint); } } if (HasLabel) { using (var fontPaint = new SKPaint { IsAntialias = true, Color = TextColor.ToSKColor(), TextSize = (float)FontSize, }) using (var fontPath = new SKPath()) { fontPath.AddPathReverse(path); canvas.DrawTextOnPath(string.Format(TextFormat, Progress), fontPath, 0, (float)FontSize / 3.0f, fontPaint); } } } }