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
use crate::*;
/// Implementation of touch point extraction from DOM touch events.
impl NativeTouchPoint {
/// Extracts all active touch points from a `TouchEvent`.
///
/// Iterates over the `touches` list of the given `TouchEvent` and
/// builds a `Vec<NativeTouchPoint>` with each touch point's
/// identifier, viewport coordinates, screen coordinates, page
/// coordinates, and offset coordinates relative to the target element.
///
/// The offset coordinates (`offset_x`, `offset_y`) are computed by
/// subtracting the target element's bounding rect from the touch's
/// client coordinates, since the browser `Touch` object does not
/// provide `offsetX`/`offsetY` directly.
///
/// # Arguments
///
/// - `&Event`: The native DOM touch event.
///
/// # Returns
///
/// - `Vec<NativeTouchPoint>`: All currently active touch points.
pub fn extract_all(event: &Event) -> Vec<NativeTouchPoint> {
let touches_value: JsValue = Reflect::get(event.as_ref(), &JsValue::from_str("touches"))
.ok()
.unwrap_or(JsValue::NULL);
let touches: Array = touches_value.unchecked_into();
let target: JsValue = event
.target()
.map_or(JsValue::NULL, |event_target: EventTarget| {
event_target.into()
});
let element: Element = target.unchecked_into();
let rect: DomRect = element.get_bounding_client_rect();
let rect_left: f64 = rect.left();
let rect_top: f64 = rect.top();
(0..touches.length())
.map(|index: u32| {
let touch: JsValue = touches.get(index);
let identifier: i32 = Reflect::get(&touch, &JsValue::from_str("identifier"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let client_x: i32 = Reflect::get(&touch, &JsValue::from_str("clientX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let client_y: i32 = Reflect::get(&touch, &JsValue::from_str("clientY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let screen_x: i32 = Reflect::get(&touch, &JsValue::from_str("screenX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let screen_y: i32 = Reflect::get(&touch, &JsValue::from_str("screenY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let page_x: i32 = Reflect::get(&touch, &JsValue::from_str("pageX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let page_y: i32 = Reflect::get(&touch, &JsValue::from_str("pageY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let offset_x: i32 = (client_x as f64 - rect_left).round() as i32;
let offset_y: i32 = (client_y as f64 - rect_top).round() as i32;
NativeTouchPoint {
identifier,
client_x,
client_y,
screen_x,
screen_y,
offset_x,
offset_y,
page_x,
page_y,
}
})
.collect()
}
/// Extracts the changed touch points from a `TouchEvent`.
///
/// The `changedTouches` list contains touch points that have changed
/// since the last touch event:
/// - For `touchstart` - newly added touch points.
/// - For `touchmove` - touch points that have moved.
/// - For `touchend` / `touchcancel` - removed touch points.
///
/// This is useful for determining which specific fingers were lifted
/// in a `touchend` event, since the `touches` list no longer contains
/// them.
///
/// # Arguments
///
/// - `&Event`: The native DOM touch event.
///
/// # Returns
///
/// - `Vec<NativeTouchPoint>`: The touch points that changed in this event.
pub fn extract_changed(event: &Event) -> Vec<NativeTouchPoint> {
let touches_value: JsValue =
Reflect::get(event.as_ref(), &JsValue::from_str("changedTouches"))
.ok()
.unwrap_or(JsValue::NULL);
let touches: Array = touches_value.unchecked_into();
let target: JsValue = event
.target()
.map_or(JsValue::NULL, |event_target: EventTarget| {
event_target.into()
});
let element: Element = target.unchecked_into();
let rect: DomRect = element.get_bounding_client_rect();
let rect_left: f64 = rect.left();
let rect_top: f64 = rect.top();
(0..touches.length())
.map(|index: u32| {
let touch: JsValue = touches.get(index);
let identifier: i32 = Reflect::get(&touch, &JsValue::from_str("identifier"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let client_x: i32 = Reflect::get(&touch, &JsValue::from_str("clientX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let client_y: i32 = Reflect::get(&touch, &JsValue::from_str("clientY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let screen_x: i32 = Reflect::get(&touch, &JsValue::from_str("screenX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let screen_y: i32 = Reflect::get(&touch, &JsValue::from_str("screenY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let page_x: i32 = Reflect::get(&touch, &JsValue::from_str("pageX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let page_y: i32 = Reflect::get(&touch, &JsValue::from_str("pageY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let offset_x: i32 = (client_x as f64 - rect_left).round() as i32;
let offset_y: i32 = (client_y as f64 - rect_top).round() as i32;
NativeTouchPoint {
identifier,
client_x,
client_y,
screen_x,
screen_y,
offset_x,
offset_y,
page_x,
page_y,
}
})
.collect()
}
}
/// Implementation of high-precision touch point extraction from DOM touch events.
impl NativeTouchPointF64 {
/// Extracts all active touch points with high-precision `f64` offset coordinates
/// from a `TouchEvent`.
///
/// Similar to `NativeTouchPoint::extract_all`, but returns `f64` precision for
/// offset/client coordinates, which is essential for canvas drawing
/// and other pixel-precise interactions.
///
/// # Arguments
///
/// - `&Event`: The native DOM touch event.
///
/// # Returns
///
/// - `Vec<NativeTouchPointF64>`: All currently active touch points with `f64` coordinates.
pub fn extract_all(event: &Event) -> Vec<NativeTouchPointF64> {
let touches_value: JsValue = Reflect::get(event.as_ref(), &JsValue::from_str("touches"))
.ok()
.unwrap_or(JsValue::NULL);
let touches: Array = touches_value.unchecked_into();
let target: JsValue = event
.target()
.map_or(JsValue::NULL, |event_target: EventTarget| {
event_target.into()
});
let element: Element = target.unchecked_into();
let rect: DomRect = element.get_bounding_client_rect();
let rect_left: f64 = rect.left();
let rect_top: f64 = rect.top();
(0..touches.length())
.map(|index: u32| {
let touch: JsValue = touches.get(index);
let identifier: i32 = Reflect::get(&touch, &JsValue::from_str("identifier"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.map(|value: f64| value as i32)
.unwrap_or(0);
let client_x: f64 = Reflect::get(&touch, &JsValue::from_str("clientX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let client_y: f64 = Reflect::get(&touch, &JsValue::from_str("clientY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let screen_x: f64 = Reflect::get(&touch, &JsValue::from_str("screenX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let screen_y: f64 = Reflect::get(&touch, &JsValue::from_str("screenY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let page_x: f64 = Reflect::get(&touch, &JsValue::from_str("pageX"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let page_y: f64 = Reflect::get(&touch, &JsValue::from_str("pageY"))
.ok()
.and_then(|value: JsValue| value.as_f64())
.unwrap_or(0.0);
let offset_x: f64 = client_x - rect_left;
let offset_y: f64 = client_y - rect_top;
NativeTouchPointF64 {
identifier,
client_x,
client_y,
screen_x,
screen_y,
offset_x,
offset_y,
page_x,
page_y,
}
})
.collect()
}
}