Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Points argument array conversion/check #8

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,29 @@ export function getStrokePoints<
* ## getShortStrokeOutlinePoints
* @description Draw an outline around a short stroke.
* @param points An array of points (as `[x, y, pressure]` or `{x, y, pressure}`). Pressure is optional.
* @param options An (optional) object with options.
* @param options An (optional) object with stroke outline options.
*/
export function getShortStrokeOutlinePoints(
points: number[][],
export function getShortStrokeOutlinePoints<
T extends number[],
K extends { x: number; y: number; pressure?: number }
>(
points: (T | K)[],
options: StrokeOutlineOptions = {} as StrokeOutlineOptions
) {
const { minSize = 2.5, maxSize = 8 } = options
const len = points.length

const aPoints = toPointsArray(points)

const len = aPoints.length

// Can't draw an outline without any points
if (len === 0) {
return []
}

const [x0, y0] = points[0],
[x1, y1] = points[len - 1],
p = points[len - 1][3],
const [x0, y0] = aPoints[0],
[x1, y1] = aPoints[len - 1],
p = aPoints[len - 1][3],
leftPts: number[][] = [],
rightPts: number[][] = [],
size = clamp(
Expand All @@ -186,10 +192,13 @@ export function getShortStrokeOutlinePoints(
* ## getStrokeOutlinePoints
* @description Get an array of points (as `[x, y]`) representing the outline of a stroke.
* @param points An array of points (as `[x, y, pressure]` or `{x, y, pressure}`). Pressure is optional.
* @param options An (optional) object with options.
* @param options An (optional) object with stroke outline options.
*/
export function getStrokeOutlinePoints(
points: number[][],
export function getStrokeOutlinePoints<
T extends number[],
K extends { x: number; y: number; pressure?: number }
>(
points: (T | K)[],
options: StrokeOutlineOptions = {} as StrokeOutlineOptions
): number[][] {
const {
Expand All @@ -200,9 +209,11 @@ export function getStrokeOutlinePoints(
smooth = 8,
} = options

let len = points.length,
p0 = points[0],
p1 = points[0],
const aPoints = toPointsArray(points)

let len = aPoints.length,
p0 = aPoints[0],
p1 = aPoints[0],
t0 = p0,
t1 = p1,
m0 = p0,
Expand All @@ -224,8 +235,8 @@ export function getStrokeOutlinePoints(
// of the shape is determined by the pressure at each point.

for (let i = 1; i < len; i++) {
const [px, py, pa] = points[i - 1]
let [x, y, angle, ip, distance, clen] = points[i]
const [px, py, pa] = aPoints[i - 1]
let [x, y, angle, ip, distance, clen] = aPoints[i]

length += clen

Expand All @@ -245,7 +256,7 @@ export function getStrokeOutlinePoints(

// Handle line start
if (!started && length > size / 2) {
const [sx, sy] = points[0]
const [sx, sy] = aPoints[0]

for (let t = 0, step = 0.25; t <= 1; t += step) {
m0 = projectPoint(sx, sy, angle + TAU + t * PI, size - 1)
Expand Down