123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- import 'dart:ui' as ui;
- import 'package:fis_measure/values/colors.dart';
- import 'package:flutter/material.dart';
- import 'package:path_drawing/path_drawing.dart';
- extension MeasureCanvasExt on Canvas {
- static String? _fontFamily;
- /// 设置字体
- static void setFontFamily(String? val) {
- _fontFamily = val;
- }
- /// 绘制文本
- ///
- /// [text] 文本内容
- ///
- /// [offset] 位置
- ///
- /// [style] 文字样式
- void drawText(
- String text,
- ui.Offset offset, {
- TextStyle? style,
- }) {
- var displayStyle = style;
- if (displayStyle != null) {
- if (displayStyle.fontFamily == null) {
- displayStyle = displayStyle.copyWith(fontFamily: _fontFamily);
- }
- }
- var textPainter = TextPainter(
- text: TextSpan(
- text: text,
- style: displayStyle,
- ),
- textAlign: ui.TextAlign.start,
- textDirection: TextDirection.ltr,
- );
- textPainter.layout();
- textPainter.paint(this, offset);
- }
- /// 绘制文本段落
- ///
- /// [text] 文本内容
- ///
- /// [offset] 位置
- ///
- /// [width] 限制长度
- ///
- /// [style] 文字样式
- ///
- /// [maxLines] 最大行数
- void drawTextParagraph(
- String text,
- ui.Offset offset,
- double width, {
- ui.TextStyle? style,
- int maxLines = 1,
- }) {
- var pb = ui.ParagraphBuilder(
- ui.ParagraphStyle(
- textAlign: TextAlign.left,
- textDirection: ui.TextDirection.ltr,
- maxLines: maxLines,
- ),
- );
- if (style != null) {
- pb.pushStyle(style);
- }
- pb.addText(text);
- final paragraph = pb.build()..layout(ui.ParagraphConstraints(width: width));
- drawParagraph(paragraph, offset);
- }
- /// 画虚线
- ///
- /// [p1] 第一个点
- ///
- /// [p2] 第二个点
- ///
- /// [dashWidth] 虚线点长度
- ///
- /// [spaceWidth] 虚线点间隔长度
- ///
- /// [paint] 画笔
- void drawDashLine(
- Offset p1,
- Offset p2,
- double dashWidth,
- double spaceWidth,
- Paint paint,
- ) {
- final path = Path()
- ..moveTo(p1.dx, p1.dy)
- ..lineTo(p2.dx, p2.dy);
- drawPath(
- dashPath(
- path,
- dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
- ),
- paint,
- );
- }
- // 画实线连线
- void drawLine(
- Offset p1,
- Offset p2,
- Paint paint,
- ) {
- final path = Path()
- ..moveTo(p1.dx, p1.dy)
- ..lineTo(p2.dx, p2.dy);
- drawPath(path, paint);
- }
- /// 画虚线贝塞尔曲线
- /// [p1] 第一个点
- /// [p2] 第二个点
- /// [dashWidth] 虚线点长度
- /// [spaceWidth] 虚线点间隔长度
- /// [paint] 画笔
- /// [c1] 第一个控制点
- /// [c2] 第二个控制点
- void drawDashBezierLine(
- Offset p1,
- Offset p2,
- double dashWidth,
- double spaceWidth,
- Paint paint, {
- Offset? c1,
- Offset? c2,
- }) {
- final path = Path()
- ..moveTo(p1.dx, p1.dy)
- ..cubicTo(
- c1?.dx ?? p1.dx,
- c1?.dy ?? p1.dy,
- c2?.dx ?? p2.dx,
- c2?.dy ?? p2.dy,
- p2.dx,
- p2.dy,
- );
- drawPath(
- dashPath(
- path,
- dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
- ),
- paint,
- );
- }
- /// 画虚线框
- ///
- /// [p1] 起始顶点
- ///
- /// [p2] 结束顶点
- ///
- /// [dashWidth] 虚线点长度
- ///
- /// [spaceWidth] 虚线点间隔长度
- ///
- /// [paint] 画笔
- void drawDashRect(
- Offset p1,
- Offset p2,
- double dashWidth,
- double spaceWidth,
- Paint paint,
- ) {
- final path = Path()
- ..moveTo(p1.dx, p1.dy)
- ..lineTo(p2.dx, p1.dy)
- ..lineTo(p2.dx, p2.dy)
- ..lineTo(p1.dx, p2.dy)
- ..close();
- drawPath(
- dashPath(
- path,
- dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
- ),
- paint,
- );
- }
- /// 画点集连线
- /// [points] 点集
- /// [paint] 画笔
- void drawPointsLine(List<Offset> points, Paint paint) {
- final path = Path();
- for (var i = 0; i < points.length; i++) {
- if (i == 0) {
- path.moveTo(points[i].dx, points[i].dy);
- } else {
- path.lineTo(points[i].dx, points[i].dy);
- }
- }
- drawPath(path, paint);
- }
- /// 画点集虚线连线
- /// [points] 点集
- /// [dashWidth] 虚线点长度
- /// [spaceWidth] 虚线点间隔长度
- /// [paint] 画笔
- /// [close] 是否闭合
- void drawDashPointsLine(
- List<Offset> points,
- double dashWidth,
- double spaceWidth,
- Paint paint, {
- bool close = false,
- }) {
- final path = Path();
- for (var i = 0; i < points.length; i++) {
- if (i == 0) {
- path.moveTo(points[i].dx, points[i].dy);
- } else {
- path.lineTo(points[i].dx, points[i].dy);
- }
- }
- if (close) {
- path.close();
- }
- drawPath(
- dashPath(
- path,
- dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
- ),
- paint,
- );
- }
- static final Paint _vertexPaint = Paint()
- ..strokeWidth = 1
- ..style = PaintingStyle.stroke
- ..isAntiAlias = true;
- /// 画顶点
- ///
- /// [offset] 位置
- ///
- /// [size] 尺寸
- ///
- /// [active] 是否活动
- void drawVertex(
- Offset offset,
- double size, {
- bool active = false,
- }) {
- final radius = size / 2.0;
- double x = offset.dx, y = offset.dy;
- save();
- final path = Path();
- // top_left -> bottom_right
- path.moveTo(x - radius, y - radius);
- path.lineTo(x + radius, y + radius);
- // top_right -> bottom_left
- path.moveTo(x + radius, y - radius);
- path.lineTo(x - radius, y + radius);
- _vertexPaint.color =
- active ? MeasureColors.ActiveCaliper : MeasureColors.ActivePrimary;
- drawPath(path, _vertexPaint);
- restore();
- }
- /// 画十字顶点
- ///
- /// [offset] 位置
- ///
- /// [size] 尺寸
- ///
- /// [active] 是否活动
- void drawCrossVertex(
- Offset offset,
- double size, {
- bool active = false,
- }) {
- final radius = size / 2.0;
- double x = offset.dx, y = offset.dy;
- save();
- final path = Path();
- // left -> right
- path.moveTo(x - radius, y);
- path.lineTo(x + radius, y);
- // top -> bottom
- path.moveTo(x, y - radius);
- path.lineTo(x, y + radius);
- _vertexPaint.color =
- active ? MeasureColors.ActiveCaliper : MeasureColors.ActivePrimary;
- drawPath(path, _vertexPaint..isAntiAlias = false);
- restore();
- }
- static final Paint _markPaint = Paint()
- ..strokeWidth = 1
- ..style = PaintingStyle.stroke
- ..isAntiAlias = false;
- ///绘制短横标记
- ///ifHorizontal true:横向标记 false:纵向标记
- void drawMark(
- Offset offset,
- double size, {
- bool active = false,
- bool ifHorizontal = true,
- }) {
- final radius = size / 3.0;
- double x = offset.dx, y = offset.dy;
- save();
- final path = Path();
- // top_left -> bottom_right
- if (ifHorizontal) {
- path.moveTo(x - radius, y);
- path.lineTo(x + radius, y);
- } else {
- path.moveTo(x, y - radius);
- path.lineTo(x, y + radius);
- }
- _markPaint.color =
- active ? MeasureColors.ActiveCaliper : MeasureColors.ActivePrimary;
- drawPath(path, _markPaint);
- restore();
- }
- }
|