Manually creating paths? #34
-
I'm trying to figure out how to use this library to create lines from some simple points that I generate algorithmically. However, it doesn't seem to deal well with minimal points that aren't curves. example:
I would expect a U shape, however what appears is a sharp V shape. 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Looks like if I add a point in between each of the existing four, closer to the next point, than the previous it creates the shape I expected...
|
Beta Was this translation helpful? Give feedback.
-
Hey @shendrick, good question! I've also been using the algo to manually create shapes and I often need to create many points between A and B. This is because the algorithm is tuned to the types of points that are generated when drawing, which tend to bunch at the start and end of the line. I've written a helper that I've been using to generate those kinds of points. It is available in function add = (A: number[], B: number[]): number[] => {
return [A[0] + B[0], A[1] + B[1]]
}
function sub = (A: number[], B: number[]): number[] => {
return [A[0] - B[0], A[1] - B[1]]
}
function mul = (A: number[], n: number): number[] => {
return [A[0] * n, A[1] * n]
}
function lrp = (A: number[], B: number[], t: number): number[] => {
return add(A, mul(sub(B, A), t))
}
/**
* Get an array of points (with simulated pressure) between two points.
* @param A The first point.
* @param B The second point.
* @param steps The number of points to return.
* @param ease An easing function to apply to the simulated pressure.
*/
function pointsBetween = (
A: number[],
B: number[],
steps = 6,
ease = (t: number) => t * t * t * t
): number[][] => {
return Array.from(Array(steps)).map((_, it) =>
[...lrp(A, B, i/steps), ease(i/steps)])
)
} Supporting more normal / sparse arrays of points would mean a significant adjustment to the algorithm, so I probably won't be changing it in that way. However, if I'm able to make the algorithm perform better with fewer input points, that could help in your case as well. |
Beta Was this translation helpful? Give feedback.
Hey @shendrick, good question! I've also been using the algo to manually create shapes and I often need to create many points between A and B. This is because the algorithm is tuned to the types of points that are generated when drawing, which tend to bunch at the start and end of the line. I've written a helper that I've been using to generate those kinds of points. It is available in
@tldraw/vec
but here's a snippet you can use, too.