Skip to content

Commit

Permalink
Add offset functions
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Nov 30, 2023
1 parent 6a297cc commit c57689a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/bezier-js": "^4.1.3",
"bezier-js": "^6.1.4",
"linearly": "^0.21.0",
"paper": "^0.12.17"
"paper": "^0.12.17",
"paperjs-offset": "^1.0.8"
}
}
67 changes: 67 additions & 0 deletions src/Path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {mat2d, scalar, vec2} from 'linearly'
import paper from 'paper'
import {OffsetOptions as PaperOffsetOptions, PaperOffset} from 'paperjs-offset'

import {Bezier} from './Bezier'
import {memoize, toFixedSimple} from './utils'
Expand Down Expand Up @@ -208,6 +209,72 @@ export namespace Path {
return length
})

interface OffsetOptions {
/**
* The join style of offset path
* @defaultValue 'miter'
*/
join?: CanvasLineJoin
/**
* The limit for miter style
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/miterLimit
* @defaultValue 10
*/
miterLimit?: number
}

/**
* Creates an offset path from the given path.
* @param path The path to offset
* @param offset The width of offset
* @param options The options
* @returns The newly created path
*/
export function offset(
path: Path,
offset: number,
options?: OffsetOptions
): Path {
const paperPath = toPaperPath(path)

const _options: PaperOffsetOptions = {
...options,
limit: options?.miterLimit,
}

return fromPaperPath(PaperOffset.offset(paperPath, offset, _options))
}

interface OffsetStrokeOptions extends OffsetOptions {
/**
* The cap style of offset path (`'square'` will be supported in future)
* @defaultValue 'butt'
*/
cap?: 'butt' | 'round'
}

/**
* Creates an offset path from the given path.
* @param path The path to offset
* @param offset The width of stroke
* @param options The options
* @returns The newly created path
*/
export function offsetStroke(
path: Path,
offset: number,
options?: OffsetStrokeOptions
) {
const paperPath = toPaperPath(path)

const _options: PaperOffsetOptions = {
...options,
limit: options?.miterLimit,
}

return fromPaperPath(PaperOffset.offsetStroke(paperPath, offset, _options))
}

/**
* Creates a rectangle path from the given two points.
* @param start The first point defining the rectangle
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3084,11 +3084,18 @@ p-try@^2.0.0:
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==

paper@^0.12.17:
paper@^0.12.17, paper@^0.12.4:
version "0.12.17"
resolved "https://registry.yarnpkg.com/paper/-/paper-0.12.17.tgz#57215615f3db5a32826ab60d3e08bc7953614052"
integrity sha512-oCe+e1C2w8hKIcGoAqUjD0GGxGPv+itrRXlEFUmp3H8tY/NTnHOkYgpJFPGw6OJ8Q1Wa6+RgzlY7Dx/2WWHtkA==

paperjs-offset@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/paperjs-offset/-/paperjs-offset-1.0.8.tgz#3ddb297785a45b99126e3da3bca852e3612b3d68"
integrity sha512-rUXMtHH4bTNVrCgDcu+x71qyGoPe+01yZtrPD7bowji4VfWiDWzl4EUdRxiZnG5ooPg7b07mk6jVx33hNSojaA==
dependencies:
paper "^0.12.4"

parent-module@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
Expand Down

0 comments on commit c57689a

Please sign in to comment.