-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
util.ts
155 lines (146 loc) · 4.06 KB
/
util.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
import type {
TPointerEvent,
Transform,
TransformAction,
BasicTransformEvent,
} from '../EventTypeDefs';
import { resolveOrigin } from '../util/misc/resolveOrigin';
import { Point } from '../Point';
import type { FabricObject } from '../shapes/Object/FabricObject';
import type { TOriginX, TOriginY } from '../typedefs';
import {
degreesToRadians,
radiansToDegrees,
} from '../util/misc/radiansDegreesConversion';
import type { Control } from './Control';
import { CENTER } from '../constants';
export const NOT_ALLOWED_CURSOR = 'not-allowed';
/**
* @param {Boolean} alreadySelected true if target is already selected
* @param {String} corner a string representing the corner ml, mr, tl ...
* @param {Event} e Event object
* @param {FabricObject} [target] inserted back to help overriding. Unused
*/
export const getActionFromCorner = (
alreadySelected: boolean,
corner: string,
e: TPointerEvent,
target: FabricObject
) => {
if (!corner || !alreadySelected) {
return 'drag';
}
const control = target.controls[corner];
return control.getActionName(e, control, target);
};
/**
* Checks if transform is centered
* @param {Object} transform transform data
* @return {Boolean} true if transform is centered
*/
export function isTransformCentered(transform: Transform) {
return transform.originX === CENTER && transform.originY === CENTER;
}
export function invertOrigin(origin: TOriginX | TOriginY) {
return -resolveOrigin(origin) + 0.5;
}
export const isLocked = (
target: FabricObject,
lockingKey:
| 'lockMovementX'
| 'lockMovementY'
| 'lockRotation'
| 'lockScalingX'
| 'lockScalingY'
| 'lockSkewingX'
| 'lockSkewingY'
| 'lockScalingFlip'
) => target[lockingKey];
export const commonEventInfo: TransformAction<
Transform,
BasicTransformEvent
> = (eventData, transform, x, y) => {
return {
e: eventData,
transform,
pointer: new Point(x, y),
};
};
/**
* Combine control position and object angle to find the control direction compared
* to the object center.
* @param {FabricObject} fabricObject the fabric object for which we are rendering controls
* @param {Control} control the control class
* @return {Number} 0 - 7 a quadrant number
*/
export function findCornerQuadrant(
fabricObject: FabricObject,
control: Control
) {
// angle is relative to canvas plane
const angle = fabricObject.getTotalAngle(),
cornerAngle =
angle + radiansToDegrees(Math.atan2(control.y, control.x)) + 360;
return Math.round((cornerAngle % 360) / 45);
}
/**
* @returns the normalized point (rotated relative to center) in local coordinates
*/
function normalizePoint(
target: FabricObject,
point: Point,
originX: TOriginX,
originY: TOriginY
): Point {
const center = target.getRelativeCenterPoint(),
p =
typeof originX !== 'undefined' && typeof originY !== 'undefined'
? target.translateToGivenOrigin(
center,
CENTER,
CENTER,
originX,
originY
)
: new Point(target.left, target.top),
p2 = target.angle
? point.rotate(-degreesToRadians(target.angle), center)
: point;
return p2.subtract(p);
}
/**
* Transforms a point to the offset from the given origin
* @param {Object} transform
* @param {String} originX
* @param {String} originY
* @param {number} x
* @param {number} y
* @return {Fabric.Point} the normalized point
*/
export function getLocalPoint(
{ target, corner }: Transform,
originX: TOriginX,
originY: TOriginY,
x: number,
y: number
) {
const control = target.controls[corner],
zoom = target.canvas?.getZoom() || 1,
padding = target.padding / zoom,
localPoint = normalizePoint(target, new Point(x, y), originX, originY);
if (localPoint.x >= padding) {
localPoint.x -= padding;
}
if (localPoint.x <= -padding) {
localPoint.x += padding;
}
if (localPoint.y >= padding) {
localPoint.y -= padding;
}
if (localPoint.y <= padding) {
localPoint.y += padding;
}
localPoint.x -= control.offsetX;
localPoint.y -= control.offsetY;
return localPoint;
}