Canon/Canon.Visualization/Models/Brush.cs
Ichirinko dbbab1c761 feat-visualization (#33)
尽管里面有点粪,但还是生成了漂亮的树,就当给树施肥了
剩下一个小问题:未考虑节点的宽度(因为名称不一样长),但目前未发现对图生成的明显影响

Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Reviewed-on: PostGuard/Canon#33
Co-authored-by: Ichirinko <1621543655@qq.com>
Co-committed-by: Ichirinko <1621543655@qq.com>
2024-04-19 14:59:45 +08:00

49 lines
1.4 KiB
C#

using SkiaSharp;
namespace Canon.Visualization.Models;
public sealed class Brush(SKCanvas canvas) : IDisposable
{
/// <summary>
/// 在指定的位置绘制
/// </summary>
/// <param name="pos"></param>
/// <param name="text"></param>
public void DrawText(SKPoint pos, string text)
{
SKPaint textPainter = new() { Color = SKColors.Black, IsAntialias = true, TextSize = 12f, StrokeWidth = 3f };
SKPaint backgroundPainter = new() { Color = SKColors.White };
SKRect textBorder = new();
textPainter.MeasureText(text, ref textBorder);
// 文字居中
float x = pos.X - textBorder.Width / 2;
float y = pos.Y - textBorder.Height / 2;
textBorder.Offset(x, y);
textBorder.Inflate(5, 5);
canvas.DrawRect(textBorder, backgroundPainter);
canvas.DrawText(text, x, y, textPainter);
}
/// <summary>
/// 绘制线段
/// </summary>
/// <param name="start">线段的起始点</param>
/// <param name="end">线段的终点</param>
public void DrawLine(SKPoint start, SKPoint end)
{
SKPaint linePainter = new()
{
Color = SKColors.Black, IsAntialias = true, StrokeWidth = 3f, StrokeCap = SKStrokeCap.Round
};
canvas.DrawLine(start, end, linePainter);
}
public void Dispose()
{
canvas.Dispose();
}
}