structure.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'dart:math';
  2. import 'dart:ui';
  3. import 'package:flutter/material.dart';
  4. /// 数据类型定义
  5. /// 绘制类型:直线、曲线
  6. enum PaintType { straightLine, curvedLine }
  7. /// 绘制状态:正在绘制、已完成、隐藏
  8. enum PaintState { doing, done, hide }
  9. /// Point 点类
  10. class Point {
  11. final double x;
  12. final double y;
  13. const Point({
  14. required this.x,
  15. required this.y,
  16. });
  17. factory Point.fromOffset(Offset offset, double width, double height) {
  18. return Point(
  19. x: double.parse((offset.dx / width).toStringAsFixed(4)),
  20. y: double.parse((offset.dy / height).toStringAsFixed(4)),
  21. );
  22. }
  23. factory Point.fromJson(
  24. Map<String, dynamic> json, double width, double height) {
  25. return Point(
  26. x: json['x'],
  27. y: json['y'],
  28. );
  29. }
  30. factory Point.fromList(List<dynamic> offSetList) {
  31. return Point(
  32. x: offSetList[0],
  33. y: offSetList[1],
  34. );
  35. }
  36. List<double> toList() {
  37. final List<double> offestList = [];
  38. offestList.addAll([x, y]);
  39. return offestList;
  40. }
  41. Map<String, dynamic> toJson(width, height) {
  42. final json = Map<String, dynamic>();
  43. json['x'] = x;
  44. json['y'] = y;
  45. return json;
  46. }
  47. double get distance => sqrt(x * x + y * y);
  48. // Point operator -(Point other) => Point(x: x - other.x, y: y - other.y);
  49. Offset toOffset(double width, double height) => Offset(x * width, y * height);
  50. }
  51. /// Line 线类
  52. class Line {
  53. List<Point> points = [];
  54. PaintState state;
  55. PaintType paintType;
  56. double strokeWidth;
  57. Color color;
  58. String userId;
  59. Line({
  60. this.color = Colors.black,
  61. this.strokeWidth = 1,
  62. this.state = PaintState.doing,
  63. this.paintType = PaintType.curvedLine,
  64. required this.userId,
  65. });
  66. void paint(Canvas canvas, Paint paint, double width, double height) {
  67. paint
  68. ..color = color
  69. ..isAntiAlias = true
  70. ..style = PaintingStyle.stroke
  71. ..strokeWidth = strokeWidth
  72. ..strokeCap = StrokeCap.round;
  73. List<Point> pointList = [];
  74. if (points.length < 10) {
  75. pointList = points;
  76. } else {
  77. for (int i = 0; i < points.length; i++) {
  78. if (i % 3 == 0) {
  79. pointList.add(points[i]);
  80. }
  81. }
  82. }
  83. canvas.drawPoints(
  84. PointMode.polygon,
  85. pointList.map<Offset>((e) => e.toOffset(width, height)).toList(),
  86. paint,
  87. );
  88. }
  89. }