Skip to content

Commit

Permalink
fix: ctx.globalAlpha should effect drawImage (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Sep 14, 2022
1 parent a3c4123 commit 9c505e2
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
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
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

0 comments on commit 9c505e2

Please sign in to comment.