Skip to content

Commit

Permalink
improve line smoothing to avoid backward curve; fixes #4063
Browse files Browse the repository at this point in the history
  • Loading branch information
junedchhipa committed Oct 16, 2023
1 parent 385ce76 commit b1621d5
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/libs/monotone-cubic.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ export const svgPath = (points) => {

for (let i = 0; i < points.length; i++) {
const point = points[i]
const prevPoint = points[i - 1]
const n = point.length
const pn = prevPoint?.length

if (n > 4) {
p += `C${point[0]}, ${point[1]}`
p += `, ${point[2]}, ${point[3]}`
p += `, ${point[4]}, ${point[5]}`
} else if (n > 2) {
p += `S${point[0]}, ${point[1]}`
if (i > 1 && Math.abs(point[n - 2] - prevPoint[pn - 2]) < 30) {
// fallback to quadratic curve if the x distance is too small
// or if the curve goes backward too much
p += `Q${point[0]}, ${point[1]}`
p += `, ${point[2]}, ${point[3]}`
} else {
if (n > 4) {
p += `C${point[0]}, ${point[1]}`
p += `, ${point[2]}, ${point[3]}`
p += `, ${point[4]}, ${point[5]}`
} else if (n > 2) {
p += `S${point[0]}, ${point[1]}`
p += `, ${point[2]}, ${point[3]}`
}
}
}

Expand Down

0 comments on commit b1621d5

Please sign in to comment.