html5 – 在Canvas上绘制的文本的抗锯齿效果差

前端之家收集整理的这篇文章主要介绍了html5 – 在Canvas上绘制的文本的抗锯齿效果差前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Canvas上绘制文字,并对抗锯齿的质量感到失望.据我所知,浏览器不会在Canvas上对文本进行亚像素反对.

这准确吗?

这在iPhone和Android上尤为明显,其中生成的文本不像其他DOM元素呈现的文本那样清晰.

有关高质量文本的任何建议都放在Canvas上吗?

茹贝尔

解决方法

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

重要的代码如下.

  1. // finally query the varIoUs pixel ratios
  2. devicePixelRatio = window.devicePixelRatio || 1,backingStoreRatio = context.webkitBackingStorePixelRatio ||
  3. context.mozBackingStorePixelRatio ||
  4. context.msBackingStorePixelRatio ||
  5. context.oBackingStorePixelRatio ||
  6. context.backingStorePixelRatio || 1,ratio = devicePixelRatio / backingStoreRatio;
  7.  
  8. // upscale the canvas if the two ratios don't match
  9. if (devicePixelRatio !== backingStoreRatio) {
  10.  
  11. var oldWidth = canvas.width;
  12. var oldHeight = canvas.height;
  13.  
  14. canvas.width = oldWidth * ratio;
  15. canvas.height = oldHeight * ratio;
  16.  
  17. canvas.style.width = oldWidth + 'px';
  18. canvas.style.height = oldHeight + 'px';
  19.  
  20. // now scale the context to counter
  21. // the fact that we've manually scaled
  22. // our canvas element
  23. context.scale(ratio,ratio);
  24.  
  25. }

猜你在找的HTML5相关文章