4 cssColorWithAlpha: function(cssColor, alpha) {
5 var parts = cssColor.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
7 if (typeof alpha == 'undefined') {
12 return 'rgba(255, 255, 255, ' + alpha + ')';
15 return 'rgba(' + parts[1] + ', ' + parts[2] + ', ' + parts[3] + ', ' + alpha + ')';
18 distance: function(x1, y1, x2, y2) {
19 var xDelta = (x1 - x2);
20 var yDelta = (y1 - y2);
22 return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
26 if (window.performance && window.performance.now) {
27 return window.performance.now.bind(window.performance);
35 * @param {HTMLElement} element
38 function ElementMetrics(element) {
39 this.element = element;
40 this.width = this.boundingRect.width;
41 this.height = this.boundingRect.height;
43 this.size = Math.max(this.width, this.height);
46 ElementMetrics.prototype = {
48 return this.element.getBoundingClientRect();
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);
57 return Math.max(topLeft, topRight, bottomLeft, bottomRight);
62 * @param {HTMLElement} element
65 function Ripple(element) {
66 this.element = element;
67 this.color = window.getComputedStyle(element).color;
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);
76 this.resetInteractionState();
79 Ripple.MAX_RADIUS = 300;
83 return this.element.recenters;
87 return this.element.center;
90 get mouseDownElapsed() {
93 if (!this.mouseDownStart) {
97 elapsed = Utility.now() - this.mouseDownStart;
99 if (this.mouseUpStart) {
100 elapsed -= this.mouseUpElapsed;
106 get mouseUpElapsed() {
107 return this.mouseUpStart ?
108 Utility.now () - this.mouseUpStart : 0;
111 get mouseDownElapsedSeconds() {
112 return this.mouseDownElapsed / 1000;
115 get mouseUpElapsedSeconds() {
116 return this.mouseUpElapsed / 1000;
119 get mouseInteractionSeconds() {
120 return this.mouseDownElapsedSeconds + this.mouseUpElapsedSeconds;
123 get initialOpacity() {
124 return this.element.initialOpacity;
127 get opacityDecayVelocity() {
128 return this.element.opacityDecayVelocity;
132 var width2 = this.containerMetrics.width * this.containerMetrics.width;
133 var height2 = this.containerMetrics.height * this.containerMetrics.height;
134 var waveRadius = Math.min(
135 Math.sqrt(width2 + height2),
139 var duration = 1.1 - 0.2 * (waveRadius / Ripple.MAX_RADIUS);
140 var timeNow = this.mouseInteractionSeconds / duration;
141 var size = waveRadius * (1 - Math.pow(80, -timeNow));
143 return Math.abs(size);
147 if (!this.mouseUpStart) {
148 return this.initialOpacity;
153 this.initialOpacity - this.mouseUpElapsedSeconds * this.opacityDecayVelocity
158 // Linear increase in background opacity, capped at the opacity
159 // of the wavefront (waveOpacity).
160 var outerOpacity = this.mouseUpElapsedSeconds * 0.3;
161 var waveOpacity = this.opacity;
165 Math.min(outerOpacity, waveOpacity)
169 get isOpacityFullyDecayed() {
170 return this.opacity < 0.01 &&
171 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
174 get isRestingAtMaxRadius() {
175 return this.opacity >= this.initialOpacity &&
176 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
179 get isAnimationComplete() {
180 return this.mouseUpStart ?
181 this.isOpacityFullyDecayed : this.isRestingAtMaxRadius;
184 get translationFraction() {
187 this.radius / this.containerMetrics.size * 2 / Math.sqrt(2)
193 return this.xStart + this.translationFraction * (this.xEnd - this.xStart);
201 return this.yStart + this.translationFraction * (this.yEnd - this.yStart);
208 return this.mouseDownStart && !this.mouseUpStart;
211 resetInteractionState: function() {
213 this.mouseDownStart = 0;
214 this.mouseUpStart = 0;
220 this.slideDistance = 0;
222 this.containerMetrics = new ElementMetrics(this.element);
231 this.wave.style.opacity = this.opacity;
233 scale = this.radius / (this.containerMetrics.size / 2);
234 dx = this.xNow - (this.containerMetrics.width / 2);
235 dy = this.yNow - (this.containerMetrics.height / 2);
238 // 2d transform for safari because of border-radius and overflow:hidden clipping bug.
239 // https://bugs.webkit.org/show_bug.cgi?id=98538
240 this.waveContainer.style.webkitTransform = 'translate(' + dx + 'px, ' + dy + 'px)';
241 this.waveContainer.style.transform = 'translate3d(' + dx + 'px, ' + dy + 'px, 0)';
242 this.wave.style.webkitTransform = 'scale(' + scale + ',' + scale + ')';
243 this.wave.style.transform = 'scale3d(' + scale + ',' + scale + ',1)';
246 /** @param {Event=} event */
247 downAction: function(event) {
248 var xCenter = this.containerMetrics.width / 2;
249 var yCenter = this.containerMetrics.height / 2;
251 this.resetInteractionState();
252 this.mouseDownStart = Utility.now();
255 this.xStart = xCenter;
256 this.yStart = yCenter;
257 this.slideDistance = Utility.distance(
258 this.xStart, this.yStart, this.xEnd, this.yEnd
261 this.xStart = event ?
262 event.detail.x - this.containerMetrics.boundingRect.left :
263 this.containerMetrics.width / 2;
264 this.yStart = event ?
265 event.detail.y - this.containerMetrics.boundingRect.top :
266 this.containerMetrics.height / 2;
269 if (this.recenters) {
272 this.slideDistance = Utility.distance(
273 this.xStart, this.yStart, this.xEnd, this.yEnd
277 this.maxRadius = this.containerMetrics.furthestCornerDistanceFrom(
282 this.waveContainer.style.top =
283 (this.containerMetrics.height - this.containerMetrics.size) / 2 + 'px';
284 this.waveContainer.style.left =
285 (this.containerMetrics.width - this.containerMetrics.size) / 2 + 'px';
287 this.waveContainer.style.width = this.containerMetrics.size + 'px';
288 this.waveContainer.style.height = this.containerMetrics.size + 'px';
291 /** @param {Event=} event */
292 upAction: function(event) {
293 if (!this.isMouseDown) {
297 this.mouseUpStart = Utility.now();
301 Polymer.dom(this.waveContainer.parentNode).removeChild(
311 Polymer.IronA11yKeysBehavior
316 * The initial opacity set on the wave.
318 * @attribute initialOpacity
328 * How fast (opacity per second) the wave fades out.
330 * @attribute opacityDecayVelocity
334 opacityDecayVelocity: {
340 * If true, ripples will exhibit a gravitational pull towards
341 * the center of their container as they fade away.
343 * @attribute recenters
353 * If true, ripples will center inside its container
355 * @attribute recenters
365 * A list of the visual ripples.
379 * True when there are visible ripples animating within the
385 reflectToAttribute: true,
390 * If true, the ripple will remain in the "down" state until `holdDown`
391 * is set to false again.
396 observer: '_holdDownChanged'
406 return this.animate.bind(this);
412 var ownerRoot = Polymer.dom(this).getOwnerRoot();
415 if (this.parentNode.nodeType == 11) { // DOCUMENT_FRAGMENT_NODE
416 target = ownerRoot.host;
418 target = this.parentNode;
425 'enter:keydown': '_onEnterKeydown',
426 'space:keydown': '_onSpaceKeydown',
427 'space:keyup': '_onSpaceKeyup'
430 attached: function() {
431 this.listen(this.target, 'up', 'upAction');
432 this.listen(this.target, 'down', 'downAction');
434 if (!this.target.hasAttribute('noink')) {
435 this.keyEventTarget = this.target;
439 get shouldKeepAnimating () {
440 for (var index = 0; index < this.ripples.length; ++index) {
441 if (!this.ripples[index].isAnimationComplete) {
449 simulatedRipple: function() {
450 this.downAction(null);
452 // Please see polymer/polymer#1305
453 this.async(function() {
458 /** @param {Event=} event */
459 downAction: function(event) {
460 if (this.holdDown && this.ripples.length > 0) {
464 var ripple = this.addRipple();
466 ripple.downAction(event);
468 if (!this._animating) {
473 /** @param {Event=} event */
474 upAction: function(event) {
479 this.ripples.forEach(function(ripple) {
480 ripple.upAction(event);
486 onAnimationComplete: function() {
487 this._animating = false;
488 this.$.background.style.backgroundColor = null;
489 this.fire('transitionend');
492 addRipple: function() {
493 var ripple = new Ripple(this);
495 Polymer.dom(this.$.waves).appendChild(ripple.waveContainer);
496 this.$.background.style.backgroundColor = ripple.color;
497 this.ripples.push(ripple);
499 this._setAnimating(true);
504 removeRipple: function(ripple) {
505 var rippleIndex = this.ripples.indexOf(ripple);
507 if (rippleIndex < 0) {
511 this.ripples.splice(rippleIndex, 1);
515 if (!this.ripples.length) {
516 this._setAnimating(false);
520 animate: function() {
524 this._animating = true;
526 for (index = 0; index < this.ripples.length; ++index) {
527 ripple = this.ripples[index];
531 this.$.background.style.opacity = ripple.outerOpacity;
533 if (ripple.isOpacityFullyDecayed && !ripple.isRestingAtMaxRadius) {
534 this.removeRipple(ripple);
538 if (!this.shouldKeepAnimating && this.ripples.length === 0) {
539 this.onAnimationComplete();
541 window.requestAnimationFrame(this._boundAnimate);
545 _onEnterKeydown: function() {
547 this.async(this.upAction, 1);
550 _onSpaceKeydown: function() {
554 _onSpaceKeyup: function() {
558 _holdDownChanged: function(holdDown) {