-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscale.js
49 lines (43 loc) · 1.09 KB
/
scale.js
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
const { chunkArray } = require('./utils')
const scale = (path, { scale = 1, scaleY, round = 3 } = {}) => {
const doValue = val => (round ? +val.toFixed(round) : val)
const _x = scale
const _y = scaleY || scale
return path.map(({ command, values }) => {
const c = command.toLowerCase()
if (c === 'v' || c === 'h') {
const [val] = values
return {
command,
values: [doValue(val * (c === 'v' ? _y : _x))],
}
}
if (c === 'a') {
const chunked = chunkArray(values, 7)
const v = chunked.reduce((acc, next) => {
const [rx, ry, xAxisRotation, largeArcFlag, sweepFLag, x, y] = next
return [
...acc,
doValue(rx * _x),
doValue(ry * _y),
xAxisRotation,
largeArcFlag,
sweepFLag,
doValue(x * _x),
doValue(y * _y),
]
}, [])
return {
command,
values: v,
}
}
return {
command,
values: values.map((val, i) => {
return doValue((val *= i % 2 ? _x : _y))
}),
}
})
}
module.exports = scale