gpt4 book ai didi

canvas - Firefox 4 文本渲染错误

转载 作者:行者123 更新时间:2023-12-01 13:00:37 28 4
gpt4 key购买 nike

我的应用程序遇到了一个错误,我将其追溯到 Chrome 和 Firefox 4 之间不一致的文本呈现。

Chrome 告诉我某个文本字符串的宽度为 215 像素,而 Firefox 4 告诉我它的宽度为 218 像素:

Chrome vs Firefox Text Rendering

一点搜索 indicates问题出在 Firefox 4,而不是 Chrome。

以下代码在 canvas 元素上以及通过 HTML 呈现文本并输出宽度:

<html>
<head>
<title>Firefox 4 Text Rendering Bug</title>
<script type="text/javascript">
function draw(){
var canvas = document.getElementById('tutorial');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.font = "bold 16pt Helvetica"
ctx.fillText("MORE INFORMATION", 0, 16)
alert(ctx.measureText("MORE INFORMATION").width);
}
}
</script>
<style type="text/css">
canvas { border: 1px solid black; }
h2 {font-size: 16pt; font-family: Helvetica; font-weight: bold }
</style>
</head>
<body onload="draw();">
<canvas id="tutorial" width="220" height="20"></canvas>
<h2>MORE INFORMATION</h2>
</body>
</html>

有没有办法在浏览器之间一致地呈现文本?

最佳答案

Is there a way to render text consistently between the browsers?

简短回答:否。不适用于 fillText。

长答案:事实上,在不同的操作系统和不同的操作系统设置上也会有所不同。这本身并不是 Firefox 的错误。如果有的话,FF4 是最一致的。

对于 Chrome 13.0.782.1、Safari 5 和 Opera 11,我得到 211

对于 IE9,我得到 220

对于 FF4 我得到 218

尤其是像 Helvetica 这样的字体,并不是每个用户都拥有这种字体。大多数 Windows 用户将使用 Arial,等等。

昨天看帖子here说说fillText目前存在的一些问题。 fillText 的最佳效果是使用网络安全字体,不要使用斜体或粗体,并进行测试以确保它在您所定位的浏览器中足够符合您的喜好。

技术上正确但蹩脚的答案:是的,有一种方法可以一致地呈现文本。制作一个 PNG 并在浏览器中渲染图像。否则,您只需找到似乎在所有浏览器上最接近的字体即可。

关于canvas - Firefox 4 文本渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231536/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com