Scaling the canvas up seems to be working fine but scaling down seems to be cutting off pixels. I've tried moving the order of things around and doing it without the in-memory-only canvas but nothing is working. My best guess is that the canvas is being sized down then scaled down further hence the reason the canvas data is not covering the entire canvas size.
// Create a newly scaled canvas from the original and then delete the original.
function ScaleCanvas(width, height, xScale, yScale) {
// Get the true overlay's current image data.
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Create an in-memory canvas at the new resolution.
const newCanvas = $("<canvas>")
.attr("width", width)
.attr("height", height)[0];
// Draw the true overlay's image data into the in-memory canvas.
newCanvas.getContext("2d").putImageData(imageData, 0, 0);
// Update the size/resolution of the true overlay.
ctx.canvas.width = width;
ctx.canvas.height = height;
// Scale the true overlay's context.
ctx.scale(xScale, yScale);
// Draw the in-memory canvas onto the true overlay.
ctx.drawImage(newCanvas, 0, 0);
}
There is a related question here where I got most of this logic: How to scale an imageData in HTML canvas?
The canvas is the appropriate resolution/size at the end of the function, but considering the fact that I have some pixels transparent and others opaque black it is obvious that the pixels at the right and bottom of the downscaled canvas are not producing the desired effect.
UPDATE Here is a jsfiddle to better illustrate the issue. Notice the one transparent pixel doesn't stay in the middle of the canvas after the canvas is scaled down.