123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import 'dart:math';
- import 'dart:ui';
- import 'package:flutter/material.dart';
- /// 数据类型定义
- /// 绘制类型:直线、曲线
- enum PaintType { straightLine, curvedLine }
- /// 绘制状态:正在绘制、已完成、隐藏
- enum PaintState { doing, done, hide }
- /// Point 点类
- class Point {
- final double x;
- final double y;
- const Point({
- required this.x,
- required this.y,
- });
- factory Point.fromOffset(Offset offset, double width, double height) {
- return Point(
- x: double.parse((offset.dx / width).toStringAsFixed(4)),
- y: double.parse((offset.dy / height).toStringAsFixed(4)),
- );
- }
- factory Point.fromJson(
- Map<String, dynamic> json, double width, double height) {
- return Point(
- x: json['x'],
- y: json['y'],
- );
- }
- factory Point.fromList(List<dynamic> offSetList) {
- return Point(
- x: offSetList[0],
- y: offSetList[1],
- );
- }
- List<double> toList() {
- final List<double> offestList = [];
- offestList.addAll([x, y]);
- return offestList;
- }
- Map<String, dynamic> toJson(width, height) {
- final json = Map<String, dynamic>();
- json['x'] = x;
- json['y'] = y;
- return json;
- }
- double get distance => sqrt(x * x + y * y);
- // Point operator -(Point other) => Point(x: x - other.x, y: y - other.y);
- Offset toOffset(double width, double height) => Offset(x * width, y * height);
- }
- /// Line 线类
- class Line {
- List<Point> points = [];
- PaintState state;
- PaintType paintType;
- double strokeWidth;
- Color color;
- String userId;
- Line({
- this.color = Colors.black,
- this.strokeWidth = 1,
- this.state = PaintState.doing,
- this.paintType = PaintType.curvedLine,
- required this.userId,
- });
- void paint(Canvas canvas, Paint paint, double width, double height) {
- paint
- ..color = color
- ..isAntiAlias = true
- ..style = PaintingStyle.stroke
- ..strokeWidth = strokeWidth
- ..strokeCap = StrokeCap.round;
- List<Point> pointList = [];
- if (points.length < 10) {
- pointList = points;
- } else {
- for (int i = 0; i < points.length; i++) {
- if (i % 3 == 0) {
- pointList.add(points[i]);
- }
- }
- }
- canvas.drawPoints(
- PointMode.polygon,
- pointList.map<Offset>((e) => e.toOffset(width, height)).toList(),
- paint,
- );
- }
- }
|