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 4357d67
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 }, 'png', 1)
})
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 4357d67

@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: 4357d67 Previous: a3c4123 Ratio
Draw house#skia-canvas 21 ops/sec (±0.38%) 19.4 ops/sec (±1.09%) 0.92
Draw house#node-canvas 17 ops/sec (±0.3%) 20.8 ops/sec (±1.17%) 1.22
Draw house#@napi-rs/skia 18 ops/sec (±1.11%) 18.9 ops/sec (±0.79%) 1.05
Draw gradient#skia-canvas 20 ops/sec (±0.44%) 19 ops/sec (±1.29%) 0.95
Draw gradient#node-canvas 16 ops/sec (±0.42%) 20 ops/sec (±1.16%) 1.25
Draw gradient#@napi-rs/skia 17 ops/sec (±0.74%) 18 ops/sec (±1.02%) 1.06

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

Please sign in to comment.