-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathcanvas_gestures.mixin.ts
211 lines (187 loc) · 5.65 KB
/
canvas_gestures.mixin.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
//@ts-nocheck
import { scalingEqually } from '../controls/actions';
import { fireEvent } from '../util/fireEvent';
(function (global) {
var fabric = global.fabric,
degreesToRadians = fabric.util.degreesToRadians,
radiansToDegrees = fabric.util.radiansToDegrees;
/**
* Adds support for multi-touch gestures using the Event.js library.
* Fires the following custom events:
* - touch:gesture
* - touch:drag
* - touch:orientation
* - touch:shake
* - touch:longpress
*/
fabric.util.object.extend(
fabric.Canvas.prototype,
/** @lends fabric.Canvas.prototype */ {
/**
* Method that defines actions when an Event.js gesture is detected on an object. Currently only supports
* 2 finger gestures.
* @param {Event} e Event object by Event.js
* @param {Event} self Event proxy object by Event.js
*/
__onTransformGesture: function (e, self) {
if (
this.isDrawingMode ||
!e.touches ||
e.touches.length !== 2 ||
'gesture' !== self.gesture
) {
return;
}
var target = this.findTarget(e);
if ('undefined' !== typeof target) {
this.__gesturesParams = {
e: e,
self: self,
target: target,
};
this.__gesturesRenderer();
}
this.fire('touch:gesture', {
target: target,
e: e,
self: self,
});
},
__gesturesParams: null,
__gesturesRenderer: function () {
if (this.__gesturesParams === null || this._currentTransform === null) {
return;
}
var self = this.__gesturesParams.self,
t = this._currentTransform,
e = this.__gesturesParams.e;
t.action = 'scale';
t.originX = t.originY = 'center';
this._scaleObjectBy(self.scale, e);
if (self.rotation !== 0) {
t.action = 'rotate';
this._rotateObjectByAngle(self.rotation, e);
}
this.requestRenderAll();
t.action = 'drag';
},
/**
* Method that defines actions when an Event.js drag is detected.
*
* @param {Event} e Event object by Event.js
* @param {Event} self Event proxy object by Event.js
*/
__onDrag: function (e, self) {
this.fire('touch:drag', {
e: e,
self: self,
});
},
/**
* Method that defines actions when an Event.js orientation event is detected.
*
* @param {Event} e Event object by Event.js
* @param {Event} self Event proxy object by Event.js
*/
__onOrientationChange: function (e, self) {
this.fire('touch:orientation', {
e: e,
self: self,
});
},
/**
* Method that defines actions when an Event.js shake event is detected.
*
* @param {Event} e Event object by Event.js
* @param {Event} self Event proxy object by Event.js
*/
__onShake: function (e, self) {
this.fire('touch:shake', {
e: e,
self: self,
});
},
/**
* Method that defines actions when an Event.js longpress event is detected.
*
* @param {Event} e Event object by Event.js
* @param {Event} self Event proxy object by Event.js
*/
__onLongPress: function (e, self) {
this.fire('touch:longpress', {
e: e,
self: self,
});
},
/**
* @private
* @param {Event} [e] Event object fired on Event.js gesture
* @param {Event} [self] Inner Event object
*/
_onGesture: function (e, self) {
this.__onTransformGesture(e, self);
},
/**
* @private
* @param {Event} [e] Event object fired on Event.js drag
* @param {Event} [self] Inner Event object
*/
_onDrag: function (e, self) {
this.__onDrag(e, self);
},
/**
* Scales an object by a factor
* @param {Number} s The scale factor to apply to the current scale level
* @param {Event} e Event object by Event.js
*/
_scaleObjectBy: function (s, e) {
var t = this._currentTransform,
target = t.target;
t.gestureScale = s;
target._scaling = true;
return scalingEqually(e, t, 0, 0);
},
/**
* Rotates object by an angle
* @param {Number} curAngle The angle of rotation in degrees
* @param {Event} e Event object by Event.js
*/
_rotateObjectByAngle: function (curAngle, e) {
var t = this._currentTransform;
if (t.target.get('lockRotation')) {
return;
}
t.target.rotate(radiansToDegrees(degreesToRadians(curAngle) + t.theta));
fireEvent('rotating', {
target: t.target,
e: e,
transform: t,
});
},
/**
* @private
* @param {Event} [e] Event object fired on Event.js orientation change
* @param {Event} [self] Inner Event object
*/
_onOrientationChange: function (e, self) {
this.__onOrientationChange(e, self);
},
/**
* @private
* @param {Event} [e] Event object fired on Event.js shake
* @param {Event} [self] Inner Event object
*/
_onShake: function (e, self) {
this.__onShake(e, self);
},
/**
* @private
* @param {Event} [e] Event object fired on Event.js shake
* @param {Event} [self] Inner Event object
*/
_onLongPress: function (e, self) {
this.__onLongPress(e, self);
},
}
);
})(typeof exports !== 'undefined' ? exports : window);