-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetCenterPoint.index.js
40 lines (36 loc) · 1.1 KB
/
getCenterPoint.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
import { eachL, go, mapL, reduce } from "fxjs/es";
import { $$createSVGPoint } from "../createSVGPoint/createSVGPoint.index.js";
import { $$getBoxPoints } from "../getBoxPoints/getBoxPoints.index.js";
import { $$getSVG } from "../getSetSVG/getSetSVG.index.js";
const $$sum = (a, b) => a + b;
const $$calcReduceMean = (nums) => {
let len = 0;
const sum = go(
nums,
eachL(() => len++),
reduce($$sum)
);
return len ? sum / len : 0;
};
const $$calcCenterPoint = ({
top_left,
top_right,
bottom_left,
bottom_right,
}) => ($svg = $$getSVG()) => {
const points = [top_left, top_right, bottom_left, bottom_right];
const x = $$calcReduceMean(mapL(({ x }) => x, points));
const y = $$calcReduceMean(mapL(({ y }) => y, points));
return $$createSVGPoint({ x, y })($svg);
};
export const $$getCenterPoint = ($el, $svg = $$getSVG()) => {
const { original: _original, transformed: _transformed } = $$getBoxPoints(
$el,
$svg
);
const [original, transformed] = mapL(
(points) => $$calcCenterPoint(points)($svg),
[_original, _transformed]
);
return { original, transformed };
};