-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeTranslateTransform.index.js
55 lines (48 loc) · 1.66 KB
/
mergeTranslateTransform.index.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
50
51
52
53
54
55
import { each, go, mapL, rangeL } from "fxjs/es";
import { $$createSVGTransformTranslate } from "../createSVGTransformTranslate/createSVGTransformTranslate.index.js";
import { $$getAttrNS } from "../getAttrNS/getAttrNS.index.js";
import { $$getBaseTransformList } from "../getBaseTransformList/getBaseTransformList.index.js";
import { $$getSVG } from "../getSetSVG/getSetSVG.index.js";
import { $$isTranslateSVGTransform } from "../isTranslateSVGTransform/isTranslateSVGTransform.index.js";
import { $$setAttrNS } from "../setAttrNS/setAttrNS.index.js";
export const $$mergeTranslateTransform = ({
index = 0,
x_name = "x",
y_name = "y",
} = {}) => ($el, $svg = $$getSVG()) => {
const base_transform_list = $$getBaseTransformList($el);
if (index > base_transform_list.numberOfItems - 1 || index < 0) {
return $el;
}
const transform = base_transform_list.getItem(index);
if (!$$isTranslateSVGTransform(transform)) {
return $el;
}
const { e: tx, f: ty } = transform.matrix;
go(
[
{ name: x_name, value: tx },
{ name: y_name, value: ty },
],
mapL(({ name, value }) => [
name,
`${parseFloat($$getAttrNS(name)($el)) + value}`,
]),
each((kv) => $$setAttrNS(kv)($el))
);
base_transform_list.removeItem(index);
go(
rangeL(base_transform_list.numberOfItems),
mapL((i) => base_transform_list.getItem(i)),
mapL((t) => [
$$createSVGTransformTranslate({ tx, ty })($svg),
t,
$$createSVGTransformTranslate({ tx: -tx, ty: -ty })($svg),
]),
each(([t1, t2, t3]) => {
const matrix = t1.matrix.multiply(t2.matrix).multiply(t3.matrix);
t2.setMatrix(matrix);
})
);
return $el;
};