mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Attempt at optimizing focus point calc
This commit is contained in:
parent
5cbd6e63a0
commit
34ec751501
3 changed files with 63 additions and 24 deletions
|
@ -46,8 +46,11 @@ export function pointFromPair<Point extends GenericPoint>(
|
|||
* @param v The vector to convert
|
||||
* @returns The point the vector points at with origin 0,0
|
||||
*/
|
||||
export function pointFromVector<P extends GenericPoint>(v: Vector): P {
|
||||
return v as unknown as P;
|
||||
export function pointFromVector<P extends GenericPoint>(
|
||||
v: Vector,
|
||||
offset: P = point(0, 0),
|
||||
): P {
|
||||
return point<P>(offset[0] + v[0], offset[1] + v[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,9 +99,12 @@ export function pointRotateRads<Point extends GenericPoint>(
|
|||
[cx, cy]: Point,
|
||||
angle: Radians,
|
||||
): Point {
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
|
||||
return point(
|
||||
(x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
|
||||
(x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy,
|
||||
(x - cx) * cos - (y - cy) * sin + cx,
|
||||
(x - cx) * sin + (y - cy) * cos + cy,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import type { GenericPoint, Vector } from "./types";
|
||||
import type { GenericPoint, Radians, Vector } from "./types";
|
||||
|
||||
/**
|
||||
* Create a vector from the x and y coordiante elements.
|
||||
|
@ -139,3 +139,16 @@ export const vectorNormalize = (v: Vector): Vector => {
|
|||
|
||||
return vector(v[0] / m, v[1] / m);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rotate a vector by the given radians
|
||||
* @param v Target vector
|
||||
* @param a Angle to rotate in radians
|
||||
* @returns The rotated vector
|
||||
*/
|
||||
export const vectorRotate = (v: Vector, a: Radians): Vector => {
|
||||
const cos = Math.cos(a);
|
||||
const sin = Math.sin(a);
|
||||
|
||||
return vector(v[0] * cos - v[1] * sin, v[0] * sin + v[1] * cos);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue