Skip to content

Commit c9235f5

Browse files
committed
Feat: Add zoom functionality to SVG example
1 parent 79cb7a6 commit c9235f5

File tree

1 file changed

+51
-1
lines changed
  • sparse_strips/vello_hybrid/examples

1 file changed

+51
-1
lines changed

sparse_strips/vello_hybrid/examples/svg.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use vello_hybrid::{
2020
use wgpu::RenderPassDescriptor;
2121
use winit::{
2222
application::ApplicationHandler,
23-
event::WindowEvent,
23+
event::{ElementState, KeyEvent, MouseScrollDelta, WindowEvent},
2424
event_loop::{ActiveEventLoop, EventLoop},
25+
keyboard::{Key, NamedKey},
2526
window::{Window, WindowId},
2627
};
2728

@@ -43,6 +44,11 @@ fn main() {
4344
.expect("Couldn't run event loop");
4445
}
4546

47+
// Constants for zoom behavior
48+
const MIN_SCALE: f64 = 0.1;
49+
const MAX_SCALE: f64 = 20.0;
50+
const ZOOM_STEP: f64 = 0.5;
51+
4652
#[derive(Debug)]
4753
enum RenderState<'s> {
4854
/// `RenderSurface` and `Window` for active rendering.
@@ -78,6 +84,13 @@ struct SvgVelloApp<'s> {
7884
parsed_svg: Option<PicoSvg>,
7985
}
8086

87+
impl SvgVelloApp<'_> {
88+
/// Adjust the render scale by the given delta, clamping to min/max values
89+
fn adjust_scale(&mut self, delta: f64) {
90+
self.render_scale = (self.render_scale + delta).clamp(MIN_SCALE, MAX_SCALE);
91+
}
92+
}
93+
8194
impl ApplicationHandler for SvgVelloApp<'_> {
8295
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
8396
let RenderState::Suspended(cached_window) = &mut self.state else {
@@ -148,6 +161,43 @@ impl ApplicationHandler for SvgVelloApp<'_> {
148161
.resize_surface(surface, size.width, size.height);
149162
}
150163

164+
WindowEvent::MouseWheel {
165+
delta: MouseScrollDelta::PixelDelta(pos),
166+
..
167+
} => {
168+
// Convert pixel delta to a scale adjustment
169+
// Divide by a factor to make the zoom less sensitive
170+
self.adjust_scale(pos.y * ZOOM_STEP / 50.0);
171+
}
172+
173+
WindowEvent::PinchGesture { delta, .. } => {
174+
self.adjust_scale(delta * ZOOM_STEP);
175+
}
176+
177+
WindowEvent::KeyboardInput {
178+
event:
179+
KeyEvent {
180+
logical_key,
181+
state: ElementState::Pressed,
182+
..
183+
},
184+
..
185+
} => {
186+
match logical_key {
187+
Key::Character(c) => match c.as_str() {
188+
"+" | "=" => self.adjust_scale(ZOOM_STEP),
189+
"-" | "_" => self.adjust_scale(-ZOOM_STEP),
190+
// Reset to original scale
191+
"0" => {
192+
self.render_scale = 5.0;
193+
}
194+
_ => {}
195+
},
196+
Key::Named(NamedKey::Escape) => event_loop.exit(),
197+
_ => {}
198+
}
199+
}
200+
151201
WindowEvent::RedrawRequested => {
152202
self.scene.reset();
153203

0 commit comments

Comments
 (0)