coordinates.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use dioxus::prelude::*;
  2. use dioxus_core::UiEvent;
  3. use dioxus_html::on::MouseData;
  4. fn main() {
  5. dioxus::desktop::launch(app);
  6. }
  7. fn app(cx: Scope) -> Element {
  8. let page_coordinates = use_state(&cx, || "".to_string());
  9. let screen_coordinates = use_state(&cx, || "".to_string());
  10. let offset_coordinates = use_state(&cx, || "".to_string());
  11. let container_style = r#"
  12. display: flex;
  13. flex-direction: column;
  14. align-items: center;
  15. "#;
  16. let rect_style = r#"
  17. background: deepskyblue;
  18. height: 50vh;
  19. width: 50vw;
  20. "#;
  21. let update_mouse_position = move |event: UiEvent<MouseData>| {
  22. let mouse_data = event.data;
  23. page_coordinates.set(format!("{:?}", (mouse_data.page_x, mouse_data.page_y)));
  24. screen_coordinates.set(format!("{:?}", (mouse_data.screen_x, mouse_data.screen_y)));
  25. offset_coordinates.set(format!("{:?}", (mouse_data.offset_x, mouse_data.offset_y)));
  26. // Note: client coordinates are also available, but they would be the same as the page coordinates in this example, because there is no scrolling.
  27. };
  28. cx.render(rsx! (
  29. div {
  30. style: "{container_style}",
  31. "Hover over to display coordinates:",
  32. div {
  33. style: "{rect_style}",
  34. onmousemove: update_mouse_position,
  35. }
  36. div {"Page coordinates: {page_coordinates}"},
  37. div {"Screen coordinates: {screen_coordinates}"},
  38. div {"Offset coordinates: {offset_coordinates}"},
  39. }
  40. ))
  41. }