1#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use std::sync::Arc;
7use strata_dom::{Attribute, ClassList, NodeBlueprint, Tag, View};
8use strata_macro::component;
9pub use strata_render::canvas2d::{
10 Canvas2DContext, CanvasCommand, CanvasLineCap, CanvasLineJoin, CanvasPathVerb, CanvasTextAlign,
11 CanvasTextBaseline, CanvasTextMetrics, IntoRoundRectRadii,
12};
13
14#[component]
19pub fn Canvas2D(
20 width: Option<f32>,
21 height: Option<f32>,
22 class: Option<&'static str>,
23 on_draw: Option<Rc<dyn Fn(&mut Canvas2DContext)>>,
24) -> View {
25 let w = width.unwrap_or(400.0);
26 let h = height.unwrap_or(300.0);
27 let custom = class.unwrap_or("");
28
29 let mut ctx = Canvas2DContext::new(w, h);
30 if let Some(draw_fn) = &on_draw {
31 draw_fn(&mut ctx);
32 }
33 let commands: Arc<Vec<CanvasCommand>> = Arc::new(ctx.finish());
34 let payload: Arc<dyn std::any::Any + Send + Sync> = commands;
35
36 let mut attrs = Vec::with_capacity(4);
37 attrs.push(Attribute::property("width", w.to_string()));
38 attrs.push(Attribute::property("height", h.to_string()));
39 let mut style_str = String::new();
40 if let Some(ew) = width {
41 style_str.push_str(&format!("width: {}px; ", ew));
42 }
43 if let Some(eh) = height {
44 style_str.push_str(&format!("height: {}px; ", eh));
45 }
46 if !style_str.is_empty() {
47 attrs.push(Attribute::property("style", style_str.trim()));
48 }
49 attrs.push(Attribute::scene_payload_arc(payload));
50
51 let class_str = if custom.is_empty() {
52 "relative overflow-hidden bg-neutral-950 rounded-xl border border-neutral-800/80".to_string()
53 } else {
54 format!(
55 "relative overflow-hidden bg-neutral-950 rounded-xl border border-neutral-800/80 {}",
56 custom
57 )
58 };
59
60 View::new(NodeBlueprint::element(
61 Tag::Div,
62 ClassList::from(vec![class_str]),
63 attrs,
64 Vec::new(),
65 ))
66}