【发布时间】:2018-07-03 04:09:51
【问题描述】:
放大画布似乎工作正常,但缩小似乎会切断像素。我已经尝试移动事物的顺序并在没有仅内存画布的情况下进行操作,但没有任何效果。我最好的猜测是画布被缩小然后进一步缩小,因此画布数据没有覆盖整个画布大小。
// 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);
}
这里有一个相关的问题,我得到了大部分逻辑:How to scale an imageData in HTML canvas?
画布在函数结束时是适当的分辨率/大小,但考虑到我有一些透明像素和其他不透明黑色的事实,很明显缩小画布右侧和底部的像素没有产生想要的效果。
更新 这是一个jsfiddle 以更好地说明这个问题。请注意,画布缩小后,一个透明像素不会停留在画布中间。
【问题讨论】:
标签: javascript css html canvas drawimage