在画布上绘制的文本的抗锯齿性差 [英] Poor anti-aliasing of text drawn on Canvas

查看:502
本文介绍了在画布上绘制的文本的抗锯齿性差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Canvas上绘制文字,对抗锯齿的质量感到失望。就我已经能够确定,浏览器不做子像素反对在Canvas上的文本。

I'm drawing text on Canvas, and am disappointed with the quality of antialiasing. As far as I've been able to determine, browsers don't do subpixel antialising of text on Canvas.

这是否正确?

这在iPhone和Android上特别明显,作为由其他DOM元素呈现的文本。

This is particularly noticeable on iPhone and Android, where the resulting text isn't as crisp as text rendered by other DOM elements.

对Canvas的高品质文字提出任何建议吗?

Any suggestions for high quality text out put on Canvas?

Joubert

推荐答案

我的回答来自这个链接,也许它会帮助别人。
http://www.html5rocks.com/en/tutorials/canvas/hidpi/

My answer came from this link, maybe it will help someone else. http://www.html5rocks.com/en/tutorials/canvas/hidpi/

重要代码如下。

// finally query the various pixel ratios
    devicePixelRatio = window.devicePixelRatio || 1,
    backingStoreRatio = context.webkitBackingStorePixelRatio ||
                        context.mozBackingStorePixelRatio ||
                        context.msBackingStorePixelRatio ||
                        context.oBackingStorePixelRatio ||
                        context.backingStorePixelRatio || 1,

    ratio = devicePixelRatio / backingStoreRatio;

// upscale the canvas if the two ratios don't match
if (devicePixelRatio !== backingStoreRatio) {

    var oldWidth = canvas.width;
    var oldHeight = canvas.height;

    canvas.width = oldWidth * ratio;
    canvas.height = oldHeight * ratio;

    canvas.style.width = oldWidth + 'px';
    canvas.style.height = oldHeight + 'px';

    // now scale the context to counter
    // the fact that we've manually scaled
    // our canvas element
    context.scale(ratio, ratio);

}

这篇关于在画布上绘制的文本的抗锯齿性差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆