【问题标题】:Canvas image quality issue画布图像质量问题
【发布时间】:2014-04-12 14:43:22
【问题描述】:

我正在尝试在画布中模拟 background-size: cover 属性。我能用,但质量很差。

这是fiddle。你可以比较两张图片(缩小,这样你可以看到两张图片并排在一起):

我的代码如下:

function drawImageProp(ctx, img, x, y, w, h, offsetY) {
    var iw = img.width,
        ih = img.height,
        r = Math.min(w / iw, h / ih),
        newW = iw * r,
        newH = ih * r,
        cx, cy, cw, ch, ar = 1;

    if (newW < w) ar = w / newW;
    if (newH < h) ar = h / newH;
    newW *= ar;
    newH *= ar;

    cw = iw / (newW / w);
    ch = ih / (newH / h);

    cy = -parseInt(offsetY) * ih / newH;

    ctx.patternQuality = 'best';
    ctx.antialias = 'default';
    ctx.filter = 'default';

    ctx.drawImage(img, 0, cy, cw, ch, x, y, w, h);

我试过添加

ctx.patternQuality = 'best';
ctx.antialias = 'default';
ctx.filter = 'default';

为了提高质量,但我仍然没有得到足够好的质量。是否可以在画布中使用原始质量的图像。

还有parseInt(x)parseInt(x) + 0.5 到坐标,没有解决问题。

*这个问题用nodejs 标记,因为我在nodejs 中使用这个代码和node-canvas 模块。

【问题讨论】:

标签: javascript image node.js canvas html5-canvas


【解决方案1】:

尝试在上下文中将 imageSmoothingEnabled 属性设置为 false:

ctx.imageSmoothingEnabled = false;

默认画布在缩放时使用双线性过滤器。它是模糊的。您可能需要最近邻过滤,这只是像素重复。

【讨论】:

    猜你喜欢
    • 2012-10-08
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    相关资源
    最近更新 更多