notes/lib/canvas/drawing_page.dart

145 lines
4.7 KiB
Dart
Raw Normal View History

2022-10-24 00:02:35 +00:00
import 'dart:math';
import 'dart:ui';
import 'package:equations/equations.dart';
import 'package:flutter/material.dart';
import 'package:notes/canvas/my_painter.dart';
import 'document_types.dart';
2022-10-24 10:27:38 +00:00
class DrawingPage extends StatefulWidget {
const DrawingPage({Key? key}) : super(key: key);
2022-10-24 00:02:35 +00:00
@override
2022-10-24 10:27:38 +00:00
State<DrawingPage> createState() => _DrawingPageState();
2022-10-24 00:02:35 +00:00
}
2022-10-24 10:27:38 +00:00
class _DrawingPageState extends State<DrawingPage> {
2022-10-24 00:02:35 +00:00
List<Stroke> _strokes = [];
bool allowDrawWithFinger = false;
2022-10-24 10:27:38 +00:00
double zoom = 0.6;
Offset scrollPos = const Offset(.0, .0);
2022-10-24 00:02:35 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(backgroundColor: Colors.blueGrey),
body: _buildCanvas(),
);
}
double _calcTiltedWidth(double baseWidth, double tilt){
return baseWidth * tilt;
}
double _calcAngleDependentWidth(List<Point> pts, double basetickness){
2022-10-24 10:27:38 +00:00
return basetickness;
2022-10-24 00:02:35 +00:00
// todo do correct linear interpolation and extimate angle
final lininterpol = PolynomialInterpolation(nodes: pts.map((e) => InterpolationNode(x: e.point.dx, y: e.point.dy)).toList(growable: false));
lininterpol.compute(1.0);
print(lininterpol.buildPolynomial().toString());
// double angle = atan((pt2.dy - pt1.dy)/(pt2.dx - pt1.dx));
final angle = 5 / (2 * pi);
// print("pt1: ${pt1}, pt2: ${pt2}, angle: ${angle}");
return basetickness * (angle / .5 + .5);
}
Widget _buildCanvas() {
2022-10-24 10:27:38 +00:00
final width = MediaQuery.of(context).size.width;
final zoomedwidth = width * zoom;
2022-10-24 00:02:35 +00:00
return Scaffold(
body: Listener(
onPointerMove: (event) {
// print(event.tilt);
final pos = event.localPosition;
final pts = _strokes.last.points;
if(pts.last.point == pos) return;
if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) {
double newWidth = _calcTiltedWidth(3.0, event.tilt);
if(_strokes.last.points.length > 1){
// todo current point not in list
newWidth = _calcAngleDependentWidth(pts.getRange(pts.length - 10 >= 0 ? pts.length -10 : 0, pts.length).toList(growable: false), event.tilt);
}
setState(() {
_strokes = List.from(_strokes, growable: false)
..last.points.add(Point(pos, newWidth));
});
}
},
onPointerDown: (event) {
if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) {
setState(() {
_strokes = List.from(_strokes)
..add(Stroke.fromPoints([Point(event.localPosition, _calcTiltedWidth(3.0, event.tilt))]));
});
}
},
2022-10-24 10:27:38 +00:00
2022-10-24 00:02:35 +00:00
onPointerUp: (event) {
if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) {
if (_strokes.last.points.length <= 1) {
// if the line consists only of one point (point) add endpoint as the same to allow drawing a line
// todo maybe solve this in custompainter in future
setState(() {
_strokes = List.from(_strokes, growable: false)
..last.points.add(_strokes.last.points.last);
});
} else {
setState(() {});
}
print(_strokes.length);
print(_strokes.last.points.length);
}
},
// child: GestureDetector(
// onPanUpdate: (DragUpdateDetails details) {
// setState(() {
// _strokes = List.from(_strokes,growable: false)..last.points.add(details.localPosition);
// });
// },
// onPanEnd: (DragEndDetails details) {
// if(_strokes.last.points.length <= 1){
// // if the line consists only of one point (point) add endpoint as the same to allow drawing a line
// // todo maybe solve this in custompainter in future
// setState(() {
// _strokes = List.from(_strokes,growable: false)..last.points.add(_strokes.last.points.last);
// });
// }else{
// setState(() {});
// }
//
//
// print(_strokes.length);
// print(_strokes.last.points.length);
// },
// onPanStart: (details) {
// setState(() {
// _strokes = List.from(_strokes)..add(Stroke.fromPoints([details.localPosition]));
// });
// },
2022-10-24 10:27:38 +00:00
child: SingleChildScrollView(
child: Center(
child: CustomPaint(
painter: MyPainter(strokes: _strokes),
// todo not working
size: Size(zoomedwidth, zoomedwidth * sqrt2), // todo add different paper dimensions
// ),
),
),
2022-10-24 00:02:35 +00:00
),
),
);
}
}