jQuery touchend イベントを使用している場合、layerX および layerY 属性は相対的な x および y を提供しますが、変換は考慮されません。そのため、css から変換スケール値を取得して手動で変換を適用しました。
e = jQuery からの touchend イベント
parent = 「変換」が適用される親要素の jQuery オブジェクト。私にとってはそうでした$(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;
最後の 2 行をタッチ イベントとクリック イベントに対応させるには:
const offsetX = e.offsetX || layerX * currentScaleX;
const offsetY = e.offsetY || layerY * currentScaleY;
変換値を 取得するための参照: jquery を使用して変換の css 値を直接取得する
Reactjs を使用している場合はonClick
、layerX と layerY の値を次のように取得できますe.nativeEvent.layerX
。何らかの理由で、React タッチ イベントで layerX と layerY を使用できません。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;
注: トランスフォームの値は、jQuery で取得するよりも Redux に格納することをお勧めします。