1use lievisual::render::{SvgRenderer, VelloPixmapRenderer};
18use lievisual::scene::{Element, FillStrokeStyle, Layer, Scene, SceneNode, Stroke};
19use lievisual::sketch::{
20 SketchFillStyle, SketchKind, SketchKinds, SketchOptions, roughen_scene, roughen_scene_fit,
21 roughen_scene_if,
22};
23use lievisual::text::{TextAlign, TextStyle};
24use lievisual::{Color, Point, Rect, Vec2};
25
26const W: f64 = 560.0;
27const H: f64 = 250.0;
28
29fn node_fill() -> Color {
30 Color::rgb(0xEC, 0xEC, 0xFF)
31}
32fn ink() -> Color {
33 Color::rgb(0x33, 0x33, 0x33)
34}
35fn grid_color() -> Color {
36 Color::rgb(0xDD, 0xDD, 0xDD)
37}
38
39fn node_style(fill: Color) -> FillStrokeStyle {
40 FillStrokeStyle {
41 fill: Some(fill.into()),
42 stroke: Some(Stroke::new(ink(), 1.5)),
43 }
44}
45
46fn label(text: &str, at: Point) -> Element {
47 Element::text(
48 text,
49 at,
50 TextStyle::new(ink(), 16.0, "sans-serif").with_align(TextAlign::Center),
51 )
52}
53
54fn build_scene() -> Scene {
56 let mut scene = Scene::new(W, H);
57
58 let mut grid = Layer::new("grid");
60 for i in 0..=7 {
61 let x = i as f64 * 80.0;
62 grid.push(Element::line(
63 Point::new(x, 0.0),
64 Point::new(x, H),
65 Stroke::new(grid_color(), 1.0),
66 ));
67 }
68 for j in 0..=3 {
69 let y = j as f64 * 80.0;
70 grid.push(Element::line(
71 Point::new(0.0, y),
72 Point::new(W, y),
73 Stroke::new(grid_color(), 1.0),
74 ));
75 }
76 scene.push_layer(grid);
77
78 scene.push(Element::rounded_rect(
80 Rect::new(40.0, 40.0, 190.0, 100.0),
81 10.0,
82 node_style(node_fill()),
83 ));
84 scene.push(label("开始", Point::new(115.0, 76.0)));
85 scene.push(Element::rounded_rect(
86 Rect::new(330.0, 40.0, 480.0, 100.0),
87 10.0,
88 node_style(node_fill()),
89 ));
90 scene.push(label("结束", Point::new(405.0, 76.0)));
91 scene.push(Element::poly(
92 vec![
93 Point::new(190.0, 70.0),
94 Point::new(260.0, 70.0),
95 Point::new(260.0, 160.0),
96 Point::new(330.0, 160.0),
97 ],
98 Stroke::new(ink(), 2.0),
99 ));
100
101 scene.push(Element::circle(
103 Point::new(80.0, 180.0),
104 26.0,
105 node_style(Color::rgb(0xFF, 0xE0, 0xE0)),
106 ));
107 scene.push(Element::ellipse(
108 Point::new(200.0, 180.0),
109 Vec2::new(44.0, 22.0),
110 0.0,
111 node_style(Color::rgb(0xE0, 0xFF, 0xE0)),
112 ));
113 scene.push(Element::polygon(
114 vec![
115 Point::new(320.0, 150.0),
116 Point::new(360.0, 180.0),
117 Point::new(320.0, 210.0),
118 Point::new(280.0, 180.0),
119 ],
120 node_style(Color::rgb(0xFF, 0xF3, 0xD6)),
121 ));
122 scene.push(Element::pie(
123 Point::new(460.0, 180.0),
124 34.0,
125 0.35,
126 4.6,
127 node_style(Color::rgb(0xEF, 0xE0, 0xFF)),
128 ));
129
130 scene
131}
132
133fn write(name: &str, svg: &str) {
134 std::fs::write(name, svg).expect("写文件失败");
135 println!(" {name:<22} {:>7} 字节", svg.len());
136}
137
138fn render_svg(scene: &Scene) -> String {
139 let mut r = SvgRenderer::new(scene.width, scene.height).with_background(scene.background);
140 scene.render(&mut r);
141 r.into_string()
142}
143
144fn main() {
145 let baseline = build_scene();
147 write("sketch_normal.svg", &render_svg(&baseline));
148
149 let mut all = build_scene();
151 roughen_scene_fit(
152 &mut all,
153 &SketchOptions::new()
154 .with_seed(42)
155 .with_fill(SketchFillStyle::Hachure),
156 8.0,
157 );
158 write("sketch_all.svg", &render_svg(&all));
159 let mut png = VelloPixmapRenderer::new(all.width.round() as u32, all.height.round() as u32)
160 .with_background(Color::WHITE);
161 let bytes = png.render_png(&all);
162 std::fs::write("sketch_all.png", &bytes).expect("写 PNG 失败");
163 println!(
164 " {:<22} {:>7} 字节 (画布 {}×{})",
165 "sketch_all.png",
166 bytes.len(),
167 all.width.round(),
168 all.height.round()
169 );
170
171 let mut selected = build_scene();
173 roughen_scene(
174 &mut selected,
175 &SketchOptions::new().with_seed(7).with_kinds(
176 SketchKinds::ALL
177 .without(SketchKind::Line)
178 .without(SketchKind::Polyline),
179 ),
180 );
181 write("sketch_selected.svg", &render_svg(&selected));
182
183 let mut fills = Scene::new(520.0, 140.0);
185 for (i, name) in ["hachure", "cross", "zigzag", "dots"]
186 .into_iter()
187 .enumerate()
188 {
189 let x = 20.0 + i as f64 * 125.0;
190 fills.push(
191 SceneNode::new(Element::rect(
192 Rect::new(x, 20.0, x + 100.0, 110.0),
193 node_style(Color::rgb(0xD6, 0xE4, 0xF7)),
194 ))
195 .with_name(name),
196 );
197 }
198 for (name, style) in [
199 ("hachure", SketchFillStyle::Hachure),
200 ("cross", SketchFillStyle::CrossHatch),
201 ("zigzag", SketchFillStyle::Zigzag),
202 ("dots", SketchFillStyle::Dots),
203 ] {
204 let want = name.to_string();
205 roughen_scene_if(
206 &mut fills,
207 &SketchOptions::new().with_seed(3).with_fill(style),
208 &move |node| node.name.as_deref() == Some(want.as_str()),
209 );
210 }
211 write("sketch_fills.svg", &render_svg(&fills));
212
213 println!("\n说明:手绘默认关闭;开启后可用 kinds / 谓词选择范围,文本与图片永不参与。");
214}