Skip to content

Commit 4b12bd9

Browse files
committed
fix color overlay on exported image on Windows
Closes #420 getImageScaledToCanvas() reused paintImage() which sets globalCompositeOperation to 'destination-over'. On some Windows GPU-accelerated canvas implementations this produces color artifacts on fresh canvases. Rewrite getImageScaledToCanvas() to draw directly with the default 'source-over' compositing, matching getImage().
1 parent 25423d8 commit 4b12bd9

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

packages/core/src/AvatarEditor.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ export class AvatarEditorCore {
400400

401401
getImageScaledToCanvas(): HTMLCanvasElement {
402402
const dimensions = this.getDimensions()
403+
const image = this.imageState
403404
const canvasEl = document.createElement('canvas')
404405

405406
if (this.isVertical()) {
@@ -410,8 +411,32 @@ export class AvatarEditorCore {
410411
canvasEl.height = dimensions.height
411412
}
412413

413-
// don't paint a border here, as it is the resulting image
414-
this.paintImage(canvasEl.getContext('2d')!, this.imageState, 0, 1)
414+
if (!image.resource) return canvasEl
415+
416+
const context = canvasEl.getContext('2d')
417+
if (!context) return canvasEl
418+
419+
const pos = this.calculatePosition(image, 0)
420+
421+
context.save()
422+
context.translate(canvasEl.width / 2, canvasEl.height / 2)
423+
context.rotate((this.config.rotate * Math.PI) / 180)
424+
context.translate(-(canvasEl.width / 2), -(canvasEl.height / 2))
425+
426+
if (this.isVertical()) {
427+
context.translate(
428+
(canvasEl.width - canvasEl.height) / 2,
429+
(canvasEl.height - canvasEl.width) / 2,
430+
)
431+
}
432+
433+
if (this.config.backgroundColor) {
434+
context.fillStyle = this.config.backgroundColor
435+
context.fillRect(0, 0, canvasEl.width, canvasEl.height)
436+
}
437+
438+
context.drawImage(image.resource, pos.x, pos.y, pos.width, pos.height)
439+
context.restore()
415440

416441
return canvasEl
417442
}

0 commit comments

Comments
 (0)