-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
scale.ts
277 lines (261 loc) · 9.14 KB
/
scale.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import type {
ControlCursorCallback,
TPointerEvent,
Transform,
TransformActionHandler,
} from '../EventTypeDefs';
import type { FabricObject } from '../shapes/Object/FabricObject';
import type { TAxis } from '../typedefs';
import type { Canvas } from '../canvas/Canvas';
import {
findCornerQuadrant,
getLocalPoint,
invertOrigin,
isLocked,
isTransformCentered,
NOT_ALLOWED_CURSOR,
} from './util';
import { wrapWithFireEvent } from './wrapWithFireEvent';
import { wrapWithFixedAnchor } from './wrapWithFixedAnchor';
type ScaleTransform = Transform & {
gestureScale?: number;
signX?: number;
signY?: number;
};
type ScaleBy = TAxis | 'equally' | '' | undefined;
/**
* Inspect event and fabricObject properties to understand if the scaling action
* @param {Event} eventData from the user action
* @param {FabricObject} fabricObject the fabric object about to scale
* @return {Boolean} true if scale is proportional
*/
export function scaleIsProportional(
eventData: TPointerEvent,
fabricObject: FabricObject
): boolean {
const canvas = fabricObject.canvas as Canvas,
uniformIsToggled = eventData[canvas.uniScaleKey!];
return (
(canvas.uniformScaling && !uniformIsToggled) ||
(!canvas.uniformScaling && uniformIsToggled)
);
}
/**
* Inspect fabricObject to understand if the current scaling action is allowed
* @param {FabricObject} fabricObject the fabric object about to scale
* @param {String} by 'x' or 'y' or ''
* @param {Boolean} scaleProportionally true if we are trying to scale proportionally
* @return {Boolean} true if scaling is not allowed at current conditions
*/
export function scalingIsForbidden(
fabricObject: FabricObject,
by: ScaleBy,
scaleProportionally: boolean
) {
const lockX = isLocked(fabricObject, 'lockScalingX'),
lockY = isLocked(fabricObject, 'lockScalingY');
if (lockX && lockY) {
return true;
}
if (!by && (lockX || lockY) && scaleProportionally) {
return true;
}
if (lockX && by === 'x') {
return true;
}
if (lockY && by === 'y') {
return true;
}
return false;
}
const scaleMap = ['e', 'se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e'];
/**
* return the correct cursor style for the scale 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 scaleCursorStyleHandler: ControlCursorCallback = (
eventData,
control,
fabricObject
) => {
const scaleProportionally = scaleIsProportional(eventData, fabricObject),
by =
control.x !== 0 && control.y === 0
? 'x'
: control.x === 0 && control.y !== 0
? 'y'
: '';
if (scalingIsForbidden(fabricObject, by, scaleProportionally)) {
return NOT_ALLOWED_CURSOR;
}
const n = findCornerQuadrant(fabricObject, control);
return `${scaleMap[n]}-resize`;
};
/**
* Basic scaling logic, reused with different constrain for scaling X,Y, freely or equally.
* 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
* @param {Object} options additional information for scaling
* @param {String} options.by 'x', 'y', 'equally' or '' to indicate type of scaling
* @return {Boolean} true if some change happened
* @private
*/
function scaleObject(
eventData: TPointerEvent,
transform: ScaleTransform,
x: number,
y: number,
options: { by?: ScaleBy } = {}
) {
const target = transform.target,
by = options.by,
scaleProportionally = scaleIsProportional(eventData, target),
forbidScaling = scalingIsForbidden(target, by, scaleProportionally);
let newPoint, scaleX, scaleY, dim, signX, signY;
if (forbidScaling) {
return false;
}
if (transform.gestureScale) {
scaleX = transform.scaleX * transform.gestureScale;
scaleY = transform.scaleY * transform.gestureScale;
} else {
newPoint = getLocalPoint(
transform,
transform.originX,
transform.originY,
x,
y
);
// use of sign: We use sign to detect change of direction of an action. sign usually change when
// we cross the origin point with the mouse. So a scale flip for example. There is an issue when scaling
// by center and scaling using one middle control ( default: mr, mt, ml, mb), the mouse movement can easily
// cross many time the origin point and flip the object. so we need a way to filter out the noise.
// This ternary here should be ok to filter out X scaling when we want Y only and vice versa.
signX = by !== 'y' ? Math.sign(newPoint.x || transform.signX || 1) : 1;
signY = by !== 'x' ? Math.sign(newPoint.y || transform.signY || 1) : 1;
if (!transform.signX) {
transform.signX = signX;
}
if (!transform.signY) {
transform.signY = signY;
}
if (
isLocked(target, 'lockScalingFlip') &&
(transform.signX !== signX || transform.signY !== signY)
) {
return false;
}
dim = target._getTransformedDimensions();
// missing detection of flip and logic to switch the origin
if (scaleProportionally && !by) {
// uniform scaling
const distance = Math.abs(newPoint.x) + Math.abs(newPoint.y),
{ original } = transform,
originalDistance =
Math.abs((dim.x * original.scaleX) / target.scaleX) +
Math.abs((dim.y * original.scaleY) / target.scaleY),
scale = distance / originalDistance;
scaleX = original.scaleX * scale;
scaleY = original.scaleY * scale;
} else {
scaleX = Math.abs((newPoint.x * target.scaleX) / dim.x);
scaleY = Math.abs((newPoint.y * target.scaleY) / dim.y);
}
// if we are scaling by center, we need to double the scale
if (isTransformCentered(transform)) {
scaleX *= 2;
scaleY *= 2;
}
if (transform.signX !== signX && by !== 'y') {
transform.originX = invertOrigin(transform.originX);
scaleX *= -1;
transform.signX = signX;
}
if (transform.signY !== signY && by !== 'x') {
transform.originY = invertOrigin(transform.originY);
scaleY *= -1;
transform.signY = signY;
}
}
// minScale is taken are in the setter.
const oldScaleX = target.scaleX,
oldScaleY = target.scaleY;
if (!by) {
!isLocked(target, 'lockScalingX') && target.set('scaleX', scaleX);
!isLocked(target, 'lockScalingY') && target.set('scaleY', scaleY);
} else {
// forbidden cases already handled on top here.
by === 'x' && target.set('scaleX', scaleX);
by === 'y' && target.set('scaleY', scaleY);
}
return oldScaleX !== target.scaleX || oldScaleY !== target.scaleY;
}
/**
* Generic scaling logic, to scale from corners either equally or freely.
* 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 scaleObjectFromCorner: TransformActionHandler<ScaleTransform> = (
eventData,
transform,
x,
y
) => {
return scaleObject(eventData, transform, x, y);
};
/**
* Scaling logic for the X axis.
* 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
*/
const scaleObjectX: TransformActionHandler<ScaleTransform> = (
eventData,
transform,
x,
y
) => {
return scaleObject(eventData, transform, x, y, { by: 'x' });
};
/**
* Scaling logic for the Y axis.
* 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
*/
const scaleObjectY: TransformActionHandler<ScaleTransform> = (
eventData,
transform,
x,
y
) => {
return scaleObject(eventData, transform, x, y, { by: 'y' });
};
export const scalingEqually = wrapWithFireEvent(
'scaling',
wrapWithFixedAnchor(scaleObjectFromCorner)
);
export const scalingX = wrapWithFireEvent(
'scaling',
wrapWithFixedAnchor(scaleObjectX)
);
export const scalingY = wrapWithFireEvent(
'scaling',
wrapWithFixedAnchor(scaleObjectY)
);