Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
steveruizok committed Sep 7, 2022
1 parent 8686c06 commit 6ae88f4
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ The options object is optional, as are each of its properties.
| `easing` | function | t => t | An easing function to apply to each point's pressure. |
| `start` | { } | | Tapering options for the start of the line. |
| `end` | { } | | Tapering options for the end of the line. |
| `last` | boolean | false | Whether the stroke is complete. |
| `last` | boolean | true | Whether the stroke is complete. |

**Note:** When the `last` property is `true`, the line's end will be drawn at the last input point, rather than slightly behind it.

Expand Down Expand Up @@ -262,20 +262,30 @@ While `getStroke` returns an array of points representing the outline of a strok
The function below will turn the points returned by `getStroke` into SVG path data.
```js
const average = (a, b) => (a + b) / 2
function getSvgPathFromStroke(stroke) {
if (!stroke.length) return ''
const d = stroke.reduce(
(acc, [x0, y0], i, arr) => {
const [x1, y1] = arr[(i + 1) % arr.length]
acc.push(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2)
return acc
},
['M', ...stroke[0], 'Q']
)
const len = points.length
d.push('Z')
return d.join(' ')
if (!len) {
return ''
}
const first = points[0]
let result = `M${first[0].toFixed(3)},${first[1].toFixed(3)}Q`
for (let i = 0, max = len - 1; i < max; i++) {
const a = points[i]
const b = points[i + 1]
result += `${a[0].toFixed(3)},${a[1].toFixed(3)} ${average(
a[0],
b[0]
).toFixed(3)},${average(a[1], b[1]).toFixed(3)} `
}
result += 'Z'
return result
}
```
Expand Down

0 comments on commit 6ae88f4

Please sign in to comment.