Skip to content

Commit

Permalink
fix: ctx.globalAlpha should effect drawImage
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Sep 14, 2022
1 parent a3c4123 commit db81920
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('transform-with-radial-gradient-x', async (t) => {
await snapshotImage(t, { canvas, ctx })
})

test('global-alpha-should-not-effect-drawImage', async (t) => {
test('fill-alpha-should-not-effect-drawImage', async (t) => {
const canvas = createCanvas(300, 320)
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'rgba(3, 169, 244, 0.5)'
Expand All @@ -93,3 +93,14 @@ test('global-alpha-should-not-effect-drawImage', async (t) => {
ctx.drawImage(await loadImage(image), 0, 0, 200, 100)
await snapshotImage(t, { ctx, canvas })
})

test('global-alpha-should-effect-drawImage', async (t) => {
const canvas = createCanvas(300, 320)
const ctx = canvas.getContext('2d')
ctx.globalAlpha = 0.2

// Image
const image = await fs.readFile(join(__dirname, 'javascript.png'))
ctx.drawImage(await loadImage(image), 0, 0, 200, 100)
await snapshotImage(t, { ctx, canvas })
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ impl CanvasRenderingContext2D {
);
return;
}

self.context.state.global_alpha = alpha;
self.context.state.paint.set_alpha((alpha * 255.0) as u8);
}

Expand Down

1 comment on commit db81920

@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: db81920 Previous: 2c27626 Ratio
Draw house#skia-canvas 24 ops/sec (±0.13%) 24 ops/sec (±1.91%) 1
Draw house#node-canvas 26 ops/sec (±0.35%) 26 ops/sec (±1.38%) 1
Draw house#@napi-rs/skia 23 ops/sec (±0.09%) 23 ops/sec (±0.27%) 1
Draw gradient#skia-canvas 23 ops/sec (±0.11%) 23 ops/sec (±0.05%) 1
Draw gradient#node-canvas 25.3 ops/sec (±0.28%) 25.2 ops/sec (±0.27%) 1.00
Draw gradient#@napi-rs/skia 22.5 ops/sec (±0.15%) 22.5 ops/sec (±0.15%) 1

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

Please sign in to comment.