Skip to content

Visualize Your Ideas With Code

feat(core): add rotate and polarLerp methods to vector (#756)

Kai Sassnowskia18bac3c1755
diff --git a/packages/core/src/types/Matrix2D.test.ts b/packages/core/src/types/Matrix2D.test.ts
index b863a4d7..e4270138 100644
--- a/packages/core/src/types/Matrix2D.test.ts
+++ b/packages/core/src/types/Matrix2D.test.ts
@@ -145,6 +145,26 @@ describe('Matrix2D', () => {
expect(matrix).toEqual(new Matrix2D(1, 0, 0, 5, 0, 0));
});
+ test('can get only the horizontal skewing', () => {
+ expect(a.skewX).toEqual(2);
+ });
+
+ test('can set only the horizontal skewing', () => {
+ const matrix = new Matrix2D();
+ matrix.skewX = 5;
+ expect(matrix).toEqual(new Matrix2D(1, 5, 0, 1, 0, 0));
+ });
+
+ test('can get only the vertical skewing', () => {
+ expect(a.skewY).toEqual(3);
+ });
+
+ test('can set only the vertical skewing', () => {
+ const matrix = new Matrix2D();
+ matrix.skewY = 5;
+ expect(matrix).toEqual(new Matrix2D(1, 0, 5, 1, 0, 0));
+ });
+
test('can set the scaling', () => {
const matrix = new Matrix2D(1, 0, 0, 1, 0, 0);
matrix.scaling = [2, 5];
diff --git a/packages/core/src/types/Matrix2D.ts b/packages/core/src/types/Matrix2D.ts
index aa8762f5..f25b15aa 100644
--- a/packages/core/src/types/Matrix2D.ts
+++ b/packages/core/src/types/Matrix2D.ts
@@ -4,6 +4,7 @@ import {DEG2RAD} from '../utils';
export type PossibleMatrix2D =
| Matrix2D
+ | DOMMatrix
| [number, number, number, number, number, number]
| [PossibleVector2, PossibleVector2, PossibleVector2]
| undefined;
@@ -60,12 +61,20 @@ export class Matrix2D implements Type {
return new Vector2(this.values[2], this.values[3]);
}
+ public set scaleX(value: number) {
+ this.values[0] = this.x.normalized.scale(value).x;
+ }
+
public get scaleX(): number {
return this.values[0];
}
- public set scaleX(value: number) {
- this.values[0] = this.x.normalized.scale(value).x;
+ public set skewX(value: number) {
+ this.values[1] = value;
+ }
+
+ public get skewX(): number {
+ return this.values[1];
}
public set scaleY(value: number) {
@@ -76,6 +85,14 @@ export class Matrix2D implements Type {
return this.values[3];
}
+ public set skewY(value: number) {
+ this.values[2] = value;
+ }
+
+ public get skewY(): number {
+ return this.values[2];
+ }
+
public set translateX(value: number) {
this.values[4] = value;
}
@@ -236,6 +253,16 @@ export class Matrix2D implements Type {
return;
}
+ if (a instanceof DOMMatrix) {
+ this.values[0] = a.m11;
+ this.values[1] = a.m12;
+ this.values[2] = a.m21;
+ this.values[3] = a.m22;
+ this.values[4] = a.m41;
+ this.values[5] = a.m42;
+ return;
+ }
+
if (a instanceof Matrix2D) {
this.values = a.values;
return;
diff --git a/packages/core/src/types/Vector.test.ts b/packages/core/src/types/Vector.test.ts
index 4a4f2704..a961df17 100644
--- a/packages/core/src/types/Vector.test.ts
+++ b/packages/core/src/types/Vector.test.ts
@@ -133,4 +133,40 @@ describe('Vector2', () => {
const vector = Vector2.fromDegrees(angle);
expect(vector.equals(new Vector2(expected as PossibleVector2))).toBe(true);
});
+
+ test.each([
+ [0, [5, 10]],
+ [30, [-0.669872981, 11.1602540378]],
+ [45, [-3.535533905, 10.6066017178]],
+ [60, [-6.160254037, 9.3301270189]],
+ [90, [-10, 5]],
+ [180, [-5, -10]],
+ [270, [10, -5]],
+ [360, [5, 10]],
+ [-90, [10, -5]],
+ [-180, [-5, -10]],
+ [-270, [-10, 5]],
+ ])('Rotates a vector around the origin: (%s°)', (angle, expected) => {
+ const vector = new Vector2(5, 10);
+ const result = vector.rotate(angle);
+ expect(result.equals(new Vector2(expected as PossibleVector2))).toBe(true);
+ });
+
+ test.each([
+ [[1, 0], 90, [1, -1]],
+ [[1, 0], -90, [1, 1]],
+ [[-1, 0], 90, [-1, 1]],
+ [[-1, 0], -90, [-1, -1]],
+ [[0, 1], 90, [1, 1]],
+ [[0, 1], -90, [-1, 1]],
+ [[0, -1], 90, [-1, -1]],
+ [[0, -1], -90, [1, -1]],
+ ])(
+ 'Rotates a vector around an arbitrary point: (%s, %s°)',
+ (center, angle, expected) => {
+ const vector = Vector2.zero;
+ const result = vector.rotate(angle, center as PossibleVector2);
+ expect(result).toEqual(new Vector2(expected as PossibleVector2));
+ },
+ );
});
diff --git a/packages/core/src/types/Vector.ts b/packages/core/src/types/Vector.ts
index c33528b6..9bca93d9 100644
--- a/packages/core/src/types/Vector.ts
+++ b/packages/core/src/types/Vector.ts
@@ -9,6 +9,7 @@ import {
SignalValue,
} from '../signals';
import {DEG2RAD, RAD2DEG} from '../utils';
+import {Matrix2D, PossibleMatrix2D} from './Matrix2D';
export type SerializedVector2<T = number> = {
x: T;
@@ -93,6 +94,95 @@ export class Vector2 implements Type {
Vector2.arcLerp(from, to, value, reverse, ratio);
}
+ /**
+ * Interpolates between two vectors on the polar plane by interpolating
+ * the angles and magnitudes of the vectors individually.
+ *
+ * @param from - The starting vector.
+ * @param to - The target vector.
+ * @param value - The t-value of the interpolation.
+ * @param counterclockwise - Whether the vector should get rotated
+ * counterclockwise. Defaults to `false`.
+ * @param origin - The center of rotation. Defaults to the origin.
+ *
+ * @remarks
+ * This function is useful when used in conjunction with {@link rotate} to
+ * animate an object's position on a circular arc (see examples).
+ *
+ * @example
+ * Animating an object in a circle around the origin
+ * ```tsx
+ * circle().position(
+ * circle().position().rotate(180),
+ * 1,
+ * easeInOutCubic,
+ * Vector2.polarLerp
+ * );
+ * ```
+ * @example
+ * Rotating an object around the point `[-200, 100]`
+ * ```ts
+ * circle().position(
+ * circle().position().rotate(180, [-200, 100]),
+ * 1,
+ * easeInOutCubic,
+ * Vector2.createPolarLerp(false, [-200, 100]),
+ * );
+ * ```
+ * @example
+ * Rotating an object counterclockwise around the origin
+ * ```ts
+ * circle().position(
+ * circle().position().rotate(180),
+ * 1,
+ * easeInOutCubic,
+ * Vector2.createPolarLerp(true),
+ * );
+ * ```
+ */
+ public static polarLerp(
+ from: Vector2,
+ to: Vector2,
+ value: number,
+ counterclockwise = false,
+ origin = Vector2.zero,
+ ) {
+ from = from.sub(origin);
+ to = to.sub(origin);
+
+ const fromAngle = from.degrees;
+ let toAngle = to.degrees;
+ const isCounterclockwise = fromAngle > toAngle;
+
+ if (isCounterclockwise !== counterclockwise) {
+ toAngle = toAngle + (counterclockwise ? -360 : 360);
+ }
+
+ const angle = map(fromAngle, toAngle, value) * DEG2RAD;
+ const magnitude = map(from.magnitude, to.magnitude, value);
+
+ return new Vector2(
+ magnitude * Math.cos(angle) + origin.x,
+ magnitude * Math.sin(angle) + origin.y,
+ );
+ }
+
+ /**
+ * Helper function to create a {@link Vector2.polarLerp} interpolation
+ * function with additional parameters.
+ *
+ * @param counterclockwise - Whether the point should get rotated
+ * counterclockwise.
+ * @param center - The center of rotation. Defaults to the origin.
+ */
+ public static createPolarLerp(
+ counterclockwise = false,
+ center: PossibleVector2 = Vector2.zero,
+ ) {
+ return (from: Vector2, to: Vector2, value: number) =>
+ Vector2.polarLerp(from, to, value, counterclockwise, new Vector2(center));
+ }
+
public static fromOrigin(origin: Origin | Direction) {
const position = new Vector2();
@@ -278,17 +368,21 @@ export class Vector2 implements Type {
return new Vector2(this.x * value, this.y * value);
}
- public transformAsPoint(matrix: DOMMatrix) {
+ public transformAsPoint(matrix: PossibleMatrix2D) {
+ const m = new Matrix2D(matrix);
+
return new Vector2(
- this.x * matrix.m11 + this.y * matrix.m21 + matrix.m41,
- this.x * matrix.m12 + this.y * matrix.m22 + matrix.m42,
+ this.x * m.scaleX + this.y * m.skewY + m.translateX,
+ this.x * m.skewX + this.y * m.scaleY + m.translateY,
);
}
- public transform(matrix: DOMMatrix) {
+ public transform(matrix: PossibleMatrix2D) {
+ const m = new Matrix2D(matrix);
+
return new Vector2(
- this.x * matrix.m11 + this.y * matrix.m21,
- this.x * matrix.m12 + this.y * matrix.m22,
+ this.x * m.scaleX + this.y * m.skewY,
+ this.x * m.skewX + this.y * m.scaleY,
);
}
@@ -327,6 +421,25 @@ export class Vector2 implements Type {
return new Vector2(this.x % vector.x, this.y % vector.y);
}
+ /**
+ * Rotates the vector around a point by the provided angle.
+ *
+ * @param angle - The angle by which to rotate in degrees.
+ * @param center - The center of rotation. Defaults to the origin.
+ */
+ public rotate(
+ angle: number,
+ center: PossibleVector2 = Vector2.zero,
+ ): Vector2 {
+ const originVector = new Vector2(center);
+
+ const matrix = Matrix2D.fromTranslation(originVector)
+ .rotate(angle)
+ .translate(originVector.flipped);
+
+ return this.transformAsPoint(matrix);
+ }
+
public addX(value: number) {
return new Vector2(this.x + value, this.y);
}
diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts
index 908dc395..07d326cb 100644
--- a/packages/core/vitest.config.ts
+++ b/packages/core/vitest.config.ts
@@ -5,5 +5,6 @@ export default defineConfig({
plugins: [markdownLiterals()],
test: {
environment: 'jsdom',
+ setupFiles: ['./vitest.setup.ts'],
},
});
diff --git a/packages/core/vitest.setup.ts b/packages/core/vitest.setup.ts
new file mode 100644
index 00000000..11041482
--- /dev/null
+++ b/packages/core/vitest.setup.ts
@@ -0,0 +1,3 @@
+import {vi} from 'vitest';
+
+vi.stubGlobal('DOMMatrix', class {});