aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/Point.js
blob: 27d81ea3f3de265235af3fc826f31525db66a144 (plain)
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
export default class Point {
  // Initialize
  constructor (...args) {
    this.init(...args)
  }

  init (x, y) {
    let source
    let base = { x: 0, y: 0 }

    // ensure source as object
    source = Array.isArray(x) ? { x: x[0], y: x[1] }
      : typeof x === 'object' ? { x: x.x, y: x.y }
        : { x: x, y: y }

    // merge source
    this.x = source.x == null ? base.x : source.x
    this.y = source.y == null ? base.y : source.y

    return this
  }

  // Clone point
  clone () {
    return new Point(this)
  }

  // transform point with matrix
  transform (m) {
    // Perform the matrix multiplication
    var x = m.a * this.x + m.c * this.y + m.e
    var y = m.b * this.x + m.d * this.y + m.f

    // Return the required point
    return new Point(x, y)
  }

  toArray () {
    return [this.x, this.y]
  }
}

export function point (x, y) {
  return new Point(x, y).transform(this.screenCTM().inverse())
}