All files / src/effects paper-wave.ts

25.24% Statements 26/103
0% Branches 0/42
4.76% Functions 1/21
24.75% Lines 25/101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348          1x 1x 1x           1x                                                                                       1x                                                                                                   1x                                   1x               1x               1x               1x                 1x                               1x                       1x               1x               1x               1x               1x               1x                                         1x               1x                         1x                                                                 1x                                                     1x                           1x       1x  
import { PLATFORM } from 'aurelia-pal';
import { ElementRect } from './element-rect';
import { Point } from './point';
 
// tslint:disable:variable-name
const _window: Window = PLATFORM.global;
const  _doc = _window.document;
const  _now = PLATFORM.performance.now.bind(PLATFORM.performance);
// tslint:enable:variable-name
 
/**
 * Provides all the logic to produce a one-time rippling effect.
 */
export class PaperWave {
  /**
   * Gets or sets the color of the wave.
   */
  public color: string | null;
 
  /**
   * Gets or sets the container metrics of the wave.
   */
  public containerRect: ElementRect;
 
  /**
   * Determines whether the wave should be re-centered towards the center of its container.
   */
  public recenters: boolean;
 
  /**
   * Determines whether the wave should start a movement from the center of its container.
   */
  public center: boolean;
 
  /**
   * Gets initial opacity of the wave.
   */
  public initialOpacity: number;
 
  /**
   * Gets opacity decay velocity of the wave.
   */
  public opacityDecayVelocity: number;
 
  /**
   * Represents the object wrapped around the `wave` DOM element that belongs to the current instance.
   */
  public $wave: HTMLDivElement;
 
  /**
   * Represents the object wrapped around the main DOM element that belongs to the current instance.
   */
  public $: HTMLDivElement;
 
  /**
   * Represents the max possible value of the wave's radius.
   */
  public static MAX_RADIUS = 300;
 
  /**
   * Gets or sets max radius of the wave.
   */
  public maxRadius: number;
 
  /**
   * Gets or sets the time of starting interaction with the wave.
   */
  public touchDownStarted: number;
 
  /**
   * Gets or sets the time of ending interaction with the wave.
   */
  private touchUpStarted: number;
 
  /**
   * Gets or sets the start position of the wave.
   */
  private startPosition: Point;
 
  /**
   * Gets or sets the end position of the wave.
   */
  private endPosition: Point;
 
  /**
   * Initializes a new instance of the `PaperWave` class with the specified `PaperRipple` instance.
   */
  constructor(options: any) {
    this.color = _window.getComputedStyle(options.$).color;
    this.containerRect = new ElementRect(options.$);
    this.recenters = options.recenters || false;
    this.center = options.center || false;
    this.initialOpacity = options.initialOpacity || 0.25;
    this.opacityDecayVelocity = options.opacityDecayVelocity || 0.8;
    this.$wave = _doc.createElement('div');
    this.$wave.classList.add('paper-ripple__wave');
    this.$wave.style.backgroundColor = this.color!;
    this.$ = _doc.createElement('div');
    this.$.classList.add('paper-ripple__wave-container');
    this.$.appendChild(this.$wave);
    this.resetDefaults();
  }
 
  /**
   * Gets the time in milliseconds elapsed from the moment where interaction with the wave was started.
   * @returns The time in milliseconds.
   */
  get touchDownElapsed() {
    if (!this.touchDownStarted) {
      return 0;
    }
 
    let elapsed = _now() - this.touchDownStarted;
 
    if (this.touchUpStarted) {
      elapsed -= this.touchUpElapsed;
    }
 
    return elapsed;
  }
 
  /**
   * Gets the time in milliseconds elapsed from the moment where interaction with the wave was ended.
   * @returns The time in milliseconds.
   */
  get touchUpElapsed() {
    return this.touchUpStarted ? _now() - this.touchUpStarted : 0;
  }
 
  /**
   * Gets the time in seconds elapsed since the moment where interaction with the wave was started.
   * @returns The time in seconds.
   */
  get touchDownElapsedSeconds() {
    return this.touchDownElapsed / 1000;
  }
 
  /**
   * Gets the time in seconds elapsed since the moment where interaction with the wave was ended.
   * @returns The time in seconds.
   */
  get touchUpElapsedSeconds() {
    return this.touchUpElapsed / 1000;
  }
 
  /**
   * Gets the total interaction time.
   * @returns The time in seconds
   */
  get mouseInteractionSeconds() {
    return this.touchDownElapsedSeconds + this.touchUpElapsedSeconds;
  }
 
  /**
   * Gets the wave's radius at the current time.
   *
   * @returns {Number} The value of the wave's radius.
   */
  get radius() {
    const radius = Math.min(
      Math.sqrt(Math.pow(this.containerRect.width, 2) + Math.pow(this.containerRect.height, 2)),
      PaperWave.MAX_RADIUS
    ) * 1.1 + 5;
    const elapsed = 1.1 - 0.2 * (radius / PaperWave.MAX_RADIUS);
    const currentTime = this.mouseInteractionSeconds / elapsed;
    const actualRadius = radius * (1 - Math.pow(80, -currentTime));
 
    return Math.abs(actualRadius);
  }
 
  /**
   * Gets the wave's opacity at the current time.
   * @returns The value of the wave's opacity.
   */
  get opacity() {
    if (!this.touchUpStarted) {
      return this.initialOpacity;
    }
 
    return Math.max(0, this.initialOpacity - this.touchUpElapsedSeconds * this.opacityDecayVelocity);
  }
 
  /**
   * Gets the wave's outer opacity at the current time.
   * @returns The value of the wave's outer opacity.
   */
  get outerOpacity() {
    return Math.max(0, Math.min(this.touchUpElapsedSeconds * 0.3, this.opacity));
  }
 
  /**
   * Determines whether the wave is fully opaque or not.
   * @returns `true`, if so, otherwise `false`.
   */
  get isWaveFullyOpaque() {
    return this.opacity < 0.01 && this.radius >= Math.min(this.maxRadius, PaperWave.MAX_RADIUS);
  }
 
  /**
   * Determines whether the wave reached its max radius or not.
   * @returns `true`, if so, otherwise `false`.
   */
  get isMaxRadiusReached() {
    return this.opacity >= this.initialOpacity && this.radius >= Math.min(this.maxRadius, PaperWave.MAX_RADIUS);
  }
 
  /**
   * Determines whether the animation of rippling effect completed or not.
   * @returns `true`, if so, otherwise `false`.
   */
  get isAnimationComplete() {
    return this.touchUpStarted ? this.isWaveFullyOpaque : this.isMaxRadiusReached;
  }
 
  /**
   * Gets the wave's translation fraction value.
   * @returns The value of the wave's translation fraction.
   */
  get translationFraction() {
    return Math.min(1, this.radius / this.containerRect.size * 2 / Math.sqrt(2));
  }
 
  /**
   * Gets the wave's current position.
   * @returns {{x: Number, y: Number}} Object containing coordinates of the wave's current position.
   */
  get currentPosition() {
    const translateFraction = this.translationFraction;
    let x = this.startPosition.x;
    let y = this.startPosition.y;
 
    if (this.endPosition.x) {
      x = this.startPosition.x + translateFraction * (this.endPosition.x - this.startPosition.x);
    }
 
    if (this.endPosition.y) {
      y = this.startPosition.y + translateFraction * (this.endPosition.y - this.startPosition.y);
    }
 
    return { x, y };
  }
 
  /**
   * Determines whether the pointing device is still in interaction with the current wave.
   *
   * @returns {Boolean} `true`, if so, otherwise `false`.
   */
  get isTouchDown() {
    return this.touchDownStarted && !this.touchUpStarted;
  }
 
  /**
   * Resets all the wave's values.
   * @returns Current instance for method chaining.
   */
  public resetDefaults() {
    this.maxRadius = 0;
    this.touchDownStarted = 0;
    this.touchUpStarted = 0;
    this.startPosition = { x: 0, y: 0 };
    this.endPosition = { x: 0, y: 0 };
    return this;
  }
 
  /**
   * Performs updating of the wave's values.
   * @returns Current instance for method chaining.
   */
  public draw() {
    const scaleFactor = this.radius / (this.containerRect.size / 2);
    const containerCenter = this.containerRect.center;
    const currentPos = this.currentPosition;
    const deltaPos = {
        x: currentPos.x - containerCenter.x,
        y: currentPos.y - containerCenter.y
      };
 
    this.$wave.style.opacity = this.opacity.toString();
 
    // cssString = 'translate(' + deltaPos.x + 'px, ' + deltaPos.y + 'px)';
    // this.$.style.webkitTransform = cssString;
    // this.$.style.mozTransform = cssString;
    // this.$.style.msTransform = cssString;
    // this.$.style.oTransform = cssString;
    this.$.style.transform = 'translate3d(' + deltaPos.x + 'px, ' + deltaPos.y + 'px, 0)';
 
    // cssString = 'scale(' + scaleFactor + ',' + scaleFactor + ')';
    // this.$wave.style.webkitTransform = cssString;
    // this.$wave.style.mozTransform = cssString;
    // this.$wave.style.msTransform = cssString;
    // this.$wave.style.oTransform = cssString;
    this.$wave.style.transform = 'scale3d(' + scaleFactor + ',' + scaleFactor + ', 1)';
 
    return this;
  }
 
  /**
   * Performs ripple-down effect on the current wave.
   * @param An object containing coordinates of interaction point to set start position of ripple effect.
   * @returns Current instance for method chaining.
   */
  public downAction(event: MouseEvent | null = null) {
    const containerCenter = this.containerRect.center;
 
    this.resetDefaults();
 
    this.touchDownStarted = _now();
    this.startPosition = this.center || !event ?
      containerCenter :
      {
        x: (event.clientX || event.x) - this.containerRect.boundingRect.left,
        y: (event.clientY || event.y) - this.containerRect.boundingRect.top
      };
    this.endPosition = this.recenters ? containerCenter : this.endPosition;
    this.maxRadius = this.containerRect.distanceToFarthestCorner(this.startPosition);
 
    this.$.style.top = (this.containerRect.height - this.containerRect.size) / 2 + 'px';
    this.$.style.left = (this.containerRect.width - this.containerRect.size) / 2 + 'px';
    this.$.style.width = this.containerRect.size + 'px';
    this.$.style.height = this.containerRect.size + 'px';
 
    return this;
  }
 
  /**
   * Performs ripple-up effect on the current wave.
   * @returns Current instance for method chaining.
   */
  public upAction() {
    if (!this.isTouchDown) {
      return this;
    }
 
    this.touchUpStarted = _now();
 
    return this;
  }
 
  /**
   * Removes the wave from a DOM.
   * @returns Current instance for method chaining.
   */
  public remove() {
    (this.$.parentNode as Node).removeChild(this.$);
    return this;
  }
}