aboutsummaryrefslogtreecommitdiffstats
path: root/src/Path.js
blob: 3557e22dedeff97ad022164ac425a92ce5a278c8 (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
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
import { proportionalSize } from './helpers.js'
import { nodeOrNew } from './tools.js'
import Shape from './Shape.js'
import PathArray from './PathArray.js'
import find from './selector.js'
import { register } from './adopter.js'
import { registerMethods } from './methods.js'

export default class Path extends Shape {
  // Initialize node
  constructor (node) {
    super(nodeOrNew('path', node), Path)
  }

  // Get array
  array () {
    return this._array || (this._array = new PathArray(this.attr('d')))
  }

  // Plot new path
  plot (d) {
    return (d == null) ? this.array()
      : this.clear().attr('d', typeof d === 'string' ? d : (this._array = new PathArray(d)))
  }

  // Clear array cache
  clear () {
    delete this._array
    return this
  }

  // Move by left top corner
  move (x, y) {
    return this.attr('d', this.array().move(x, y))
  }

  // Move by left top corner over x-axis
  x (x) {
    return x == null ? this.bbox().x : this.move(x, this.bbox().y)
  }

  // Move by left top corner over y-axis
  y (y) {
    return y == null ? this.bbox().y : this.move(this.bbox().x, y)
  }

  // Set element size to given width and height
  size (width, height) {
    var p = proportionalSize(this, width, height)
    return this.attr('d', this.array().size(p.width, p.height))
  }

  // Set width of element
  width (width) {
    return width == null ? this.bbox().width : this.size(width, this.bbox().height)
  }

  // Set height of element
  height (height) {
    return height == null ? this.bbox().height : this.size(this.bbox().width, height)
  }

  targets () {
    return find('svg textpath [href*="' + this.id() + '"]')
  }
}

// Define morphable array
Path.prototype.MorphArray = PathArray

// Add parent method
registerMethods({
  Container: {
    // Create a wrapped path element
    path (d) {
      // make sure plot is called as a setter
      return this.put(new Path()).plot(d || new PathArray())
    }
  }
})

register(Path)