-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
skew.ts
243 lines (227 loc) · 7.71 KB
/
skew.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import type {
ControlCursorCallback,
TPointerEvent,
Transform,
TransformActionHandler,
} from '../EventTypeDefs';
import { resolveOrigin } from '../util/misc/resolveOrigin';
import { Point } from '../Point';
import type { TAxis, TAxisKey } from '../typedefs';
import {
degreesToRadians,
radiansToDegrees,
} from '../util/misc/radiansDegreesConversion';
import {
findCornerQuadrant,
getLocalPoint,
isLocked,
NOT_ALLOWED_CURSOR,
} from './util';
import { wrapWithFireEvent } from './wrapWithFireEvent';
import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';
import { CENTER } from '../constants';
export type SkewTransform = Transform & { skewingSide: -1 | 1 };
const AXIS_KEYS: Record<
TAxis,
{
counterAxis: TAxis;
scale: TAxisKey<'scale'>;
skew: TAxisKey<'skew'>;
lockSkewing: TAxisKey<'lockSkewing'>;
origin: TAxisKey<'origin'>;
flip: TAxisKey<'flip'>;
}
> = {
x: {
counterAxis: 'y',
scale: 'scaleX',
skew: 'skewX',
lockSkewing: 'lockSkewingX',
origin: 'originX',
flip: 'flipX',
},
y: {
counterAxis: 'x',
scale: 'scaleY',
skew: 'skewY',
lockSkewing: 'lockSkewingY',
origin: 'originY',
flip: 'flipY',
},
};
const skewMap = ['ns', 'nesw', 'ew', 'nwse'];
/**
* return the correct cursor style for the skew action
* @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 skewCursorStyleHandler: ControlCursorCallback = (
eventData,
control,
fabricObject
) => {
if (control.x !== 0 && isLocked(fabricObject, 'lockSkewingY')) {
return NOT_ALLOWED_CURSOR;
}
if (control.y !== 0 && isLocked(fabricObject, 'lockSkewingX')) {
return NOT_ALLOWED_CURSOR;
}
const n = findCornerQuadrant(fabricObject, control) % 4;
return `${skewMap[n]}-resize`;
};
/**
* Since skewing is applied before scaling, calculations are done in a scaleless plane
* @see https://github.com/fabricjs/fabric.js/pull/8380
*/
function skewObject(
axis: TAxis,
{ target, ex, ey, skewingSide, ...transform }: SkewTransform,
pointer: Point
) {
const { skew: skewKey } = AXIS_KEYS[axis],
offset = pointer
.subtract(new Point(ex, ey))
.divide(new Point(target.scaleX, target.scaleY))[axis],
skewingBefore = target[skewKey],
skewingStart = transform[skewKey],
shearingStart = Math.tan(degreesToRadians(skewingStart)),
// let a, b be the size of target
// let a' be the value of a after applying skewing
// then:
// a' = a + b * skewA => skewA = (a' - a) / b
// the value b is tricky since skewY is applied before skewX
b =
axis === 'y'
? target._getTransformedDimensions({
scaleX: 1,
scaleY: 1,
// since skewY is applied before skewX, b (=width) is not affected by skewX
skewX: 0,
}).x
: target._getTransformedDimensions({
scaleX: 1,
scaleY: 1,
}).y;
const shearing =
(2 * offset * skewingSide) /
// we max out fractions to safeguard from asymptotic behavior
Math.max(b, 1) +
// add starting state
shearingStart;
const skewing = radiansToDegrees(Math.atan(shearing));
target.set(skewKey, skewing);
const changed = skewingBefore !== target[skewKey];
if (changed && axis === 'y') {
// we don't want skewing to affect scaleX
// so we factor it by the inverse skewing diff to make it seem unchanged to the viewer
const { skewX, scaleX } = target,
dimBefore = target._getTransformedDimensions({ skewY: skewingBefore }),
dimAfter = target._getTransformedDimensions(),
compensationFactor = skewX !== 0 ? dimBefore.x / dimAfter.x : 1;
compensationFactor !== 1 &&
target.set('scaleX', compensationFactor * scaleX);
}
return changed;
}
/**
* Wrapped Action handler for skewing on a given axis, takes care of the
* skew direction and determines the correct transform origin for the anchor point
* @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
*/
function skewHandler(
axis: TAxis,
eventData: TPointerEvent,
transform: Transform,
x: number,
y: number
) {
const { target } = transform,
{
counterAxis,
origin: originKey,
lockSkewing: lockSkewingKey,
skew: skewKey,
flip: flipKey,
} = AXIS_KEYS[axis];
if (isLocked(target, lockSkewingKey)) {
return false;
}
const { origin: counterOriginKey, flip: counterFlipKey } =
AXIS_KEYS[counterAxis],
counterOriginFactor =
resolveOrigin(transform[counterOriginKey]) *
(target[counterFlipKey] ? -1 : 1),
// if the counter origin is top/left (= -0.5) then we are skewing x/y values on the bottom/right side of target respectively.
// if the counter origin is bottom/right (= 0.5) then we are skewing x/y values on the top/left side of target respectively.
// skewing direction on the top/left side of target is OPPOSITE to the direction of the movement of the pointer,
// so we factor skewing direction by this value.
skewingSide = (-Math.sign(counterOriginFactor) *
(target[flipKey] ? -1 : 1)) as 1 | -1,
skewingDirection =
((target[skewKey] === 0 &&
// in case skewing equals 0 we use the pointer offset from target center to determine the direction of skewing
getLocalPoint(transform, CENTER, CENTER, x, y)[axis] > 0) ||
// in case target has skewing we use that as the direction
target[skewKey] > 0
? 1
: -1) * skewingSide,
// anchor to the opposite side of the skewing direction
// normalize value from [-1, 1] to origin value [0, 1]
origin = -skewingDirection * 0.5 + 0.5;
const finalHandler = wrapWithFireEvent<SkewTransform>(
'skewing',
wrapWithFixedAnchor((eventData, transform, x, y) =>
skewObject(axis, transform, new Point(x, y))
)
);
return finalHandler(
eventData,
{
...transform,
[originKey]: origin,
skewingSide,
},
x,
y
);
}
/**
* Wrapped Action handler for skewing on the X axis, takes care of the
* skew direction and determines the correct transform origin for the anchor point
* @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 skewHandlerX: TransformActionHandler = (
eventData,
transform,
x,
y
) => {
return skewHandler('x', eventData, transform, x, y);
};
/**
* Wrapped Action handler for skewing on the Y axis, takes care of the
* skew direction and determines the correct transform origin for the anchor point
* @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 skewHandlerY: TransformActionHandler = (
eventData,
transform,
x,
y
) => {
return skewHandler('y', eventData, transform, x, y);
};