-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
changeWidth.ts
53 lines (51 loc) · 1.8 KB
/
changeWidth.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { TransformActionHandler } from '../EventTypeDefs';
import { CENTER, LEFT, RIGHT } from '../constants';
import { getLocalPoint, isTransformCentered } from './util';
import { wrapWithFireEvent } from './wrapWithFireEvent';
import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';
/**
* Action handler to change object's width
* Needs to be wrapped with `wrapWithFixedAnchor` to be effective
* @param {Event} eventData javascript event that is doing the transform
* @param {Object} transform javascript object containing a series of information around the current transform
* @param {number} x current mouse x position, canvas normalized
* @param {number} y current mouse y position, canvas normalized
* @return {Boolean} true if some change happened
*/
export const changeObjectWidth: TransformActionHandler = (
eventData,
transform,
x,
y
) => {
const localPoint = getLocalPoint(
transform,
transform.originX,
transform.originY,
x,
y
);
// make sure the control changes width ONLY from it's side of target
if (
transform.originX === CENTER ||
(transform.originX === RIGHT && localPoint.x < 0) ||
(transform.originX === LEFT && localPoint.x > 0)
) {
const { target } = transform,
strokePadding =
target.strokeWidth / (target.strokeUniform ? target.scaleX : 1),
multiplier = isTransformCentered(transform) ? 2 : 1,
oldWidth = target.width,
newWidth = Math.ceil(
Math.abs((localPoint.x * multiplier) / target.scaleX) - strokePadding
);
target.set('width', Math.max(newWidth, 0));
// check against actual target width in case `newWidth` was rejected
return oldWidth !== target.width;
}
return false;
};
export const changeWidth = wrapWithFireEvent(
'resizing',
wrapWithFixedAnchor(changeObjectWidth)
);