apply curve smoothing to drawn lines
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 10s
Python / lint-format (pull_request) Successful in 17s
Python / test (pull_request) Successful in 29s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m10s

This commit is contained in:
2026-06-20 20:09:03 +02:00
parent e619632077
commit 8a87f86758
3 changed files with 46 additions and 10 deletions
+1 -4
View File
@@ -187,10 +187,7 @@ Item {
return;
}
if (!drawingHandler.point.pressedButtons) {
root.drawing.content.showHover(x, y);
return;
}
root.drawing.content.showHover(x, y);
}
if (!root.visibilities.bar && Config.barConfig.autoHide && y < root.bar.implicitHeight)
+24 -2
View File
@@ -97,9 +97,31 @@ void StrokeCanvasItem::beginStroke(qreal x, qreal y) {
}
void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
if (shouldAddPoint(m_currentStroke.points, {x, y}, 2.0))
m_currentStroke.points.append({x, y});
const QPointF incoming{x, y};
if (!shouldAddPoint(m_currentStroke.points, incoming, 2.0))
return;
QPointF smoothed;
if (m_currentStroke.points.isEmpty()) {
smoothed = incoming;
} else {
const QPointF last = m_currentStroke.points.last();
const QPointF delta = incoming - last;
const qreal dist = std::sqrt(QPointF::dotProduct(delta, delta));
constexpr qreal minDist = 6.0;
constexpr qreal maxDist = 20.0;
constexpr qreal minAlpha = 0.1;
constexpr qreal maxAlpha = 0.3;
const qreal t = std::clamp((dist - minDist) / (maxDist - minDist), 0.0, 1.0);
const qreal alpha = minAlpha + t * (maxAlpha - minAlpha);
smoothed = last * (1.0 - alpha) + incoming * alpha;
}
m_currentStroke.points.append(smoothed);
update();
}
@@ -25,14 +25,31 @@ static void drawStroke(
return;
}
auto catmullToBezier = [](
const QPointF &p0, const QPointF &p1,
const QPointF &p2, const QPointF &p3,
float tension,
QPointF &cp1, QPointF &cp2)
{
cp1 = p1 + (p2 - p0) * tension / 3.0f;
cp2 = p2 - (p3 - p1) * tension / 3.0f;
};
const float tension = 0.5f; // increase toward 1.0 for tighter curves
painter->beginPath();
painter->moveTo(points[0]);
for (int i = 1; i < points.size() - 1; ++i) {
QPointF mid = (points[i] + points [i + 1]) / 2;
painter->quadraticCurveTo(points[i], mid);
for (int i = 0; i < points.size() - 1; ++i) {
const QPointF &p0 = points[qMax(i - 1, 0)];
const QPointF &p1 = points[i];
const QPointF &p2 = points[i + 1];
const QPointF &p3 = points[qMin(i + 2, points.size() - 1)];
QPointF cp1, cp2;
catmullToBezier(p0, p1, p2, p3, tension, cp1, cp2);
painter->bezierCurveTo(cp1, cp2, p2);
}
painter->lineTo(points.last());
painter->stroke();
}