Skip to main content

sketch_demo/
sketch_demo.rs

1//! 手绘(sketch)风格演示。
2//!
3//! ```text
4//! cargo run --example sketch_demo
5//! ```
6//!
7//! 产出(写在工作目录):
8//! - `sketch_normal.svg`    —— 不开手绘:与改造前完全一致
9//! - `sketch_all.svg/.png`  —— 全量手绘 + 排线填充
10//! - `sketch_selected.svg`  —— **选择性**手绘:节点/图元手绘,网格线与连线保持笔直
11//! - `sketch_fills.svg`     —— 四种填充样式(分四次按名字选择不同的图元)
12//!
13//! 两个要点:
14//! 1. 手绘是**额外开启**的:不调用任何 `roughen_*`,场景与输出完全不变。
15//! 2. 开启后仍可**选择**范围:`SketchOptions::kinds` 按图元类型,`roughen_scene_if` 按谓词。
16
17use 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
54/// 一个"流程图 + 若干图元 + 网格"的场景。
55fn build_scene() -> Scene {
56    let mut scene = Scene::new(W, H);
57
58    // 网格(独立图层,用来演示"网格不参与手绘")。
59    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    // 两个圆角矩形节点 + 连线。
79    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    // 一排几何图元,覆盖 circle / ellipse / polygon / pie。
102    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    // 1) 基线:不调用任何 roughen_* —— 与改造前逐字节一致。
146    let baseline = build_scene();
147    write("sketch_normal.svg", &render_svg(&baseline));
148
149    // 2) 全量手绘 + 排线填充,并重新贴合画布(抖动可能顶到边缘)。
150    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    // 3) 选择性:图元手绘,网格线与连线保持笔直(kinds 排除 Line / Polyline)。
172    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    // 4) 四种填充样式:同一个场景里按 name 选择,分四次各自开启一种样式。
184    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}