Skip to content

Commit 3d68a08

Browse files
author
Jonas Habel
committed
fixing bug in pinch to zoom:
avoid using changedTouches on touch events since we rely on information of two touchpoints always Passing fake clientX and clientY properties to zoom function when using pinch to zoom since property is required to focus adding parameter to adjust the force by which the zoom factor is being calculated
1 parent 82a3f3d commit 3d68a08

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

js/main.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -897,10 +897,10 @@ class PainterroProc {
897897
}
898898
return handled;
899899
}
900-
zoomImage({ wheelDelta, clientX, clientY }, forceWheenDelta) {
900+
zoomImage({ wheelDelta, clientX, clientY }, forceWheenDelta, forceZoomForce) {
901901
let whD = wheelDelta;
902902
if (forceWheenDelta !== undefined) {
903-
whD = 1;
903+
whD = forceWheenDelta;
904904
}
905905
let minFactor = 1;
906906
if (this.size.w > this.wrapper.documentClientWidth) {
@@ -912,7 +912,12 @@ class PainterroProc {
912912
if (!this.zoom && this.zoomFactor > minFactor) {
913913
this.zoomFactor = minFactor;
914914
}
915-
this.zoomFactor += Math.sign(whD) * 0.2;
915+
let zoomForce = 0.2;
916+
if (forceZoomForce !== undefined) {
917+
zoomForce = forceZoomForce;
918+
}
919+
920+
this.zoomFactor += Math.sign(whD) * zoomForce;
916921
if (this.zoomFactor < minFactor) {
917922
this.zoom = false;
918923
this.zoomFactor = minFactor;
@@ -950,11 +955,11 @@ class PainterroProc {
950955
this.documentHandlers.mousedown(e);
951956
} else if (e.touches.length === 2) {
952957
const fingersDist = distance({
953-
x: e.changedTouches[0].clientX,
954-
y: e.changedTouches[0].clientY,
958+
x: e.touches[0].clientX,
959+
y: e.touches[0].clientY,
955960
}, {
956-
x: e.changedTouches[1].clientX,
957-
y: e.changedTouches[1].clientY,
961+
x: e.touches[1].clientX,
962+
y: e.touches[1].clientY,
958963
});
959964
this.lastFingerDist = fingersDist;
960965
}
@@ -966,23 +971,31 @@ class PainterroProc {
966971
},
967972
touchmove: (e) => {
968973
if (e.touches.length === 1) {
969-
e.clientX = e.changedTouches[0].clientX;
970-
e.clientY = e.changedTouches[0].clientY;
974+
e.clientX = e.touches[0].clientX;
975+
e.clientY = e.touches[0].clientY;
971976
this.documentHandlers.mousemove(e);
972977
} else if (e.touches.length === 2) {
973978
const fingersDist = distance({
974-
x: e.changedTouches[0].clientX,
975-
y: e.changedTouches[0].clientY,
979+
x: e.touches[0].clientX,
980+
y: e.touches[0].clientY,
976981
}, {
977-
x: e.changedTouches[1].clientX,
978-
y: e.changedTouches[1].clientY,
982+
x: e.touches[1].clientX,
983+
y: e.touches[1].clientY,
979984
});
980985

981-
if (fingersDist > this.lastFingerDist) {
982-
this.documentHandlers.wheel(e, 1, true);
983-
} else if (fingersDist < this.lastFingerDist) {
984-
this.documentHandlers.wheel(e, 1, true);
985-
}
986+
const center = {
987+
x: (e.touches[0].clientX + e.touches[1].clientX) / 2,
988+
y: (e.touches[0].clientY + e.touches[1].clientY) / 2,
989+
};
990+
991+
e.clientX = center.x;
992+
e.clientY = center.y;
993+
994+
const fingerDistDiff = Math.abs(fingersDist - this.lastFingerDist);
995+
const zoomForce = (fingersDist > this.lastFingerDist ? 1 : -1);
996+
997+
this.documentHandlers.wheel(e, zoomForce, true, fingerDistDiff * 0.001);
998+
986999
this.lastFingerDist = fingersDist;
9871000
e.stopPropagation();
9881001
if (!this.zoomButtonActive) e.preventDefault();
@@ -1011,10 +1024,10 @@ class PainterroProc {
10111024
this.colorPicker.handleMouseUp(e);
10121025
}
10131026
},
1014-
wheel: (e, forceWheenDelta, forceCtrlKey) => {
1027+
wheel: (e, forceWheenDelta, forceCtrlKey, zoomForce) => {
10151028
if (this.shown) {
10161029
if (forceCtrlKey !== undefined ? forceCtrlKey : e.ctrlKey) {
1017-
this.zoomImage(e, forceWheenDelta);
1030+
this.zoomImage(e, forceWheenDelta, zoomForce);
10181031
e.preventDefault();
10191032
}
10201033
}

0 commit comments

Comments
 (0)