49 lines
1.7 KiB
Dart
49 lines
1.7 KiB
Dart
import 'dart:ui';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class TracingPainter extends CustomPainter {
|
|
final List<Offset> userPath; // 사용자가 그린 선
|
|
final List<Offset> targetPath; // 목표 가이드 선 (점들의 집합)
|
|
|
|
TracingPainter({required this.userPath, required this.targetPath});
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
// 1. 목표 가이드 라인 그리기 (회색 점선 스타일)
|
|
final guidePaint = Paint()
|
|
..color = Colors.grey.shade300
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = 24.0
|
|
..strokeCap = StrokeCap.round
|
|
..strokeJoin = StrokeJoin.round;
|
|
|
|
// 점들을 연결해서 경로 생성
|
|
if (targetPath.isNotEmpty) {
|
|
final path = Path()..moveTo(targetPath.first.dx, targetPath.first.dy);
|
|
for (int i = 1; i < targetPath.length; i++) {
|
|
path.lineTo(targetPath[i].dx, targetPath[i].dy);
|
|
}
|
|
// 닫힌 도형이면 마지막 점과 첫 점 연결 (필요시 옵션 처리)
|
|
// path.close();
|
|
canvas.drawPath(path, guidePaint);
|
|
}
|
|
|
|
// 2. 사용자가 그리는 선 (파란색 실선)
|
|
final userPaint = Paint()
|
|
..color = Colors.blueAccent.withOpacity(0.8)
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = 16.0
|
|
..strokeCap = StrokeCap.round
|
|
..strokeJoin = StrokeJoin.round;
|
|
|
|
if (userPath.isNotEmpty) {
|
|
// 포인트가 너무 많으면 성능 저하가 올 수 있으므로 PointMode.polygon 사용
|
|
canvas.drawPoints(PointMode.polygon, userPath, userPaint);
|
|
}
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(covariant TracingPainter oldDelegate) {
|
|
return true; // 실시간으로 계속 다시 그려야 함
|
|
}
|
|
} |