Make the clip example match the NanoVG example

This commit is contained in:
Patrick Walton 2020-04-11 19:08:20 -07:00
parent 3e9558957f
commit fda9b8b9e1
1 changed files with 9 additions and 6 deletions

View File

@ -751,9 +751,10 @@ fn draw_clip(context: &mut CanvasRenderingContext2D, origin: Vector2F, time: f32
Transform2F::from_rotation(angle::angle_from_degrees(5.0)).translate(origin); Transform2F::from_rotation(angle::angle_from_degrees(5.0)).translate(origin);
context.set_transform(&transform_a); context.set_transform(&transform_a);
context.set_fill_style(rgbu(255, 0, 0)); context.set_fill_style(rgbu(255, 0, 0));
let mut clip_path = Path2D::new(); let mut clip_path_a = Path2D::new();
clip_path.rect(RectF::new(vec2f(-20.0, -20.0), vec2f(60.0, 40.0))); let clip_rect_a = RectF::new(vec2f(-20.0, -20.0), vec2f(60.0, 40.0));
context.fill_path(clip_path.clone(), FillRule::Winding); clip_path_a.rect(clip_rect_a);
context.fill_path(clip_path_a, FillRule::Winding);
// Draw second rectangle with no clip. // Draw second rectangle with no clip.
let transform_b = transform_a * Transform2F::from_rotation(time).translate(vec2f(40.0, 0.0)); let transform_b = transform_a * Transform2F::from_rotation(time).translate(vec2f(40.0, 0.0));
@ -763,9 +764,11 @@ fn draw_clip(context: &mut CanvasRenderingContext2D, origin: Vector2F, time: f32
context.fill_rect(fill_rect); context.fill_rect(fill_rect);
// Draw second rectangle with clip. // Draw second rectangle with clip.
context.set_transform(&transform_a); let mut clip_path_b = Path2D::new();
context.clip_path(clip_path, FillRule::Winding); let clip_rect_b = (transform_b.inverse() * transform_a * clip_rect_a).intersection(fill_rect)
context.set_transform(&transform_b); .unwrap_or_default();
clip_path_b.rect(clip_rect_b);
context.clip_path(clip_path_b, FillRule::Winding);
context.set_fill_style(rgbu(255, 128, 0)); context.set_fill_style(rgbu(255, 128, 0));
context.fill_rect(fill_rect); context.fill_rect(fill_rect);