MCPcopy Create free account
hub / github.com/Logicalshift/flo_draw / main

Function main

draw/examples/wibble.rs:19–100  ·  view source on GitHub ↗

Demonstrates capturing the paths for text rendering, and then distorting them using flo_curves

()

Source from the content-addressed store, hash-verified

17/// Demonstrates capturing the paths for text rendering, and then distorting them using flo_curves
18///
19pub fn main() {
20 with_2d_graphics(|| {
21 let lato = CanvasFontFace::from_slice(include_bytes!("Lato-Regular.ttf"));
22
23 // Create a window
24 let canvas = create_drawing_window("Wibble");
25
26 // Measure the text
27 let text_string = "Wibble";
28 let wibble_size = measure_text(&lato, text_string, 200.0);
29 let (min, max) = wibble_size.inner_bounds;
30
31 let x_pos = (1000.0 - (max.x()-min.x()))/2.0;
32 let y_pos = 400.0;
33
34 // Render the text to a set of paths
35 let mut render_text = vec![];
36 render_text.define_font_data(FontId(1), Arc::clone(&lato));
37 render_text.set_font_size(FontId(1), 200.0);
38 render_text.draw_text(FontId(1), text_string.to_string(), x_pos as _, y_pos as _);
39
40 // Lay out the text, convert the glyphs to paths, convert the drawing instructions to SimpleBezierPaths
41 let render_text = stream::iter(render_text.into_iter());
42 let text_paths = drawing_with_laid_out_text(render_text);
43 let text_paths = drawing_with_text_as_paths(text_paths);
44 let text_paths = drawing_to_paths::<SimpleBezierPath, _>(text_paths);
45 let text_paths = executor::block_on(async move { text_paths.collect::<Vec<_>>().await });
46
47 // Draw the text with a moving distortion
48 let start_time = Instant::now();
49
50 loop {
51 // Get the current time where we're rendering this
52 let since_start = Instant::now().duration_since(start_time);
53 let since_start = since_start.as_nanos() as f64;
54 let amplitude = 12.0;
55
56 // Distort each of the paths in turn
57 let distorted_text_paths = text_paths.iter()
58 .map(|path_set| path_set.iter()
59 .map(move |path: &SimpleBezierPath| distort_path::<_, _, SimpleBezierPath>(path, |point: Coord2, _curve, _t| {
60 let distance = point.magnitude();
61 let ripple = (since_start / (f64::consts::PI * 500_000_000.0)) * 10.0;
62
63 let offset_x = (distance / (f64::consts::PI*5.0) + ripple).sin() * amplitude * 0.5;
64 let offset_y = (distance / (f64::consts::PI*4.0) + ripple).cos() * amplitude * 0.5;
65
66 Coord2(point.x() + offset_x, point.y() + offset_y)
67 }, 1.0, 0.1).unwrap())
68 .collect::<Vec<_>>());
69
70 // Render the current frame
71 canvas.draw(|gc| {
72 // Clear the canvas
73 gc.clear_canvas(Color::Rgba(0.7, 0.9, 0.9, 1.0));
74 gc.canvas_height(1000.0);
75 gc.center_region(0.0, 0.0, 1000.0, 1000.0);
76

Callers

nothing calls this directly

Calls 15

with_2d_graphicsFunction · 0.85
create_drawing_windowFunction · 0.85
measure_textFunction · 0.85
FontIdClass · 0.85
define_font_dataMethod · 0.80
set_font_sizeMethod · 0.80
draw_textMethod · 0.80
to_stringMethod · 0.80
mapMethod · 0.80
clear_canvasMethod · 0.80

Tested by

no test coverage detected