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: shadowBlur position #945

Merged
merged 1 commit into from
Nov 19, 2024
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
14 changes: 14 additions & 0 deletions __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,17 @@ test('restore from scale(0, 0)', (t) => {
ctx.restore()
})
})

// https://github.com/Brooooooklyn/canvas/issues/856
test('shadow-blur-with-translate', 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.shadowColor = 'rgb(255, 0, 0)'
ctx.shadowBlur = 10
ctx.translate(50, 50)
ctx.fillText('TEST', 0, 0)
ctx.strokeRect(-50, -50, 200, 100)
await snapshotImage(t, { ctx, canvas })
})
Binary file added __test__/snapshots/shadow-blur-with-translate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,9 @@ impl Context {
shadow_offset_x: f32,
shadow_offset_y: f32,
) -> result::Result<(), SkError> {
let current_transform = canvas.get_transform_matrix();
let invert = current_transform
.invert()
.ok_or_else(|| SkError::Generic("Invert matrix failed".to_owned()))?;
canvas.concat(&invert);
let mut shadow_offset = current_transform.clone();
shadow_offset.pre_translate(shadow_offset_x, shadow_offset_y);
canvas.concat(&shadow_offset);
canvas.concat(&current_transform);
let mut shadow_transform = canvas.get_transform_matrix().clone();
shadow_transform.pre_translate(shadow_offset_x, shadow_offset_y);
canvas.set_transform(&shadow_transform);
Ok(())
}

Expand Down
Loading