Skip to content

Commit c0fb878

Browse files
authored
Fix/canvas stencil clipping (#185)
* feat: implement profile, profileEnd and timeStamp console methods with logging * feat: implement canvas clipping path with stencil buffer rendering * style: fix lint issue
1 parent 76ad779 commit c0fb878

File tree

4 files changed

+241
-9
lines changed

4 files changed

+241
-9
lines changed

runtime/src/ext/canvas/context2d.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ pub fn internal_canvas_clear_rect<'gc>(
485485
shadow_offset_x: 0.0,
486486
shadow_offset_y: 0.0,
487487
composite_operation: CompositeOperation::default(),
488+
clip_path: None,
488489
},
489490
);
490491
} else {
@@ -595,6 +596,7 @@ pub fn internal_canvas_fill_rect<'gc>(
595596
shadow_offset_x: data.shadow_offset_x,
596597
shadow_offset_y: data.shadow_offset_y,
597598
composite_operation: data.composite_operation,
599+
clip_path: None,
598600
},
599601
);
600602
} else {
@@ -736,6 +738,7 @@ pub fn internal_canvas_fill<'gc>(
736738
shadow_offset_x: data.shadow_offset_x,
737739
shadow_offset_y: data.shadow_offset_y,
738740
composite_operation: data.composite_operation,
741+
clip_path: None,
739742
},
740743
);
741744
}
@@ -788,6 +791,7 @@ pub fn internal_canvas_stroke<'gc>(
788791
shadow_offset_x: data.shadow_offset_x,
789792
shadow_offset_y: data.shadow_offset_y,
790793
composite_operation: data.composite_operation,
794+
clip_path: None,
791795
},
792796
);
793797
}
@@ -1426,6 +1430,7 @@ pub fn process_all_commands<'gc>(
14261430
shadow_offset_x: ctx.shadow_offset_x,
14271431
shadow_offset_y: ctx.shadow_offset_y,
14281432
composite_operation: ctx.composite_operation,
1433+
clip_path: None,
14291434
},
14301435
);
14311436
}
@@ -1446,6 +1451,7 @@ pub fn process_all_commands<'gc>(
14461451
shadow_offset_x: ctx.shadow_offset_x,
14471452
shadow_offset_y: ctx.shadow_offset_y,
14481453
composite_operation: ctx.composite_operation,
1454+
clip_path: None,
14491455
},
14501456
ctx.line_width,
14511457
);
@@ -1483,6 +1489,7 @@ pub fn process_all_commands<'gc>(
14831489
shadow_offset_x: ctx.shadow_offset_x,
14841490
shadow_offset_y: ctx.shadow_offset_y,
14851491
composite_operation: ctx.composite_operation,
1492+
clip_path: None,
14861493
},
14871494
);
14881495
}
@@ -1528,6 +1535,7 @@ pub fn process_all_commands<'gc>(
15281535
shadow_offset_x: ctx.shadow_offset_x,
15291536
shadow_offset_y: ctx.shadow_offset_y,
15301537
composite_operation: ctx.composite_operation,
1538+
clip_path: None,
15311539
},
15321540
ctx.line_width,
15331541
);
@@ -1575,6 +1583,7 @@ pub fn process_all_commands<'gc>(
15751583
shadow_offset_x: 0.0,
15761584
shadow_offset_y: 0.0,
15771585
composite_operation: CompositeOperation::default(),
1586+
clip_path: None,
15781587
},
15791588
); // White background
15801589
}
@@ -1598,10 +1607,9 @@ pub fn process_all_commands<'gc>(
15981607
// Update stroke style
15991608
// Note: The actual style change is handled by the renderer
16001609
}
1601-
CanvasCommand::Clip { path: _ } => {
1602-
// Apply clipping to the current renderer state
1603-
// TODO: Implement proper clipping in the renderer
1604-
// For now, this is a placeholder that stores the clipping path
1610+
CanvasCommand::Clip { path } => {
1611+
// Apply clipping to the renderer
1612+
renderer.set_clip_path(Some(path.clone()));
16051613
}
16061614
CanvasCommand::CreateLinearGradient { .. }
16071615
| CanvasCommand::CreateRadialGradient { .. }
@@ -1633,6 +1641,7 @@ pub fn process_all_commands<'gc>(
16331641
shadow_offset_x: ctx.shadow_offset_x,
16341642
shadow_offset_y: ctx.shadow_offset_y,
16351643
composite_operation: ctx.composite_operation,
1644+
clip_path: None,
16361645
};
16371646

16381647
// Render the image

runtime/src/ext/canvas/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,7 @@ impl CanvasExt {
23042304
shadow_offset_x: 0.0,
23052305
shadow_offset_y: 0.0,
23062306
composite_operation: renderer::CompositeOperation::default(),
2307+
clip_path: None,
23072308
};
23082309

23092310
renderer.render_stroke_rect(rect, &render_state, stroke_color, line_width as f32);
@@ -2687,14 +2688,15 @@ impl CanvasExt {
26872688
fill_style: data.fill_style,
26882689
global_alpha: data.global_alpha,
26892690
transform: data.transform,
2690-
line_cap: renderer::LineCap::default(),
2691-
line_join: renderer::LineJoin::default(),
2692-
miter_limit: 10.0,
2691+
line_cap: data.line_cap,
2692+
line_join: data.line_join,
2693+
miter_limit: data.miter_limit,
26932694
shadow_blur: data.shadow_blur,
26942695
shadow_color: data.shadow_color,
26952696
shadow_offset_x: data.shadow_offset_x,
26962697
shadow_offset_y: data.shadow_offset_y,
26972698
composite_operation: data.composite_operation,
2699+
clip_path: None,
26982700
};
26992701

27002702
// Render the image
@@ -3772,6 +3774,7 @@ impl CanvasExt {
37723774
shadow_offset_x: canvas.shadow_offset_x,
37733775
shadow_offset_y: canvas.shadow_offset_y,
37743776
composite_operation: canvas.composite_operation,
3777+
clip_path: None,
37753778
};
37763779

37773780
// Calculate baseline adjustment
@@ -3897,6 +3900,7 @@ impl CanvasExt {
38973900
shadow_offset_x: canvas.shadow_offset_x,
38983901
shadow_offset_y: canvas.shadow_offset_y,
38993902
composite_operation: canvas.composite_operation,
3903+
clip_path: None,
39003904
};
39013905

39023906
let baseline_offset = calculate_baseline_offset(

0 commit comments

Comments
 (0)