svg.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Thanks to @japsu and their project https://github.com/japsu/jatsi for the example!
  2. use dioxus::prelude::*;
  3. use rand::{thread_rng, Rng};
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. fn app() -> Element {
  8. rsx! {
  9. div { user_select: "none", webkit_user_select: "none", margin_left: "10%", margin_right: "10%",
  10. h1 { "Click die to generate a new value" }
  11. div { cursor: "pointer", height: "100%", width: "100%", Dice {} }
  12. }
  13. }
  14. }
  15. #[component]
  16. fn Dice() -> Element {
  17. const Y: bool = true;
  18. const N: bool = false;
  19. const DOTS: [(i64, i64); 7] = [(-1, -1), (-1, -0), (-1, 1), (1, -1), (1, 0), (1, 1), (0, 0)];
  20. const DOTS_FOR_VALUE: [[bool; 7]; 6] = [
  21. [N, N, N, N, N, N, Y],
  22. [N, N, Y, Y, N, N, N],
  23. [N, N, Y, Y, N, N, Y],
  24. [Y, N, Y, Y, N, Y, N],
  25. [Y, N, Y, Y, N, Y, Y],
  26. [Y, Y, Y, Y, Y, Y, N],
  27. ];
  28. let value = use_signal(|| 5);
  29. let active_dots = use_selector(move || &DOTS_FOR_VALUE[(value() - 1) as usize]);
  30. rsx! {
  31. svg {
  32. view_box: "-1000 -1000 2000 2000",
  33. prevent_default: "onclick",
  34. onclick: move |e| value.set(thread_rng().gen_range(1..=6)),
  35. rect { x: -1000, y: -1000, width: 2000, height: 2000, rx: 200, fill: "#aaa" }
  36. for ((x, y), _) in DOTS.iter().zip(active_dots.read().iter()).filter(|(_, &active)| active) {
  37. circle {
  38. cx: *x * 600,
  39. cy: *y * 600,
  40. r: 200,
  41. fill: "#333"
  42. }
  43. }
  44. }
  45. }
  46. }