-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinch.js
70 lines (60 loc) · 1.78 KB
/
pinch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function () {
"use strict";
var initDist,
scale = 1,
initScale = 1,
processing = false,
scaling = false;
function drawBody() {
document.body.style.width = (window.innerWidth / scale) + "px";
document.body.style.transform = "scale(" + scale + ", " + scale + ")";
processing = false;
}
// Change origin to top left for scaling
function setView() {
document.body.style.transformOrigin = "top left";
drawBody();
}
if (document.readyState === "complete") {
setView();
} else {
document.addEventListener("DOMContentLoaded", setView, false);
}
// Length between objects of type Touch
function dist(point1, point2) {
return Math.sqrt(Math.pow((point1.pageX - point2.pageX), 2) +
Math.pow((point1.pageY - point2.pageY), 2));
}
function pinchStart(e) {
initDist = dist(e.touches[0], e.touches[1]);
initScale = scale;
}
function pinchMove(e) {
if (processing === false) {
var d = dist(e.touches[0], e.touches[1]);
scale = initScale * d / initDist;
window.requestAnimationFrame(drawBody);
processing = true;
}
}
document.addEventListener('touchstart', function (e) {
if (e.touches.length === 2) {
e.preventDefault();
scaling = true;
pinchStart(e);
} else {
scaling = false;
}
}, false);
document.addEventListener('touchmove', function (e) {
if (scaling) {
e.preventDefault();
pinchMove(e);
}
}, false);
document.addEventListener('touchend', function (e) {
if (scaling) {
scaling = false;
}
}, false);
}());