1 | import { min as v2Min, max as v2Max, scale as v2Scale, distance as v2Distance, add as v2Add, clone as v2Clone, sub as v2Sub } from '../../core/vector.js';
|
2 | export default function smoothBezier(points, smooth, isLoop, constraint) {
|
3 | var cps = [];
|
4 | var v = [];
|
5 | var v1 = [];
|
6 | var v2 = [];
|
7 | var prevPoint;
|
8 | var nextPoint;
|
9 | var min;
|
10 | var max;
|
11 | if (constraint) {
|
12 | min = [Infinity, Infinity];
|
13 | max = [-Infinity, -Infinity];
|
14 | for (var i = 0, len = points.length; i < len; i++) {
|
15 | v2Min(min, min, points[i]);
|
16 | v2Max(max, max, points[i]);
|
17 | }
|
18 | v2Min(min, min, constraint[0]);
|
19 | v2Max(max, max, constraint[1]);
|
20 | }
|
21 | for (var i = 0, len = points.length; i < len; i++) {
|
22 | var point = points[i];
|
23 | if (isLoop) {
|
24 | prevPoint = points[i ? i - 1 : len - 1];
|
25 | nextPoint = points[(i + 1) % len];
|
26 | }
|
27 | else {
|
28 | if (i === 0 || i === len - 1) {
|
29 | cps.push(v2Clone(points[i]));
|
30 | continue;
|
31 | }
|
32 | else {
|
33 | prevPoint = points[i - 1];
|
34 | nextPoint = points[i + 1];
|
35 | }
|
36 | }
|
37 | v2Sub(v, nextPoint, prevPoint);
|
38 | v2Scale(v, v, smooth);
|
39 | var d0 = v2Distance(point, prevPoint);
|
40 | var d1 = v2Distance(point, nextPoint);
|
41 | var sum = d0 + d1;
|
42 | if (sum !== 0) {
|
43 | d0 /= sum;
|
44 | d1 /= sum;
|
45 | }
|
46 | v2Scale(v1, v, -d0);
|
47 | v2Scale(v2, v, d1);
|
48 | var cp0 = v2Add([], point, v1);
|
49 | var cp1 = v2Add([], point, v2);
|
50 | if (constraint) {
|
51 | v2Max(cp0, cp0, min);
|
52 | v2Min(cp0, cp0, max);
|
53 | v2Max(cp1, cp1, min);
|
54 | v2Min(cp1, cp1, max);
|
55 | }
|
56 | cps.push(cp0);
|
57 | cps.push(cp1);
|
58 | }
|
59 | if (isLoop) {
|
60 | cps.push(cps.shift());
|
61 | }
|
62 | return cps;
|
63 | }
|