简体   繁体   English

Android浏览器中的画布绘制不一致

[英]Inconsistent canvas drawing in Android browser

In putting together a small canvas app I've stumbled across a weird behavior that only seems to occur in the default browser in Android. 在组装一个小型canvas应用程序时,我偶然发现了一种奇怪的行为,该行为似乎只发生在Android的默认浏览器中。

When drawing to a canvas that has the globalCompositeOperation set to 'destination-out' to act as the 'eraser' tool, Android browser sometimes acts as expected, sometimes does not update the pixels in the canvas at all. 绘制到将globalCompositeOperation设置为'destination-out'以用作“橡皮擦”工具的画布时,Android浏览器有时会按预期运行,有时根本不会更新画布中的像素。

the setup: 设置:

context.clearRect(0,0, canvas.width, canvas.height);
context.drawImage(img, 0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = 'destination-out';

draw a circle to erase pixels from the canvas: 画一个圆圈以擦除画布上的像素:

context.fillStyle = '#FFFFFF';
context.beginPath();
context.arc(x,y,25,0,TWO_PI,true);
context.fill();
context.closePath();

a small demo to illustrate the issue can be seen here: http://gumbojuice.com/files/source-out/ and the javascript is here: http://gumbojuice.com/files/source-out/js/main.js 可以在这里看到一个演示该问题的小演示: http : //gumbojuice.com/files/source-out/ ,而javascript则在这里: http : //gumbojuice.com/files/source-out/js/main。 js

this has been tested in multiple desktop and mobile browsers and behaves as expected. 此功能已在多种台式机和移动浏览器中进行了测试,并且运行正常。 On Android native browser after refreshing the page sometimes it works, sometimes nothing happens. 在刷新页面后,在Android本机浏览器上,有时它可以工作,有时则什么也没有发生。

I've seen other hacks that move the canvas by a pixel in order to force a redraw but this is not an ideal solution.. 我见过其他黑客将画布移动一个像素以强制重绘,但这不是理想的解决方案。

Thanks all. 谢谢大家

I did something like this, which forces the detachment of the canvas: 我做了这样的事情,迫使画布脱离:

ctx.clearRect(0, 0, canvas.width, canvas.height);

if (isStockAndroid) {
    canvas.style.display = "none";
    canvas.offsetHeight;
    canvas.style.display = "block";
}

That seems to be the most efficient as far as FPS is concerned. 就FPS而言,这似乎是最有效的。 Otherwise it's the not-so-nice: 否则就不太好了:

canvas.width = canvas.width;

...which seemed to also get it all working normally for me. ...这似乎也对我来说一切正常。 Haven't tested to see if the first is essentially the same as the second and resets canvas settings, though, but it seems to be getting a higher frame rate? 还没有测试看第一个是否与第二个基本相同,并且重置了画布设置,但是似乎获得了更高的帧率? Anyway that definitely clears things. 无论如何,这肯定清除了一切。 For the native detection stuff try here: How to detect only the native Android browser 对于本机检测,请尝试以下操作: 如何仅检测本机Android浏览器

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

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