在画布上绘制的文本的抗锯齿性差
我在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);
}