improve pen thicknes calculations

This commit is contained in:
lukas-heiligenbrunner 2022-10-28 17:09:14 +02:00
parent 327cc1bf02
commit ecfc894214

View File

@ -1,3 +1,4 @@
import 'dart:math';
import 'dart:ui'; import 'dart:ui';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:notes/canvas/my_painter.dart'; import 'package:notes/canvas/my_painter.dart';
@ -52,26 +53,26 @@ class _DrawingPageState extends State<DrawingPage> {
} }
double _calcTiltedWidth(double baseWidth, double tilt) { double _calcTiltedWidth(double baseWidth, double tilt) {
if(tilt == .0) return baseWidth;
return baseWidth * tilt; return baseWidth * tilt;
} }
double _calcAngleDependentWidth(List<Point> pts, double basetickness) { double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) {
return basetickness; double dx = pt2.point.dx - pt1.point.dx;
double dy = pt2.point.dy - pt1.point.dy;
// todo do correct linear interpolation and extimate angle // todo those deltas to small to get an accurate direction!
// final lininterpol = PolynomialInterpolation(
// nodes: pts double alpha = atan(dx / dy);
// .map((e) => InterpolationNode(x: e.point.dx, y: e.point.dy)) // alpha has range from 0 - 2pi
// .toList(growable: false)); // we want 0.5 -1;
// lininterpol.compute(1.0);
// print(lininterpol.buildPolynomial().toString()); alpha /= (2 * pi * 2);
// alpha += .5;
// // double angle = atan((pt2.dy - pt1.dy)/(pt2.dx - pt1.dx));
//
// final angle = 5 / (2 * pi); double thickness = basetickness * alpha;
// // print("pt1: ${pt1}, pt2: ${pt2}, angle: ${angle}"); return thickness;
//
// return basetickness * (angle / .5 + .5);
} }
// calculate new page offset from mousepointer delta // calculate new page offset from mousepointer delta
@ -116,17 +117,12 @@ class _DrawingPageState extends State<DrawingPage> {
if (pts.last.point == pos) return; if (pts.last.point == pos) return;
double newWidth = _calcTiltedWidth(3.0, event.tilt); double newWidth = _calcTiltedWidth(5.0, event.tilt);
if (_strokes.last.points.length > 1) { if (_strokes.last.points.length > 1) {
// todo current point not in list
newWidth = _calcAngleDependentWidth( newWidth = _calcAngleDependentWidth(
pts pts.last, pts[pts.length - 2], newWidth);
.getRange(
pts.length - 10 >= 0 ? pts.length - 10 : 0, pts.length)
.toList(growable: false),
event.tilt);
} }
setState(() { setState(() {
_strokes = List.from(_strokes, growable: false) _strokes = List.from(_strokes, growable: false)
..last.addPoint(Point(pos, newWidth)); ..last.addPoint(Point(pos, newWidth));