blob: 9e59ed7050dc30425ae7a0f10643d1554f2a651a (
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
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
|
import SVGArray from './SVGArray.js'
import parser from '../modules/core/parser.js'
import Box from './Box.js'
import { pathParser } from '../utils/pathParser.js'
function arrayToString(a) {
let s = ''
for (let i = 0, il = a.length; i < il; i++) {
s += a[i][0]
if (a[i][1] != null) {
s += a[i][1]
if (a[i][2] != null) {
s += ' '
s += a[i][2]
if (a[i][3] != null) {
s += ' '
s += a[i][3]
s += ' '
s += a[i][4]
if (a[i][5] != null) {
s += ' '
s += a[i][5]
s += ' '
s += a[i][6]
if (a[i][7] != null) {
s += ' '
s += a[i][7]
}
}
}
}
}
}
return s + ' '
}
export default class PathArray extends SVGArray {
// Get bounding box of path
bbox() {
parser().path.setAttribute('d', this.toString())
return new Box(parser.nodes.path.getBBox())
}
// Move path string
move(x, y) {
// get bounding box of current situation
const box = this.bbox()
// get relative offset
x -= box.x
y -= box.y
if (!isNaN(x) && !isNaN(y)) {
// move every point
for (let l, i = this.length - 1; i >= 0; i--) {
l = this[i][0]
if (l === 'M' || l === 'L' || l === 'T') {
this[i][1] += x
this[i][2] += y
} else if (l === 'H') {
this[i][1] += x
} else if (l === 'V') {
this[i][1] += y
} else if (l === 'C' || l === 'S' || l === 'Q') {
this[i][1] += x
this[i][2] += y
this[i][3] += x
this[i][4] += y
if (l === 'C') {
this[i][5] += x
this[i][6] += y
}
} else if (l === 'A') {
this[i][6] += x
this[i][7] += y
}
}
}
return this
}
// Absolutize and parse path to array
parse(d = 'M0 0') {
if (Array.isArray(d)) {
d = Array.prototype.concat.apply([], d).toString()
}
return pathParser(d)
}
// Resize path string
size(width, height) {
// get bounding box of current situation
const box = this.bbox()
let i, l
// If the box width or height is 0 then we ignore
// transformations on the respective axis
box.width = box.width === 0 ? 1 : box.width
box.height = box.height === 0 ? 1 : box.height
// recalculate position of all points according to new size
for (i = this.length - 1; i >= 0; i--) {
l = this[i][0]
if (l === 'M' || l === 'L' || l === 'T') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
this[i][2] = ((this[i][2] - box.y) * height) / box.height + box.y
} else if (l === 'H') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
} else if (l === 'V') {
this[i][1] = ((this[i][1] - box.y) * height) / box.height + box.y
} else if (l === 'C' || l === 'S' || l === 'Q') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
this[i][2] = ((this[i][2] - box.y) * height) / box.height + box.y
this[i][3] = ((this[i][3] - box.x) * width) / box.width + box.x
this[i][4] = ((this[i][4] - box.y) * height) / box.height + box.y
if (l === 'C') {
this[i][5] = ((this[i][5] - box.x) * width) / box.width + box.x
this[i][6] = ((this[i][6] - box.y) * height) / box.height + box.y
}
} else if (l === 'A') {
// resize radii
this[i][1] = (this[i][1] * width) / box.width
this[i][2] = (this[i][2] * height) / box.height
// move position values
this[i][6] = ((this[i][6] - box.x) * width) / box.width + box.x
this[i][7] = ((this[i][7] - box.y) * height) / box.height + box.y
}
}
return this
}
// Convert array to string
toString() {
return arrayToString(this)
}
}
|