Skip to content

Commit

Permalink
[Editor] Add a checkerboard behind the image preview in the new alt-t…
Browse files Browse the repository at this point in the history
…ext dialog in order to see its potential transparency
  • Loading branch information
calixteman committed Jul 30, 2024
1 parent 8f45374 commit da4acaa
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,33 @@ class StampEditor extends AnnotationEditor {

canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d", {
willReadFrequently: true,
});
const ctx = canvas.getContext("2d");
ctx.filter = this._uiManager.hcmFilter;

if (createImageData && this._uiManager.hcmFilter !== "none") {
// Add a checkerboard pattern as a background in case the image has some
// transparency.
let white = "white",
black = "#cfcfd8";
if (this._uiManager.hcmFilter !== "none") {
black = "black";
} else if (window.matchMedia?.("(prefers-color-scheme: dark)").matches) {
white = "#8f8f9d";
black = "#42414d";
}
const boxDim = 15;
ctx.fillStyle = white;

Check warning

Code scanning / CodeQL

Useless assignment to property Warning

This write to property 'fillStyle' is useless, since
another property write
always overrides it.
ctx.fillStyle = black;
const pattern = new OffscreenCanvas(boxDim * 2, boxDim * 2);
const patternCtx = pattern.getContext("2d");
patternCtx.fillStyle = white;
patternCtx.fillRect(0, 0, boxDim * 2, boxDim * 2);
patternCtx.fillStyle = black;
patternCtx.fillRect(0, 0, boxDim, boxDim);
patternCtx.fillRect(boxDim, boxDim, boxDim, boxDim);
ctx.fillStyle = ctx.createPattern(pattern, "repeat");
ctx.fillRect(0, 0, width, height);

if (createImageData) {
const offscreen = new OffscreenCanvas(width, height);
const offscreenCtx = offscreen.getContext("2d", {
willReadFrequently: true,
Expand Down Expand Up @@ -422,15 +443,7 @@ class StampEditor extends AnnotationEditor {
width,
height
);
let imageData = null;
if (createImageData) {
imageData = {
width,
height,
data: ctx.getImageData(0, 0, width, height).data,
};
}
return { canvas, imageData };
return { canvas, imageData: null };
}

/**
Expand Down

0 comments on commit da4acaa

Please sign in to comment.