light-h5-core-lib 2.8.2 → 2.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ /**
40
+ * 异步处理工具
41
+ * @author Aonaufly
42
+ */
43
+ var PromiseUtil = /** @class */ (function () {
44
+ function PromiseUtil() {
45
+ }
46
+ /**
47
+ * 执行所有异步并返回数据
48
+ */
49
+ PromiseUtil.all = function (promiseArr) {
50
+ return __awaiter(this, void 0, void 0, function () {
51
+ return __generator(this, function (_a) {
52
+ return [2 /*return*/, new Promise(function (resolve, reject) {
53
+ var promises = Array.from(promiseArr);
54
+ var r = [];
55
+ var len = promises.length;
56
+ var count = 0;
57
+ var _loop_1 = function (i) {
58
+ Promise.resolve(promises[i]).then(function (o) {
59
+ r[i] = o;
60
+ if (++count === len) {
61
+ resolve(r);
62
+ }
63
+ }).catch(function (e) { return reject(e); });
64
+ };
65
+ for (var i = 0; i < len; i++) {
66
+ _loop_1(i);
67
+ }
68
+ })];
69
+ });
70
+ });
71
+ };
72
+ return PromiseUtil;
73
+ }());
74
+ exports.default = PromiseUtil;
@@ -1,81 +1,92 @@
1
- /**
2
- * 随机数生成工具
3
- * @author aonaufly
4
- */
5
- export class RandomNumUtil {
6
- /**
7
- * 获取一定范围的随机整数
8
- *
9
- * <b>( min ≤ r ≤ max )</b>
10
- *
11
- * @param min 最小数
12
- * @param max 最大数
13
- * @param isInt 是否返回整数
14
- */
15
- public static randomNumBoth(min: number, max: number, isInt: boolean = true): number {
16
- const Range: number = max - min;
17
- if (Range > 0.0) {
18
- const Rand: number = Math.random();
19
- let num: number;
20
- if (isInt) {
21
- num = min + Math.round(Rand * Range); //四舍五入
22
- } else {
23
- num = min + Rand * Range;
24
- }
25
- return num;
26
- } else {
27
- return isInt ? min : Math.round(min);
28
- }
29
- }
30
-
31
- /**
32
- * 打乱数组
33
- */
34
- public static shuffleSort<T>(arr: T[]): void {
35
- let n: number = arr.length;
36
- let index: number;
37
- let temp: T;
38
- while (n--) {
39
- index = Math.floor(Math.random() * n);
40
- temp = arr[index];
41
- arr[index] = arr[n];
42
- arr[n] = temp;
43
- }
44
- }
45
-
46
- /**
47
- * 获得偏振系数
48
- * @param factor 偏振因子(>0)
49
- * @param negativeIntRadio 负数的比率(> 0的整数)
50
- * @param positiveIntRadio 正数的比率 (>0的整数)
51
- * @return -factor 或者factor
52
- */
53
- public static getPolarizationFactor(factor: number = 1, negativeIntRadio: number = 1, positiveIntRadio: number = 1): number {
54
- const r: number = RandomNumUtil.randomNumWithRadio(0, 1, [negativeIntRadio, positiveIntRadio]);
55
- if (r == 0) return -1 * factor;
56
- return r * factor;
57
- }
58
-
59
- /**
60
- * 按照比率获得随机数(对于整数)
61
- * @param min 最小数
62
- * @param max 最大数
63
- * @param radioArr 比率
64
- */
65
- public static randomNumWithRadio(min: number, max: number, radioArr: number[]): number {
66
- let total: number = 0;
67
- let i: number;
68
- for (i = 0; i < radioArr.length; i++) {
69
- total += radioArr[i];
70
- }
71
- const r: number = RandomNumUtil.randomNumBoth(1, total, true);
72
- let target: number = 0;
73
- for (i = 0; i < radioArr.length; i++) {
74
- target += radioArr[i];
75
- if (r <= target) {
76
- return i + min;
77
- }
78
- }
79
- return max;
80
- }
81
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RandomNumUtil = void 0;
4
+ /**
5
+ * 随机数生成工具
6
+ * @author aonaufly
7
+ */
8
+ var RandomNumUtil = /** @class */ (function () {
9
+ function RandomNumUtil() {
10
+ }
11
+ /**
12
+ * 获取一定范围的随机整数
13
+ *
14
+ * <b>( min ≤ r ≤ max )</b>
15
+ *
16
+ * @param min 最小数
17
+ * @param max 最大数
18
+ * @param isInt 是否返回整数
19
+ */
20
+ RandomNumUtil.randomNumBoth = function (min, max, isInt) {
21
+ if (isInt === void 0) { isInt = true; }
22
+ var Range = max - min;
23
+ if (Range > 0.0) {
24
+ var Rand = Math.random();
25
+ var num = void 0;
26
+ if (isInt) {
27
+ num = min + Math.round(Rand * Range); //四舍五入
28
+ }
29
+ else {
30
+ num = min + Rand * Range;
31
+ }
32
+ return num;
33
+ }
34
+ else {
35
+ return isInt ? min : Math.round(min);
36
+ }
37
+ };
38
+ /**
39
+ * 打乱数组
40
+ */
41
+ RandomNumUtil.shuffleSort = function (arr) {
42
+ var n = arr.length;
43
+ var index;
44
+ var temp;
45
+ while (n--) {
46
+ index = Math.floor(Math.random() * n);
47
+ temp = arr[index];
48
+ arr[index] = arr[n];
49
+ arr[n] = temp;
50
+ }
51
+ };
52
+ /**
53
+ * 获得偏振系数
54
+ * @param factor 偏振因子(>0
55
+ * @param negativeIntRadio 负数的比率(> 0的整数)
56
+ * @param positiveIntRadio 正数的比率 (>0的整数)
57
+ * @return -factor 或者factor
58
+ */
59
+ RandomNumUtil.getPolarizationFactor = function (factor, negativeIntRadio, positiveIntRadio) {
60
+ if (factor === void 0) { factor = 1; }
61
+ if (negativeIntRadio === void 0) { negativeIntRadio = 1; }
62
+ if (positiveIntRadio === void 0) { positiveIntRadio = 1; }
63
+ var r = RandomNumUtil.randomNumWithRadio(0, 1, [negativeIntRadio, positiveIntRadio]);
64
+ if (r == 0)
65
+ return -1 * factor;
66
+ return r * factor;
67
+ };
68
+ /**
69
+ * 按照比率获得随机数(对于整数)
70
+ * @param min 最小数
71
+ * @param max 最大数
72
+ * @param radioArr 比率
73
+ */
74
+ RandomNumUtil.randomNumWithRadio = function (min, max, radioArr) {
75
+ var total = 0;
76
+ var i;
77
+ for (i = 0; i < radioArr.length; i++) {
78
+ total += radioArr[i];
79
+ }
80
+ var r = RandomNumUtil.randomNumBoth(1, total, true);
81
+ var target = 0;
82
+ for (i = 0; i < radioArr.length; i++) {
83
+ target += radioArr[i];
84
+ if (r <= target) {
85
+ return i + min;
86
+ }
87
+ }
88
+ return max;
89
+ };
90
+ return RandomNumUtil;
91
+ }());
92
+ exports.RandomNumUtil = RandomNumUtil;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SelfDecrypt = void 0;
4
+ /**
5
+ * 解码
6
+ * @author Aonaufly
7
+ */
8
+ var SelfDecrypt = /** @class */ (function () {
9
+ function SelfDecrypt() {
10
+ }
11
+ SelfDecrypt.b64Decode = function (b64) {
12
+ // 浏览器安全解码,自动补 =
13
+ var s = b64.replace(/[^A-Za-z0-9+/]/g, '');
14
+ var bin = atob(s);
15
+ return new Uint8Array(bin.length).map(function (_, i) { return bin.charCodeAt(i); });
16
+ };
17
+ SelfDecrypt.b64Encode = function (bytes) {
18
+ var bin = '';
19
+ for (var i = 0; i < bytes.length; i++) {
20
+ bin += String.fromCharCode(bytes[i]);
21
+ }
22
+ return btoa(bin);
23
+ };
24
+ SelfDecrypt.xorBytes = function (bytes, key) {
25
+ var k = new TextEncoder().encode(key);
26
+ return bytes.map(function (b, i) { return b ^ k[i % k.length]; });
27
+ };
28
+ /**
29
+ * 解码
30
+ */
31
+ SelfDecrypt.decryptBase64 = function (b64, key) {
32
+ var cipher = SelfDecrypt.b64Decode(b64);
33
+ var plain = SelfDecrypt.xorBytes(cipher, key);
34
+ return new TextDecoder().decode(plain);
35
+ };
36
+ return SelfDecrypt;
37
+ }());
38
+ exports.SelfDecrypt = SelfDecrypt;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringUtil = void 0;
4
+ /**
5
+ * 字符串工具
6
+ * @author aonaufly
7
+ */
8
+ var StringUtil = /** @class */ (function () {
9
+ function StringUtil() {
10
+ }
11
+ /**
12
+ * 字符串的格式化 , 使用 {index = 0}的模式
13
+ * @param str 源字符串
14
+ * @param args 填充参数
15
+ */
16
+ StringUtil.format = function (str) {
17
+ var args = [];
18
+ for (var _i = 1; _i < arguments.length; _i++) {
19
+ args[_i - 1] = arguments[_i];
20
+ }
21
+ if (!args || args.length == 0)
22
+ return str;
23
+ for (var i = 0, j = args.length; i < j; i++) {
24
+ str = str.replace("{".concat(i, "}"), args[i]);
25
+ }
26
+ return str;
27
+ };
28
+ /**
29
+ * 将字符串中所有的\/替换成/
30
+ */
31
+ StringUtil.replaceBackslash = function (input) {
32
+ // 使用正则表达式替换所有的 \\/ 为 /
33
+ return input.replace(/\\\//g, '/');
34
+ };
35
+ StringUtil.code2utf8 = function (uni) {
36
+ var uni2 = uni.toString(2);
37
+ if (uni < 128) {
38
+ return uni.toString(16);
39
+ }
40
+ else if (uni < 2048) {
41
+ uni2 = ("00000000000000000" + uni2).slice(-11);
42
+ var s1 = parseInt("110" + uni2.substring(0, 5), 2);
43
+ var s2 = parseInt("10" + uni2.substring(5), 2);
44
+ return s1.toString(16) + "," + s2.toString(16);
45
+ }
46
+ else {
47
+ uni2 = ("00000000000000000" + uni2).slice(-16);
48
+ var s1 = parseInt("1110" + uni2.substring(0, 4), 2);
49
+ var s2 = parseInt("10" + uni2.substring(4, 10), 2);
50
+ var s3 = parseInt("10" + uni2.substring(10), 2);
51
+ return s1.toString(16) + "," + s2.toString(16) + "," + s3.toString(16);
52
+ }
53
+ };
54
+ /**
55
+ * 将字符串转化成UTF8的Buffer
56
+ * @param str 字符串
57
+ */
58
+ StringUtil.string2UTF8Buffer = function (str) {
59
+ var val = "";
60
+ for (var i = 0; i < str.length; i++) {
61
+ val += "," + StringUtil.code2utf8(str.charCodeAt(i));
62
+ }
63
+ val += ",00";
64
+ // 将16进制转化为ArrayBuffer
65
+ return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
66
+ return parseInt(h, 16);
67
+ })).buffer;
68
+ };
69
+ // /**
70
+ // * 将buffer转化成UTF8
71
+ // * @param buffer
72
+ // */
73
+ // public static buffer2UTF8String(buffer: ArrayBuffer): string {
74
+ // const encodedString: string = String.fromCharCode.apply(null, new Uint8Array(buffer));
75
+ // // const decodedString: string = decodeURIComponent(escape(atob(encodedString)));
76
+ // return encodedString;
77
+ // }
78
+ /* 将buffer转化成UTF8
79
+ * @param buffer
80
+ */
81
+ StringUtil.buffer2UTF8String = function (buffer) {
82
+ var uint8Array = new Uint8Array(buffer);
83
+ var data = uint8Array.reduce(function (acc, i) { return (acc += String.fromCharCode.apply(null, [i])); }, "");
84
+ return data;
85
+ };
86
+ return StringUtil;
87
+ }());
88
+ exports.StringUtil = StringUtil;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniqueIDUtil = void 0;
4
+ /**
5
+ * 唯一ID生成器
6
+ * @author Aonaufly
7
+ */
8
+ var UniqueIDUtil = /** @class */ (function () {
9
+ function UniqueIDUtil() {
10
+ }
11
+ /**生成唯一ID值*/
12
+ UniqueIDUtil.getUniqueId = function () {
13
+ return new Date().getTime() + Math.random().toString(36).substring(2);
14
+ };
15
+ /**生成复合的Key值*/
16
+ UniqueIDUtil.getCompositeKey = function (key, uniqueId) {
17
+ return "".concat(key, "-id:").concat(uniqueId);
18
+ };
19
+ return UniqueIDUtil;
20
+ }());
21
+ exports.UniqueIDUtil = UniqueIDUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "light-h5-core-lib",
3
- "version": "2.8.2",
3
+ "version": "2.8.3",
4
4
  "description": "light core lib",
5
5
  "main": "./LightCore.js",
6
6
  "types": "./type/index.d.ts",
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PoolObj = void 0;
4
+ /**
5
+ * 对象池(泛型)
6
+ * @author Aonaufly
7
+ */
8
+ var PoolObj = /** @class */ (function () {
9
+ /**
10
+ * @param cap 容量(容积)
11
+ */
12
+ function PoolObj(cap) {
13
+ if (cap === void 0) { cap = 5; }
14
+ this._cap = cap;
15
+ this._pool = [];
16
+ }
17
+ PoolObj.prototype.put = function (obj) {
18
+ if (!obj || this._pool.length >= this._cap)
19
+ return false;
20
+ this._pool.push(obj);
21
+ return true;
22
+ };
23
+ PoolObj.prototype.one = function () {
24
+ if (this._pool.length == 0)
25
+ return null;
26
+ return this._pool.shift();
27
+ };
28
+ Object.defineProperty(PoolObj.prototype, "cap", {
29
+ /**
30
+ * 获取容量(容积)
31
+ */
32
+ get: function () {
33
+ return this._cap;
34
+ },
35
+ enumerable: false,
36
+ configurable: true
37
+ });
38
+ Object.defineProperty(PoolObj.prototype, "size", {
39
+ /**
40
+ * 获取当前数量(已经保存的元素个数)
41
+ */
42
+ get: function () {
43
+ if (!this._pool)
44
+ return 0;
45
+ return this._pool.length;
46
+ },
47
+ enumerable: false,
48
+ configurable: true
49
+ });
50
+ /**
51
+ * 清理对象池
52
+ */
53
+ PoolObj.prototype.clear = function () {
54
+ if (!this._pool)
55
+ return;
56
+ if (this._pool.length == 0)
57
+ return;
58
+ var obj;
59
+ for (var i = 0, j = this._pool.length; i < j; i++) {
60
+ obj = this._pool[i];
61
+ if (obj.destroy) {
62
+ obj.destroy();
63
+ }
64
+ else if (obj.onDestroy) {
65
+ obj.onDestroy();
66
+ }
67
+ obj = null;
68
+ }
69
+ this._pool.length = 0;
70
+ };
71
+ PoolObj.prototype.destroy = function (isCleared) {
72
+ if (!isCleared) {
73
+ this.clear();
74
+ }
75
+ this._pool && (this._pool = null);
76
+ };
77
+ return PoolObj;
78
+ }());
79
+ exports.PoolObj = PoolObj;
package/LightCore.ts DELETED
@@ -1,49 +0,0 @@
1
- import {PoolObj} from "./pool/PoolObj";
2
- import {BaseSingleton} from "./common/base/BaseSingleton";
3
- import {BitUtil} from "./common/util/BitUtil";
4
- import {DateUtil} from "./common/util/DateUtil";
5
- import {FloatDigitUtil} from "./common/util/FloatDigitUtil";
6
- import {RandomNumUtil} from "./common/util/RandomNumUtil";
7
- import {StringUtil} from "./common/util/StringUtil";
8
- import {UniqueIDUtil} from "./common/util/UniqueIDUtil";
9
- import {BeizerUtil} from "./common/util/BeizerUtil";
10
- import {MD5} from "./common/md5/MD5";
11
- import PromiseUtil from "./common/util/PromiseUtil";
12
- import Crc32Util from "./common/util/Crc32Util";
13
- import {BooleanUtil} from "./common/util/BooleanUtil";
14
- import {NumberUtil} from "./common/util/NumberUtil";
15
- import {SelfDecrypt} from "./common/util/SelfDecrypt";
16
-
17
- module.exports = {
18
- PoolObj: PoolObj,
19
- BaseSingleton: BaseSingleton,
20
- MD5: MD5,
21
- BitUtil: BitUtil,
22
- DateUtil: DateUtil,
23
- FloatDigitUtil: FloatDigitUtil,
24
- RandomNumUtil: RandomNumUtil,
25
- StringUtil: StringUtil,
26
- UniqueIDUtil: UniqueIDUtil,
27
- BeizerUtil: BeizerUtil,
28
- PromiseUtil: PromiseUtil,
29
- Crc32Util: Crc32Util,
30
- BooleanUtil: BooleanUtil,
31
- NumberUtil: NumberUtil,
32
- SelfDecrypt: SelfDecrypt,
33
- };
34
-
35
- export {PoolObj};
36
- export {BaseSingleton};
37
- export {MD5};
38
- export {BitUtil};
39
- export {DateUtil};
40
- export {FloatDigitUtil};
41
- export {RandomNumUtil};
42
- export {StringUtil};
43
- export {UniqueIDUtil};
44
- export {BeizerUtil};
45
- export {PromiseUtil};
46
- export {Crc32Util};
47
- export {BooleanUtil};
48
- export {NumberUtil};
49
- export {SelfDecrypt};
@@ -1,28 +0,0 @@
1
- import {IDestroy} from "./IDestroy";
2
-
3
- /**
4
- * 泛型单例基类
5
- * @author Aonaufly
6
- */
7
- export abstract class BaseSingleton<T> implements IDestroy {
8
- private static _instance: any = null;
9
-
10
- public static getInstance<T>(c: { new(): T }): T {
11
- if (this._instance == null) {
12
- this._instance = new c();
13
- this._instance.ctor();
14
- }
15
- return this._instance;
16
- }
17
-
18
- /**
19
- * 相当于构造函数
20
- */
21
- protected ctor(): void {
22
-
23
- }
24
-
25
- public destroy(): void {
26
- BaseSingleton._instance = null;
27
- }
28
- }
@@ -1,5 +0,0 @@
1
- import { IDestroy } from "./IDestroy";
2
-
3
- export interface IClear extends IDestroy {
4
- clear(): void;
5
- }
@@ -1,3 +0,0 @@
1
- export interface IDestroy {
2
- destroy(): void;
3
- }
@@ -1,87 +0,0 @@
1
- /**
2
- * 贝塞尔曲线运动
3
- * @author Aonaufly
4
- */
5
- export class BeizerUtil {
6
- //#region 获得当前坐标
7
- /**
8
- * 获得当前坐标
9
- * @param timer 时间[0,1]
10
- * @returns
11
- */
12
- public static getPosition(
13
- points: { x: number; y: number }[],
14
- timer: number
15
- ): { x: number; y: number } {
16
- let x: number = 0,
17
- y: number = 0;
18
- let bezier: number;
19
- for (let i: number = 0, n: number = points.length; i < n; i++) {
20
- bezier =
21
- Math.min(Math.pow(1 - timer, n - i - 1) * Math.pow(timer, i)) *
22
- BeizerUtil.calculationC(i, n);
23
- x += points[i].x * bezier;
24
- y += points[i].y * bezier;
25
- }
26
- return { x, y };
27
- }
28
-
29
- private static calculationC: (i: number, n: number) => number = (i, n): number => {
30
- return (
31
- BeizerUtil.factorial(n - 1) /
32
- (BeizerUtil.factorial(i) * BeizerUtil.factorial(n - 1 - i))
33
- );
34
- };
35
-
36
- private static factorial: (num: number) => number = (num): number => {
37
- if (num < 0) {
38
- return 1;
39
- } else if (num === 0 || num === 1) {
40
- return 1;
41
- } else {
42
- return num * BeizerUtil.factorial(num - 1);
43
- }
44
- };
45
- //#endregion
46
-
47
- //region 获得路线的长度
48
- private static distance(p1: { x: number; y: number }, p2: { x: number; y: number }): number {
49
- const dx: number = p2.x - p1.x;
50
- const dy: number = p2.y - p1.y;
51
- return Math.sqrt(dx * dx + dy * dy);
52
- }
53
- /**
54
- * 获得整个路线的长度
55
- * @param precision 精确度(微积分方案)
56
- */
57
- public static getLength(points: { x: number; y: number }[], precision: number = 100): number {
58
- let length: number = 0;
59
- let point1: { x: number; y: number };
60
- let point2: { x: number; y: number };
61
- for (let t: number = 0; t < 1; t += 1 / precision) {
62
- point1 = BeizerUtil.getBezierPoint(points, t);
63
- point2 = BeizerUtil.getBezierPoint(points, t + 1 / precision);
64
- length += BeizerUtil.distance(point1, point2);
65
- }
66
- return length;
67
- }
68
-
69
- private static getBezierPoint: (
70
- points: { x: number; y: number }[],
71
- t: number
72
- ) => { x: number; y: number } = (points, t) => {
73
- if (points.length === 1) {
74
- return points[0];
75
- }
76
- const nextPoints: { x: number; y: number }[] = [];
77
- let x: number;
78
- let y: number;
79
- for (let i: number = 0; i < points.length - 1; i++) {
80
- x = (1 - t) * points[i].x + t * points[i + 1].x;
81
- y = (1 - t) * points[i].y + t * points[i + 1].y;
82
- nextPoints.push({ x, y });
83
- }
84
- return BeizerUtil.getBezierPoint(nextPoints, t);
85
- };
86
- //#endregion
87
- }