UNPKG

2.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.segmentQuadFactory = void 0;
4var distance_square_root_1 = require("./distance-square-root");
5/**
6 * Returns the {x,y} coordinates of a point at a
7 * given length of a quadratic-bezier segment.
8 *
9 * @see https://github.com/substack/point-at-length
10 */
11function getPointAtQuadSegmentLength(x1, y1, cx, cy, x2, y2, t) {
12 var t1 = 1 - t;
13 return {
14 x: Math.pow(t1, 2) * x1 + 2 * t1 * t * cx + Math.pow(t, 2) * x2,
15 y: Math.pow(t1, 2) * y1 + 2 * t1 * t * cy + Math.pow(t, 2) * y2,
16 };
17}
18/**
19 * Returns a {x,y} point at a given length, the total length and
20 * the minimum and maximum {x,y} coordinates of a Q (quadratic-bezier) segment.
21 */
22function segmentQuadFactory(x1, y1, qx, qy, x2, y2, distance, options) {
23 var _a;
24 var _b = options.bbox, bbox = _b === void 0 ? true : _b, _c = options.length, length = _c === void 0 ? true : _c, _d = options.sampleSize, sampleSize = _d === void 0 ? 10 : _d;
25 var distanceIsNumber = typeof distance === 'number';
26 var x = x1;
27 var y = y1;
28 var LENGTH = 0;
29 var prev = [x, y, LENGTH];
30 var cur = [x, y];
31 var t = 0;
32 var POINT = { x: 0, y: 0 };
33 var POINTS = [{ x: x, y: y }];
34 if (distanceIsNumber && distance <= 0) {
35 POINT = { x: x, y: y };
36 }
37 for (var j = 0; j <= sampleSize; j += 1) {
38 t = j / sampleSize;
39 (_a = getPointAtQuadSegmentLength(x1, y1, qx, qy, x2, y2, t), x = _a.x, y = _a.y);
40 if (bbox) {
41 POINTS.push({ x: x, y: y });
42 }
43 if (length) {
44 LENGTH += (0, distance_square_root_1.distanceSquareRoot)(cur, [x, y]);
45 }
46 cur = [x, y];
47 if (distanceIsNumber && LENGTH >= distance && distance > prev[2]) {
48 var dv = (LENGTH - distance) / (LENGTH - prev[2]);
49 POINT = {
50 x: cur[0] * (1 - dv) + prev[0] * dv,
51 y: cur[1] * (1 - dv) + prev[1] * dv,
52 };
53 }
54 prev = [x, y, LENGTH];
55 }
56 /* istanbul ignore else */
57 if (distanceIsNumber && distance >= LENGTH) {
58 POINT = { x: x2, y: y2 };
59 }
60 return {
61 length: LENGTH,
62 point: POINT,
63 min: {
64 x: Math.min.apply(null, POINTS.map(function (n) { return n.x; })),
65 y: Math.min.apply(null, POINTS.map(function (n) { return n.y; })),
66 },
67 max: {
68 x: Math.max.apply(null, POINTS.map(function (n) { return n.x; })),
69 y: Math.max.apply(null, POINTS.map(function (n) { return n.y; })),
70 },
71 };
72}
73exports.segmentQuadFactory = segmentQuadFactory;
74//# sourceMappingURL=segment-quad-factory.js.map
\No newline at end of file