@@ -19,18 +19,50 @@ use vello_hybrid::Scene;
19
19
20
20
/// Clip scene state
21
21
#[ derive( Debug ) ]
22
- pub struct ClipScene { }
22
+ pub struct ClipScene {
23
+ use_clip_path : bool ,
24
+ num_circles : usize ,
25
+ }
23
26
24
27
impl ExampleScene for ClipScene {
25
28
fn render ( & mut self , ctx : & mut Scene , root_transform : Affine ) {
26
- render ( ctx, root_transform) ;
29
+ render ( ctx, root_transform, self . use_clip_path , self . num_circles ) ;
30
+ }
31
+
32
+ fn handle_key ( & mut self , key : & str ) -> bool {
33
+ match key {
34
+ "c" | "C" => {
35
+ self . toggle_clip ( ) ;
36
+ true
37
+ }
38
+ "m" | "M" => {
39
+ self . add_circle ( ) ;
40
+ true
41
+ }
42
+ _ => false ,
43
+ }
27
44
}
28
45
}
29
46
30
47
impl ClipScene {
31
48
/// Create a new `ClipScene`
32
49
pub fn new ( ) -> Self {
33
- Self { }
50
+ Self {
51
+ use_clip_path : false ,
52
+ num_circles : 1 ,
53
+ }
54
+ }
55
+
56
+ /// Toggle using clip path
57
+ pub fn toggle_clip ( & mut self ) {
58
+ self . use_clip_path = !self . use_clip_path ;
59
+ println ! ( "Use clip path: {}" , self . use_clip_path) ;
60
+ }
61
+
62
+ /// Add another circle to the scene
63
+ pub fn add_circle ( & mut self ) {
64
+ self . num_circles += 1 ;
65
+ println ! ( "Number of circles: {}" , self . num_circles) ;
34
66
}
35
67
}
36
68
@@ -48,7 +80,7 @@ fn draw_clipping_outline(ctx: &mut Scene, path: &BezPath) {
48
80
}
49
81
50
82
/// Draws a deeply nested clip of circles.
51
- pub fn render ( ctx : & mut Scene , root_transform : Affine ) {
83
+ pub fn render ( ctx : & mut Scene , root_transform : Affine , use_clip_path : bool , num_circles : usize ) {
52
84
const INITIAL_RADIUS : f64 = 48.0 ;
53
85
const RADIUS_DECREMENT : f64 = 2.5 ;
54
86
const INNER_COUNT : usize = 10 ;
@@ -68,26 +100,57 @@ pub fn render(ctx: &mut Scene, root_transform: Affine) {
68
100
DARK_GREEN ,
69
101
] ;
70
102
71
- const COVER_RECT : Rect = Rect :: new ( 0.0 , 0.0 , 100.0 , 100.0 ) ;
72
- const CENTER : Point = Point :: new ( 50.0 , 50.0 ) ;
73
- let mut radius = INITIAL_RADIUS ;
103
+ const SPACING : f64 = 120.0 ;
104
+ const BASE_X : f64 = 50.0 ;
105
+ const BASE_Y : f64 = 50.0 ;
74
106
75
107
ctx. set_transform ( root_transform) ;
76
- for _ in 0 ..outer_count {
77
- for color in COLORS . iter ( ) {
78
- let clip_circle = Circle :: new ( CENTER , radius) . to_path ( 0.1 ) ;
79
- draw_clipping_outline ( ctx, & clip_circle) ;
80
- ctx. push_clip_layer ( & clip_circle) ;
81
108
82
- ctx. set_paint ( * color) ;
83
- ctx. fill_rect ( & COVER_RECT ) ;
109
+ // Draw multiple circles in a checkerboard pattern
110
+ for circle_idx in 0 ..num_circles {
111
+ // Calculate checkerboard position
112
+ // Create a grid pattern where circles are placed in a checkerboard layout
113
+ let row = circle_idx / 4 ;
114
+ let col = circle_idx % 4 ;
115
+
116
+ // Create checkerboard offset pattern
117
+ let offset_x = if ( row + col) % 2 == 0 {
118
+ 0.0
119
+ } else {
120
+ SPACING / 2.0
121
+ } ;
122
+ let x = BASE_X + col as f64 * SPACING + offset_x;
123
+ let y = BASE_Y + row as f64 * SPACING ;
124
+
125
+ let center = Point :: new ( x, y) ;
126
+ let cover_rect = Rect :: new ( x - 50.0 , y - 50.0 , x + 50.0 , y + 50.0 ) ;
127
+ let mut radius = INITIAL_RADIUS ;
128
+
129
+ for _ in 0 ..outer_count {
130
+ for color in COLORS . iter ( ) {
131
+ let clip_circle = Circle :: new ( center, radius) . to_path ( 0.1 ) ;
132
+ draw_clipping_outline ( ctx, & clip_circle) ;
133
+ if use_clip_path {
134
+ ctx. push_clip_path ( & clip_circle) ;
135
+ } else {
136
+ ctx. push_clip_layer ( & clip_circle) ;
137
+ }
84
138
85
- radius -= RADIUS_DECREMENT ;
139
+ ctx. set_paint ( * color) ;
140
+ ctx. fill_rect ( & cover_rect) ;
141
+
142
+ radius -= RADIUS_DECREMENT ;
143
+ }
86
144
}
87
- }
88
- for _ in 0 ..outer_count {
89
- for _ in COLORS . iter ( ) {
90
- ctx. pop_layer ( ) ;
145
+
146
+ for _ in 0 ..outer_count {
147
+ for _ in COLORS . iter ( ) {
148
+ if !use_clip_path {
149
+ ctx. pop_layer ( ) ;
150
+ } else {
151
+ ctx. pop_clip_path ( ) ;
152
+ }
153
+ }
91
154
}
92
155
}
93
156
}
0 commit comments