1
0

svg.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. launch(|| {
  6. rsx! {
  7. div { user_select: "none", webkit_user_select: "none", margin_left: "10%", margin_right: "10%",
  8. h1 { "Click die to generate a new value" }
  9. div { cursor: "pointer", height: "100%", width: "100%", Dice {} }
  10. }
  11. }
  12. });
  13. }
  14. #[component]
  15. fn Dice() -> Element {
  16. const Y: bool = true;
  17. const N: bool = false;
  18. const DOTS: [(i64, i64); 7] = [(-1, -1), (-1, -0), (-1, 1), (1, -1), (1, 0), (1, 1), (0, 0)];
  19. const DOTS_FOR_VALUE: [[bool; 7]; 6] = [
  20. [N, N, N, N, N, N, Y],
  21. [N, N, Y, Y, N, N, N],
  22. [N, N, Y, Y, N, N, Y],
  23. [Y, N, Y, Y, N, Y, N],
  24. [Y, N, Y, Y, N, Y, Y],
  25. [Y, Y, Y, Y, Y, Y, N],
  26. ];
  27. let mut value = use_signal(|| 5);
  28. let active_dots = use_memo(move || &DOTS_FOR_VALUE[(value() - 1) as usize]);
  29. rsx! {
  30. svg {
  31. view_box: "-1000 -1000 2000 2000",
  32. prevent_default: "onclick",
  33. onclick: move |_| value.set(thread_rng().gen_range(1..=6)),
  34. rect { x: -1000, y: -1000, width: 2000, height: 2000, rx: 200, fill: "#aaa" }
  35. for ((x, y), _) in DOTS.iter().zip(active_dots.read().iter()).filter(|(_, &active)| active) {
  36. circle {
  37. cx: *x * 600,
  38. cy: *y * 600,
  39. r: 200,
  40. fill: "#333"
  41. }
  42. }
  43. }
  44. }
  45. }