If you use jQuery touch events, the layerX and layerY attributes provide relative x and y but do not take into account the conversion. Therefore, I manually applied the transform, taking the scale values ββof the transform from CSS.
e = touch event from jQuery
parent = jQuery object of the parent element to which the "transform" is applied. For me it was $(e.target.parentNode);
const layerX = e.originalEvent.layerX; const layerY = e.originalEvent.layerY; const currentScale = 1 / parseFloat(parent.css('transform').match(/-?[\d\.]+/g)[0]); const offsetX = layerX * currentScale; const offsetY = layerY * currentScale;
To make the last two lines compatible with touch and click events:
const offsetX = e.offsetX || layerX * currentScaleX; const offsetY = e.offsetY || layerY * currentScaleY;
Link to capture conversion values: Get the conversion css value directly using jquery
If you use Reactjs onClick , then you can get the value of Layerx and layerY with: e.nativeEvent.layerX . For some reason, layerX and layerY are not available in React touch events. If you need to use React onTouchEnd :
const rect = e.nativeEvent.target.getBoundingClientRect(); const layerX = e.changedTouches[0].clientX - rect.left; const layerY = e.changedTouches[0].clientY - rect.top; const currentScale = 1 / parseFloat(parent.css('transform').match(/-?[\d\.]+/g)[0]); const offsetX = layerX * currentScale; const offsetY = layerY * currentScale;
Note. It is better to store conversion values ββin Redux rather than capturing them using jQuery.
source share