Update utils.ts (#989)

This commit is contained in:
Steve Ruiz 2022-09-19 16:54:53 +01:00 committed by GitHub
parent 0e8e000f79
commit 31f0f02adf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1335,28 +1335,34 @@ left past the initial left edge) then swap points on that axis.
/** /**
* Turn an array of points into a path of quadradic curves. * Turn an array of points into a path of quadradic curves.
* @param points - the points returned from perfect-freehand *
* @param points The points returned from perfect-freehand
* @param closed Whether the stroke is closed
*/ */
static getSvgPathFromStroke(points: number[][]): string { static getSvgPathFromStroke(points: number[][], closed = true): string {
const len = points.length const len = points.length
if (!len) { if (len < 4) {
return '' return ``
} }
const first = points[0] let a = points[0]
let result = `M${first[0].toFixed(3)},${first[1].toFixed(3)}Q` let b = points[1]
const c = points[2]
for (let i = 0, max = len - 1; i < max; i++) { let result = `M${a[0].toFixed(2)},${a[1].toFixed(2)} Q${b[0].toFixed(2)},${b[1].toFixed(
const a = points[i] 2
const b = points[i + 1] )} ${average(b[0], c[0]).toFixed(2)},${average(b[1], c[1]).toFixed(2)} T`
result += `${a[0].toFixed(3)},${a[1].toFixed(3)} ${average(a[0], b[0]).toFixed(3)},${average(
a[1], for (let i = 2, max = len - 1; i < max; i++) {
b[1] a = points[i]
).toFixed(3)} ` b = points[i + 1]
result += `${average(a[0], b[0]).toFixed(2)},${average(a[1], b[1]).toFixed(2)} `
} }
if (closed) {
result += 'Z' result += 'Z'
}
return result return result
} }
@ -1365,23 +1371,29 @@ left past the initial left edge) then swap points on that axis.
* Turn an array of stroke points into a path of quadradic curves. * Turn an array of stroke points into a path of quadradic curves.
* @param points - the stroke points returned from perfect-freehand * @param points - the stroke points returned from perfect-freehand
*/ */
static getSvgPathFromStrokePoints(points: StrokePoint[]): string { static getSvgPathFromStrokePoints(points: StrokePoint[], closed = false): string {
const len = points.length const len = points.length
if (!len) { if (len < 4) {
return '' return ``
} }
const first = points[0].point let a = points[0].point
let result = `M${first[0].toFixed(3)},${first[1].toFixed(3)}Q` let b = points[1].point
const c = points[2].point
for (let i = 0, max = len - 1; i < max; i++) { let result = `M${a[0].toFixed(2)},${a[1].toFixed(2)} Q${b[0].toFixed(2)},${b[1].toFixed(
const a = points[i].point 2
const b = points[i + 1].point )} ${average(b[0], c[0]).toFixed(2)},${average(b[1], c[1]).toFixed(2)} T`
result += `${a[0].toFixed(3)},${a[1].toFixed(3)} ${average(a[0], b[0]).toFixed(3)},${average(
a[1], for (let i = 2, max = len - 1; i < max; i++) {
b[1] a = points[i].point
).toFixed(3)} ` b = points[i + 1].point
result += `${average(a[0], b[0]).toFixed(2)},${average(a[1], b[1]).toFixed(2)} `
}
if (closed) {
result += 'Z'
} }
return result return result