-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
scaleSkew.ts
92 lines (88 loc) · 3.35 KB
/
scaleSkew.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
ControlCallback,
ControlCursorCallback,
TPointerEvent,
TransformActionHandler,
} from '../EventTypeDefs';
import type { FabricObject } from '../shapes/Object/FabricObject';
import { TAxisKey } from '../typedefs';
import { scaleCursorStyleHandler, scalingX, scalingY } from './scale';
import { skewCursorStyleHandler, skewHandlerX, skewHandlerY } from './skew';
function isAltAction(eventData: TPointerEvent, target: FabricObject) {
return eventData[target.canvas!.altActionKey!];
}
/**
* Inspect event, control and fabricObject to return the correct action name
* @param {Event} eventData the javascript event that is causing the scale
* @param {Control} control the control that is interested in the action
* @param {FabricObject} fabricObject the fabric object that is interested in the action
* @return {String} an action name
*/
export const scaleOrSkewActionName: ControlCallback<
TAxisKey<'skew' | 'scale'> | ''
> = (eventData, control, fabricObject) => {
const isAlternative = isAltAction(eventData, fabricObject);
if (control.x === 0) {
// then is scaleY or skewX
return isAlternative ? 'skewX' : 'scaleY';
}
if (control.y === 0) {
// then is scaleY or skewX
return isAlternative ? 'skewY' : 'scaleX';
}
return '';
};
/**
* Combine skew and scale style handlers to cover fabric standard use case
* @param {Event} eventData the javascript event that is causing the scale
* @param {Control} control the control that is interested in the action
* @param {FabricObject} fabricObject the fabric object that is interested in the action
* @return {String} a valid css string for the cursor
*/
export const scaleSkewCursorStyleHandler: ControlCursorCallback = (
eventData,
control,
fabricObject
) => {
return isAltAction(eventData, fabricObject)
? skewCursorStyleHandler(eventData, control, fabricObject)
: scaleCursorStyleHandler(eventData, control, fabricObject);
};
/**
* Composed action handler to either scale X or skew Y
* 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 scalingXOrSkewingY: TransformActionHandler = (
eventData,
transform,
x,
y
) => {
return isAltAction(eventData, transform.target)
? skewHandlerY(eventData, transform, x, y)
: scalingX(eventData, transform, x, y);
};
/**
* Composed action handler to either scale Y or skew X
* 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 scalingYOrSkewingX: TransformActionHandler = (
eventData,
transform,
x,
y
) => {
return isAltAction(eventData, transform.target)
? skewHandlerX(eventData, transform, x, y)
: scalingY(eventData, transform, x, y);
};