sculp-js 1.13.3-beta.0 → 1.13.6
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.
- package/dist/cjs/_virtual/_commonjsHelpers.js +3 -3
- package/dist/cjs/array.js +36 -46
- package/dist/cjs/async.js +41 -43
- package/dist/cjs/base64.js +77 -66
- package/dist/cjs/clipboard.js +54 -53
- package/dist/cjs/cloneDeep.js +90 -91
- package/dist/cjs/cookie.js +27 -30
- package/dist/cjs/date.js +101 -98
- package/dist/cjs/dom.js +114 -121
- package/dist/cjs/download.js +77 -78
- package/dist/cjs/easing.js +29 -32
- package/dist/cjs/file.js +147 -152
- package/dist/cjs/func.js +64 -78
- package/dist/cjs/index.js +1 -3
- package/dist/cjs/math.js +32 -38
- package/dist/cjs/node_modules/bezier-easing/src/index.js +35 -23
- package/dist/cjs/number.js +58 -61
- package/dist/cjs/object.js +112 -126
- package/dist/cjs/path.js +39 -43
- package/dist/cjs/qs.js +34 -41
- package/dist/cjs/random.js +31 -34
- package/dist/cjs/string.js +62 -63
- package/dist/cjs/tooltip.js +66 -69
- package/dist/cjs/tree.js +265 -263
- package/dist/cjs/type.js +42 -46
- package/dist/cjs/unique.js +39 -42
- package/dist/cjs/url.js +47 -48
- package/dist/cjs/validator.js +38 -38
- package/dist/cjs/variable.js +27 -21
- package/dist/cjs/watermark.js +78 -67
- package/dist/esm/array.js +36 -46
- package/dist/esm/async.js +41 -43
- package/dist/esm/base64.js +77 -66
- package/dist/esm/clipboard.js +55 -54
- package/dist/esm/cloneDeep.js +90 -91
- package/dist/esm/cookie.js +27 -30
- package/dist/esm/date.js +101 -98
- package/dist/esm/dom.js +114 -121
- package/dist/esm/download.js +78 -79
- package/dist/esm/easing.js +29 -32
- package/dist/esm/file.js +147 -152
- package/dist/esm/func.js +64 -78
- package/dist/esm/index.js +96 -8
- package/dist/esm/math.js +32 -38
- package/dist/esm/number.js +58 -61
- package/dist/esm/object.js +124 -127
- package/dist/esm/path.js +39 -43
- package/dist/esm/qs.js +34 -41
- package/dist/esm/random.js +31 -34
- package/dist/esm/string.js +74 -64
- package/dist/esm/tooltip.js +66 -69
- package/dist/esm/tree.js +265 -263
- package/dist/esm/type.js +68 -47
- package/dist/esm/unique.js +39 -42
- package/dist/esm/url.js +47 -48
- package/dist/esm/validator.js +56 -39
- package/dist/esm/variable.js +27 -21
- package/dist/esm/watermark.js +79 -68
- package/dist/types/array.d.ts +0 -1
- package/dist/types/async.d.ts +0 -1
- package/dist/types/base64.d.ts +0 -1
- package/dist/types/clipboard.d.ts +0 -1
- package/dist/types/cloneDeep.d.ts +0 -1
- package/dist/types/cookie.d.ts +0 -1
- package/dist/types/date.d.ts +0 -1
- package/dist/types/dom.d.ts +0 -1
- package/dist/types/download.d.ts +0 -1
- package/dist/types/easing.d.ts +0 -1
- package/dist/types/file.d.ts +0 -1
- package/dist/types/func.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/math.d.ts +0 -1
- package/dist/types/number.d.ts +0 -1
- package/dist/types/object.d.ts +0 -1
- package/dist/types/path.d.ts +0 -1
- package/dist/types/qs.d.ts +0 -1
- package/dist/types/random.d.ts +0 -1
- package/dist/types/string.d.ts +0 -1
- package/dist/types/tooltip.d.ts +0 -1
- package/dist/types/tree.d.ts +2 -2
- package/dist/types/type.d.ts +0 -1
- package/dist/types/unique.d.ts +0 -1
- package/dist/types/url.d.ts +0 -1
- package/dist/types/validator.d.ts +0 -1
- package/dist/types/variable.d.ts +0 -1
- package/dist/types/watermark.d.ts +0 -1
- package/dist/umd/index.min.js +2 -2
- package/package.json +12 -6
- package/dist/types/array.d.ts.map +0 -1
- package/dist/types/async.d.ts.map +0 -1
- package/dist/types/base64.d.ts.map +0 -1
- package/dist/types/clipboard.d.ts.map +0 -1
- package/dist/types/cloneDeep.d.ts.map +0 -1
- package/dist/types/cookie.d.ts.map +0 -1
- package/dist/types/core-index.d.ts +0 -17
- package/dist/types/core-index.d.ts.map +0 -1
- package/dist/types/date.d.ts.map +0 -1
- package/dist/types/dom.d.ts.map +0 -1
- package/dist/types/download.d.ts.map +0 -1
- package/dist/types/easing.d.ts.map +0 -1
- package/dist/types/file.d.ts.map +0 -1
- package/dist/types/func.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3278
- package/dist/types/math.d.ts.map +0 -1
- package/dist/types/number.d.ts.map +0 -1
- package/dist/types/object.d.ts.map +0 -1
- package/dist/types/path.d.ts.map +0 -1
- package/dist/types/qs.d.ts.map +0 -1
- package/dist/types/random.d.ts.map +0 -1
- package/dist/types/string.d.ts.map +0 -1
- package/dist/types/tooltip.d.ts.map +0 -1
- package/dist/types/tree.d.ts.map +0 -1
- package/dist/types/type.d.ts.map +0 -1
- package/dist/types/unique.d.ts.map +0 -1
- package/dist/types/url.d.ts.map +0 -1
- package/dist/types/validator.d.ts.map +0 -1
- package/dist/types/variable.d.ts.map +0 -1
- package/dist/types/watermark.d.ts.map +0 -1
package/dist/cjs/file.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -13,7 +13,7 @@ var type = require('./type.js');
|
|
|
13
13
|
* @returns {boolean}
|
|
14
14
|
*/
|
|
15
15
|
function supportCanvas() {
|
|
16
|
-
|
|
16
|
+
return !!document.createElement('canvas').getContext;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* 选择本地文件
|
|
@@ -22,22 +22,22 @@ function supportCanvas() {
|
|
|
22
22
|
* @returns {HTMLInputElement}
|
|
23
23
|
*/
|
|
24
24
|
function chooseLocalFile(accept, changeCb) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
25
|
+
let inputObj = document.createElement('input');
|
|
26
|
+
inputObj.setAttribute('id', String(Date.now()));
|
|
27
|
+
inputObj.setAttribute('type', 'file');
|
|
28
|
+
inputObj.setAttribute('style', 'visibility:hidden');
|
|
29
|
+
inputObj.setAttribute('accept', accept);
|
|
30
|
+
document.body.appendChild(inputObj);
|
|
31
|
+
inputObj.click();
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
inputObj.onchange = e => {
|
|
34
|
+
changeCb(e.target.files);
|
|
35
|
+
setTimeout(() => {
|
|
36
|
+
document.body.removeChild(inputObj);
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
inputObj = null;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* 计算图片压缩后的尺寸
|
|
@@ -49,20 +49,20 @@ function chooseLocalFile(accept, changeCb) {
|
|
|
49
49
|
* @returns {*}
|
|
50
50
|
*/
|
|
51
51
|
function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
52
|
+
let width = originWidth,
|
|
53
|
+
height = originHeight;
|
|
54
|
+
// 图片尺寸超过限制
|
|
55
|
+
if (originWidth > maxWidth || originHeight > maxHeight) {
|
|
56
|
+
if (originWidth / originHeight > maxWidth / maxHeight) {
|
|
57
|
+
// 更宽,按照宽度限定尺寸
|
|
58
|
+
width = maxWidth;
|
|
59
|
+
height = Math.round(maxWidth * (originHeight / originWidth));
|
|
60
|
+
} else {
|
|
61
|
+
height = maxHeight;
|
|
62
|
+
width = Math.round(maxHeight * (originWidth / originHeight));
|
|
64
63
|
}
|
|
65
|
-
|
|
64
|
+
}
|
|
65
|
+
return { width, height };
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
68
|
* 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
|
|
@@ -74,41 +74,42 @@ function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
|
|
|
74
74
|
* @returns {*} {width, height}
|
|
75
75
|
*/
|
|
76
76
|
function scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight }) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
77
|
+
let targetWidth = originWidth,
|
|
78
|
+
targetHeight = originHeight;
|
|
79
|
+
if (type.isNumber(maxSize)) {
|
|
80
|
+
const { width, height } = calculateSize({ maxWidth: maxSize, maxHeight: maxSize, originWidth, originHeight });
|
|
81
|
+
targetWidth = width;
|
|
82
|
+
targetHeight = height;
|
|
83
|
+
} else if (sizeKB < 500) {
|
|
84
|
+
// [50KB, 500KB)
|
|
85
|
+
const maxWidth = 1200,
|
|
86
|
+
maxHeight = 1200;
|
|
87
|
+
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
88
|
+
targetWidth = width;
|
|
89
|
+
targetHeight = height;
|
|
90
|
+
} else if (sizeKB < 5 * 1024) {
|
|
91
|
+
// [500KB, 5MB)
|
|
92
|
+
const maxWidth = 1400,
|
|
93
|
+
maxHeight = 1400;
|
|
94
|
+
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
95
|
+
targetWidth = width;
|
|
96
|
+
targetHeight = height;
|
|
97
|
+
} else if (sizeKB < 10 * 1024) {
|
|
98
|
+
// [5MB, 10MB)
|
|
99
|
+
const maxWidth = 1600,
|
|
100
|
+
maxHeight = 1600;
|
|
101
|
+
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
102
|
+
targetWidth = width;
|
|
103
|
+
targetHeight = height;
|
|
104
|
+
} else if (10 * 1024 <= sizeKB) {
|
|
105
|
+
// [10MB, Infinity)
|
|
106
|
+
const maxWidth = originWidth > 15000 ? 8192 : originWidth > 10000 ? 4096 : 2048,
|
|
107
|
+
maxHeight = originHeight > 15000 ? 8192 : originHeight > 10000 ? 4096 : 2048;
|
|
108
|
+
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
109
|
+
targetWidth = width;
|
|
110
|
+
targetHeight = height;
|
|
111
|
+
}
|
|
112
|
+
return { width: targetWidth, height: targetHeight };
|
|
112
113
|
}
|
|
113
114
|
/**
|
|
114
115
|
* Web端:等比例压缩图片批量处理 (小于minFileSizeKB:50,不压缩), 支持压缩全景图或长截图
|
|
@@ -122,94 +123,88 @@ function scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight }) {
|
|
|
122
123
|
* @returns {Promise<ICompressImgResult | ICompressImgResult[] | null>}
|
|
123
124
|
*/
|
|
124
125
|
function compressImg(file, options = { mime: 'image/jpeg', minFileSizeKB: 50 }) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
if (!(file instanceof File || file instanceof FileList)) {
|
|
127
|
+
throw new Error(`${file} require be File or FileList`);
|
|
128
|
+
} else if (!supportCanvas()) {
|
|
129
|
+
throw new Error(`Current runtime environment not support Canvas`);
|
|
130
|
+
}
|
|
131
|
+
const { quality, mime = 'image/jpeg', maxSize: size, minFileSizeKB = 50 } = type.isObject(options) ? options : {};
|
|
132
|
+
let targetQuality = quality,
|
|
133
|
+
maxSize;
|
|
134
|
+
if (quality) {
|
|
135
|
+
targetQuality = quality;
|
|
136
|
+
} else if (file instanceof File) {
|
|
137
|
+
const sizeKB = +parseInt((file.size / 1024).toFixed(2));
|
|
138
|
+
if (sizeKB < minFileSizeKB) {
|
|
139
|
+
targetQuality = 1;
|
|
140
|
+
} else if (sizeKB < 1 * 1024) {
|
|
141
|
+
targetQuality = 0.85;
|
|
142
|
+
} else if (sizeKB < 5 * 1024) {
|
|
143
|
+
targetQuality = 0.8;
|
|
144
|
+
} else {
|
|
145
|
+
targetQuality = 0.75;
|
|
135
146
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (file instanceof FileList) {
|
|
155
|
-
return Promise.all(Array.from(file).map(el => compressImg(el, { maxSize, mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
|
|
156
|
-
}
|
|
157
|
-
else if (file instanceof File) {
|
|
158
|
-
return new Promise(resolve => {
|
|
159
|
-
const ext = {
|
|
160
|
-
'image/webp': 'webp',
|
|
161
|
-
'image/jpeg': 'jpg',
|
|
162
|
-
'image/png': 'png'
|
|
163
|
-
};
|
|
164
|
-
const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
|
|
165
|
-
const sizeKB = +parseInt((file.size / 1024).toFixed(2));
|
|
166
|
-
if (sizeKB < minFileSizeKB) {
|
|
167
|
-
resolve({
|
|
168
|
-
file: file
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
const reader = new FileReader(); // 创建 FileReader
|
|
173
|
-
// @ts-ignore
|
|
174
|
-
reader.onload = ({ target: { result: src } }) => {
|
|
175
|
-
const image = new Image(); // 创建 img 元素
|
|
176
|
-
image.onload = () => {
|
|
177
|
-
const canvas = document.createElement('canvas'); // 创建 canvas 元素
|
|
178
|
-
const context = canvas.getContext('2d');
|
|
179
|
-
const originWidth = image.width;
|
|
180
|
-
const originHeight = image.height;
|
|
181
|
-
const { width, height } = scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight });
|
|
182
|
-
canvas.width = width;
|
|
183
|
-
canvas.height = height;
|
|
184
|
-
context.clearRect(0, 0, width, height);
|
|
185
|
-
context.drawImage(image, 0, 0, width, height); // 绘制 canvas
|
|
186
|
-
const canvasURL = canvas.toDataURL(mime, targetQuality);
|
|
187
|
-
const buffer = atob(canvasURL.split(',')[1]);
|
|
188
|
-
let length = buffer.length;
|
|
189
|
-
const bufferArray = new Uint8Array(new ArrayBuffer(length));
|
|
190
|
-
while (length--) {
|
|
191
|
-
bufferArray[length] = buffer.charCodeAt(length);
|
|
192
|
-
}
|
|
193
|
-
const miniFile = new File([bufferArray], fileName, {
|
|
194
|
-
type: mime
|
|
195
|
-
});
|
|
196
|
-
resolve({
|
|
197
|
-
file: miniFile,
|
|
198
|
-
bufferArray,
|
|
199
|
-
origin: file,
|
|
200
|
-
beforeSrc: src,
|
|
201
|
-
afterSrc: canvasURL,
|
|
202
|
-
beforeKB: sizeKB,
|
|
203
|
-
afterKB: Number((miniFile.size / 1024).toFixed(2))
|
|
204
|
-
});
|
|
205
|
-
};
|
|
206
|
-
image.src = src;
|
|
207
|
-
};
|
|
208
|
-
reader.readAsDataURL(file);
|
|
209
|
-
}
|
|
147
|
+
}
|
|
148
|
+
if (type.isNumber(size)) {
|
|
149
|
+
maxSize = size >= 1200 ? size : 1200;
|
|
150
|
+
}
|
|
151
|
+
if (file instanceof FileList) {
|
|
152
|
+
return Promise.all(Array.from(file).map(el => compressImg(el, { maxSize, mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
|
|
153
|
+
} else if (file instanceof File) {
|
|
154
|
+
return new Promise(resolve => {
|
|
155
|
+
const ext = {
|
|
156
|
+
'image/webp': 'webp',
|
|
157
|
+
'image/jpeg': 'jpg',
|
|
158
|
+
'image/png': 'png'
|
|
159
|
+
};
|
|
160
|
+
const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
|
|
161
|
+
const sizeKB = +parseInt((file.size / 1024).toFixed(2));
|
|
162
|
+
if (sizeKB < minFileSizeKB) {
|
|
163
|
+
resolve({
|
|
164
|
+
file: file
|
|
210
165
|
});
|
|
211
|
-
|
|
212
|
-
|
|
166
|
+
} else {
|
|
167
|
+
const reader = new FileReader(); // 创建 FileReader
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
reader.onload = ({ target: { result: src } }) => {
|
|
170
|
+
const image = new Image(); // 创建 img 元素
|
|
171
|
+
image.onload = () => {
|
|
172
|
+
const canvas = document.createElement('canvas'); // 创建 canvas 元素
|
|
173
|
+
const context = canvas.getContext('2d');
|
|
174
|
+
const originWidth = image.width;
|
|
175
|
+
const originHeight = image.height;
|
|
176
|
+
const { width, height } = scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight });
|
|
177
|
+
canvas.width = width;
|
|
178
|
+
canvas.height = height;
|
|
179
|
+
context.clearRect(0, 0, width, height);
|
|
180
|
+
context.drawImage(image, 0, 0, width, height); // 绘制 canvas
|
|
181
|
+
const canvasURL = canvas.toDataURL(mime, targetQuality);
|
|
182
|
+
const buffer = atob(canvasURL.split(',')[1]);
|
|
183
|
+
let length = buffer.length;
|
|
184
|
+
const bufferArray = new Uint8Array(new ArrayBuffer(length));
|
|
185
|
+
while (length--) {
|
|
186
|
+
bufferArray[length] = buffer.charCodeAt(length);
|
|
187
|
+
}
|
|
188
|
+
const miniFile = new File([bufferArray], fileName, {
|
|
189
|
+
type: mime
|
|
190
|
+
});
|
|
191
|
+
resolve({
|
|
192
|
+
file: miniFile,
|
|
193
|
+
bufferArray,
|
|
194
|
+
origin: file,
|
|
195
|
+
beforeSrc: src,
|
|
196
|
+
afterSrc: canvasURL,
|
|
197
|
+
beforeKB: sizeKB,
|
|
198
|
+
afterKB: Number((miniFile.size / 1024).toFixed(2))
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
image.src = src;
|
|
202
|
+
};
|
|
203
|
+
reader.readAsDataURL(file);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return Promise.resolve(null);
|
|
213
208
|
}
|
|
214
209
|
|
|
215
210
|
exports.chooseLocalFile = chooseLocalFile;
|
package/dist/cjs/func.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -15,21 +15,20 @@
|
|
|
15
15
|
* @returns {DebounceFunc<F>}
|
|
16
16
|
*/
|
|
17
17
|
const debounce = (func, wait) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return f;
|
|
18
|
+
let timeout;
|
|
19
|
+
let canceled = false;
|
|
20
|
+
const f = function (...args) {
|
|
21
|
+
if (canceled) return;
|
|
22
|
+
clearTimeout(timeout);
|
|
23
|
+
timeout = setTimeout(() => {
|
|
24
|
+
func.call(this, ...args);
|
|
25
|
+
}, wait);
|
|
26
|
+
};
|
|
27
|
+
f.cancel = () => {
|
|
28
|
+
clearTimeout(timeout);
|
|
29
|
+
canceled = true;
|
|
30
|
+
};
|
|
31
|
+
return f;
|
|
33
32
|
};
|
|
34
33
|
/**
|
|
35
34
|
* 节流函数
|
|
@@ -41,57 +40,53 @@ const debounce = (func, wait) => {
|
|
|
41
40
|
* @returns {ThrottleFunc<F>}
|
|
42
41
|
*/
|
|
43
42
|
const throttle = (func, wait, immediate) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
func.call(this, ...args);
|
|
54
|
-
};
|
|
55
|
-
// 第一次执行
|
|
56
|
-
if (lastCalledTime === 0) {
|
|
57
|
-
if (immediate) {
|
|
58
|
-
return call();
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
lastCalledTime = now;
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const remain = lastCalledTime + wait - now;
|
|
66
|
-
if (remain > 0) {
|
|
67
|
-
clearTimeout(timeout);
|
|
68
|
-
timeout = setTimeout(() => call(), wait);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
call();
|
|
72
|
-
}
|
|
43
|
+
let timeout;
|
|
44
|
+
let canceled = false;
|
|
45
|
+
let lastCalledTime = 0;
|
|
46
|
+
const f = function (...args) {
|
|
47
|
+
if (canceled) return;
|
|
48
|
+
const now = Date.now();
|
|
49
|
+
const call = () => {
|
|
50
|
+
lastCalledTime = now;
|
|
51
|
+
func.call(this, ...args);
|
|
73
52
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
53
|
+
// 第一次执行
|
|
54
|
+
if (lastCalledTime === 0) {
|
|
55
|
+
if (immediate) {
|
|
56
|
+
return call();
|
|
57
|
+
} else {
|
|
58
|
+
lastCalledTime = now;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const remain = lastCalledTime + wait - now;
|
|
63
|
+
if (remain > 0) {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
timeout = setTimeout(() => call(), wait);
|
|
66
|
+
} else {
|
|
67
|
+
call();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
f.cancel = () => {
|
|
71
|
+
clearTimeout(timeout);
|
|
72
|
+
canceled = true;
|
|
73
|
+
};
|
|
74
|
+
return f;
|
|
79
75
|
};
|
|
80
76
|
/**
|
|
81
77
|
* 单次函数
|
|
82
78
|
* @param {AnyFunc} func
|
|
83
79
|
* @returns {AnyFunc}
|
|
84
80
|
*/
|
|
85
|
-
const once =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
81
|
+
const once = func => {
|
|
82
|
+
let called = false;
|
|
83
|
+
let result;
|
|
84
|
+
return function (...args) {
|
|
85
|
+
if (called) return result;
|
|
86
|
+
called = true;
|
|
87
|
+
result = func.call(this, ...args);
|
|
88
|
+
return result;
|
|
89
|
+
};
|
|
95
90
|
};
|
|
96
91
|
/**
|
|
97
92
|
* 设置全局变量
|
|
@@ -99,16 +94,11 @@ const once = (func) => {
|
|
|
99
94
|
* @param val
|
|
100
95
|
*/
|
|
101
96
|
function setGlobal(key, val) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
global[key] = val;
|
|
108
|
-
else if (typeof self !== 'undefined')
|
|
109
|
-
self[key] = val;
|
|
110
|
-
else
|
|
111
|
-
throw new SyntaxError('当前环境下无法设置全局属性');
|
|
97
|
+
if (typeof globalThis !== 'undefined') globalThis[key] = val;
|
|
98
|
+
else if (typeof window !== 'undefined') window[key] = val;
|
|
99
|
+
else if (typeof global !== 'undefined') global[key] = val;
|
|
100
|
+
else if (typeof self !== 'undefined') self[key] = val;
|
|
101
|
+
else throw new SyntaxError('当前环境下无法设置全局属性');
|
|
112
102
|
}
|
|
113
103
|
/**
|
|
114
104
|
* 获取全局变量
|
|
@@ -116,14 +106,10 @@ function setGlobal(key, val) {
|
|
|
116
106
|
* @param val
|
|
117
107
|
*/
|
|
118
108
|
function getGlobal(key) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
else if (typeof global !== 'undefined')
|
|
124
|
-
return global[key];
|
|
125
|
-
else if (typeof self !== 'undefined')
|
|
126
|
-
return self[key];
|
|
109
|
+
if (typeof globalThis !== 'undefined') return globalThis[key];
|
|
110
|
+
else if (typeof window !== 'undefined') return window[key];
|
|
111
|
+
else if (typeof global !== 'undefined') return global[key];
|
|
112
|
+
else if (typeof self !== 'undefined') return self[key];
|
|
127
113
|
}
|
|
128
114
|
|
|
129
115
|
exports.debounce = debounce;
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -34,8 +34,6 @@ var variable = require('./variable.js');
|
|
|
34
34
|
var cloneDeep = require('./cloneDeep.js');
|
|
35
35
|
var easing = require('./easing.js');
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
37
|
exports.arrayEach = array.arrayEach;
|
|
40
38
|
exports.arrayEachAsync = array.arrayEachAsync;
|
|
41
39
|
exports.arrayInsertBefore = array.arrayInsertBefore;
|
package/dist/cjs/math.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -12,14 +12,12 @@
|
|
|
12
12
|
* @param arg2 数值2
|
|
13
13
|
*/
|
|
14
14
|
const multiply = (arg1, arg2) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
m += s2.split('.')[1].length;
|
|
22
|
-
return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
|
|
15
|
+
let m = 0;
|
|
16
|
+
const s1 = arg1.toString();
|
|
17
|
+
const s2 = arg2.toString();
|
|
18
|
+
if (s1.split('.')[1] !== undefined) m += s1.split('.')[1].length;
|
|
19
|
+
if (s2.split('.')[1] !== undefined) m += s2.split('.')[1].length;
|
|
20
|
+
return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
|
|
23
21
|
};
|
|
24
22
|
/**
|
|
25
23
|
* 数值安全加法
|
|
@@ -27,23 +25,21 @@ const multiply = (arg1, arg2) => {
|
|
|
27
25
|
* @param arg2 数值2
|
|
28
26
|
*/
|
|
29
27
|
const add = (arg1, arg2) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
m = 10 ** Math.max(r1, r2);
|
|
46
|
-
return (multiply(arg1, m) + multiply(arg2, m)) / m;
|
|
28
|
+
let r1 = 0;
|
|
29
|
+
let r2 = 0;
|
|
30
|
+
let m = 0;
|
|
31
|
+
try {
|
|
32
|
+
r1 = arg1.toString().split('.')[1].length;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
r1 = 0;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
r2 = arg2.toString().split('.')[1].length;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
r2 = 0;
|
|
40
|
+
}
|
|
41
|
+
m = 10 ** Math.max(r1, r2);
|
|
42
|
+
return (multiply(arg1, m) + multiply(arg2, m)) / m;
|
|
47
43
|
};
|
|
48
44
|
/**
|
|
49
45
|
* 数值安全减法
|
|
@@ -57,17 +53,15 @@ const subtract = (arg1, arg2) => add(arg1, -arg2);
|
|
|
57
53
|
* @param arg2 数值2
|
|
58
54
|
*/
|
|
59
55
|
const divide = (arg1, arg2) => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
r2 = Number(arg2.toString().replace('.', ''));
|
|
70
|
-
return (r1 / r2) * Math.pow(10, t2 - t1);
|
|
56
|
+
let t1 = 0;
|
|
57
|
+
let t2 = 0;
|
|
58
|
+
let r1 = 0;
|
|
59
|
+
let r2 = 0;
|
|
60
|
+
if (arg1.toString().split('.')[1] !== undefined) t1 = arg1.toString().split('.')[1].length;
|
|
61
|
+
if (arg2.toString().split('.')[1] !== undefined) t2 = arg2.toString().split('.')[1].length;
|
|
62
|
+
r1 = Number(arg1.toString().replace('.', ''));
|
|
63
|
+
r2 = Number(arg2.toString().replace('.', ''));
|
|
64
|
+
return (r1 / r2) * Math.pow(10, t2 - t1);
|
|
71
65
|
};
|
|
72
66
|
/**
|
|
73
67
|
* Correct the given number to specifying significant digits.
|
|
@@ -78,7 +72,7 @@ const divide = (arg1, arg2) => {
|
|
|
78
72
|
* @example strip(0.09999999999999998) === 0.1 // true
|
|
79
73
|
*/
|
|
80
74
|
function strip(num, precision = 15) {
|
|
81
|
-
|
|
75
|
+
return +parseFloat(Number(num).toPrecision(precision));
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
exports.add = add;
|