javascript - Dynamically disabling touch-action (overscroll) for SVG elements -


i'm having problems touch screen overscrolling on chrome.

i have document svg element in it, contains shape, rectangle:

enter image description here

now, want make rectangle draggable, means want disable kinds of touch actions on respective <rect> element, setting it's style property touch-action: none.

this works fine on desktop browsers, except chrome. on chrome, when touch , move on rectangle, browser's overscroll feature kicks in. causes browser window awkwardly move, pointer events have set on rectangle cancelled.

i.e. pointermove registered fraction of second, stops when overscroll kicks in. pointerup never called when touch released.

enter image description here

now, if had html element instead of svg element, setting touch-action: none job. same thing on svg element fails.

technically, solved either setting touch-action: none on document.body or wrapping whole svg <div> element touch-action: none set.

unfortunately, not option me since need document (and rest of rectangle-surrounding svg) retain of original touch gestures, except when directly on rectangle.

as solution, did try dynamically set touch-action: none on document: body when pointerdown event occurs on rectangle.

// element var o = document.getelementbyid( "test" );  // disable touch action on press of svg element o.addeventlistener( "pointerdown", function(e) {   document.body.style.touchaction = "none"; } );  // re-enable touch action when released o.addeventlistener( "pointerup", function(e) {   document.body.style.touchaction = "auto"; } ); 

unfortunately, not help. style on body gets set , next time try dragging rectangle works expected (because pointerup event never gets executed), not time.

adding preventdefault() event handlers has no effect.

i appreciate if have had similar experience share solution.

here's live example of above.

// element  var o = document.getelementbyid( "test" );    // disable touch action on press of svg element  o.addeventlistener( "pointerdown", function(e) {    document.body.style.touchaction = "none";  } );    // re-enable touch action when released  o.addeventlistener( "pointerup", function(e) {    document.body.style.touchaction = "auto";  } );
<svg id="test" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="width: 300px; height: 300px; border: 1px solid #eee;">    <g transform="translate(50,50)">      <rect fill="#00fff0" width="200" height="200" style="touch-action: none;"></rect>    </g>  </svg>


update: looks using preventdefault() on touchstart event trick.

however, seems wrong use both modern pointerdown , legacy touchstart @ same time. seems bug in chrome 60. if confirm it, great.

i think enough preventing touchstart:

window.addeventlistener( "touchstart", function(e) {   // determine wether or not panning rectangle:   if (e.target ....)       e.preventdefault(); } ); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -