-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pinch_zoom_gestures.html
132 lines (121 loc) · 3.55 KB
/
Pinch_zoom_gestures.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang=en>
<head>
<title>Neodigm | Pointer Events Pinch/Zoom pattern</title>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=2, minimum-scale=1, width=device-width, height=device-height"/>
</head>
<body>
<section
data-panzoom="true">
<img src="https://www.ltdcommodities.com/images/product/90323_1324569_WOY_zm1.jpg">
</section>
<hr>
<pre>ant</pre>
<script>
var eLog = document.querySelector("pre");
var fThrottle = ( function( _t ){
var bOk = true;
return {
"doFnc": function( fnc ){
if( bOk ){
bOk = false;
setTimeout( fThrottle.doDelay, _t );
fnc();
}
},
"doDelay": function(){ bOk = true; }
};
})( 256 );
var fPinch = ((_d)=>{
var aElem = []; // Elements
var nXYdiff = -1, fZi, fZo;
return {
"init": (sQuery, _fZi, _fZo)=>{ // DOM Query, Zoom In, Zoom Out call backs
var aTarg = _d.querySelectorAll( sQuery );
if( _fZi ) fZi = _fZi;
if( _fZo ) fZo = _fZo;
aTarg.forEach( ( eTarg )=>{
var eImg = eTarg.querySelector( "img" );
eTarg.aEv = [];
aElem.push( eTarg );
eTarg.onpointerdown = fPinch.pointDown;
eTarg.onpointermove = fPinch.pointMove;
eTarg.onpointerup = fPinch.pointUp;
eTarg.onpointercancel = fPinch.pointUp;
eTarg.onpointerout = fPinch.pointUp;
eTarg.onpointerleave = fPinch.pointUp;
});
return ( aTarg.length !== 0 );
},
"pointDown": function(ev){ this.aEv.push( ev ); },
"pointMove": function(ev){
this.aEv = this.aEv.map(function( _ev ){ // Update Event
return ( _ev.pointerId === ev.pointerId ) ? ev : _ev;
});
if( this.aEv.length == 2) { // Distance between two pointers
var nXYnew = Math.abs(this.aEv[0].clientX - this.aEv[1].clientX);
if( nXYdiff > 100) {
if( nXYnew > nXYdiff) { fThrottle.doFnc( fPinch.doZoomIn ); }
if( nXYnew < nXYdiff) { fThrottle.doFnc( fPinch.doZoomOut ); }
}
nXYdiff = nXYnew;
}
},
"pointUp": function(ev){
this.aEv = this.aEv.filter(function( _ev ){
return ( _ev.pointerId !== ev.pointerId ); });
if( this.aEv.length < 2 ) nXYdiff = -1; // reset
},
"doZoomIn": function(){ if( fZi ) fZi(); },
"doZoomOut": function(){ if( fZo ) fZo(); }
};
})( document );
var fZoom = (function( _d, _q ){
var eZImg, nPerCent = 100;
eZimg = _d.querySelector( _q );
return {
"doIn": function(){
if( eZimg ){
nPerCent = nPerCent + ( (nPerCent < 320) ? 20 : 0 );
eZimg.style.width = nPerCent + "%";
}
},
"doOut": function(){
if( eZimg ){
nPerCent = nPerCent - ( (nPerCent > 80 ) ? 20 : 0 );
eZimg.style.width = nPerCent + "%";
}
}
};
})( document, "[data-panzoom] img" );
fPinch.init("[data-panzoom]",
()=>{ // Zoom In callback function
eLog.innerHTML = " in\n" + eLog.innerHTML;
fZoom.doIn();
},
()=>{ // Zoom Out callback function
eLog.innerHTML = " out\n" + eLog.innerHTML;
fZoom.doOut();
});
</script>
<style>
[data-panzoom] {
width: 100%; height: calc(100vw - 88px);
margin: 8px auto;
border: solid 1px #444;
touch-action:none;
overflow: hidden;
}
[data-panzoom] img {
width: 100%;
transition: width .6s;
}
</style>
<p align="center">
<a target="_blank" href="https://www.thescottkrause.com">
<img src="https://neodigm.github.io/pan-fried-monkey-fisticuffs/thescottkrause_contact_card.png" title="Three.js 🚀 TypeScript 🍭 WASM ✨ Go">
</a>
</p>
</body>
</html>