Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: components/dom_distiller/core/javascript/dom_distiller_viewer.js

Issue 1009703002: Changing font size with pinch gesture in Reader Mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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.3;
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;
jdduke (slow) 2015/03/27 01:26:25 We'll probably want some kind of minimum span thre
wychen 2015/04/02 02:05:59 Done.
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 return sum/count;
154 }
155
156 function touchClientMid(e) {
157 var count = e.touches.length;
158 var sumX = 0;
159 var sumY = 0;
160 for (var i = 0; i < count; i++) {
161 sumX += e.touches[i].clientX;
162 sumY += e.touches[i].clientY;
163 }
164 return {x: sumX/count, y: sumY/count};
165 }
166
167 function touchPageMid(e) {
168 var count = e.touches.length;
169 var sumX = 0;
170 var sumY = 0;
171 for (var i = 0; i < count; i++) {
172 sumX += e.touches[i].pageX;
173 sumY += e.touches[i].pageY;
174 }
175 return {x: sumX/count, y: sumY/count};
176 }
177
178 return {
179 handleTouchStart: function(e) {
180 if (e.touches.length < 2) return;
181 e.preventDefault();
182
183 var span = touchSpan(e);
184 var clientMid = touchClientMid(e);
185
186 if (e.touches.length > 2) {
187 lastSpan = span;
188 lastClientMid = clientMid;
189 refreshTransform();
190 return;
191 }
192
193 scale = 1;
194 shiftX = 0;
195 shiftY = 0;
196
197 pinching = span > 0;
198 fontSizeAnchor =
199 parseFloat(getComputedStyle(document.documentElement).fontSize) /
200 baseSize;
201
202 var pinchOrigin = touchPageMid(e);
203 document.body.style.transformOrigin =
204 pinchOrigin.x + 'px ' + pinchOrigin.y + 'px';
205
206 // Try to preserve the pinching center after text reflow.
207 // This is accurate to the HTML element level.
208 focusElement = document.elementFromPoint(clientMid.x, clientMid.y);
209 var rect = focusElement.getBoundingClientRect();
210 initClientMid = clientMid;
211 focusPos = (initClientMid.y - rect.top) / (rect.bottom - rect.top);
212
213 lastSpan = span;
214 lastClientMid = clientMid;
215
216 refreshTransform();
217 },
218
219 handleTouchMove: function(e) {
220 if (!pinching) return;
221 if (e.touches.length < 2) return;
222 e.preventDefault();
223
224 var span = touchSpan(e);
225 var clientMid = touchClientMid(e);
226
227 scale *= touchSpan(e) / lastSpan;
228 shiftX += clientMid.x - lastClientMid.x;
229 shiftY += clientMid.y - lastClientMid.y;
230
231 refreshTransform();
232
233 lastSpan = span;
234 lastClientMid = clientMid;
235 },
236
237 handleTouchEnd: function(e) {
238 if (!pinching) return;
239 e.preventDefault();
240
241 var span = touchSpan(e);
242 var clientMid = touchClientMid(e);
243
244 if (e.touches.length >= 2) {
245 lastSpan = span;
246 lastClientMid = clientMid;
247 refreshTransform();
248 return;
249 }
250
251 endPinch();
252 },
253
254 handleTouchCancel: function(e) {
255 endPinch();
256 },
257
258 reset: function() {
259 scale = 1;
260 shiftX = 0;
261 shiftY = 0;
262 clampedScale = 1;
263 document.documentElement.style.fontSize = clampedScale * baseSize + "px";
264 },
265
266 status: function() {
267 return {
268 scale: scale,
269 clampedScale: clampedScale,
270 shiftX: shiftX,
271 shiftY: shiftY
272 };
273 }
274 };
275 }());
276
277 window.addEventListener('touchstart', pincher.handleTouchStart, false);
278 window.addEventListener('touchmove', pincher.handleTouchMove, false);
279 window.addEventListener('touchend', pincher.handleTouchEnd, false);
280 window.addEventListener('touchcancel', pincher.handleTouchCancel, false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698