LibWeb+LibGfx: Implement shadowBlur for Canvas2D

This commit is contained in:
Lucien Fiorini 2024-12-03 21:22:44 +01:00 committed by Alexander Kalenik
commit e8cc0dc998
Notes: github-actions[bot] 2024-12-05 16:08:13 +00:00
8 changed files with 73 additions and 19 deletions

View file

@ -26,6 +26,9 @@ public:
virtual float shadow_offset_y() const = 0;
virtual void set_shadow_offset_y(float offsetY) = 0;
virtual float shadow_blur() const = 0;
virtual void set_shadow_blur(float offsetY) = 0;
virtual String shadow_color() const = 0;
virtual void set_shadow_color(String color) = 0;

View file

@ -3,6 +3,6 @@ interface mixin CanvasShadowStyles {
// shadows
attribute unrestricted double shadowOffsetX; // (default 0)
attribute unrestricted double shadowOffsetY; // (default 0)
[FIXME] attribute unrestricted double shadowBlur; // (default 0)
attribute unrestricted double shadowBlur; // (default 0)
attribute DOMString shadowColor; // (default transparent black)
};

View file

@ -82,6 +82,7 @@ public:
FillOrStrokeStyle stroke_style { Gfx::Color::Black };
float shadow_offset_x { 0.0f };
float shadow_offset_y { 0.0f };
float shadow_blur { 0.0f };
Gfx::Color shadow_color { Gfx::Color::Transparent };
float line_width { 1 };
Bindings::CanvasLineCap line_cap { Bindings::CanvasLineCap::Butt };

View file

@ -782,6 +782,22 @@ void CanvasRenderingContext2D::set_shadow_offset_y(float offsetY)
drawing_state().shadow_offset_y = offsetY;
}
float CanvasRenderingContext2D::shadow_blur() const
{
return drawing_state().shadow_blur;
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-shadowblur
void CanvasRenderingContext2D::set_shadow_blur(float blur_radius)
{
// On setting, the attribute must be set to the new value,
// except if the value is negative, infinite or NaN, in which case the new value must be ignored.
if (blur_radius < 0 || isinf(blur_radius) || isnan(blur_radius))
return;
drawing_state().shadow_blur = blur_radius;
}
String CanvasRenderingContext2D::shadow_color() const
{
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-shadowcolor
@ -824,7 +840,7 @@ void CanvasRenderingContext2D::paint_shadow_for_fill_internal(Gfx::Path const& p
Gfx::AffineTransform transform;
transform.translate(state.shadow_offset_x, state.shadow_offset_y);
painter->set_transform(transform);
painter->fill_path(path_to_fill, state.shadow_color.with_opacity(state.global_alpha), winding_rule);
painter->fill_path(path_to_fill, state.shadow_color.with_opacity(state.global_alpha), winding_rule, state.shadow_blur);
painter->restore();
@ -844,7 +860,7 @@ void CanvasRenderingContext2D::paint_shadow_for_stroke_internal(Gfx::Path const&
Gfx::AffineTransform transform;
transform.translate(state.shadow_offset_x, state.shadow_offset_y);
painter->set_transform(transform);
painter->stroke_path(path, state.shadow_color.with_opacity(state.global_alpha), state.line_width);
painter->stroke_path(path, state.shadow_color.with_opacity(state.global_alpha), state.line_width, state.shadow_blur);
painter->restore();

View file

@ -105,6 +105,8 @@ public:
virtual void set_shadow_offset_x(float) override;
virtual float shadow_offset_y() const override;
virtual void set_shadow_offset_y(float) override;
virtual float shadow_blur() const override;
virtual void set_shadow_blur(float) override;
virtual String shadow_color() const override;
virtual void set_shadow_color(String) override;