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 fillYouTubePlaceholders(); | 9 fillYouTubePlaceholders(); |
10 } | 10 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 156 |
157 document.getElementById('feedbackContainer').addEventListener('animationend', | 157 document.getElementById('feedbackContainer').addEventListener('animationend', |
158 function(e) { | 158 function(e) { |
159 document.getElementById('feedbackContainer').style.display = 'none'; | 159 document.getElementById('feedbackContainer').style.display = 'none'; |
160 // Close the gap where the feedback form was. | 160 // Close the gap where the feedback form was. |
161 var contentWrap = document.getElementById('contentWrap'); | 161 var contentWrap = document.getElementById('contentWrap'); |
162 contentWrap.style.transition = '0.5s'; | 162 contentWrap.style.transition = '0.5s'; |
163 contentWrap.style.paddingBottom = '0px'; | 163 contentWrap.style.paddingBottom = '0px'; |
164 }, true); | 164 }, true); |
165 | 165 |
| 166 document.getElementById('contentWrap').addEventListener('transitionend', |
| 167 function(e) { |
| 168 var contentWrap = document.getElementById('contentWrap'); |
| 169 contentWrap.style.transition = ''; |
| 170 }, true); |
| 171 |
| 172 var pincher = (function() { |
| 173 'use strict'; |
| 174 // When users pinch in Reader Mode, the page would zoom in or out as if it |
| 175 // is a normal web page allowing user-zoom. At the end of pinch gesture, the |
| 176 // page would do text reflow. These pinch-to-zoom and text reflow effects |
| 177 // are not native, but are emulated using CSS and JavaScript. |
| 178 // |
| 179 // In order to achieve near-native zooming and panning frame rate, fake 3D |
| 180 // transform is used so that the layer doesn't repaint for each frame. |
| 181 // |
| 182 // After the text reflow, the web content shown in the viewport should |
| 183 // roughly be the same paragraph before zooming. |
| 184 // |
| 185 // The control point of font size is the html element, so that both "em" and |
| 186 // "rem" are adjusted. |
| 187 // |
| 188 // TODO(wychen): Improve scroll position when elementFromPoint is body. |
| 189 |
| 190 var pinching = false; |
| 191 var fontSizeAnchor = 1.0; |
| 192 |
| 193 var focusElement = null; |
| 194 var focusPos = 0; |
| 195 var initClientMid; |
| 196 |
| 197 var clampedScale = 1; |
| 198 |
| 199 var lastSpan; |
| 200 var lastClientMid; |
| 201 |
| 202 var scale = 1; |
| 203 var shiftX; |
| 204 var shiftY; |
| 205 |
| 206 // The zooming speed relative to pinching speed. |
| 207 const FONT_SCALE_MULTIPLIER = 0.5; |
| 208 const MIN_SPAN_LENGTH = 20; |
| 209 |
| 210 // The font size is guaranteed to be in px. |
| 211 var baseSize = |
| 212 parseFloat(getComputedStyle(document.documentElement).fontSize); |
| 213 |
| 214 var refreshTransform = function() { |
| 215 var slowedScale = Math.exp(Math.log(scale) * FONT_SCALE_MULTIPLIER); |
| 216 clampedScale = Math.max(0.4, Math.min(2.5, fontSizeAnchor * slowedScale)); |
| 217 |
| 218 // Use "fake" 3D transform so that the layer is not repainted. |
| 219 // With 2D transform, the frame rate would be much lower. |
| 220 document.body.style.transform = |
| 221 'translate3d(' + shiftX + 'px,' + |
| 222 shiftY + 'px, 0px)' + |
| 223 'scale(' + clampedScale/fontSizeAnchor + ')'; |
| 224 }; |
| 225 |
| 226 function endPinch() { |
| 227 pinching = false; |
| 228 |
| 229 document.body.style.transformOrigin = ''; |
| 230 document.body.style.transform = ''; |
| 231 document.documentElement.style.fontSize = clampedScale * baseSize + "px"; |
| 232 |
| 233 var rect = focusElement.getBoundingClientRect(); |
| 234 var targetTop = focusPos * (rect.bottom - rect.top) + rect.top + |
| 235 document.body.scrollTop - (initClientMid.y + shiftY); |
| 236 document.body.scrollTop = targetTop; |
| 237 } |
| 238 |
| 239 function touchSpan(e) { |
| 240 var count = e.touches.length; |
| 241 var mid = touchClientMid(e); |
| 242 var sum = 0; |
| 243 for (var i = 0; i < count; i++) { |
| 244 var dx = (e.touches[i].clientX - mid.x); |
| 245 var dy = (e.touches[i].clientY - mid.y); |
| 246 sum += Math.hypot(dx, dy); |
| 247 } |
| 248 // Avoid very small span. |
| 249 return Math.max(MIN_SPAN_LENGTH, sum/count); |
| 250 } |
| 251 |
| 252 function touchClientMid(e) { |
| 253 var count = e.touches.length; |
| 254 var sumX = 0; |
| 255 var sumY = 0; |
| 256 for (var i = 0; i < count; i++) { |
| 257 sumX += e.touches[i].clientX; |
| 258 sumY += e.touches[i].clientY; |
| 259 } |
| 260 return {x: sumX/count, y: sumY/count}; |
| 261 } |
| 262 |
| 263 function touchPageMid(e) { |
| 264 var clientMid = touchClientMid(e); |
| 265 return {x: clientMid.x - e.touches[0].clientX + e.touches[0].pageX, |
| 266 y: clientMid.y - e.touches[0].clientY + e.touches[0].pageY}; |
| 267 } |
| 268 |
| 269 return { |
| 270 handleTouchStart: function(e) { |
| 271 if (e.touches.length < 2) return; |
| 272 e.preventDefault(); |
| 273 |
| 274 var span = touchSpan(e); |
| 275 var clientMid = touchClientMid(e); |
| 276 |
| 277 if (e.touches.length > 2) { |
| 278 lastSpan = span; |
| 279 lastClientMid = clientMid; |
| 280 refreshTransform(); |
| 281 return; |
| 282 } |
| 283 |
| 284 scale = 1; |
| 285 shiftX = 0; |
| 286 shiftY = 0; |
| 287 |
| 288 pinching = true; |
| 289 fontSizeAnchor = |
| 290 parseFloat(getComputedStyle(document.documentElement).fontSize) / |
| 291 baseSize; |
| 292 |
| 293 var pinchOrigin = touchPageMid(e); |
| 294 document.body.style.transformOrigin = |
| 295 pinchOrigin.x + 'px ' + pinchOrigin.y + 'px'; |
| 296 |
| 297 // Try to preserve the pinching center after text reflow. |
| 298 // This is accurate to the HTML element level. |
| 299 focusElement = document.elementFromPoint(clientMid.x, clientMid.y); |
| 300 var rect = focusElement.getBoundingClientRect(); |
| 301 initClientMid = clientMid; |
| 302 focusPos = (initClientMid.y - rect.top) / (rect.bottom - rect.top); |
| 303 |
| 304 lastSpan = span; |
| 305 lastClientMid = clientMid; |
| 306 |
| 307 refreshTransform(); |
| 308 }, |
| 309 |
| 310 handleTouchMove: function(e) { |
| 311 if (!pinching) return; |
| 312 if (e.touches.length < 2) return; |
| 313 e.preventDefault(); |
| 314 |
| 315 var span = touchSpan(e); |
| 316 var clientMid = touchClientMid(e); |
| 317 |
| 318 scale *= touchSpan(e) / lastSpan; |
| 319 shiftX += clientMid.x - lastClientMid.x; |
| 320 shiftY += clientMid.y - lastClientMid.y; |
| 321 |
| 322 refreshTransform(); |
| 323 |
| 324 lastSpan = span; |
| 325 lastClientMid = clientMid; |
| 326 }, |
| 327 |
| 328 handleTouchEnd: function(e) { |
| 329 if (!pinching) return; |
| 330 e.preventDefault(); |
| 331 |
| 332 var span = touchSpan(e); |
| 333 var clientMid = touchClientMid(e); |
| 334 |
| 335 if (e.touches.length >= 2) { |
| 336 lastSpan = span; |
| 337 lastClientMid = clientMid; |
| 338 refreshTransform(); |
| 339 return; |
| 340 } |
| 341 |
| 342 endPinch(); |
| 343 }, |
| 344 |
| 345 handleTouchCancel: function(e) { |
| 346 endPinch(); |
| 347 }, |
| 348 |
| 349 reset: function() { |
| 350 scale = 1; |
| 351 shiftX = 0; |
| 352 shiftY = 0; |
| 353 clampedScale = 1; |
| 354 document.documentElement.style.fontSize = clampedScale * baseSize + "px"; |
| 355 }, |
| 356 |
| 357 status: function() { |
| 358 return { |
| 359 scale: scale, |
| 360 clampedScale: clampedScale, |
| 361 shiftX: shiftX, |
| 362 shiftY: shiftY |
| 363 }; |
| 364 } |
| 365 }; |
| 366 }()); |
| 367 |
| 368 window.addEventListener('touchstart', pincher.handleTouchStart, false); |
| 369 window.addEventListener('touchmove', pincher.handleTouchMove, false); |
| 370 window.addEventListener('touchend', pincher.handleTouchEnd, false); |
| 371 window.addEventListener('touchcancel', pincher.handleTouchCancel, false); |
OLD | NEW |