Skip to content

Commit

Permalink
fix: shadow color was not applied correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Aug 5, 2022
1 parent e3fe43c commit 4054e44
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Binary file modified __test__/snapshots/shadowOffsetX.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __test__/snapshots/shadowOffsetY.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 21 additions & 9 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,16 +639,18 @@ impl Context {
return None;
}
let mut drop_shadow_paint = paint.clone();
let sigma = last_state.shadow_blur / 2f32;
let a = shadow_color.alpha;
let r = shadow_color.red;
let g = shadow_color.green;
let b = shadow_color.blue;
let transform = last_state.transform.get_transform();
let sigma_x = last_state.shadow_blur / (2f32 * transform.scale_x());
let sigma_y = last_state.shadow_blur / (2f32 * transform.scale_y());
let shadow_effect = ImageFilter::make_drop_shadow_only(
last_state.shadow_offset_x,
last_state.shadow_offset_y,
sigma,
sigma,
sigma_x,
sigma_y,
(a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | b as u32,
None,
)?;
Expand All @@ -673,13 +675,23 @@ impl Context {
return None;
}
let mut drop_shadow_paint = paint.clone();
drop_shadow_paint.set_color(
shadow_color.red,
shadow_color.green,
shadow_color.blue,
shadow_color.alpha,
);
let a = shadow_color.alpha;
let r = shadow_color.red;
let g = shadow_color.green;
let b = shadow_color.blue;
let transform = last_state.transform.get_transform();
let sigma_x = last_state.shadow_blur / (2f32 * transform.scale_x());
let sigma_y = last_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(last_state.shadow_blur / 2f32)?;
drop_shadow_paint.set_mask_filter(&blur_effect);
Some(drop_shadow_paint)
Expand Down
17 changes: 17 additions & 0 deletions src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3017,12 +3017,19 @@ impl Drop for Matrix {
}

#[derive(Copy, Clone, PartialEq, Debug)]
/// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
pub struct Transform {
/// (m11) Horizontal scaling. A value of `1` results in no scaling.
pub a: f32,
/// (m12) Vertical skewing.
pub b: f32,
/// (m21) Horizontal skewing.
pub c: f32,
/// (m22) Vertical scaling. A value of `1` results in no scaling.
pub d: f32,
/// (dx) Horizontal translation (moving).
pub e: f32,
/// (dy) Vertical translation (moving).
pub f: f32,
}

Expand Down Expand Up @@ -3097,6 +3104,16 @@ impl Transform {
f: (m[2] * m[3] - m[0] * m[5]) / det,
})
}

#[inline]
pub fn scale_x(&self) -> f32 {
self.a
}

#[inline]
pub fn scale_y(&self) -> f32 {
self.d
}
}

impl Default for Transform {
Expand Down

1 comment on commit 4054e44

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 4054e44 Previous: e3fe43c Ratio
Draw house#skia-canvas 24 ops/sec (±0.07%) 24 ops/sec (±0.08%) 1
Draw house#node-canvas 26 ops/sec (±0.56%) 26 ops/sec (±0.42%) 1
Draw house#@napi-rs/skia 23 ops/sec (±0.13%) 23 ops/sec (±0.12%) 1
Draw gradient#skia-canvas 23 ops/sec (±0.03%) 23 ops/sec (±0.19%) 1
Draw gradient#node-canvas 25 ops/sec (±0.33%) 25 ops/sec (±0.28%) 1
Draw gradient#@napi-rs/skia 22 ops/sec (±2.05%) 22 ops/sec (±0.12%) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.