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

Side by Side Diff: third_party/polymer/v0_8/components-chromium/paper-ripple/paper-ripple-extracted.js

Issue 1082403004: Import Polymer 0.8 and several key elements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Also remove polymer/explainer Created 5 years, 7 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
(Empty)
1
2 (function() {
3 var Utility = {
4 cssColorWithAlpha: function(cssColor, alpha) {
5 var parts = cssColor.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
6
7 if (typeof alpha == 'undefined') {
8 alpha = 1;
9 }
10
11 if (!parts) {
12 return 'rgba(255, 255, 255, ' + alpha + ')';
13 }
14
15 return 'rgba(' + parts[1] + ', ' + parts[2] + ', ' + parts[3] + ', ' + a lpha + ')';
16 },
17
18 distance: function(x1, y1, x2, y2) {
19 var xDelta = (x1 - x2);
20 var yDelta = (y1 - y2);
21
22 return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
23 },
24
25 now: (function() {
26 if (window.performance && window.performance.now) {
27 return window.performance.now.bind(window.performance);
28 }
29
30 return Date.now;
31 })()
32 };
33
34 /**
35 * @param {HTMLElement} element
36 * @constructor
37 */
38 function ElementMetrics(element) {
39 this.element = element;
40 this.width = this.boundingRect.width;
41 this.height = this.boundingRect.height;
42
43 this.size = Math.max(this.width, this.height);
44 }
45
46 ElementMetrics.prototype = {
47 get boundingRect () {
48 return this.element.getBoundingClientRect();
49 },
50
51 furthestCornerDistanceFrom: function(x, y) {
52 var topLeft = Utility.distance(x, y, 0, 0);
53 var topRight = Utility.distance(x, y, this.width, 0);
54 var bottomLeft = Utility.distance(x, y, 0, this.height);
55 var bottomRight = Utility.distance(x, y, this.width, this.height);
56
57 return Math.max(topLeft, topRight, bottomLeft, bottomRight);
58 }
59 };
60
61 /**
62 * @param {HTMLElement} element
63 * @constructor
64 */
65 function Ripple(element) {
66 this.element = element;
67 this.color = window.getComputedStyle(element).color;
68
69 this.wave = document.createElement('div');
70 this.waveContainer = document.createElement('div');
71 this.wave.style.backgroundColor = this.color;
72 this.wave.classList.add('wave');
73 this.waveContainer.classList.add('wave-container');
74 Polymer.dom(this.waveContainer).appendChild(this.wave);
75
76 this.resetInteractionState();
77 }
78
79 Ripple.MAX_RADIUS = 300;
80
81 Ripple.prototype = {
82 get recenters() {
83 return this.element.recenters;
84 },
85
86 get mouseDownElapsed() {
87 var elapsed;
88
89 if (!this.mouseDownStart) {
90 return 0;
91 }
92
93 elapsed = Utility.now() - this.mouseDownStart;
94
95 if (this.mouseUpStart) {
96 elapsed -= this.mouseUpElapsed;
97 }
98
99 return elapsed;
100 },
101
102 get mouseUpElapsed() {
103 return this.mouseUpStart ?
104 Utility.now () - this.mouseUpStart : 0;
105 },
106
107 get mouseDownElapsedSeconds() {
108 return this.mouseDownElapsed / 1000;
109 },
110
111 get mouseUpElapsedSeconds() {
112 return this.mouseUpElapsed / 1000;
113 },
114
115 get mouseInteractionSeconds() {
116 return this.mouseDownElapsedSeconds + this.mouseUpElapsedSeconds;
117 },
118
119 get initialOpacity() {
120 return this.element.initialOpacity;
121 },
122
123 get opacityDecayVelocity() {
124 return this.element.opacityDecayVelocity;
125 },
126
127 get radius() {
128 var width2 = this.containerMetrics.width * this.containerMetrics.width;
129 var height2 = this.containerMetrics.height * this.containerMetrics.heigh t;
130 var waveRadius = Math.min(
131 Math.sqrt(width2 + height2),
132 Ripple.MAX_RADIUS
133 ) * 1.1 + 5;
134
135 var duration = 1.1 - 0.2 * (waveRadius / Ripple.MAX_RADIUS);
136 var timeNow = this.mouseInteractionSeconds / duration;
137 var size = waveRadius * (1 - Math.pow(80, -timeNow));
138
139 return Math.abs(size);
140 },
141
142 get opacity() {
143 if (!this.mouseUpStart) {
144 return this.initialOpacity;
145 }
146
147 return Math.max(
148 0,
149 this.initialOpacity - this.mouseUpElapsedSeconds * this.opacityDecayVe locity
150 );
151 },
152
153 get outerOpacity() {
154 // Linear increase in background opacity, capped at the opacity
155 // of the wavefront (waveOpacity).
156 var outerOpacity = this.mouseUpElapsedSeconds * 0.3;
157 var waveOpacity = this.opacity;
158
159 return Math.max(
160 0,
161 Math.min(outerOpacity, waveOpacity)
162 );
163 },
164
165 get isOpacityFullyDecayed() {
166 return this.opacity < 0.01 &&
167 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
168 },
169
170 get isRestingAtMaxRadius() {
171 return this.opacity >= this.initialOpacity &&
172 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
173 },
174
175 get isAnimationComplete() {
176 return this.mouseUpStart ?
177 this.isOpacityFullyDecayed : this.isRestingAtMaxRadius;
178 },
179
180 get translationFraction() {
181 return Math.min(
182 1,
183 this.radius / this.containerMetrics.size * 2 / Math.sqrt(2)
184 );
185 },
186
187 get xNow() {
188 if (this.xEnd) {
189 return this.xStart + this.translationFraction * (this.xEnd - this.xSta rt);
190 }
191
192 return this.xStart;
193 },
194
195 get yNow() {
196 if (this.yEnd) {
197 return this.yStart + this.translationFraction * (this.yEnd - this.ySta rt);
198 }
199
200 return this.yStart;
201 },
202
203 get isMouseDown() {
204 return this.mouseDownStart && !this.mouseUpStart;
205 },
206
207 resetInteractionState: function() {
208 this.maxRadius = 0;
209 this.mouseDownStart = 0;
210 this.mouseUpStart = 0;
211
212 this.xStart = 0;
213 this.yStart = 0;
214 this.xEnd = 0;
215 this.yEnd = 0;
216 this.slideDistance = 0;
217
218 this.containerMetrics = new ElementMetrics(this.element);
219 },
220
221 draw: function() {
222 var scale;
223 var translateString;
224 var dx;
225 var dy;
226
227 this.wave.style.opacity = this.opacity;
228
229 scale = this.radius / (this.containerMetrics.size / 2);
230 dx = this.xNow - (this.containerMetrics.width / 2);
231 dy = this.yNow - (this.containerMetrics.height / 2);
232
233 Polymer.Base.translate3d(this.waveContainer, dx + 'px', dy + 'px', 0);
234
235 // 2d transform for safari because of border-radius and overflow:hidden clipping bug.
236 // https://bugs.webkit.org/show_bug.cgi?id=98538
237 this.wave.style.webkitTransform = 'scale(' + scale + ',' + scale + ')';
238 this.wave.style.transform = 'scale3d(' + scale + ',' + scale + ',1)';
239 },
240
241 mousedownAction: function(event) {
242 this.resetInteractionState();
243 this.mouseDownStart = Utility.now();
244
245 this.xStart = event ?
246 event.x - this.containerMetrics.boundingRect.left :
247 this.containerMetrics.width / 2;
248 this.yStart = event ?
249 event.y - this.containerMetrics.boundingRect.top :
250 this.containerMetrics.height / 2;
251
252 if (this.recenters) {
253 this.xEnd = this.containerMetrics.width / 2;
254 this.yEnd = this.containerMetrics.height / 2;
255 this.slideDistance = Utility.distance(
256 this.xStart, this.yStart, this.xEnd, this.yEnd
257 );
258 }
259
260 this.maxRadius = this.containerMetrics.furthestCornerDistanceFrom(
261 this.xStart,
262 this.yStart
263 );
264
265 this.waveContainer.style.top =
266 (this.containerMetrics.height - this.containerMetrics.size) / 2 + 'px' ;
267 this.waveContainer.style.left =
268 (this.containerMetrics.width - this.containerMetrics.size) / 2 + 'px';
269
270 this.waveContainer.style.width = this.containerMetrics.size + 'px';
271 this.waveContainer.style.height = this.containerMetrics.size + 'px';
272 },
273
274 mouseupAction: function(event) {
275 if (!this.isMouseDown) {
276 return;
277 }
278
279 this.mouseUpStart = Utility.now();
280 },
281
282 remove: function() {
283 Polymer.dom(this.waveContainer.parentNode).removeChild(
284 this.waveContainer
285 );
286 }
287 };
288
289 Polymer({
290 is: 'paper-ripple',
291
292 properties: {
293 /**
294 * The initial opacity set on the wave.
295 *
296 * @attribute initialOpacity
297 * @type number
298 * @default 0.25
299 */
300 initialOpacity: {
301 type: Number,
302 value: 0.25
303 },
304
305 /**
306 * How fast (opacity per second) the wave fades out.
307 *
308 * @attribute opacityDecayVelocity
309 * @type number
310 * @default 0.8
311 */
312 opacityDecayVelocity: {
313 type: Number,
314 value: 0.8
315 },
316
317 /**
318 * If true, ripples will exhibit a gravitational pull towards
319 * the center of their container as they fade away.
320 *
321 * @attribute recenters
322 * @type boolean
323 * @default false
324 */
325 recenters: {
326 type: Boolean,
327 value: false
328 },
329
330 /**
331 * A list of the visual ripples.
332 *
333 * @attribute ripples
334 * @type Array
335 * @default []
336 */
337 ripples: {
338 type: Array,
339 value: function() {
340 return [];
341 }
342 },
343
344 _animating: {
345 type: Boolean
346 },
347
348 _boundAnimate: {
349 type: Function,
350 value: function() {
351 return this.animate.bind(this);
352 }
353 },
354
355 _boundMousedownAction: {
356 type: Function,
357 value: function() {
358 return this.mousedownAction.bind(this);
359 }
360 },
361
362 _boundMouseupAction: {
363 type: Function,
364 value: function() {
365 return this.mouseupAction.bind(this);
366 }
367 }
368 },
369
370 get target () {
371 return this.host || this.parentNode;
372 },
373
374 attached: function() {
375 this.target.addEventListener('mousedown', this._boundMousedownAction);
376 this.target.addEventListener('mouseup', this._boundMouseupAction);
377 },
378
379 detached: function() {
380 this.target.removeEventListener('mousedown', this._boundMousedownAction) ;
381 this.target.removeEventListener('mouseup', this._boundMouseupAction);
382 },
383
384 /* TODO(cdata): Replace the above attached / detached listeners when
385 PolymerGestures equivalent lands in 0.8.
386 listeners: {
387 mousedown: 'mousedownAction',
388 mouseup: 'mouseupAction'
389 },
390 */
391
392 get shouldKeepAnimating () {
393 for (var index = 0; index < this.ripples.length; ++index) {
394 if (!this.ripples[index].isAnimationComplete) {
395 return true;
396 }
397 }
398
399 return false;
400 },
401
402 simulatedRipple: function() {
403 this.mousedownAction(null);
404
405 // Please see polymer/polymer#1305
406 this.async(function() {
407 this.mouseupAction();
408 }, 1);
409 },
410
411 mousedownAction: function(event) {
412 var ripple = this.addRipple();
413
414 ripple.mousedownAction(event);
415
416 if (!this._animating) {
417 this.animate();
418 }
419 },
420
421 mouseupAction: function(event) {
422 this.ripples.forEach(function(ripple) {
423 ripple.mouseupAction(event);
424 });
425
426 this.animate();
427 },
428
429 onAnimationComplete: function() {
430 this._animating = false;
431 this.$.background.style.backgroundColor = null;
432 this.fire('transitionend');
433 },
434
435 addRipple: function() {
436 var ripple = new Ripple(this);
437
438 Polymer.dom(this.$.waves).appendChild(ripple.waveContainer);
439 this.$.background.style.backgroundColor = ripple.color;
440 this.ripples.push(ripple);
441
442 return ripple;
443 },
444
445 removeRipple: function(ripple) {
446 var rippleIndex = this.ripples.indexOf(ripple);
447
448 if (rippleIndex < 0) {
449 return;
450 }
451
452 this.ripples.splice(rippleIndex, 1);
453
454 ripple.remove();
455 },
456
457 animate: function() {
458 var index;
459 var ripple;
460
461 this._animating = true;
462
463 for (index = 0; index < this.ripples.length; ++index) {
464 ripple = this.ripples[index];
465
466 ripple.draw();
467
468 this.$.background.style.opacity = ripple.outerOpacity;
469
470 if (ripple.isOpacityFullyDecayed && !ripple.isRestingAtMaxRadius) {
471 this.removeRipple(ripple);
472 }
473 }
474
475 if (this.shouldKeepAnimating) {
476 window.requestAnimationFrame(this._boundAnimate);
477 } else if (this.ripples.length === 0) {
478 this.onAnimationComplete();
479 }
480 }
481 });
482 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698