【问题标题】:filltext() canvas text position discrepencies between browsers浏览器之间的填充文本()画布文本位置差异
【发布时间】:2012-10-13 06:22:08
【问题描述】:

可以在问题底部的屏幕截图中看到,或者通过 directly to the game。 文本的放置方式因浏览器而异(firefox 15.0.1 的呈现方式与 IE 9.9 和 Chrome 21 不同)。 调用绘图函数:

context.fillText(this.wlines[i], this.xcoord, this.ycoord + y + (t) * this.sizey);

对象的构造函数:

function textItem(text, xcoord, ycoord, sizex, sizey,style, context) {
this.wlines = [];
this.text = text;
this.xcoord = xcoord;
this.ycoord = ycoord;
this.sizex = sizex;
this.sizey = sizey;
this.style = style;

if (text == null) {
    text = "";
}
var lines = text.split("~");
// this is first line text
context.save();
if (this.style < 3) {
    context.shadowOffsetY = 2;
    context.font = 'bold 18px "palatino linotype"';
} else if (this.style == 4) {
    this.font = '16px "palatino linotype"';
    this.shadowOffsetX = 2;
    this.shadowOffsetY = 1;
    this.shadowColor = "rgba(255,255,255,1)";
}
if (this.style == 5) {
    this.wlines.push(text);
} else {
    for (j = 0; j < lines.length; j += 1) {
        var words = lines[j].split(" ");
        var lastLine = "";
        var l = sizex;
        var measure = 0;
        for (i = 0; i < words.length; i += 1) {
            var w = words[i];
            measure = context.measureText(lastLine + w).width;
            if (measure < l) {
                lastLine += (w + " ");
            } else {
                //this is body text
                if (this.style == 6) {
                    lastLine += "...";
                }
                this.wlines.push(lastLine);
                lastLine = (w + " ");
                if (this.style < 3) {
                    context.font = 'bold 14px "palatino linotype"';
                }
            }
            if (i == words.length - 1) {
                this.wlines.push(lastLine);
                break;
            }
        }
    }
}
context.restore();
}

text,xcoorc,ycoord,xsize,ysize 是从 xml 文件中解析出来的。本例中的化合物名称:

<sizex>196</sizex>
<sizey>20</sizey>
<xcoord>383</xcoord>
<ycoord>14</ycoord>

style 是基于所需文本效果的定义值,context 是要绘制的画布的 2d 上下文(用于分层效果)。

如图所示,所有值在浏览器之间完全相同。我在浏览器之间做的唯一检查是

<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"/>

在 html 页面的标题中。

我真的不知道线高差异是从哪里来的,我们将不胜感激。

行高差异会根据文本而变化,但不会以我尚未弄清楚的方式发生变化。如果有任何我遗漏的信息,请随时询问。 ff: ff screen http://www.sunshinecompound.com/images/firefoxscreen.png 铬合金: chrome screen http://www.sunshinecompound.com/images/googlescreen.png

更新我的程序的解决方案至少是构建使用偏移量。此外,通过创建文本对象然后将文本对象另存为图像,我获得了巨大的性能提升。在 FF 中,这是最慢的浏览器,我看到整个程序运行时间减少了 5 倍多一点。尽管每次在程序中动态更改文本时都必须重新创建文本对象(我每 200 毫秒更改一次动态计数器和鼠标悬停效果,但根据我目前获得的性能,我可能会将其提高到 100 毫秒)。

【问题讨论】:

    标签: javascript internet-explorer firefox canvas


    【解决方案1】:

    是的。

    它在浏览器之间的放置、缩放、字距、别名甚至测量方式不同(如measureText)。

    如果您的游戏需要像素一致性,那么您将不得不使用图像而不是文本。对不起。 :(

    使measureText 一致的唯一方法是预先计算。

    使fillText 保持一致的唯一方法是使用图像而不是文本。 It's must faster, anyway.

    如果文本非常动态,这两种方法都是站不住脚的,但如果你只在应用中写过少于 100 条不同的文本,那么图像可能是你最好的选择。

    否则,您可以使用从图像生成的像素字体(对每个字母或常用词使用 drawImage)并希望性能良好,缓存较长的常用字符串。

    【讨论】:

    • 绝对不理想(尽管我在做基础研究后假设)。我们有数千行文本和动态文本。我们本来希望能够使用文本效果。看起来我将不得不为 IE 和 Chrome 使用偏移量(不能完美地工作,但可能会变得足够接近)或者想出一个图像形式的字母表。感谢您的回复,证实了我所期望的结果令人失望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2012-05-02
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多