HTML5 画布元素上的子像素抗锯齿文本
我对画布元素抗锯齿文本的方式有点困惑,希望你们都能提供帮助。
在下面的屏幕截图中,顶部的“Quick Brown Fox”是一个 H1 元素,底部的元素是一个画布元素,上面渲染有文本。在底部,您可以看到两个“F”并排放置并放大。注意 H1 元素如何与背景更好地融合:
这是我用来渲染画布文本的代码:
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.font = '26px Arial';
ctx.fillText('Quick Brown Fox', 0, 26);
}
是否可以在画布上渲染文本,使其看起来与 H1 元素相同?为什么它们不同?
I'm a bit confused with the way the canvas element anti-aliases text and am hoping you all can help.
In the following screenshot the top "Quick Brown Fox" is an H1 element and the bottom one is a canvas element with text rendered on it. On the bottom you can see both "F"s placed side by side and zoomed in. Notice how the H1 element blends better with the background:
Here's the code I'm using to render the canvas text:
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.font = '26px Arial';
ctx.fillText('Quick Brown Fox', 0, 26);
}
Is it possible to render the text on the canvas in a way so that it looks identical to the H1 element? And why are they different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
现在可以通过创建不透明的画布上下文来获得子像素字体渲染。在 Safari 和 Chrome 中,您可以使用以下代码片段获取此内容:
var ctx = canvas.getContext("2d", {alpha: false})
我从此 博客文章。
It's now possible to get sub-pixel font rendering by creating an opaque canvas context. In Safari and Chrome you can get this using this snippet:
var ctx = canvas.getContext("2d", {alpha: false})
I found this from this blog post.
回答我自己的问题:
可以使用 此网站。
唯一的问题是它在生产应用程序中实现起来太慢。如果有人跑得更快,请告诉我。
Answering my own question:
It is possible using the technique demonstrated on this site.
The only problem is that its too slow to implement in a production app. If anyone runs across a faster way please let me know.
马特,上周我遇到了(相同/相似)的问题,就我而言,结果是由于我正在测试的设备上的像素密度存在差异;我今晚写了关于它的文章 - http://joubert .posterous.com/crisp-html-5-canvas-text-on-mobile-phones-and
海报上的链接已失效,所以这里是源代码的要点:
https://gist.github.com/joubertnel/870190
代码片段本身:
Matt, I sat with the (same/similar) problem last week, which, in my case, turned out to be because of differences in pixel densities on the devices I was testing; I wrote about it tonight - http://joubert.posterous.com/crisp-html-5-canvas-text-on-mobile-phones-and
The link at posterous is dead, so here is the gist with the source code:
https://gist.github.com/joubertnel/870190
And the snippet itself:
这是对任何画布内容(文本、图像、矢量等)进行子像素渲染的方法。 http://johnvalentine.co.uk/archive.php?art=tft 。
虽然还有微调的余地,但对于简单的方法来说这是一个很大的收获。
Here's a way of doing sub-pixel rendering for any canvas content (text, images, vectors, etc). http://johnvalentine.co.uk/archive.php?art=tft.
There's scope for fine-tuning, but it's a big gain for a simple method.
这通常称为子像素抗锯齿,或子像素抗锯齿。 Windows 上的 microsoft.com/typography/cleartype/tuner/step1.aspx" rel="nofollow">ClearType。我不知道目前有任何操作系统/浏览器组合支持 Canvas。
我有兴趣看到一些使用文本子像素偏移的测试,看看是否有浏览器甚至使用基于像素的字体渲染提示(例如,在像素边界上对齐上升部分)。我的假设是否定的。
编辑:我的假设是错误的; Safari、Chrome 和 Firefox 似乎都使用了一些像素字体提示。 Safari 和 Chrome 看起来相同,都捕捉到整个像素边界,但与 Firefox 不同(捕捉到半像素边界?)。请在此处查看测试的视觉结果(在 OS X 上):http://phrogz.net/tmp/canvas_text_subpixel。 html
This is generically called subpixel anti-aliasing, or ClearType on Windows. I'm not aware of any OS/browser combinations that currently support this for Canvas.
I'd be interested to see some tests using sub-pixel offsets for the text to see if any browsers even use pixel-based hinting of the font rendering (aligning ascenders on pixel boundaries, for example). My assumption would be no.
Edit: My assumption was wrong; it would appear that Safari, Chrome, and Firefox all utilize some pixel font hinting. Safari and Chrome appear the same, snapping to whole pixel boundaries, but are different from Firefox (snapping to half-pixel boundaries?). See the visual results of testing (on OS X) here: http://phrogz.net/tmp/canvas_text_subpixel.html
您可以使用相当简单的技术使字体更加清晰。
您可以在 CSS 中将画布缩放为两倍:
在 HTML 中将画布尺寸加倍:
最后以双倍大小在画布上绘制所有内容。
你会发现字体清晰多了。
这与 HTML 中的 H1 并不完全相同,但看起来比正常的字体渲染要好得多。
You could make the fonts a lot clearer with fairly easy technique.
You can scale the canvas in CSS twice as small:
In the HTML double the dimensions of the canvas:
Finally draw everything on the canvas in double size.
You will notice that the fonts are a lot clearer.
This is not completely the same as H1 in the HTML but a looks lot better than normal font rendering.