All files index.js

100% Statements 49/49
100% Branches 18/18
100% Functions 17/17
100% Lines 47/47
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                                                38x 18x                 20x                                 2x 2x                           16x 16x 16x 16x 16x 16x   16x                                   4x 4x 4x 2x   4x                   2x                 4x 4x 4x                 2x                 2x                           16x                       4x               4x             28x             18x               10x 4x     6x 2x     4x 4x 4x 4x 4x 4x 4x   4x                               8x 2x     6x 2x     4x 4x                                   2x                
import { Map } from 'extendable-immutable'
 
/**
 * A library for creating affine transform matrix (3x3) that are Immutable.
 * These matrices can be used for matrix calcuations on SVG CTMs (current transform matrix).
 *
 * @module immutable-transform-matrix
 */
 
/**
 * @class Matrix
 * @extends Immutable.Map
 */
class Matrix extends Map {
  /**
   * Construct a Matrix. Creates an Identiy matrix if no params are supplied.
   * @param {number} a
   * @param {number} b
   * @param {number} c
   * @param {number} d
   * @param {number} e
   * @param {number} f
   */
  constructor (...args) {
    if (args.length > 0) {
      super({
        a: args[0],
        b: args[1],
        c: args[2],
        d: args[3],
        e: args[4],
        f: args[5]
      })
    } else {
      super({
        a: 1,
        b: 0,
        c: 0,
        d: 1,
        e: 0,
        f: 0
      })
    }
  }
 
  /**
   * Construct a new Matrix constructed from an SVGMatrix
   * @param {SVGMatrix} ctm
   * @return {Matrix}
   */
  static fromCTM (ctm) {
    const {a, b, c, d, e, f} = ctm
    return new Matrix(a, b, c, d, e, f)
  }
 
  /**
   * Multiplies current matrix with new matrix values.
   * @param {number} a2 - scale x
   * @param {number} b2 - shear y
   * @param {number} c2 - shear x
   * @param {number} d2 - scale y
   * @param {number} e2 - translate x
   * @param {number} f2 - translate y
   * @return {Matrix}
   */
  transform (a2, b2, c2, d2, e2, f2) {
    const a1 = this.get('a')
    const b1 = this.get('b')
    const c1 = this.get('c')
    const d1 = this.get('d')
    const e1 = this.get('e')
    const f1 = this.get('f')
 
    return this.withMutations(matrix => matrix
      .set('a', (a1 * a2) + (c1 * b2))
      .set('b', (b1 * a2) + (d1 * b2))
      .set('c', a1 * c2 + c1 * d2)
      .set('d', b1 * c2 + d1 * d2)
      .set('e', a1 * e2 + c1 * f2 + e1)
      .set('f', b1 * e2 + d1 * f2 + f1)
    )
  }
 
  /**
   * Scales current matrix accumulative.
   * If the second param is ommitted it scale uniformly.
   * @param {number} sx - scale factor x (1 does nothing)
   * @param {number} [sy] - scale factor y (1 does nothing)
   * @return {Matrix}
   */
  scale (...args) {
    const sx = args[0]
    let sy = sx
    if (args.length > 1) {
      sy = args[1]
    }
    return this.transform(sx, 0, 0, sy, 0, 0)
  }
 
  /**
   * Translate current matrix accumulative.
   * @param {number} tx - translation for x
   * @param {number} ty - translation for y
   * @return {Matrix}
   */
  translate (tx, ty) {
    return this.transform(1, 0, 0, 1, tx, ty)
  }
 
  /**
   * Rotates current matrix accumulative by angle.
   * @param {number} angle - angle in radians
   * @return {Matrix}
   */
  rotate (angle) {
    var cos = Math.cos(angle)
    var sin = Math.sin(angle)
    return this.transform(cos, sin, -sin, cos, 0, 0)
  }
 
  /**
   * Helper method to make a rotation based on an angle in degrees.
   * @param {number} angle - angle in degrees
   * @return {Matrix}
   */
  rotateDeg (angle) {
    return this.rotate(angle * Math.PI / 180)
  }
 
  /**
   * Multiplies current matrix with an other matrix.
   * @param {Matrix} m - the other matrix
   * @return {Matrix}
   */
  multiply (matrix) {
    return this.transform(
      matrix.get('a'),
      matrix.get('b'),
      matrix.get('c'),
      matrix.get('d'),
      matrix.get('e'),
      matrix.get('f')
    )
  }
 
  /**
   * @return {boolean} true if identity (no transforms applied)
   */
  isIdentity () {
    return this.get('a') === 1 &&
      this.get('b') === 0 &&
      this.get('c') === 0 &&
      this.get('d') === 1 &&
      this.get('e') === 0 &&
      this.get('f') === 0
  }
 
  /**
   * @return {string}
   */
  toString () {
    const values = [
      this.get('a'),
      this.get('b'),
      this.get('c'),
      this.get('d'),
      this.get('e'),
      this.get('f')
    ]
    return `matrix(${values.join()})`
  }
 
  /**
   * @return {number} determinant of the current matrix
   */
  determinant () {
    return (this.get('a') * this.get('d')) - (this.get('b') * this.get('c'))
  }
 
  /**
   * @return {boolean} true if matrix is invertible
   */
  isInvertible () {
    return this.determinant() !== 0
  }
 
  /**
   * @return {Matrix} inverse of the current matrix.
   * @throws Will throw an error if the matrix is not invertable
   */
  inverse () {
    if (this.isIdentity()) {
      return new Matrix()
    }
 
    if (!this.isInvertible()) {
      throw new Error('Matrix is not invertible.')
    }
 
    const dt = this.determinant()
    const a = this.get('a')
    const b = this.get('b')
    const c = this.get('c')
    const d = this.get('d')
    const e = this.get('e')
    const f = this.get('f')
 
    return this.withMutations(matrix => matrix
      .set('a', d / dt)
      .set('b', -b / dt)
      .set('c', -c / dt)
      .set('d', a / dt)
      .set('e', ((c * f) - (d * e)) / dt)
      .set('f', -((a * f) - (b * e)) / dt)
    )
  }
 
  /**
   * @param {Matrix} m - matrix divisor
   * @return {Matrix}
   * @throws Will throw if m is not invertible or not a Matrix
   */
  divide (m) {
    if (!(m instanceof Matrix)) {
      throw new Error('Must pass a Matrix to divide.')
    }
 
    if (!m.isInvertible()) {
      throw new Error('Input matrix is not invertible.')
    }
 
    const inverse = m.inverse()
    return this.transform(
      inverse.get('a'),
      inverse.get('b'),
      inverse.get('c'),
      inverse.get('d'),
      inverse.get('e'),
      inverse.get('f')
    )
  }
 
  /**
   * Apply current matrix to x and y point.
   *
   * @param {number} x - value for x
   * @param {number} y - value for y
   * @returns {{x: number, y: number}} A new transformed point object
   */
  applyToPoint (x, y) {
    return {
      x: (x * this.get('a')) + (y * this.get('c')) + this.get('e'),
      y: (x * this.get('b')) + (y * this.get('d')) + this.get('f')
    }
  }
}
 
export default Matrix