简体   繁体   English

getComputedStyle 宽度和高度在真实 Android 设备上的chrome webview 中不起作用

[英]getComputedStyle width and height not working in chromium webview on real Android devices

I had this javascript function:我有这个 javascript 函数:

function onFontLoad(cb,font,size,table,interval)
{
var div=document.createElement("div");
div.style.fontFamily=font;
div.style.fontSize=size;
div.style.position="absolute";
div.style.top="-100px"
div.style.left="-100px"
document.body.appendChild(div);
var checkInterval=setInterval(function()
{
    for(character in table)
    {
        div.textContent=character;
        var t=table[character];
        var s=getComputedStyle(div);
        if(parseInt(s.width)!=t[0]||parseInt(s.height)!=t[1]) return;
    }
    div.parentNode.removeChild(div);
    clearTimeout(checkInterval);
    cb();
},interval||200);

And it worked since webview in android was based on webkit.它之所以有效,是因为 android 中的 webview 是基于 webkit 的。 Since WebView was changed to chromium my function stop working even in Chromium browser.由于 WebView 已更改为 Chromium,我的功能即使在 Chromium 浏览器中也停止工作。 I got suggestion to use Math.ceil with rounding, and also avoid using parseInt.我建议使用带有舍入的 Math.ceil,并避免使用 parseInt。

Now I have this function:现在我有这个功能:

function onFontLoad(cb, font, size, table, interval) {
var div = document.createElement("div");
div.style.fontFamily = font;
div.style.fontSize = size;
div.style.position = "absolute";
document.body.appendChild(div);
var getRawPixels = function (cssUnit) {
    // Round up to the highest unit.
    var re = /([\d.]+)(px)/; // css measure units.
    var results = cssUnit.replace(re, "$1");
    return Math.ceil((results * 10) / 10) ;
};
var checkInterval = setInterval(function () {
    for (var character in table) {
        div.textContent = character;
        var t = table[character];
        var s = getComputedStyle(div);
        if (getRawPixels(s.width) != t[0] || getRawPixels(s.height) != t[1]) return;
    }
    div.parentNode.removeChild(div);
    clearTimeout(checkInterval);
    cb();
}, interval || 200);

And function works like intended now in Chromium browser or in Android (starting 4.4 to 6 emulators) and I have no problem with webview rendering in emulators.并且功能现在在 Chromium 浏览器或 Android(从 4.4 到 6 模拟器)中按预期工作,我在模拟器中渲染 webview 没有问题。 But its blank on some real devices, even without webview hardware acceleration.(mostly android 5.x devices) But I'm pretty there is no problem with canvas rendering, since if I comment or remove this string:但它在某些真实设备上是空白的,即使没有 webview 硬件加速。(主要是 android 5.x 设备)但我很高兴画布渲染没有问题,因为如果我评论或删除这个字符串:

  if (getRawPixels(s.width) != t[0] || getRawPixels(s.height) != t[1]) return;

Webview will start render as intended again even with real android devices I test application with, but without applying style from onFontLoad function.即使使用我用来测试应用程序的真实 android 设备,Webview 也将按预期再次开始渲染,但没有从 onFontLoad 函数应用样式。

Another thing I found in process is that broken webview in Chrome Developer Tools adds <i> after div.我在过程中发现的另一件事是 Chrome 开发者工具中损坏的 webview 在 div 之后添加了<i> But same code running in emulator displaying canvas nicely and there no any <i> after div.但是在模拟器中运行的相同代码可以很好地显示画布,并且在 div 之后没有任何<i> However I can broke canvas in emulator if I remove string with div position.但是,如果我删除带有 div 位置的字符串,我可以在模拟器中破坏画布。 And after this doom action I'll see <i> in page source code after div too.在这个厄运行动之后,我也会在 div 之后的页面源代码中看到<i>

Also, I found that Chromium had some issues with getComputedStyle in past too.另外,我发现 Chromium 过去在 getComputedStyle 上也有一些问题。 But I think getComputedStyle is working ok.但我认为 getComputedStyle 工作正常。

It was something with div hiding.这是div隐藏的东西。 I just removed hiding, since after hiding div destroyed itself with application in canvas.我刚刚删除了隐藏,因为隐藏 div 后在画布中使用应用程序破坏了自己。 Simplicity really a key there.简单真的是一个关键。

function onFontLoad(cb, font, size, table, interval) {
    var div = document.createElement("div");
    div.style.fontFamily = font;
    div.style.fontSize = size;
    //div.style.position = "relative";
    document.body.appendChild(div);
    var checkInterval = setInterval(function () {
        for (var character in table) {
            div.textContent = character;
            var t = table[character];
            var s = getComputedStyle(div);
        }
        clearTimeout(checkInterval);
        cb();
    }, interval || 200);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM