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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
import React = require("react");
import * as beziercurve from 'bezier-curve';
import * as fitCurve from 'fit-curve';
import "./InteractionUtils.scss";
import { Utils } from "../../Utils";
export namespace InteractionUtils {
export const MOUSETYPE = "mouse";
export const TOUCHTYPE = "touch";
export const PENTYPE = "pen";
export const ERASERTYPE = "eraser";
const POINTER_PEN_BUTTON = -1;
const REACT_POINTER_PEN_BUTTON = 0;
const ERASER_BUTTON = 5;
export class MultiTouchEvent<T extends React.TouchEvent | TouchEvent> {
constructor(
readonly fingers: number,
readonly targetTouches: T extends React.TouchEvent ? React.Touch[] : Touch[],
readonly touches: T extends React.TouchEvent ? React.Touch[] : Touch[],
readonly changedTouches: T extends React.TouchEvent ? React.Touch[] : Touch[],
readonly touchEvent: T extends React.TouchEvent ? React.TouchEvent : TouchEvent
) { }
}
export interface MultiTouchEventDisposer { (): void; }
/**
*
* @param element - element to turn into a touch target
* @param startFunc - event handler, typically Touchable.onTouchStart (classes that inherit touchable can pass in this.onTouchStart)
*/
export function MakeMultiTouchTarget(
element: HTMLElement,
startFunc: (e: Event, me: MultiTouchEvent<React.TouchEvent>) => void
): MultiTouchEventDisposer {
const onMultiTouchStartHandler = (e: Event) => startFunc(e, (e as CustomEvent<MultiTouchEvent<React.TouchEvent>>).detail);
// const onMultiTouchMoveHandler = moveFunc ? (e: Event) => moveFunc(e, (e as CustomEvent<MultiTouchEvent<TouchEvent>>).detail) : undefined;
// const onMultiTouchEndHandler = endFunc ? (e: Event) => endFunc(e, (e as CustomEvent<MultiTouchEvent<TouchEvent>>).detail) : undefined;
element.addEventListener("dashOnTouchStart", onMultiTouchStartHandler);
// if (onMultiTouchMoveHandler) {
// element.addEventListener("dashOnTouchMove", onMultiTouchMoveHandler);
// }
// if (onMultiTouchEndHandler) {
// element.addEventListener("dashOnTouchEnd", onMultiTouchEndHandler);
// }
return () => {
element.removeEventListener("dashOnTouchStart", onMultiTouchStartHandler);
// if (onMultiTouchMoveHandler) {
// element.removeEventListener("dashOnTouchMove", onMultiTouchMoveHandler);
// }
// if (onMultiTouchEndHandler) {
// element.removeEventListener("dashOnTouchend", onMultiTouchEndHandler);
// }
};
}
/**
* Turns an element onto a target for touch hold handling.
* @param element - element to add events to
* @param func - function to add to the event
*/
export function MakeHoldTouchTarget(
element: HTMLElement,
func: (e: Event, me: MultiTouchEvent<React.TouchEvent>) => void
): MultiTouchEventDisposer {
const handler = (e: Event) => func(e, (e as CustomEvent<MultiTouchEvent<React.TouchEvent>>).detail);
element.addEventListener("dashOnTouchHoldStart", handler);
return () => {
element.removeEventListener("dashOnTouchHoldStart", handler);
};
}
export function GetMyTargetTouches(mte: InteractionUtils.MultiTouchEvent<React.TouchEvent | TouchEvent>, prevPoints: Map<number, React.Touch>, ignorePen: boolean): React.Touch[] {
const myTouches = new Array<React.Touch>();
for (const pt of mte.touches) {
if (!ignorePen || ((pt as any).radiusX > 1 && (pt as any).radiusY > 1)) {
for (const tPt of mte.targetTouches) {
if (tPt?.screenX === pt?.screenX && tPt?.screenY === pt?.screenY) {
if (pt && prevPoints.has(pt.identifier)) {
myTouches.push(pt);
}
}
}
}
}
// if (mte.touches.length !== myTouches.length) {
// throw Error("opo")
// }
return myTouches;
}
export function CreatePolyline(points: { X: number, Y: number }[], left: number, top: number,
color: string, width: number, strokeWidth: number, bezier: string, fill: string, arrowStart: string, arrowEnd: string,
dash: string, scalex: number, scaley: number, shape: string, pevents: string, drawHalo: boolean, nodefs: boolean) {
let pts: { X: number; Y: number; }[] = [];
if (shape) { //if any of the shape are true
pts = makePolygon(shape, points);
}
else if (points.length > 1 && points[points.length - 1].X === points[0].X && points[points.length - 1].Y === points[0].Y) {
//pointer is up (first and last points are the same)
const newPoints = points.reduce((p, pts) => { p.push([pts.X, pts.Y]); return p; }, [] as number[][]);
newPoints.pop();
const bezierCurves = fitCurve(newPoints, parseInt(bezier));
for (const curve of bezierCurves) {
for (var t = 0; t < 1; t += 0.01) {
const point = beziercurve(t, curve);
pts.push({ X: point[0], Y: point[1] });
}
}
} else {
pts = points;
}
const strpts = pts.reduce((acc: string, pt: { X: number, Y: number }) => acc +
`${(pt.X - left - width / 2) * scalex + width / 2},
${(pt.Y - top - width / 2) * scaley + width / 2} `, "");
const dashArray = String(Number(width) * Number(dash));
const defGuid = Utils.GenerateGuid();
return (<svg fill={fill === "none" ? color : fill}> {/* setting the svg fill sets the arrowhead fill */}
{nodefs ? (null) : <defs>
<marker id={`dot${defGuid}`} orient="auto" overflow="visible">
<circle r={1} fill="context-stroke" />
</marker>
<marker id={`arrowHead${defGuid}`} orient="auto" overflow="visible" refX="3" refY="1" markerWidth="10" markerHeight="7">
<polygon points="3 0, 3 2, 0 1" />
</marker>
<marker id={`arrowEnd${defGuid}`} orient="auto" overflow="visible" refX="0" refY="1" markerWidth="10" markerHeight="7">
<polygon points="0 0, 3 1, 0 2" />
</marker>
</defs>}
<polyline
points={strpts}
style={{
filter: drawHalo ? "url(#inkSelectionHalo)" : undefined,
fill,
opacity: strokeWidth !== width ? 0.5 : undefined,
pointerEvents: pevents as any,
stroke: color ?? "rgb(0, 0, 0)",
strokeWidth: strokeWidth,
strokeLinejoin: "round",
strokeLinecap: "round",
strokeDasharray: dashArray
}}
markerStart={`url(#${arrowStart + defGuid})`}
markerEnd={`url(#${arrowEnd + defGuid})`}
/>
</svg>);
}
// export function makeArrow() {
// return (
// InkOptionsMenu.Instance.getColors().map(color => {
// const id1 = "arrowHeadTest" + color;
// console.log(color);
// <marker id={id1} orient="auto" overflow="visible" refX="0" refY="1" markerWidth="10" markerHeight="7">
// <polygon points="0 0, 3 1, 0 2" fill={"#" + color} />
// </marker>;
// })
// );
// }
export function makePolygon(shape: string, points: { X: number, Y: number }[]) {
if (points.length > 1 && points[points.length - 1].X === points[0].X && points[points.length - 1].Y + 1 === points[0].Y) {
//pointer is up (first and last points are the same)
if (shape === "arrow" || shape === "line") {
//if arrow or line, the two end points should be the starting and the ending point
var left = points[0].X;
var top = points[0].Y;
var right = points[1].X;
var bottom = points[1].Y;
} else {
//otherwise take max and min
const xs = points.map(p => p.X);
const ys = points.map(p => p.Y);
right = Math.max(...xs);
left = Math.min(...xs);
bottom = Math.max(...ys);
top = Math.min(...ys);
}
} else {
//if in the middle of drawing
//take first and last points
right = points[points.length - 1].X;
left = points[0].X;
bottom = points[points.length - 1].Y;
top = points[0].Y;
if (shape !== "arrow" && shape !== "line") {
//switch left/right and top/bottom if needed
if (left > right) {
const temp = right;
right = left;
left = temp;
}
if (top > bottom) {
const temp = top;
top = bottom;
bottom = temp;
}
}
}
points = [];
switch (shape) {
case "rectangle":
points.push({ X: left, Y: top });
points.push({ X: right, Y: top });
points.push({ X: right, Y: bottom });
points.push({ X: left, Y: bottom });
points.push({ X: left, Y: top });
return points;
case "triangle":
points.push({ X: left, Y: bottom });
points.push({ X: right, Y: bottom });
points.push({ X: (right + left) / 2, Y: top });
points.push({ X: left, Y: bottom });
return points;
case "circle":
const centerX = (right + left) / 2;
const centerY = (bottom + top) / 2;
const radius = bottom - centerY;
for (var y = top; y < bottom; y++) {
const x = Math.sqrt(Math.pow(radius, 2) - (Math.pow((y - centerY), 2))) + centerX;
points.push({ X: x, Y: y });
}
for (var y = bottom; y > top; y--) {
const x = Math.sqrt(Math.pow(radius, 2) - (Math.pow((y - centerY), 2))) + centerX;
const newX = centerX - (x - centerX);
points.push({ X: newX, Y: y });
}
points.push({ X: Math.sqrt(Math.pow(radius, 2) - (Math.pow((top - centerY), 2))) + centerX, Y: top });
return points;
// case "arrow":
// const x1 = left;
// const y1 = top;
// const x2 = right;
// const y2 = bottom;
// const L1 = Math.sqrt(Math.pow(Math.abs(x1 - x2), 2) + (Math.pow(Math.abs(y1 - y2), 2)));
// const L2 = L1 / 5;
// const angle = 0.785398;
// const x3 = x2 + (L2 / L1) * ((x1 - x2) * Math.cos(angle) + (y1 - y2) * Math.sin(angle));
// const y3 = y2 + (L2 / L1) * ((y1 - y2) * Math.cos(angle) - (x1 - x2) * Math.sin(angle));
// const x4 = x2 + (L2 / L1) * ((x1 - x2) * Math.cos(angle) - (y1 - y2) * Math.sin(angle));
// const y4 = y2 + (L2 / L1) * ((y1 - y2) * Math.cos(angle) + (x1 - x2) * Math.sin(angle));
// points.push({ X: x1, Y: y1 });
// points.push({ X: x2, Y: y2 });
// points.push({ X: x3, Y: y3 });
// points.push({ X: x4, Y: y4 });
// points.push({ X: x2, Y: y2 });
// return points;
case "line":
points.push({ X: left, Y: top });
points.push({ X: right, Y: bottom });
return points;
default:
return points;
}
}
/**
* Returns whether or not the pointer event passed in is of the type passed in
* @param e - pointer event. this event could be from a mouse, a pen, or a finger
* @param type - InteractionUtils.(PENTYPE | ERASERTYPE | MOUSETYPE | TOUCHTYPE)
*/
export function IsType(e: PointerEvent | React.PointerEvent, type: string): boolean {
switch (type) {
// pen and eraser are both pointer type 'pen', but pen is button 0 and eraser is button 5. -syip2
case PENTYPE:
return e.pointerType === PENTYPE && (e.button === -1 || e.button === 0);
case ERASERTYPE:
return e.pointerType === PENTYPE && e.button === (e instanceof PointerEvent ? ERASER_BUTTON : ERASER_BUTTON);
default:
return e.pointerType === type;
}
}
/**
* Returns euclidean distance between two points
* @param pt1
* @param pt2
*/
export function TwoPointEuclidist(pt1: React.Touch, pt2: React.Touch): number {
return Math.sqrt(Math.pow(pt1.clientX - pt2.clientX, 2) + Math.pow(pt1.clientY - pt2.clientY, 2));
}
/**
* Returns the centroid of an n-arbitrary long list of points (takes the average the x and y components of each point)
* @param pts - n-arbitrary long list of points
*/
export function CenterPoint(pts: React.Touch[]): { X: number, Y: number } {
const centerX = pts.map(pt => pt.clientX).reduce((a, b) => a + b, 0) / pts.length;
const centerY = pts.map(pt => pt.clientY).reduce((a, b) => a + b, 0) / pts.length;
return { X: centerX, Y: centerY };
}
/**
* Returns -1 if pinching out, 0 if not pinching, and 1 if pinching in
* @param pt1 - new point that corresponds to oldPoint1
* @param pt2 - new point that corresponds to oldPoint2
* @param oldPoint1 - previous point 1
* @param oldPoint2 - previous point 2
*/
export function Pinching(pt1: React.Touch, pt2: React.Touch, oldPoint1: React.Touch, oldPoint2: React.Touch): number {
const threshold = 4;
const oldDist = TwoPointEuclidist(oldPoint1, oldPoint2);
const newDist = TwoPointEuclidist(pt1, pt2);
/** if they have the same sign, then we are either pinching in or out.
* threshold it by 10 (it has to be pinching by at least threshold to be a valid pinch)
* so that it can still pan without freaking out
*/
if (Math.sign(oldDist) === Math.sign(newDist) && Math.abs(oldDist - newDist) > threshold) {
return Math.sign(oldDist - newDist);
}
return 0;
}
/**
* Returns -1 if pinning and pinching out, 0 if not pinning, and 1 if pinching in
* @param pt1 - new point that corresponds to oldPoint1
* @param pt2 - new point that corresponds to oldPoint2
* @param oldPoint1 - previous point 1
* @param oldPoint2 - previous point 2
*/
export function Pinning(pt1: React.Touch, pt2: React.Touch, oldPoint1: React.Touch, oldPoint2: React.Touch): number {
const threshold = 4;
const pt1Dist = TwoPointEuclidist(oldPoint1, pt1);
const pt2Dist = TwoPointEuclidist(oldPoint2, pt2);
const pinching = Pinching(pt1, pt2, oldPoint1, oldPoint2);
if (pinching !== 0) {
if ((pt1Dist < threshold && pt2Dist > threshold) || (pt1Dist > threshold && pt2Dist < threshold)) {
return pinching;
}
}
return 0;
}
export function IsDragging(oldTouches: Map<number, React.Touch>, newTouches: React.Touch[], leniency: number): boolean {
for (const touch of newTouches) {
if (touch) {
const oldTouch = oldTouches.get(touch.identifier);
if (oldTouch) {
if (TwoPointEuclidist(touch, oldTouch) >= leniency) {
return true;
}
}
}
}
return false;
}
// These might not be very useful anymore, but I'll leave them here for now -syip2
{
/**
* Returns the type of Touch Interaction from a list of points.
* Also returns any data that is associated with a Touch Interaction
* @param pts - List of points
*/
// export function InterpretPointers(pts: React.Touch[]): { type: Opt<TouchInteraction>, data?: any } {
// const leniency = 200;
// switch (pts.length) {
// case 1:
// return { type: OneFinger };
// case 2:
// return { type: TwoSeperateFingers };
// case 3:
// let pt1 = pts[0];
// let pt2 = pts[1];
// let pt3 = pts[2];
// if (pt1 && pt2 && pt3) {
// let dist12 = TwoPointEuclidist(pt1, pt2);
// let dist23 = TwoPointEuclidist(pt2, pt3);
// let dist13 = TwoPointEuclidist(pt1, pt3);
// console.log(`distances: ${dist12}, ${dist23}, ${dist13}`);
// let dist12close = dist12 < leniency;
// let dist23close = dist23 < leniency;
// let dist13close = dist13 < leniency;
// let xor2313 = dist23close ? !dist13close : dist13close;
// let xor = dist12close ? !xor2313 : xor2313;
// // three input xor because javascript doesn't have logical xor's
// if (xor) {
// let points: number[] = [];
// let min = Math.min(dist12, dist23, dist13);
// switch (min) {
// case dist12:
// points = [0, 1, 2];
// break;
// case dist23:
// points = [1, 2, 0];
// break;
// case dist13:
// points = [0, 2, 1];
// break;
// }
// return { type: TwoToOneFingers, data: points };
// }
// else {
// return { type: ThreeSeperateFingers, data: null };
// }
// }
// default:
// return { type: undefined };
// }
// }
}
}
|