OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 function addToPage(html) { | 5 function addToPage(html) { |
6 var div = document.createElement('div'); | 6 var div = document.createElement('div'); |
7 div.innerHTML = html; | 7 div.innerHTML = html; |
8 document.getElementById('content').appendChild(div); | 8 document.getElementById('content').appendChild(div); |
9 } | 9 } |
10 | 10 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 }(); | 67 }(); |
68 | 68 |
69 // Add a listener to the "View Original" link to report opt-outs. | 69 // Add a listener to the "View Original" link to report opt-outs. |
70 document.getElementById('showOriginal').addEventListener('click', function(e) { | 70 document.getElementById('showOriginal').addEventListener('click', function(e) { |
71 var img = document.createElement('img'); | 71 var img = document.createElement('img'); |
72 img.src = "/vieworiginal"; | 72 img.src = "/vieworiginal"; |
73 img.style.display = "none"; | 73 img.style.display = "none"; |
74 document.body.appendChild(img); | 74 document.body.appendChild(img); |
75 }, true); | 75 }, true); |
76 | 76 |
77 var pincher = (function() { | |
78 'use strict'; | |
79 // When users pinch in Reader Mode, the page would zoom in or out as if it | |
80 // is a normal web page allowing user-zoom. At the end of pinch gesture, the | |
81 // page would do text reflow. These pinch-to-zoom and text reflow effects | |
82 // are not native, but are emulated using CSS and JavaScript. | |
83 // | |
84 // In order to achieve near-native zooming and panning frame rate, fake 3D | |
85 // transform is used so that the layer doesn't repaint for each frame. | |
86 // | |
87 // After the text reflow, the web content shown in viewport should roughly be | |
88 // the same paragraph before zooming. | |
89 // | |
90 // The control point of font size is the html element, so that both "em" and | |
91 // "rem" are adjusted. | |
92 // | |
93 // TODO(wychen): Improve scroll position when elementFromPoint is body. | |
94 | |
95 var pinching = false; | |
96 var fontSizeAnchor = 1.0; | |
97 | |
98 var focusElement = null; | |
99 var focusPos = 0; | |
100 var initClientMid; | |
101 | |
102 var clampedScale = 1; | |
103 | |
104 var lastSpan; | |
105 var lastClientMid; | |
106 | |
107 var scale = 1; | |
108 var shiftX; | |
109 var shiftY; | |
110 | |
111 // The zooming speed relative to pinching speed. | |
112 // @const | |
113 var FONT_SCALE_MULTIPLIER = 0.5; | |
114 | |
115 // The font size is guaranteed to be in px. | |
116 var baseSize = | |
117 parseFloat(getComputedStyle(document.documentElement).fontSize); | |
118 | |
119 var refreshTransform = function() { | |
120 var slowedScale = Math.exp(Math.log(scale) * FONT_SCALE_MULTIPLIER); | |
121 clampedScale = Math.max(0.4, Math.min(2.5, fontSizeAnchor * slowedScale)); | |
122 | |
123 // Use "fake" 3D transform so that the layer is not repainted. | |
124 // With 2D transform, the frame rate would be much lower. | |
125 document.body.style.transform = | |
126 'translate3d(' + shiftX + 'px,' + | |
127 shiftY + 'px, 0px)' + | |
128 'scale(' + clampedScale/fontSizeAnchor + ')'; | |
129 }; | |
130 | |
131 function endPinch() { | |
132 pinching = false; | |
133 | |
134 document.body.style.transformOrigin = ''; | |
135 document.body.style.transform = ''; | |
136 document.documentElement.style.fontSize = clampedScale * baseSize + "px"; | |
137 | |
138 var rect = focusElement.getBoundingClientRect(); | |
139 var targetTop = focusPos * (rect.bottom - rect.top) + rect.top + | |
140 document.body.scrollTop - (initClientMid.y + shiftY); | |
141 document.body.scrollTop = targetTop; | |
142 } | |
143 | |
144 function touchSpan(e) { | |
145 var count = e.touches.length; | |
146 var mid = touchClientMid(e); | |
147 var sum = 0; | |
148 for (var i = 0; i < count; i++) { | |
149 var dx = (e.touches[i].clientX - mid.x); | |
150 var dy = (e.touches[i].clientY - mid.y); | |
151 sum += Math.hypot(dx, dy); | |
152 } | |
153 // Avoid very small span. | |
154 return Math.max(20, sum/count); | |
jdduke (slow)
2015/04/02 23:34:22
Let's stick the 20 value as a constant at the op (
wychen
2015/04/03 07:04:39
Done.
| |
155 } | |
156 | |
157 function touchClientMid(e) { | |
158 var count = e.touches.length; | |
159 var sumX = 0; | |
160 var sumY = 0; | |
161 for (var i = 0; i < count; i++) { | |
162 sumX += e.touches[i].clientX; | |
163 sumY += e.touches[i].clientY; | |
164 } | |
165 return {x: sumX/count, y: sumY/count}; | |
166 } | |
167 | |
168 function touchPageMid(e) { | |
169 var count = e.touches.length; | |
170 var sumX = 0; | |
171 var sumY = 0; | |
172 for (var i = 0; i < count; i++) { | |
173 sumX += e.touches[i].pageX; | |
174 sumY += e.touches[i].pageY; | |
175 } | |
176 return {x: sumX/count, y: sumY/count}; | |
177 } | |
178 | |
179 return { | |
180 handleTouchStart: function(e) { | |
181 if (e.touches.length < 2) return; | |
182 e.preventDefault(); | |
183 | |
184 var span = touchSpan(e); | |
185 var clientMid = touchClientMid(e); | |
186 | |
187 if (e.touches.length > 2) { | |
188 lastSpan = span; | |
189 lastClientMid = clientMid; | |
190 refreshTransform(); | |
191 return; | |
192 } | |
193 | |
194 scale = 1; | |
195 shiftX = 0; | |
196 shiftY = 0; | |
197 | |
198 pinching = true; | |
199 fontSizeAnchor = | |
200 parseFloat(getComputedStyle(document.documentElement).fontSize) / | |
201 baseSize; | |
202 | |
203 var pinchOrigin = touchPageMid(e); | |
204 document.body.style.transformOrigin = | |
205 pinchOrigin.x + 'px ' + pinchOrigin.y + 'px'; | |
206 | |
207 // Try to preserve the pinching center after text reflow. | |
208 // This is accurate to the HTML element level. | |
209 focusElement = document.elementFromPoint(clientMid.x, clientMid.y); | |
210 var rect = focusElement.getBoundingClientRect(); | |
211 initClientMid = clientMid; | |
212 focusPos = (initClientMid.y - rect.top) / (rect.bottom - rect.top); | |
213 | |
214 lastSpan = span; | |
215 lastClientMid = clientMid; | |
216 | |
217 refreshTransform(); | |
218 }, | |
219 | |
220 handleTouchMove: function(e) { | |
221 if (!pinching) return; | |
222 if (e.touches.length < 2) return; | |
223 e.preventDefault(); | |
224 | |
225 var span = touchSpan(e); | |
226 var clientMid = touchClientMid(e); | |
227 | |
228 scale *= touchSpan(e) / lastSpan; | |
229 shiftX += clientMid.x - lastClientMid.x; | |
230 shiftY += clientMid.y - lastClientMid.y; | |
231 | |
232 refreshTransform(); | |
233 | |
234 lastSpan = span; | |
235 lastClientMid = clientMid; | |
236 }, | |
237 | |
238 handleTouchEnd: function(e) { | |
239 if (!pinching) return; | |
240 e.preventDefault(); | |
241 | |
242 var span = touchSpan(e); | |
243 var clientMid = touchClientMid(e); | |
244 | |
245 if (e.touches.length >= 2) { | |
246 lastSpan = span; | |
247 lastClientMid = clientMid; | |
248 refreshTransform(); | |
249 return; | |
250 } | |
251 | |
252 endPinch(); | |
253 }, | |
254 | |
255 handleTouchCancel: function(e) { | |
256 endPinch(); | |
257 }, | |
258 | |
259 reset: function() { | |
260 scale = 1; | |
261 shiftX = 0; | |
262 shiftY = 0; | |
263 clampedScale = 1; | |
264 document.documentElement.style.fontSize = clampedScale * baseSize + "px"; | |
265 }, | |
266 | |
267 status: function() { | |
268 return { | |
269 scale: scale, | |
270 clampedScale: clampedScale, | |
271 shiftX: shiftX, | |
272 shiftY: shiftY | |
273 }; | |
274 } | |
275 }; | |
276 }()); | |
277 | |
278 window.addEventListener('touchstart', pincher.handleTouchStart, false); | |
279 window.addEventListener('touchmove', pincher.handleTouchMove, false); | |
280 window.addEventListener('touchend', pincher.handleTouchEnd, false); | |
281 window.addEventListener('touchcancel', pincher.handleTouchCancel, false); | |
OLD | NEW |