forked from konvajs/konva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointerEvents.ts
67 lines (50 loc) · 1.6 KB
/
PointerEvents.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
import { KonvaEventObject } from './Node';
import { Konva } from './Global';
import { Shape } from './Shape';
import { Stage } from './Stage';
const Captures = new Map<number, Shape | Stage>();
// we may use this module for capturing touch events too
// so make sure we don't do something super specific to pointer
const SUPPORT_POINTER_EVENTS = Konva._global['PointerEvent'] !== undefined;
export interface KonvaPointerEvent extends KonvaEventObject<PointerEvent> {
pointerId: number;
}
export function getCapturedShape(pointerId: number) {
return Captures.get(pointerId);
}
export function createEvent(evt: PointerEvent): KonvaPointerEvent {
return {
evt,
pointerId: evt.pointerId
} as any;
}
export function hasPointerCapture(pointerId: number, shape: Shape | Stage) {
return Captures.get(pointerId) === shape;
}
export function setPointerCapture(pointerId: number, shape: Shape | Stage) {
releaseCapture(pointerId);
const stage = shape.getStage();
if (!stage) return;
Captures.set(pointerId, shape);
if (SUPPORT_POINTER_EVENTS) {
shape._fire(
'gotpointercapture',
createEvent(new PointerEvent('gotpointercapture'))
);
}
}
export function releaseCapture(pointerId: number, target?: Shape | Stage) {
const shape = Captures.get(pointerId);
if (!shape) return;
const stage = shape.getStage();
if (stage && stage.content) {
// stage.content.releasePointerCapture(pointerId);
}
Captures.delete(pointerId);
if (SUPPORT_POINTER_EVENTS) {
shape._fire(
'lostpointercapture',
createEvent(new PointerEvent('lostpointercapture'))
);
}
}