Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shadow blur 0 makes text shadow not render at all #946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,18 @@ test('shadow-blur-with-translate', async (t) => {
ctx.strokeRect(-50, -50, 200, 100)
await snapshotImage(t, { ctx, canvas })
})

// https://github.com/Brooooooklyn/canvas/issues/857
test('shadow-blur-zero-with-text', async (t) => {
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'iosevka-slab-regular.ttf'))
const canvas = createCanvas(500, 500)
const ctx = canvas.getContext('2d')
ctx.font = '48px Iosevka Slab'
ctx.shadowBlur = 0
ctx.shadowOffsetX = 20
ctx.shadowOffsetY = 20
ctx.shadowColor = 'red'
ctx.fillStyle = 'green'
ctx.fillText('TEST', 100, 100)
await snapshotImage(t, { ctx, canvas })
})
Binary file added __test__/snapshots/shadow-blur-zero-with-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 22 additions & 15 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,21 +675,28 @@ impl Context {
let r = shadow_color.red;
let g = shadow_color.green;
let b = shadow_color.blue;
let transform = state.transform.get_transform();
let sigma_x = state.shadow_blur / (2f32 * transform.scale_x());
let sigma_y = state.shadow_blur / (2f32 * transform.scale_y());
let shadow_effect = ImageFilter::make_drop_shadow_only(
0.0,
0.0,
sigma_x,
sigma_y,
(a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | b as u32,
None,
)?;
drop_shadow_paint.set_alpha(shadow_alpha);
drop_shadow_paint.set_image_filter(&shadow_effect);
let blur_effect = MaskFilter::make_blur(state.shadow_blur / 2f32)?;
drop_shadow_paint.set_mask_filter(&blur_effect);
if state.shadow_blur == 0f32 {
// No blur, so set the paint color to the shadow color without any blur effects
drop_shadow_paint.set_color(r, g, b, a);
} else {
let transform = state.transform.get_transform();
let sigma_x = state.shadow_blur / (2f32 * transform.scale_x());
let sigma_y = state.shadow_blur / (2f32 * transform.scale_y());
// If sigma_x and sigma_y are zero, make_drop_shadow_only will return None
// So we need to handle that case separately
let shadow_effect = ImageFilter::make_drop_shadow_only(
0.0,
0.0,
sigma_x,
sigma_y,
(a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | b as u32,
None,
)?;
drop_shadow_paint.set_alpha(shadow_alpha);
drop_shadow_paint.set_image_filter(&shadow_effect);
let blur_effect = MaskFilter::make_blur(state.shadow_blur / 2f32)?;
drop_shadow_paint.set_mask_filter(&blur_effect);
}
Some(drop_shadow_paint)
}

Expand Down