@js-draw/math 1.22.0 → 1.24.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. package/dist/cjs/Color4.d.ts +24 -1
  2. package/dist/cjs/Color4.js +33 -1
  3. package/dist/cjs/Mat33.d.ts +20 -0
  4. package/dist/cjs/Mat33.js +23 -1
  5. package/dist/cjs/Vec3.d.ts +14 -5
  6. package/dist/cjs/Vec3.js +11 -2
  7. package/dist/cjs/lib.d.ts +3 -0
  8. package/dist/cjs/lib.js +3 -0
  9. package/dist/cjs/rounding/cleanUpNumber.js +1 -1
  10. package/dist/cjs/shapes/BezierJSWrapper.d.ts +9 -1
  11. package/dist/cjs/shapes/BezierJSWrapper.js +2 -0
  12. package/dist/cjs/shapes/Path.d.ts +1 -0
  13. package/dist/cjs/shapes/Path.js +1 -0
  14. package/dist/cjs/shapes/QuadraticBezier.d.ts +19 -2
  15. package/dist/cjs/shapes/QuadraticBezier.js +26 -3
  16. package/dist/cjs/shapes/Rect2.d.ts +13 -0
  17. package/dist/cjs/shapes/Rect2.js +22 -1
  18. package/dist/mjs/Color4.d.ts +24 -1
  19. package/dist/mjs/Color4.mjs +33 -1
  20. package/dist/mjs/Mat33.d.ts +20 -0
  21. package/dist/mjs/Mat33.mjs +23 -1
  22. package/dist/mjs/Vec3.d.ts +14 -5
  23. package/dist/mjs/Vec3.mjs +11 -2
  24. package/dist/mjs/lib.d.ts +3 -0
  25. package/dist/mjs/lib.mjs +3 -0
  26. package/dist/mjs/rounding/cleanUpNumber.mjs +1 -1
  27. package/dist/mjs/shapes/BezierJSWrapper.d.ts +9 -1
  28. package/dist/mjs/shapes/BezierJSWrapper.mjs +2 -0
  29. package/dist/mjs/shapes/Path.d.ts +1 -0
  30. package/dist/mjs/shapes/Path.mjs +1 -0
  31. package/dist/mjs/shapes/QuadraticBezier.d.ts +19 -2
  32. package/dist/mjs/shapes/QuadraticBezier.mjs +26 -3
  33. package/dist/mjs/shapes/Rect2.d.ts +13 -0
  34. package/dist/mjs/shapes/Rect2.mjs +22 -1
  35. package/dist-test/test_imports/test-require.cjs +1 -0
  36. package/package.json +3 -3
  37. package/src/Color4.test.ts +5 -0
  38. package/src/Color4.ts +39 -1
  39. package/src/Mat33.fromCSSMatrix.test.ts +4 -1
  40. package/src/Mat33.test.ts +6 -6
  41. package/src/Mat33.ts +23 -1
  42. package/src/Vec3.ts +14 -5
  43. package/src/lib.ts +3 -0
  44. package/src/rounding/cleanUpNumber.test.ts +2 -0
  45. package/src/rounding/cleanUpNumber.ts +1 -1
  46. package/src/shapes/BezierJSWrapper.ts +11 -4
  47. package/src/shapes/Path.ts +1 -0
  48. package/src/shapes/QuadraticBezier.ts +22 -2
  49. package/src/shapes/Rect2.ts +17 -0
@@ -4,6 +4,11 @@ import LineSegment2 from './LineSegment2';
4
4
  import Rect2 from './Rect2';
5
5
  import Parameterized2DShape from './Parameterized2DShape';
6
6
 
7
+ // The typings for Bezier are incorrect in some cases:
8
+ interface CorrectedBezierType extends Bezier {
9
+ dderivative(t: number): { x: number; y: number };
10
+ }
11
+
7
12
  /**
8
13
  * A lazy-initializing wrapper around Bezier-js.
9
14
  *
@@ -14,13 +19,13 @@ import Parameterized2DShape from './Parameterized2DShape';
14
19
  * @internal
15
20
  */
16
21
  export abstract class BezierJSWrapper extends Parameterized2DShape {
17
- #bezierJs: Bezier | null = null;
22
+ #bezierJs: CorrectedBezierType | null = null;
18
23
 
19
24
  protected constructor(bezierJsBezier?: Bezier) {
20
25
  super();
21
26
 
22
27
  if (bezierJsBezier) {
23
- this.#bezierJs = bezierJsBezier;
28
+ this.#bezierJs = bezierJsBezier as CorrectedBezierType;
24
29
  }
25
30
  }
26
31
 
@@ -29,7 +34,7 @@ export abstract class BezierJSWrapper extends Parameterized2DShape {
29
34
 
30
35
  protected getBezier() {
31
36
  if (!this.#bezierJs) {
32
- this.#bezierJs = new Bezier(this.getPoints().map((p) => p.xy));
37
+ this.#bezierJs = new Bezier(this.getPoints().map((p) => p.xy)) as CorrectedBezierType;
33
38
  }
34
39
  return this.#bezierJs;
35
40
  }
@@ -56,14 +61,16 @@ export abstract class BezierJSWrapper extends Parameterized2DShape {
56
61
  return Vec2.ofXY(this.getBezier().get(t));
57
62
  }
58
63
 
64
+ /** @returns the curve's directional derivative at `t`. */
59
65
  public derivativeAt(t: number): Point2 {
60
66
  return Vec2.ofXY(this.getBezier().derivative(t));
61
67
  }
62
68
 
63
69
  public secondDerivativeAt(t: number): Point2 {
64
- return Vec2.ofXY((this.getBezier() as any).dderivative(t));
70
+ return Vec2.ofXY(this.getBezier().dderivative(t));
65
71
  }
66
72
 
73
+ /** @returns the [normal vector](https://en.wikipedia.org/wiki/Normal_(geometry)) to this curve at `t`. */
67
74
  public normal(t: number): Vec2 {
68
75
  return Vec2.ofXY(this.getBezier().normal(t));
69
76
  }
@@ -11,6 +11,7 @@ import Parameterized2DShape from './Parameterized2DShape';
11
11
  import BezierJSWrapper from './BezierJSWrapper';
12
12
  import convexHull2Of from '../utils/convexHull2Of';
13
13
 
14
+ /** Identifiers for different path commands. These commands can make up a {@link Path}. */
14
15
  export enum PathCommandType {
15
16
  LineTo,
16
17
  MoveTo,
@@ -4,14 +4,34 @@ import BezierJSWrapper from './BezierJSWrapper';
4
4
  import Rect2 from './Rect2';
5
5
 
6
6
  /**
7
- * Represents a 2D Bézier curve.
7
+ * Represents a 2D [Bézier curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
8
8
  *
9
- * **Note**: Many Bézier operations use `bezier-js`'s.
9
+ * Example:
10
+ * ```ts,runnable,console
11
+ * import { QuadraticBezier, Vec2 } from '@js-draw/math';
12
+ *
13
+ * const startPoint = Vec2.of(4, 3);
14
+ * const controlPoint = Vec2.of(1, 1);
15
+ * const endPoint = Vec2.of(1, 3);
16
+ *
17
+ * const curve = new QuadraticBezier(
18
+ * startPoint,
19
+ * controlPoint,
20
+ * endPoint,
21
+ * );
22
+ *
23
+ * console.log('Curve:', curve);
24
+ * ```
25
+ *
26
+ * **Note**: Some Bézier operations internally use the `bezier-js` library.
10
27
  */
11
28
  export class QuadraticBezier extends BezierJSWrapper {
12
29
  public constructor(
30
+ // Start point
13
31
  public readonly p0: Point2,
32
+ // Control point
14
33
  public readonly p1: Point2,
34
+ // End point
15
35
  public readonly p2: Point2,
16
36
  ) {
17
37
  super();
@@ -17,6 +17,18 @@ export interface RectTemplate {
17
17
  /**
18
18
  * Represents a rectangle in 2D space, parallel to the XY axes.
19
19
  *
20
+ * **Example**:
21
+ * ```ts,runnable,console
22
+ * import { Rect2, Vec2 } from '@js-draw/math';
23
+ *
24
+ * const rect = Rect2.fromCorners(
25
+ * Vec2.of(0, 0),
26
+ * Vec2.of(10, 10),
27
+ * );
28
+ * console.log('area', rect.area);
29
+ * console.log('topLeft', rect.topLeft);
30
+ * ```
31
+ *
20
32
  * `invariant: w ≥ 0, h ≥ 0, immutable`
21
33
  */
22
34
  export class Rect2 extends Abstract2DShape {
@@ -28,9 +40,13 @@ export class Rect2 extends Abstract2DShape {
28
40
  public readonly area: number;
29
41
 
30
42
  public constructor(
43
+ // Top left x coordinate
31
44
  public readonly x: number,
45
+ // Top left y coordinate
32
46
  public readonly y: number,
47
+ // Width
33
48
  public readonly w: number,
49
+ // Height
34
50
  public readonly h: number,
35
51
  ) {
36
52
  super();
@@ -69,6 +85,7 @@ export class Rect2 extends Abstract2DShape {
69
85
  );
70
86
  }
71
87
 
88
+ /** @returns true iff `other` is completely within this `Rect2`. */
72
89
  public containsRect(other: Rect2): boolean {
73
90
  return (
74
91
  this.x <= other.x &&