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

Function main

draw/examples/gradient.rs:10–65  ·  view source on GitHub ↗

Displays a circle with a linear gradient fill

()

Source from the content-addressed store, hash-verified

8/// Displays a circle with a linear gradient fill
9///
10pub fn main() {
11 with_2d_graphics(|| {
12 // Create a window
13 let canvas = create_drawing_window("Gradient");
14
15 let mut angle = 0.0;
16
17 loop {
18 // Draw a circle
19 canvas.draw(|gc| {
20 gc.layer(LayerId(0));
21 gc.clear_layer();
22
23 // Set up the canvas
24 gc.canvas_height(1000.0);
25 gc.center_region(0.0, 0.0, 1000.0, 1000.0);
26
27 // Set up a gradient
28 gc.create_gradient(GradientId(1), Color::Rgba(0.8, 0.0, 0.0, 1.0));
29 gc.gradient_stop(GradientId(1), 0.33, Color::Rgba(0.3, 0.8, 0.0, 1.0));
30 gc.gradient_stop(GradientId(1), 0.66, Color::Rgba(0.0, 0.3, 0.8, 1.0));
31 gc.gradient_stop(GradientId(1), 1.0, Color::Rgba(0.6, 0.3, 0.9, 1.0));
32
33 let x1 = 500.0 - 300.0*f32::cos(angle);
34 let y1 = 500.0 - 300.0*f32::sin(angle);
35 let x2 = 500.0 + 300.0*f32::cos(angle);
36 let y2 = 500.0 + 300.0*f32::sin(angle);
37
38 // Draw a circle using the gradient
39 gc.new_path();
40 gc.circle(500.0, 500.0, 250.0);
41 gc.fill_gradient(GradientId(1), x1, y1, x2, y2);
42 gc.fill();
43
44 gc.line_width(4.0);
45 gc.stroke_color(Color::Rgba(0.0, 0.0, 0.0, 1.0));
46 gc.stroke();
47
48 // Draw indicators where the gradient is moving between
49 gc.line_width(1.0);
50
51 gc.new_path();
52 gc.circle(x1, y1, 8.0);
53 gc.stroke();
54
55 gc.new_path();
56 gc.circle(x2, y2, 8.0);
57 gc.stroke();
58 });
59
60 // Wait a frame
61 thread::sleep(Duration::from_nanos(1_000_000_000 / 60));
62 angle += 0.01;
63 }
64 });
65}

Callers

nothing calls this directly

Calls 15

with_2d_graphicsFunction · 0.85
create_drawing_windowFunction · 0.85
LayerIdClass · 0.85
GradientIdClass · 0.85
clear_layerMethod · 0.80
canvas_heightMethod · 0.80
center_regionMethod · 0.80
create_gradientMethod · 0.80
gradient_stopMethod · 0.80
new_pathMethod · 0.80
circleMethod · 0.80
fill_gradientMethod · 0.80

Tested by

no test coverage detected