Update zoom on touch start and end
@@ -148,12 +148,26 @@ impl Window
Ok(())
}
- fn add_touchstart_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>) -> Result<(), Error>
+ fn add_touchstart_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>, last_zoom: Rc<RefCell<Option<f64>>>) -> Result<(), Error>
{
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
+ if event.touches().length() == 1 {
let touch = event.touches().item(0).unwrap();
- *last_position.borrow_mut() = Some((touch.page_x(), touch.page_y()));
(*events).borrow_mut().push(Event::MouseClick {state: State::Pressed, button: MouseButton::Left, position: (touch.page_x() as f64, touch.page_y() as f64)});
+ *last_position.borrow_mut() = Some((touch.page_x(), touch.page_y()));
+ *last_zoom.borrow_mut() = None;
+ }
+ else if event.touches().length() == 2 {
+ let touch0 = event.touches().item(0).unwrap();
+ let touch1 = event.touches().item(1).unwrap();
+ let zoom = f64::sqrt(f64::powi((touch0.page_x() - touch1.page_x()) as f64, 2) + f64::powi((touch0.page_y() - touch1.page_y()) as f64, 2));
+ *last_zoom.borrow_mut() = Some(zoom);
+ *last_position.borrow_mut() = None;
+ }
+ else {
+ *last_zoom.borrow_mut() = None;
+ *last_position.borrow_mut() = None;
+ }
}) as Box<dyn FnMut(_)>);
self.canvas.add_event_listener_with_callback("touchstart", closure.as_ref().unchecked_ref())
.map_err(|e| Error::EventListenerError {message: format!("Unable to add touch start event listener. Error code: {:?}", e)})?;
@@ -161,11 +175,12 @@ impl Window
Ok(())
}
- fn add_touchend_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>) -> Result<(), Error>
+ fn add_touchend_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>, last_zoom: Rc<RefCell<Option<f64>>>) -> Result<(), Error>
{
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
let touch = event.touches().item(0).unwrap();
*last_position.borrow_mut() = None;
+ *last_zoom.borrow_mut() = None;
(*events).borrow_mut().push(Event::MouseClick {state: State::Released, button: MouseButton::Left, position: (touch.page_x() as f64, touch.page_y() as f64)});
}) as Box<dyn FnMut(_)>);
self.canvas.add_event_listener_with_callback("touchend", closure.as_ref().unchecked_ref())
