Visualize Your Ideas With Code
feat: add DEG2RAD and RAD2DEG constants (#630)
Jacob01801e876605
diff --git a/packages/2d/src/components/Circle.ts b/packages/2d/src/components/Circle.tsindex 00e105f2..4da92db7 100644--- a/packages/2d/src/components/Circle.ts+++ b/packages/2d/src/components/Circle.ts@@ -1,6 +1,7 @@import {Shape, ShapeProps} from './Shape';import {SignalValue, SimpleSignal} from '@motion-canvas/core/lib/signals';import {initial, signal} from '../decorators';+import {DEG2RAD} from '@motion-canvas/core/lib/utils';export interface CircleProps extends ShapeProps {/**@@ -157,8 +158,8 @@ export class Circle extends Shape {protected createPath(expand = 0) {const path = new Path2D();- const start = (this.startAngle() / 180) * Math.PI;- const end = (this.endAngle() / 180) * Math.PI;+ const start = this.startAngle() * DEG2RAD;+ const end = this.endAngle() * DEG2RAD;const size = this.size().scale(0.5);const closed = this.closed();if (closed) {diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.tsindex 8b6924f6..fc0537ca 100644--- a/packages/2d/src/components/Node.ts+++ b/packages/2d/src/components/Node.ts@@ -171,7 +171,7 @@ export class Node implements Promisable<Node> {protected getAbsoluteRotation() {const matrix = this.localToWorld();- return (Math.atan2(matrix.m12, matrix.m11) * 180) / Math.PI;+ return Vector2.degrees(matrix.m11, matrix.m12);}protected setAbsoluteRotation(value: SignalValue<number>) {diff --git a/packages/core/src/types/Matrix.ts b/packages/core/src/types/Matrix.tsindex be41e943..83c33aed 100644--- a/packages/core/src/types/Matrix.ts+++ b/packages/core/src/types/Matrix.ts@@ -1,11 +1,9 @@import {Vector2} from './Vector';export function transformAngle(angle: number, matrix: DOMMatrix) {- const radians = (angle / 180) * Math.PI;- const vector = Vector2.fromRadians(radians).transform(matrix);- return (vector.radians * 180) / Math.PI;+ return Vector2.fromDegrees(angle).transform(matrix).degrees;}export function transformScalar(scalar: number, matrix: DOMMatrix) {- return Math.sqrt(matrix.m11 * matrix.m11 + matrix.m12 * matrix.m12) * scalar;+ return Vector2.magnitude(matrix.m11, matrix.m12) * scalar;}diff --git a/packages/core/src/types/Matrix2D.ts b/packages/core/src/types/Matrix2D.tsindex 69e060ce..aa8762f5 100644--- a/packages/core/src/types/Matrix2D.ts+++ b/packages/core/src/types/Matrix2D.ts@@ -1,5 +1,6 @@import {Type, EPSILON} from './Type';import {PossibleVector2, Vector2} from './Vector';+import {DEG2RAD} from '../utils';export type PossibleMatrix2D =| Matrix2D@@ -101,10 +102,10 @@ export class Matrix2D implements Type {}public get rotation(): number {- return (Math.atan2(this.values[1], this.values[0]) * 180) / Math.PI;+ return Vector2.degrees(this.values[0], this.values[1]);}- public set translation(translation: PossibleVector2<number>) {+ public set translation(translation: PossibleVector2) {const vec = new Vector2(translation);this.values[4] = vec.x;this.values[5] = vec.y;@@ -421,12 +422,12 @@ export class Matrix2D implements Type {* // )* ```*- * @param angle - The angle by which to rotate the matrix- * @param radians - Whether the angle is provided in radians+ * @param angle - The angle by which to rotate the matrix.+ * @param degrees - Whether the angle is provided in degrees.*/public rotate(angle: number, degrees = true): Matrix2D {if (degrees) {- angle = (angle * Math.PI) / 180;+ angle *= DEG2RAD;}const a0 = this.values[0],diff --git a/packages/core/src/types/Vector.ts b/packages/core/src/types/Vector.tsindex 9ebd4c4a..8e5d9d1b 100644--- a/packages/core/src/types/Vector.ts+++ b/packages/core/src/types/Vector.ts@@ -8,6 +8,7 @@ import {Signal,SignalValue,} from '../signals';+import {DEG2RAD, RAD2DEG} from '../utils';export type SerializedVector2<T = number> = {x: T;@@ -118,8 +119,7 @@ export class Vector2 implements Type {}public static fromDegrees(degrees: number) {- const radians = (degrees * Math.PI) / 180;- return Vector2.fromRadians(radians);+ return Vector2.fromRadians(degrees * DEG2RAD);}/**@@ -144,7 +144,7 @@ export class Vector2 implements Type {* The returned angle will be between -180 and 180 degrees.*/public static degrees(x: number, y: number) {- return (Vector2.radians(x, y) * 180) / Math.PI;+ return Vector2.radians(x, y) * RAD2DEG;}public static magnitude(x: number, y: number) {diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.tsindex 617b155c..b7ff27f7 100644--- a/packages/core/src/utils/index.ts+++ b/packages/core/src/utils/index.ts@@ -11,6 +11,7 @@ export * from './deprecate';export * from './DetailedError';export * from './errorToLog';export * from './getContext';+export * from './math';export * from './range';export * from './Semaphore';export * from './usePlayback';diff --git a/packages/core/src/utils/math.ts b/packages/core/src/utils/math.tsnew file mode 100644index 00000000..08910d0d--- /dev/null+++ b/packages/core/src/utils/math.ts@@ -0,0 +1,15 @@+/**+ * A constant for converting radians to degrees+ *+ * @example+ * const degrees = 0.6 * RAD2DEG;+ */+export const RAD2DEG = 180 / Math.PI;++/**+ * A constant for converting degrees to radians+ *+ * @example+ * const radians = 30 * DEG2RAD;+ */+export const DEG2RAD = Math.PI / 180;