Convert stroke points to SVG path data
mainThe getStroke function returns an array of points representing the outline of a stroke. To render these points in an SVG, you can use a helper function to convert the points into an SVG path data string. This string can then be used in an <path d={pathData} /> element.
const average = (a, b) => (a + b) / 2
function getSvgPathFromStroke(points, closed = true) {
const len = points.length
if (len < 4) {
return ``
}
let a = points[0]
let b = points[1]
const c = points[2]
let result = `M${a[0].toFixed(2)},${a[1].toFixed(2)} Q${b[0].toFixed(
2
)},${b[1].toFixed(2)} ${average(b[0], c[0]).toFixed(2)},${average(
b[1],
c[1]
).toFixed(2)} T`
for (let i = 2, max = len - 1; i < max; i++) {
a = points[i]
b = points[i + 1]
result += `${average(a[0], b[0]).toFixed(2)},${average(
a[1], b[1]
).toFixed(2)} `
}
if (closed) {
result += 'Z'
}
return result
}
// Usage:
const outlinePoints = getStroke(inputPoints)
const pathData = getSvgPathFromStroke(outlinePoints)