main.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use color::{palette::css::WHITE, parse_color};
  2. use color::{OpaqueColor, Srgb};
  3. use demo_renderer::{DemoMessage, DemoPaintSource};
  4. use dioxus::prelude::*;
  5. use dioxus_native::use_wgpu;
  6. use std::any::Any;
  7. use wgpu::{Features, Limits};
  8. mod demo_renderer;
  9. // CSS Styles
  10. static STYLES: Asset = asset!("./src/styles.css");
  11. // WGPU settings required by this example
  12. const FEATURES: Features = Features::PUSH_CONSTANTS;
  13. fn limits() -> Limits {
  14. Limits {
  15. max_push_constant_size: 16,
  16. ..Limits::default()
  17. }
  18. }
  19. type Color = OpaqueColor<Srgb>;
  20. fn main() {
  21. #[cfg(feature = "tracing")]
  22. tracing_subscriber::fmt::init();
  23. let config: Vec<Box<dyn Any>> = vec![Box::new(FEATURES), Box::new(limits())];
  24. dioxus_native::launch_cfg(app, Vec::new(), config);
  25. }
  26. fn app() -> Element {
  27. let mut show_cube = use_signal(|| true);
  28. let color_str = use_signal(|| String::from("red"));
  29. let color = use_memo(move || {
  30. parse_color(&color_str())
  31. .map(|c| c.to_alpha_color())
  32. .unwrap_or(WHITE)
  33. .split()
  34. .0
  35. });
  36. use_effect(move || println!("{:?}", color().components));
  37. rsx!(
  38. document::Link { rel: "stylesheet", href: STYLES }
  39. div { id:"overlay",
  40. h2 { "Control Panel" },
  41. button {
  42. onclick: move |_| show_cube.toggle(),
  43. if show_cube() {
  44. "Hide cube"
  45. } else {
  46. "Show cube"
  47. }
  48. }
  49. br {}
  50. ColorControl { label: "Color:", color_str },
  51. p { "This overlay demonstrates that the custom WGPU content can be rendered beneath layers of HTML content" }
  52. }
  53. div { id:"underlay",
  54. h2 { "Underlay" },
  55. p { "This underlay demonstrates that the custom WGPU content can be rendered above layers and blended with the content underneath" }
  56. }
  57. header {
  58. h1 { "Blitz WGPU Demo" }
  59. }
  60. if show_cube() {
  61. SpinningCube { color }
  62. }
  63. )
  64. }
  65. #[component]
  66. fn ColorControl(label: &'static str, color_str: Signal<String>) -> Element {
  67. rsx!(div {
  68. class: "color-control",
  69. { label },
  70. input {
  71. value: color_str(),
  72. oninput: move |evt| {
  73. *color_str.write() = evt.value()
  74. }
  75. }
  76. })
  77. }
  78. #[component]
  79. fn SpinningCube(color: Memo<Color>) -> Element {
  80. // Create custom paint source and register it with the renderer
  81. let paint_source = DemoPaintSource::new();
  82. let sender = paint_source.sender();
  83. let paint_source_id = use_wgpu(move || paint_source);
  84. use_effect(move || {
  85. sender.send(DemoMessage::SetColor(color())).unwrap();
  86. });
  87. rsx!(
  88. div { id:"canvas-container",
  89. canvas {
  90. id: "demo-canvas",
  91. "src": paint_source_id
  92. }
  93. }
  94. )
  95. }