1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! End-to-end visual demonstration of the v1.5 polyline-primitive
//! channels on `LineGeom` and `SegmentGeom`.
//!
//! Three renders:
//! - `polyline_1_corner_radius.png` — four identical zigzag polylines
//! stacked vertically, each with a different `"corner_radius"`. Goes
//! from sharp (`0pt`) through to heavily filleted (`24pt`).
//! - `polyline_2_corner_max_angle.png` — same mixed-angle polyline
//! drawn twice. The top copy uses the default `max_angle = ∞` and
//! rounds every corner; the bottom copy uses `max_angle = 60°` and
//! leaves the obtuse corners sharp while still rounding the acute
//! ones.
//! - `polyline_3_endpoint_clip.png` — a small node-and-edge graph.
//! The edges are `SegmentGeom`; `clip_start_radius` /
//! `clip_end_radius` (matched to the node's pt radius) pull each
//! connector back to the visual node boundary so segments don't
//! shoot through the dots.
use hephaestus::backend::vello::VelloRenderer;
use hephaestus::color::{rgb8, rgba, Color};
use hephaestus::composition::{Composition, Patch, Span};
use hephaestus::geometry::Size;
use hephaestus::plot::{LineGeom, Plot, PlotComposition, PointGeom, Raw, SegmentGeom, TextGeom};
use hephaestus::scene::SceneBuilder;
use hephaestus::Renderer;
fn main() {
let (w, h) = (1200u32, 500u32);
let dpi = 96.0;
let comp = || Composition::empty(1, 1).place(1, 1, Span::cell(), Patch::new("panel"));
let mut renderer = VelloRenderer::new().expect("vello renderer init");
let bg: Color = rgb8(248, 248, 252);
// ── Render 1: corner_radius progression ──────────────────────────
{
// Same zigzag shape stacked at four y positions, each with a
// different corner_radius value. Vertices live in panel-fraction
// space — `Raw` bypasses any scale binding.
let radii_pt = [0.0_f64, 6.0, 14.0, 24.0];
let y_rows = [0.84_f64, 0.62, 0.40, 0.18];
let mut plot = Plot::new(&comp(), "panel");
for (i, &y) in y_rows.iter().enumerate() {
let dy = 0.07;
let xs = vec![
0.18_f64, 0.30, 0.30, 0.46, 0.46, 0.62, 0.62, 0.78, 0.78, 0.94,
];
let ys = vec![y, y, y + dy, y + dy, y - dy, y - dy, y + dy, y + dy, y, y];
plot.add_geom(
LineGeom::builder()
.keys(vec![i as i64; xs.len()])
.set("x", Raw(xs))
.set("y", Raw(ys))
.set("stroke", rgb8(70, 100, 180))
.set("linewidth", 3.0_f64)
.set("cap", "round")
.set("join", "round")
.set("corner_radius", radii_pt[i])
.build(),
);
plot.add_geom(
TextGeom::builder()
.set("x", Raw(vec![0.02_f64]))
.set("y", Raw(vec![y]))
.set(
"text",
vec![format!("corner_radius = {}pt", radii_pt[i] as i32)],
)
.set("anchor_x", 0.0_f64)
.set("anchor_y", 0.5_f64)
.set("size", 13.0_f64)
.set("fill", rgb8(40, 40, 50))
.build(),
);
}
let mut view = PlotComposition::new(&comp()).with_plot(plot);
render_to(
&mut renderer,
&mut view,
w,
h,
dpi,
bg,
"examples/polyline_1_corner_radius.png",
);
}
// ── Render 2: corner_max_angle filter ────────────────────────────
{
// A polyline that alternates between gentle and sharp peaks.
// With x-step = 0.10 between adjacent peaks:
// y-step = 0.025 → interior angle ≈ 152° (obtuse)
// y-step = 0.20 → interior angle ≈ 53° (acute)
// The top copy rounds every corner (default `max_angle = ∞`);
// the bottom copy only rounds the acute corners (those with
// interior angle ≤ 60°), leaving the obtuse ones sharp.
let mixed_shape = |y_base: f64| {
let xs = vec![0.10_f64, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90];
let pattern = [0.0_f64, 0.025, 0.0, -0.20, 0.0, 0.025, 0.0, -0.20, 0.0];
let ys = pattern.iter().map(|p| y_base + p).collect::<Vec<_>>();
(xs, ys)
};
let mut plot = Plot::new(&comp(), "panel");
let (x_top, y_top) = mixed_shape(0.75);
plot.add_geom(
LineGeom::builder()
.set("x", Raw(x_top))
.set("y", Raw(y_top))
.set("stroke", rgb8(180, 70, 90))
.set("linewidth", 3.0_f64)
.set("cap", "round")
.set("join", "round")
.set("corner_radius", 28.0_f64)
.build(),
);
plot.add_geom(
TextGeom::builder()
.set("x", Raw(vec![0.5_f64]))
.set("y", Raw(vec![0.92_f64]))
.set(
"text",
vec!["corner_max_angle = ∞ (default) — every corner rounds"],
)
.set("anchor_x", 0.5_f64)
.set("anchor_y", 0.5_f64)
.set("size", 13.0_f64)
.set("weight", 600.0_f64)
.set("fill", rgb8(180, 70, 90))
.build(),
);
let (x_bot, y_bot) = mixed_shape(0.30);
plot.add_geom(
LineGeom::builder()
.set("x", Raw(x_bot))
.set("y", Raw(y_bot))
.set("stroke", rgb8(60, 140, 100))
.set("linewidth", 3.0_f64)
.set("cap", "round")
.set("join", "round")
.set("corner_radius", 28.0_f64)
.set("corner_max_angle", 60.0_f64)
.build(),
);
plot.add_geom(
TextGeom::builder()
.set("x", Raw(vec![0.5_f64]))
.set("y", Raw(vec![0.08_f64]))
.set(
"text",
vec!["corner_max_angle = 60° — only acute corners round"],
)
.set("anchor_x", 0.5_f64)
.set("anchor_y", 0.5_f64)
.set("size", 13.0_f64)
.set("weight", 600.0_f64)
.set("fill", rgb8(60, 140, 100))
.build(),
);
// Use a square panel for this render so the geometry-defined
// interior angles match what you see — corner rounding works in
// panel-pixel space, not panel-fraction space.
let mut view = PlotComposition::new(&comp()).with_plot(plot);
render_to(
&mut renderer,
&mut view,
600,
600,
dpi,
bg,
"examples/polyline_2_corner_max_angle.png",
);
}
// ── Render 3: endpoint-clipped network edges ─────────────────────
{
// A tiny node-and-edge graph: five nodes drawn as PointGeom
// circles, six edges drawn as SegmentGeoms with both endpoints
// clipped to the node radius. Without the clip the segments
// would run from centre to centre and dive into the dots.
let node_xs = vec![0.15_f64, 0.40, 0.40, 0.65, 0.85];
let node_ys = vec![0.50_f64, 0.20, 0.80, 0.50, 0.50];
let node_radius_pt = 14.0_f64;
let node_diameter_pt = node_radius_pt * 2.0;
let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (0, 3)];
let seg_x: Vec<f64> = edges.iter().map(|(a, _)| node_xs[*a]).collect();
let seg_y: Vec<f64> = edges.iter().map(|(a, _)| node_ys[*a]).collect();
let seg_x2: Vec<f64> = edges.iter().map(|(_, b)| node_xs[*b]).collect();
let seg_y2: Vec<f64> = edges.iter().map(|(_, b)| node_ys[*b]).collect();
let mut plot = Plot::new(&comp(), "panel");
// Edges first so the nodes draw on top.
plot.add_geom(
SegmentGeom::builder()
.set("x", Raw(seg_x))
.set("y", Raw(seg_y))
.set("x2", Raw(seg_x2))
.set("y2", Raw(seg_y2))
.set("x_band", 0.0_f64)
.set("x2_band", 0.0_f64)
.set("y_band", 0.0_f64)
.set("y2_band", 0.0_f64)
.set("stroke", rgb8(110, 120, 140))
.set("linewidth", 2.0_f64)
.set("cap", "round")
.set("clip_start_radius", node_radius_pt)
.set("clip_end_radius", node_radius_pt)
.build(),
);
// Nodes — fill only. PointGeom's stroke is currently applied
// after the size affine, so a stroked large glyph swallows the
// fill; we keep the demo to fills only to avoid that
// pre-existing issue.
plot.add_geom(
PointGeom::builder()
.set("x", Raw(node_xs.clone()))
.set("y", Raw(node_ys.clone()))
.set("fill", rgb8(220, 100, 80))
.set("size", node_diameter_pt)
.build(),
);
// Title strip.
plot.add_geom(
TextGeom::builder()
.set("x", Raw(vec![0.5_f64]))
.set("y", Raw(vec![0.95_f64]))
.set(
"text",
vec!["SegmentGeom: clip_*_radius trims edges to node boundary"],
)
.set("anchor_x", 0.5_f64)
.set("anchor_y", 0.5_f64)
.set("size", 14.0_f64)
.set("weight", 600.0_f64)
.set("fill", rgb8(40, 40, 50))
.set("bg_fill", rgba(1.0, 1.0, 1.0, 0.85))
.set("bg_padding", 6.0_f64)
.set("bg_corner_radius", 4.0_f64)
.build(),
);
let mut view = PlotComposition::new(&comp()).with_plot(plot);
render_to(
&mut renderer,
&mut view,
w,
h,
dpi,
bg,
"examples/polyline_3_endpoint_clip.png",
);
}
}
fn render_to(
renderer: &mut VelloRenderer,
view: &mut PlotComposition,
w: u32,
h: u32,
dpi: f64,
bg: Color,
out_relative: &str,
) {
{
let scene = renderer.scene();
scene.clear();
view.render(scene, Size::new(w as f64, h as f64), dpi);
}
let mut pixels = vec![0u8; (w * h * 4) as usize];
renderer
.render_to_buffer(w, h, bg, &mut pixels)
.expect("render");
let path = std::env::current_dir().unwrap().join(out_relative);
hephaestus::image::write_png(&path, w, h, &pixels).expect("write png");
println!("wrote {}", path.display());
}