4 distance: function(x1, y1, x2, y2) {
5 var xDelta = (x1 - x2);
6 var yDelta = (y1 - y2);
8 return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
11 now: window.performance && window.performance.now ?
12 window.performance.now.bind(window.performance) : Date.now
16 * @param {HTMLElement} element
19 function ElementMetrics(element) {
20 this.element = element;
21 this.width = this.boundingRect.width;
22 this.height = this.boundingRect.height;
24 this.size = Math.max(this.width, this.height);
27 ElementMetrics.prototype = {
29 return this.element.getBoundingClientRect();
32 furthestCornerDistanceFrom: function(x, y) {
33 var topLeft = Utility.distance(x, y, 0, 0);
34 var topRight = Utility.distance(x, y, this.width, 0);
35 var bottomLeft = Utility.distance(x, y, 0, this.height);
36 var bottomRight = Utility.distance(x, y, this.width, this.height);
38 return Math.max(topLeft, topRight, bottomLeft, bottomRight);
43 * @param {HTMLElement} element
46 function Ripple(element) {
47 this.element = element;
48 this.color = window.getComputedStyle(element).color;
50 this.wave = document.createElement('div');
51 this.waveContainer = document.createElement('div');
52 this.wave.style.backgroundColor = this.color;
53 this.wave.classList.add('wave');
54 this.waveContainer.classList.add('wave-container');
55 Polymer.dom(this.waveContainer).appendChild(this.wave);
57 this.resetInteractionState();
60 Ripple.MAX_RADIUS = 300;
64 return this.element.recenters;
68 return this.element.center;
71 get mouseDownElapsed() {
74 if (!this.mouseDownStart) {
78 elapsed = Utility.now() - this.mouseDownStart;
80 if (this.mouseUpStart) {
81 elapsed -= this.mouseUpElapsed;
87 get mouseUpElapsed() {
88 return this.mouseUpStart ?
89 Utility.now () - this.mouseUpStart : 0;
92 get mouseDownElapsedSeconds() {
93 return this.mouseDownElapsed / 1000;
96 get mouseUpElapsedSeconds() {
97 return this.mouseUpElapsed / 1000;
100 get mouseInteractionSeconds() {
101 return this.mouseDownElapsedSeconds + this.mouseUpElapsedSeconds;
104 get initialOpacity() {
105 return this.element.initialOpacity;
108 get opacityDecayVelocity() {
109 return this.element.opacityDecayVelocity;
113 var width2 = this.containerMetrics.width * this.containerMetrics.width;
114 var height2 = this.containerMetrics.height * this.containerMetrics.height;
115 var waveRadius = Math.min(
116 Math.sqrt(width2 + height2),
120 var duration = 1.1 - 0.2 * (waveRadius / Ripple.MAX_RADIUS);
121 var timeNow = this.mouseInteractionSeconds / duration;
122 var size = waveRadius * (1 - Math.pow(80, -timeNow));
124 return Math.abs(size);
128 if (!this.mouseUpStart) {
129 return this.initialOpacity;
134 this.initialOpacity - this.mouseUpElapsedSeconds * this.opacityDecayVelocity
139 // Linear increase in background opacity, capped at the opacity
140 // of the wavefront (waveOpacity).
141 var outerOpacity = this.mouseUpElapsedSeconds * 0.3;
142 var waveOpacity = this.opacity;
146 Math.min(outerOpacity, waveOpacity)
150 get isOpacityFullyDecayed() {
151 return this.opacity < 0.01 &&
152 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
155 get isRestingAtMaxRadius() {
156 return this.opacity >= this.initialOpacity &&
157 this.radius >= Math.min(this.maxRadius, Ripple.MAX_RADIUS);
160 get isAnimationComplete() {
161 return this.mouseUpStart ?
162 this.isOpacityFullyDecayed : this.isRestingAtMaxRadius;
165 get translationFraction() {
168 this.radius / this.containerMetrics.size * 2 / Math.sqrt(2)
174 return this.xStart + this.translationFraction * (this.xEnd - this.xStart);
182 return this.yStart + this.translationFraction * (this.yEnd - this.yStart);
189 return this.mouseDownStart && !this.mouseUpStart;
192 resetInteractionState: function() {
194 this.mouseDownStart = 0;
195 this.mouseUpStart = 0;
201 this.slideDistance = 0;
203 this.containerMetrics = new ElementMetrics(this.element);
212 this.wave.style.opacity = this.opacity;
214 scale = this.radius / (this.containerMetrics.size / 2);
215 dx = this.xNow - (this.containerMetrics.width / 2);
216 dy = this.yNow - (this.containerMetrics.height / 2);
219 // 2d transform for safari because of border-radius and overflow:hidden clipping bug.
220 // https://bugs.webkit.org/show_bug.cgi?id=98538
221 this.waveContainer.style.webkitTransform = 'translate(' + dx + 'px, ' + dy + 'px)';
222 this.waveContainer.style.transform = 'translate3d(' + dx + 'px, ' + dy + 'px, 0)';
223 this.wave.style.webkitTransform = 'scale(' + scale + ',' + scale + ')';
224 this.wave.style.transform = 'scale3d(' + scale + ',' + scale + ',1)';
227 /** @param {Event=} event */
228 downAction: function(event) {
229 var xCenter = this.containerMetrics.width / 2;
230 var yCenter = this.containerMetrics.height / 2;
232 this.resetInteractionState();
233 this.mouseDownStart = Utility.now();
236 this.xStart = xCenter;
237 this.yStart = yCenter;
238 this.slideDistance = Utility.distance(
239 this.xStart, this.yStart, this.xEnd, this.yEnd
242 this.xStart = event ?
243 event.detail.x - this.containerMetrics.boundingRect.left :
244 this.containerMetrics.width / 2;
245 this.yStart = event ?
246 event.detail.y - this.containerMetrics.boundingRect.top :
247 this.containerMetrics.height / 2;
250 if (this.recenters) {
253 this.slideDistance = Utility.distance(
254 this.xStart, this.yStart, this.xEnd, this.yEnd
258 this.maxRadius = this.containerMetrics.furthestCornerDistanceFrom(
263 this.waveContainer.style.top =
264 (this.containerMetrics.height - this.containerMetrics.size) / 2 + 'px';
265 this.waveContainer.style.left =
266 (this.containerMetrics.width - this.containerMetrics.size) / 2 + 'px';
268 this.waveContainer.style.width = this.containerMetrics.size + 'px';
269 this.waveContainer.style.height = this.containerMetrics.size + 'px';
272 /** @param {Event=} event */
273 upAction: function(event) {
274 if (!this.isMouseDown) {
278 this.mouseUpStart = Utility.now();
282 Polymer.dom(this.waveContainer.parentNode).removeChild(
292 Polymer.IronA11yKeysBehavior
297 * The initial opacity set on the wave.
299 * @attribute initialOpacity
309 * How fast (opacity per second) the wave fades out.
311 * @attribute opacityDecayVelocity
315 opacityDecayVelocity: {
321 * If true, ripples will exhibit a gravitational pull towards
322 * the center of their container as they fade away.
324 * @attribute recenters
334 * If true, ripples will center inside its container
336 * @attribute recenters
346 * A list of the visual ripples.
360 * True when there are visible ripples animating within the
366 reflectToAttribute: true,
371 * If true, the ripple will remain in the "down" state until `holdDown`
372 * is set to false again.
377 observer: '_holdDownChanged'
387 return this.animate.bind(this);
393 var ownerRoot = Polymer.dom(this).getOwnerRoot();
396 if (this.parentNode.nodeType == 11) { // DOCUMENT_FRAGMENT_NODE
397 target = ownerRoot.host;
399 target = this.parentNode;
406 'enter:keydown': '_onEnterKeydown',
407 'space:keydown': '_onSpaceKeydown',
408 'space:keyup': '_onSpaceKeyup'
411 attached: function() {
412 this.listen(this.target, 'up', 'upAction');
413 this.listen(this.target, 'down', 'downAction');
415 if (!this.target.hasAttribute('noink')) {
416 this.keyEventTarget = this.target;
420 get shouldKeepAnimating () {
421 for (var index = 0; index < this.ripples.length; ++index) {
422 if (!this.ripples[index].isAnimationComplete) {
430 simulatedRipple: function() {
431 this.downAction(null);
433 // Please see polymer/polymer#1305
434 this.async(function() {
439 /** @param {Event=} event */
440 downAction: function(event) {
441 if (this.holdDown && this.ripples.length > 0) {
445 var ripple = this.addRipple();
447 ripple.downAction(event);
449 if (!this._animating) {
454 /** @param {Event=} event */
455 upAction: function(event) {
460 this.ripples.forEach(function(ripple) {
461 ripple.upAction(event);
467 onAnimationComplete: function() {
468 this._animating = false;
469 this.$.background.style.backgroundColor = null;
470 this.fire('transitionend');
473 addRipple: function() {
474 var ripple = new Ripple(this);
476 Polymer.dom(this.$.waves).appendChild(ripple.waveContainer);
477 this.$.background.style.backgroundColor = ripple.color;
478 this.ripples.push(ripple);
480 this._setAnimating(true);
485 removeRipple: function(ripple) {
486 var rippleIndex = this.ripples.indexOf(ripple);
488 if (rippleIndex < 0) {
492 this.ripples.splice(rippleIndex, 1);
496 if (!this.ripples.length) {
497 this._setAnimating(false);
501 animate: function() {
505 this._animating = true;
507 for (index = 0; index < this.ripples.length; ++index) {
508 ripple = this.ripples[index];
512 this.$.background.style.opacity = ripple.outerOpacity;
514 if (ripple.isOpacityFullyDecayed && !ripple.isRestingAtMaxRadius) {
515 this.removeRipple(ripple);
519 if (!this.shouldKeepAnimating && this.ripples.length === 0) {
520 this.onAnimationComplete();
522 window.requestAnimationFrame(this._boundAnimate);
526 _onEnterKeydown: function() {
528 this.async(this.upAction, 1);
531 _onSpaceKeydown: function() {
535 _onSpaceKeyup: function() {
539 _holdDownChanged: function(holdDown) {