This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function throttle<T extends (...args: any[]) => any>( | |
fn: T, | |
wait: number | |
) { | |
let inThrottle: boolean, lastFn: any, lastTime: number | |
return function(...a: Parameters<T>): ReturnType<T> { | |
const context = this, | |
args = arguments | |
if (!inThrottle) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the minimum distance from a point P to a line with a segment AB. | |
* @param A The start of the line. | |
* @param B The end of the line. | |
* @param P A point. | |
* @returns | |
*/ | |
export function distanceToLine(A: number[], B: number[], P: number[]) { | |
const delta = sub(B, A) | |
const angle = Math.atan2(delta[1], delta[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBezierCurveSegments(points: number[][], tension = 0.4) { | |
const len = points.length, | |
cpoints: number[][] = points.slice(0) | |
if (len < 2) { | |
throw Error('Curve must have at least two points.') | |
} | |
for (let i = 1; i < len - 1; i++) { | |
let pp = points[i - 1], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function interpolateCubicBezier( | |
p0: { x: number; y: number }, | |
c0: { x: number; y: number }, | |
c1: { x: number; y: number }, | |
p1: { x: number; y: number } | |
) { | |
// 0 <= t <= 1 | |
return function interpolator(t: number) { | |
return [ | |
pow(1 - t, 3) * p0.x + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getOuterTangents( | |
x0: number, | |
y0: number, | |
r0: number, | |
x1: number, | |
y1: number, | |
r1: number | |
) { | |
const dx = x1 - x0, | |
dy = y1 - y0, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Use this with canvas.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, px, py). | |
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo | |
Uses a Catmull-Rom spline (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline). | |
*/ | |
/** | |
* Get a bezier curve data to for a spline that fits an array of points. | |
* @param points An array of points formatted as [x, y] | |
* @param k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get unique values from an array of primitives | |
export function unique<T extends any>(arr: T[]) { | |
return Array.from(new Set(arr).values()) | |
} | |
export function uniqueAdjacent<T extends any>(arr: T[]) { | |
return arr.filter((t, i) => i === 0 || !(t === arr[i - 1])) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pointsAreClockwise(A: number[], B: number[], C: number[]) { | |
return (B[0] - A[0]) * (C[1] - A[1]) - (C[0] - A[0]) * (B[1] - A[1]) > 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simplify a line (using Ramer-Douglas-Peucker algorithm). | |
* @param points An array of points as [x, y, ...][] | |
* @param tolerance The minimum line distance (also called epsilon). | |
* @returns Simplified array as [x, y, ...][] | |
*/ | |
export function simplify(points: number[][], tolerance = 1) { | |
const len = points.length, | |
a = points[0], | |
b = points[len - 1], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getConcatenatedCodeFromWorker(...args: any[]) { | |
return "..." | |
} | |
async function getRunnableBundle< | |
Files extends Record<string, string>, | |
Entry extends keyof Files | |
>({ | |
files, | |
entry, |