notes/lib/canvas/my_painter.dart

73 lines
1.9 KiB
Dart
Raw Normal View History

import 'dart:math';
2022-10-24 00:02:35 +00:00
import 'package:flutter/material.dart';
import 'paint_controller.dart';
import 'screen_document_mapping.dart';
2022-10-24 00:02:35 +00:00
final Rect a4Page =
Rect.fromPoints(const Offset(.0, .0), const Offset(210, 210 * sqrt2));
2022-10-24 00:02:35 +00:00
class MyPainter extends CustomPainter {
2022-10-24 14:28:56 +00:00
double zoom;
Offset offset;
Size canvasSize;
PaintController controller;
late Pen activePen;
2022-10-24 00:02:35 +00:00
MyPainter(
{required this.zoom,
required this.offset,
required this.canvasSize,
required this.controller})
: super(repaint: controller) {
activePen = controller.activePen;
controller.addListener(() {
activePen = controller.activePen;
});
}
2022-10-24 00:02:35 +00:00
Offset _translatept(Offset pt, Size canvasSize) {
final scale = calcPageDependentScale(zoom, a4Page, canvasSize);
return translateDocumentToScreenPoint(pt, scale, offset);
}
Paint backgroundPaint = Paint()..color = Colors.white;
2022-10-24 00:02:35 +00:00
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.blue
..strokeCap = StrokeCap.square;
// clipping canvas to reqired size
canvas.clipRect(Offset.zero & size);
canvas.drawColor(const Color(0xff6e6e6e), BlendMode.src);
canvas.drawRect(
Rect.fromPoints(_translatept(const Offset(0, .0), size),
_translatept(a4Page.bottomRight, size)),
backgroundPaint);
2022-10-24 14:28:56 +00:00
for (final stroke in controller.strokes) {
paint.color = stroke.color;
for (int i = 0; i < stroke.points.length - 1; i++) {
Offset pt1 = stroke.points[i].point;
pt1 = _translatept(pt1, size);
Offset pt2 = stroke.points[i + 1].point;
pt2 = _translatept(pt2, size);
2022-10-24 00:02:35 +00:00
canvas.drawLine(
2022-11-02 22:40:42 +00:00
pt1, pt2, paint..strokeWidth = stroke.points[i].thickness * zoom);
2022-10-24 00:02:35 +00:00
}
}
}
@override
bool shouldRepaint(MyPainter oldDelegate) {
return oldDelegate.zoom != zoom || oldDelegate.offset != offset;
2022-10-24 00:02:35 +00:00
}
}