Newer
Older
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
export function orientation_lock(orientation) {
window.screen.orientation.lock(orientation)
}
export function orientation_unlock() {
window.screen.orientation.unlock()
}
export function make_selector_fullscreen(selector) {
const el = document.querySelector(selector);
if (el) {
el.requestFullscreen()
}
}
export function exit_fullscreen() {
document.exitFullscreen()
}
export function is_fullscreen() {
return document.fullscreenEnabled
}
export function is_touch_device() {
return (navigator?.maxTouchPoints ?? 0) > 0
}
export function toggle_selector_fullscreen(selector) {
if (is_fullscreen()) {
exit_fullscreen()
} else {
make_selector_fullscreen(selector)
}
}
export function bind_selector_touch_events(selector) {
function touch(evt) {
let phase = null
switch(evt.type) {
case 'touchstart':
phase = 'Started';
break;
case 'touchend':
phase = 'Ended';
break;
case 'touchmove':
phase = 'Moved';
break;
case 'touchcancel':
phase = 'Cancelled';
break;
}
if (phase == null) {
return
}
for (const touch of evt.changedTouches) {
console.log(touch)
window.touch_events.push({
id: touch.identifier,
phase,
position: [touch.pageX, window.document.documentElement.getBoundingClientRect().height - touch.pageY],
force: null,
})
}
}
if (window.touch_events == null) {
window.touch_events = []
window.touch_handlers = window.touch_handlers || {}
}
const el = document.querySelector(selector)
if (el != null) {
window.touch_handlers[selector] = touch
el.addEventListener('touchstart', touch)
el.addEventListener('touchend', touch)
el.addEventListener('touchcancel', touch)
el.addEventListener('touchmove', touch)
}
}
export function teardown_selector_touch_events(selector) {
const handler = window.touch_handlers?.[selector]
delete window.touch_handlers?.[selector]
if (handler != null) {
const el = document.querySelector(selector)
if (el != null) {
el.removeEventListener('touchstart', handler)
el.removeEventListener('touchend', handler)
el.removeEventListener('touchcancel', handler)
el.removeEventListener('touchmove', handler)
}
}
}
export function take_touch_events() {
if (window.touch_events) {
let events = window.touch_events
window.touch_events = []
return JSON.stringify(events)
}
return '[]'
}