【发布时间】:2019-02-10 01:25:31
【问题描述】:
当我使用drawImage() 函数在画布上绘制缩放图像时,它在 Chrome 和 Opera 中看起来有点模糊,但如果我先绘制全尺寸图像,然后再绘制缩放图像,它看起来很清晰。是什么导致模糊,我该如何解决?
const img = new Image();
const crisptCanvas = document.getElementById('crisp-canvas');
const crispContext = crisptCanvas.getContext('2d');
const blurryCanvas = document.getElementById('blurry-canvas');
const blurryContext = blurryCanvas.getContext('2d');
const sx = 0, sy = 0, sWidth = 1980, sHeight = 1251;
const dx = 0, dy = 0;
const scaleFactor = 0.4762626262626263;
// Draw an image on canvas
function scaleImage(scale, context)
{
const dWidth = (sWidth*scale);
const dHeight = (sHeight*scale);
context.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
};
// When image is loaded draw it on both canvases
img.onload = function(){
// First draw the source image in full scale and then using the -scaleFactor
setTimeout(()=> {
scaleImage(1, crispContext);
scaleImage(scaleFactor, crispContext);
}, 0);
// Draw the image using the -scaleFactor
scaleImage(scaleFactor, blurryContext);
}
img.src = "https://i.stack.imgur.com/eWDSw.png"
<canvas width="944" height="596" id="crisp-canvas" ></canvas>
<canvas width="944" height="596" id="blurry-canvas" ></canvas>
【问题讨论】:
-
奇怪,当我在 Chrome 中运行你的 sn-p 时,我真的看不出任何区别。您确定两者的区别是 scale=1 的初始绘制,而不是时间?如果您将 blurryContext 绘制移动到 setInterval 中(并且只是为了踢,将间隔增加到 1000 毫秒),差异会消失吗?
-
顺便说一下,我使用的是最新的 chrome 版本-“版本 69.0.3497.81 (Official Build) (64-bit)”,我的屏幕分辨率是 1366x768。我使用 setTimeout 的唯一原因是让时间先绘制模糊画布,然后再绘制清晰画布,因为一旦我绘制了全尺寸图像,问题就解决了,之后我可以绘制缩放图像就好了在每个画布上。
-
是的,如果我在 setTimeout 中以 (1s -1000ms) 间隔设置我的 blurryContext 绘制,它可以解决问题,但这是预期的,因为我们在第一次绘制完整之后在 blurryContext 中绘制图像缩放图像。所以问题是为什么我首先需要以全比例绘制图像,以便在我想缩放它时获得清晰的图像。在 firefox 中没有模糊,但在 Opera 中也会出现这种效果,因为 opera 和 chrome 都使用相同的渲染引擎。
标签: html google-chrome opera blurry