-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathwrapWithFixedAnchor.ts
26 lines (25 loc) · 1.07 KB
/
wrapWithFixedAnchor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import type { Transform, TransformActionHandler } from '../EventTypeDefs';
/**
* Wrap an action handler with saving/restoring object position on the transform.
* this is the code that permits to objects to keep their position while transforming.
* @param {Function} actionHandler the function to wrap
* @return {Function} a function with an action handler signature
*/
export function wrapWithFixedAnchor<T extends Transform>(
actionHandler: TransformActionHandler<T>
) {
return ((eventData, transform, x, y) => {
const { target, originX, originY } = transform,
centerPoint = target.getRelativeCenterPoint(),
constraint = target.translateToOriginPoint(centerPoint, originX, originY),
actionPerformed = actionHandler(eventData, transform, x, y);
// flipping requires to change the transform origin, so we read from the mutated transform
// instead of leveraging the one destructured before
target.setPositionByOrigin(
constraint,
transform.originX,
transform.originY
);
return actionPerformed;
}) as TransformActionHandler<T>;
}