All files / src/lib canvas-list.js

100% Statements 12/12
100% Branches 0/0
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          2x   2x 14x 14x 14x                   1x 1x 1x   1x 1x   1x 1x      
/**
 * Extends Array with additional methods to esily keep
 * track of all canvas in layout.
 * @class
 * @extends Array
 */
export class MemoryCanvaslist extends Array {
  /**
   * All given HTMLCanvasElements are included in {`MemoryCanvaslist`}
   * Correction of width and height values are important.
   * @param {Array} canvas list of canvas elements.
   */
  addCanvas(canvas) {
    const self = this;
 
    canvas.forEach((c) => {
      c.width = parseInt(c.offsetWidth);
      c.height = parseInt(c.offsetHeight);
      self.push(c);
    });
  }
 
  /**
   * Chooses randomly pair Canvas.
   * @param {{}} srcCanvas Canvas element that needs to be paired.
   * @return {{}} Paired elements.
   */
  randomPair(srcCanvas) {
    const srcIndex = this.indexOf(srcCanvas);
    const result = [];
    result.push(this.splice(srcIndex, 1)[0]);
 
    const length = this.length;
    const rnd = Math.floor(Math.random() * length);
 
    result.push(this.splice(rnd, 1)[0]);
    return result;
  }
}