UNPKG

2.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getPathArea = void 0;
4var path_2_curve_1 = require("../convert/path-2-curve");
5/**
6 * Returns the area of a single cubic-bezier segment.
7 *
8 * http://objectmix.com/graphics/133553-area-closed-bezier-curve.html
9 */
10function getCubicSegArea(x1, y1, c1x, c1y, c2x, c2y, x2, y2) {
11 // https://stackoverflow.com/a/15845996
12 return ((3 *
13 ((y2 - y1) * (c1x + c2x) -
14 (x2 - x1) * (c1y + c2y) +
15 c1y * (x1 - c2x) -
16 c1x * (y1 - c2y) +
17 y2 * (c2x + x1 / 3) -
18 x2 * (c2y + y1 / 3))) /
19 20);
20}
21/**
22 * Returns the area of a shape.
23 * @author Jürg Lehni & Jonathan Puckey
24 *
25 * @see https://github.com/paperjs/paper.js/blob/develop/src/path/Path.js
26 */
27function getPathArea(path) {
28 var x = 0;
29 var y = 0;
30 var len = 0;
31 return (0, path_2_curve_1.path2Curve)(path)
32 .map(function (seg) {
33 var _a;
34 switch (seg[0]) {
35 case 'M':
36 x = seg[1], y = seg[2];
37 return 0;
38 default:
39 // @ts-ignore
40 var _b = seg.slice(1), c1x = _b[0], c1y = _b[1], c2x = _b[2], c2y = _b[3], x2 = _b[4], y2 = _b[5];
41 len = getCubicSegArea(x, y, c1x, c1y, c2x, c2y, x2, y2);
42 _a = seg.slice(-2), x = _a[0], y = _a[1];
43 return len;
44 }
45 })
46 .reduce(function (a, b) { return a + b; }, 0);
47}
48exports.getPathArea = getPathArea;
49// export function getPathArea(pathArray: AbsoluteArray) {
50// let x = 0;
51// let y = 0;
52// let mx = 0;
53// let my = 0;
54// let len = 0;
55// return pathArray
56// .map((seg) => {
57// switch (seg[0]) {
58// case 'M':
59// case 'Z':
60// mx = seg[0] === 'M' ? seg[1] : mx;
61// my = seg[0] === 'M' ? seg[2] : my;
62// x = mx;
63// y = my;
64// return 0;
65// default:
66// // @ts-ignore
67// len = getCubicSegArea.apply(0, [x, y].concat(seg.slice(1)));
68// [x, y] = seg.slice(-2) as [number, number];
69// return len;
70// }
71// })
72// .reduce((a, b) => a + b, 0);
73// }
74//# sourceMappingURL=get-path-area.js.map
\No newline at end of file