UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.segmentCubicFactory = void 0;
4var distance_square_root_1 = require("./distance-square-root");
5/**
6 * Returns a {x,y} point at a given length, the total length and
7 * the minimum and maximum {x,y} coordinates of a C (cubic-bezier) segment.
8 */
9function getPointAtCubicSegmentLength(x1, y1, c1x, c1y, c2x, c2y, x2, y2, t) {
10 var t1 = 1 - t;
11 return {
12 x: Math.pow(t1, 3) * x1 + 3 * Math.pow(t1, 2) * t * c1x + 3 * t1 * Math.pow(t, 2) * c2x + Math.pow(t, 3) * x2,
13 y: Math.pow(t1, 3) * y1 + 3 * Math.pow(t1, 2) * t * c1y + 3 * t1 * Math.pow(t, 2) * c2y + Math.pow(t, 3) * y2,
14 };
15}
16/**
17 * Returns the length of a C (cubic-bezier) segment
18 * or an {x,y} point at a given length.
19 */
20function segmentCubicFactory(x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance, options) {
21 var _a;
22 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;
23 var distanceIsNumber = typeof distance === 'number';
24 var x = x1;
25 var y = y1;
26 var LENGTH = 0;
27 var prev = [x, y, LENGTH];
28 var cur = [x, y];
29 var t = 0;
30 var POINT = { x: 0, y: 0 };
31 var POINTS = [{ x: x, y: y }];
32 if (distanceIsNumber && distance <= 0) {
33 POINT = { x: x, y: y };
34 }
35 // bad perf when size = 300
36 for (var j = 0; j <= sampleSize; j += 1) {
37 t = j / sampleSize;
38 (_a = getPointAtCubicSegmentLength(x1, y1, c1x, c1y, c2x, c2y, x2, y2, t), x = _a.x, y = _a.y);
39 if (bbox) {
40 POINTS.push({ x: x, y: y });
41 }
42 if (length) {
43 LENGTH += (0, distance_square_root_1.distanceSquareRoot)(cur, [x, y]);
44 }
45 cur = [x, y];
46 if (distanceIsNumber && LENGTH >= distance && distance > prev[2]) {
47 var dv = (LENGTH - distance) / (LENGTH - prev[2]);
48 POINT = {
49 x: cur[0] * (1 - dv) + prev[0] * dv,
50 y: cur[1] * (1 - dv) + prev[1] * dv,
51 };
52 }
53 prev = [x, y, LENGTH];
54 }
55 if (distanceIsNumber && distance >= LENGTH) {
56 POINT = { x: x2, y: y2 };
57 }
58 return {
59 length: LENGTH,
60 point: POINT,
61 min: {
62 x: Math.min.apply(null, POINTS.map(function (n) { return n.x; })),
63 y: Math.min.apply(null, POINTS.map(function (n) { return n.y; })),
64 },
65 max: {
66 x: Math.max.apply(null, POINTS.map(function (n) { return n.x; })),
67 y: Math.max.apply(null, POINTS.map(function (n) { return n.y; })),
68 },
69 };
70}
71exports.segmentCubicFactory = segmentCubicFactory;
72//# sourceMappingURL=segment-cubic-factory.js.map
\No newline at end of file