KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

Kinetic on Android

Open atomictag opened this issue 13 years ago • 13 comments

I tried the latest version on Android on a Galaxy S2 with Android 4.1.2 and noticed that the canvas is not cleared after the first draw. If you try this tween example you'll notice that the initial position of the rectangle is not cleared and it remains on screen until the end of the animation (so at the end you see 2 rectangles). The same happened with other code I have tried.

BTW, nothing is displayed on Chrome for Android (incl. the example above). Did anyone experience the same?

atomictag avatar May 23 '13 15:05 atomictag

I do have the same issue (https://github.com/ericdrowell/KineticJS/issues/130). I cant use my application on Android.

The problem exists in every version of Android 4.0.4+ and Google does not give a damn about this :( Do you use KineticJS in a WebView?

I asked this once but ... Is it possible to fix this issue directly in KineticJS? I guess its possible but would require too much time!?

hur0n avatar May 23 '13 19:05 hur0n

I experience only issues at a Samsung Galaxy Note Tab. It seems it simply does not receive/handles touch input with Kinetic. (i've tried the tutorials and labs on KineticJS.com as well).

fervanrijswijk avatar May 24 '13 07:05 fervanrijswijk

Tried official examples with Samsung Note and Note 2 using Android Chrome browser, none of them work...

thomastai avatar May 31 '13 10:05 thomastai

I found many posts around that refer to this issue - which is not a Kinetic issue, for what I can tell, but a problem with Android browsers not implementing clearRect correctly. A work-around that seems to work is to use fillRect instead of clearRect on clear - but that of course assumes you know which fill to use and, in my tests, degrades performance quite a bit. Another hack that seems to do the trick is to reset the canvas width - which, depending on the browser, could be disastrous performance-wise or acceptable (but I haven't really tried that)

atomictag avatar Jun 03 '13 10:06 atomictag

I am trying to work this through using KineticJS, but this is how I resolved a simple canvas. Assuming you are using jQuery, simply detach the canvas element from the DOM and add it back. Seems to minimally impact performance at all. Worked for me when the canvas.width and clearRect failed.

function fixCanvasDrawBug(canvas) {
        var parent = $(canvas).parent();
        var copy = $(canvas).detach();
        parent.append(copy);
}

jjhester avatar Aug 16 '13 19:08 jjhester

The suggested work-arounds don't seem to work form me.

Looks like this is the best solution I've found so far:

$(window).resize(function (e) { if (e.target == window) { //android fix canvas duplicate issue $('canvas').css('opacity', 0.99); setTimeout(function () { $('canvas').css('opacity', 1); }, 0); } });

But I'm not sure this fixed the problem 100%, so please do share any other solutions

JV1988 avatar Jan 23 '14 10:01 JV1988

Thanks @jjhester, that work around worked for me for now.

jeremib avatar Jan 28 '14 09:01 jeremib

This is a bug with early versions of Android.

http://www.html5gamedevs.com/topic/114-canvasclearrect-bug-in-android-stock-browser/

The workaround is to define your stage and layer, but then add the layer to the stage after a timeout (I've used 50ms with success)

ericdrowell avatar Feb 26 '14 17:02 ericdrowell

Instead of this:

var stage = new Kinetic.Stage(conf);
var layer = new Kinetic.Layer();
stage.add(layer);
// other stuff

We have to do this:

var stage = new Kinetic.Stage(conf);
setTimeout(function(){
  var layer = new Kinetic.Layer();
  stage.add(layer);
  // other stuff
}, 50);

@ericdrowell , am I right?

lavrton avatar Feb 27 '14 09:02 lavrton

no, you have to create the layer, and THEN draw with a timeout, or else the timeout won't do any good. The bug with Android is that the first thing that's drawn onto the canvas is stuck there forever, and can't be cleared. So, if you create a layer, and add it to the stage with no shapes in it, then canvas in the layer will render with nothing in it. Subsequent draws will be clearable.

var stage = new Kinetic.Stage(conf); var layer = new Kinetic.Layer();

stage.add(layer);

setTimeout(function(){ // add shapes to the layer and then do layer.draw() }, 50);

https://github.com/ericdrowell/KineticJS/blob/master/test/unit/DragAndDrop-test.js#L17

ericdrowell avatar Feb 27 '14 17:02 ericdrowell

Hello... After I try a lot of "solution" to fix duplicate canvas with kineticjs, like:

1 - Set all parent's div to overflow: visible; Fail 2 - Set all parent's div to position: relative; Fail 3 - Set canvas to position: relative; Fail 4 - setInterval of the 0 or 1ms with the function that transform canvas opacity between 0.99 and 1; Fail 5 - setTimeout 50ms; Fail 6 - more and more...

Well, I go to my android code and change the sequence of my set webview and settings and it's work, I don't know why, but it's work :)

webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); webView.getSettings().setSupportZoom(false); webview.setBackgroundColor(Color.TRANSPARENT); webView.setWebChromeClient(new WebChromeClient()); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

if(savedInstanceState == null) webView.loadUrl("file:///android_asset/index.html");

Good luck (- _ - )/

felipehaack avatar Mar 28 '14 14:03 felipehaack

Hi people, I had the same issue dragging a group with an image and a couple of shapes inside. The timeout workaround didn't work for me and I only was able to solve it moving the draggable layer to the top on the contentTouchMove event:

this.stage.on('contentTouchmove', function(evt) { selectedGroup.getLayer().moveToTop(); }

I'll keep searching for a better solution as this may kill performance.

Hope this helps.

dbelay22 avatar Apr 22 '14 23:04 dbelay22

1 You may try to use throttle function http://underscorejs.org/#throttle (or any alternative) to save performance

stage.on('contentTouchmove', _.throttle(function(evt) {
  layer.moveToTop();
}, 300);

2 Also look at other hacks: http://slash-system.com/en/how-to-fix-android-html5-canvas-issues/

lavrton avatar Apr 23 '14 00:04 lavrton