jmchart 1.2.18 → 1.2.19
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/jmchart.amd.js +628 -61
- package/dist/jmchart.cjs.js +628 -61
- package/dist/jmchart.esm.js +628 -61
- package/dist/jmchart.iife.js +628 -61
- package/dist/jmchart.system.js +628 -61
- package/package.json +2 -2
package/dist/jmchart.cjs.js
CHANGED
|
@@ -154,11 +154,13 @@ const colorKeywords = {
|
|
|
154
154
|
darkseagreen: "#8fbc8f",
|
|
155
155
|
darkslateblue: "#483d8b",
|
|
156
156
|
darkslategray: "#2f4f4f",
|
|
157
|
+
darkslategrey: "#2f4f4f",
|
|
157
158
|
darkturquoise: "#00ced1",
|
|
158
159
|
darkviolet: "#9400d3",
|
|
159
160
|
deeppink: "#ff1493",
|
|
160
161
|
deepskyblue: "#00bfff",
|
|
161
162
|
dimgray: "#696969",
|
|
163
|
+
dimgrey: "#696969",
|
|
162
164
|
dodgerblue: "#1e90ff",
|
|
163
165
|
firebrick: "#b22222",
|
|
164
166
|
floralwhite: "#fffaf0",
|
|
@@ -193,6 +195,7 @@ const colorKeywords = {
|
|
|
193
195
|
lightseagreen: "#20b2aa",
|
|
194
196
|
lightskyblue: "#87cefa",
|
|
195
197
|
lightslategray: "#778899",
|
|
198
|
+
lightslategrey: "#778899",
|
|
196
199
|
lightsteelblue: "#b0c4de",
|
|
197
200
|
lightyellow: "#ffffe0",
|
|
198
201
|
lime: "#00ff00",
|
|
@@ -233,6 +236,7 @@ const colorKeywords = {
|
|
|
233
236
|
powderblue: "#b0e0e6",
|
|
234
237
|
purple: "#800080",
|
|
235
238
|
red: "#ff0000",
|
|
239
|
+
rebeccapurple: "#663399",
|
|
236
240
|
rosybrown: "#bc8f8f",
|
|
237
241
|
royalblue: "#4169e1",
|
|
238
242
|
saddlebrown: "#8b4513",
|
|
@@ -245,6 +249,7 @@ const colorKeywords = {
|
|
|
245
249
|
skyblue: "#87ceeb",
|
|
246
250
|
slateblue: "#6a5acd",
|
|
247
251
|
slategray: "#708090",
|
|
252
|
+
slategrey: "#708090",
|
|
248
253
|
snow: "#fffafa",
|
|
249
254
|
springgreen: "#00ff7f",
|
|
250
255
|
steelblue: "#4682b4",
|
|
@@ -259,7 +264,37 @@ const colorKeywords = {
|
|
|
259
264
|
whitesmoke: "#f5f5f5",
|
|
260
265
|
yellow: "#ffff00",
|
|
261
266
|
yellowgreen: "#9acd32",
|
|
262
|
-
transparent: "rgba(0,0,0,0)"
|
|
267
|
+
transparent: "rgba(0,0,0,0)",
|
|
268
|
+
// grey 别名(已有 darkslategrey/lightslategrey/slategrey/dimgrey)
|
|
269
|
+
// 以下为 CSS 系统颜色
|
|
270
|
+
activeborder: "#bfcaca",
|
|
271
|
+
activecaption: "#000080",
|
|
272
|
+
appworkspace: "#ababab",
|
|
273
|
+
background: "#636363",
|
|
274
|
+
buttonface: "#c0c0c0",
|
|
275
|
+
buttonhighlight: "#dedede",
|
|
276
|
+
buttonshadow: "#808080",
|
|
277
|
+
buttontext: "#000000",
|
|
278
|
+
captiontext: "#000000",
|
|
279
|
+
graytext: "#808080",
|
|
280
|
+
highlight: "#b3d4fc",
|
|
281
|
+
highlighttext: "#000000",
|
|
282
|
+
inactiveborder: "#d4d0c8",
|
|
283
|
+
inactivecaption: "#bfbbb0",
|
|
284
|
+
inactivecaptiontext: "#545454",
|
|
285
|
+
infobackground: "#fbfcc5",
|
|
286
|
+
infotext: "#000000",
|
|
287
|
+
menu: "#c0c0c0",
|
|
288
|
+
menutext: "#000000",
|
|
289
|
+
scrollbar: "#c0c0c0",
|
|
290
|
+
threeddarkshadow: "#696969",
|
|
291
|
+
threedface: "#c0c0c0",
|
|
292
|
+
threedhighlight: "#dfdfdf",
|
|
293
|
+
threedlightshadow: "#dcdcdc",
|
|
294
|
+
threedshadow: "#808080",
|
|
295
|
+
window: "#ffffff",
|
|
296
|
+
windowframe: "#646464",
|
|
297
|
+
windowtext: "#000000"
|
|
263
298
|
};
|
|
264
299
|
|
|
265
300
|
/**
|
|
@@ -1095,28 +1130,65 @@ class jmGradient {
|
|
|
1095
1130
|
d = location.radius * 2;
|
|
1096
1131
|
}
|
|
1097
1132
|
if(!d) {
|
|
1098
|
-
d = Math.min(location.width,location.height);
|
|
1133
|
+
d = Math.min(location.width || 0, location.height || 0);
|
|
1099
1134
|
}
|
|
1135
|
+
if(d <= 0) {
|
|
1136
|
+
d = Math.max(bounds.width || 0, bounds.height || 0, 100);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
const width = bounds.width || d;
|
|
1140
|
+
const height = bounds.height || d;
|
|
1100
1141
|
|
|
1101
|
-
//let offsetLine = 1;//渐变长度或半径
|
|
1102
|
-
//处理百分比参数
|
|
1103
1142
|
if(jmUtils.checkPercent(x1)) {
|
|
1104
|
-
x1 = jmUtils.percentToNumber(x1) *
|
|
1143
|
+
x1 = jmUtils.percentToNumber(x1) * width;
|
|
1144
|
+
}
|
|
1145
|
+
else if(typeof x1 === 'number' && x1 >= 0 && x1 <= 1) {
|
|
1146
|
+
x1 = x1 * width;
|
|
1147
|
+
}
|
|
1148
|
+
else if(typeof x1 === 'string') {
|
|
1149
|
+
const num = parseFloat(x1);
|
|
1150
|
+
if(!isNaN(num)) x1 = num;
|
|
1105
1151
|
}
|
|
1106
1152
|
if(jmUtils.checkPercent(x2)) {
|
|
1107
|
-
x2 = jmUtils.percentToNumber(x2) *
|
|
1153
|
+
x2 = jmUtils.percentToNumber(x2) * width;
|
|
1154
|
+
}
|
|
1155
|
+
else if(typeof x2 === 'number' && x2 >= 0 && x2 <= 1) {
|
|
1156
|
+
x2 = x2 * width;
|
|
1157
|
+
}
|
|
1158
|
+
else if(typeof x2 === 'string') {
|
|
1159
|
+
const num = parseFloat(x2);
|
|
1160
|
+
if(!isNaN(num)) x2 = num;
|
|
1108
1161
|
}
|
|
1109
1162
|
if(jmUtils.checkPercent(y1)) {
|
|
1110
|
-
y1 = jmUtils.percentToNumber(y1) *
|
|
1163
|
+
y1 = jmUtils.percentToNumber(y1) * height;
|
|
1164
|
+
}
|
|
1165
|
+
else if(typeof y1 === 'number' && y1 >= 0 && y1 <= 1) {
|
|
1166
|
+
y1 = y1 * height;
|
|
1167
|
+
}
|
|
1168
|
+
else if(typeof y1 === 'string') {
|
|
1169
|
+
const num = parseFloat(y1);
|
|
1170
|
+
if(!isNaN(num)) y1 = num;
|
|
1111
1171
|
}
|
|
1112
1172
|
if(jmUtils.checkPercent(y2)) {
|
|
1113
|
-
y2 = jmUtils.percentToNumber(y2) *
|
|
1114
|
-
}
|
|
1173
|
+
y2 = jmUtils.percentToNumber(y2) * height;
|
|
1174
|
+
}
|
|
1175
|
+
else if(typeof y2 === 'number' && y2 >= 0 && y2 <= 1) {
|
|
1176
|
+
y2 = y2 * height;
|
|
1177
|
+
}
|
|
1178
|
+
else if(typeof y2 === 'string') {
|
|
1179
|
+
const num = parseFloat(y2);
|
|
1180
|
+
if(!isNaN(num)) y2 = num;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
x1 = Number(x1) || 0;
|
|
1184
|
+
y1 = Number(y1) || 0;
|
|
1185
|
+
x2 = Number(x2) || 0;
|
|
1186
|
+
y2 = Number(y2) || 0;
|
|
1115
1187
|
|
|
1116
|
-
let sx1 =
|
|
1117
|
-
let sy1 =
|
|
1118
|
-
let sx2 =
|
|
1119
|
-
let sy2 =
|
|
1188
|
+
let sx1 = x1 + (bounds.left || 0);
|
|
1189
|
+
let sy1 = y1 + (bounds.top || 0);
|
|
1190
|
+
let sx2 = x2 + (bounds.left || 0);
|
|
1191
|
+
let sy2 = y2 + (bounds.top || 0);
|
|
1120
1192
|
if(this.type === 'linear') {
|
|
1121
1193
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1122
1194
|
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
@@ -1130,24 +1202,47 @@ class jmGradient {
|
|
|
1130
1202
|
else if(this.type === 'radial') {
|
|
1131
1203
|
let r1 = this.r1||0;
|
|
1132
1204
|
let r2 = this.r2;
|
|
1205
|
+
|
|
1206
|
+
if(d <= 0) {
|
|
1207
|
+
d = Math.max(bounds.width || 0, bounds.height || 0, 1);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1133
1210
|
if(jmUtils.checkPercent(r1)) {
|
|
1134
1211
|
r1 = jmUtils.percentToNumber(r1);
|
|
1135
1212
|
r1 = d * r1;
|
|
1136
1213
|
}
|
|
1214
|
+
else if(typeof r1 === 'number' && r1 >= 0 && r1 <= 1) {
|
|
1215
|
+
r1 = r1 * d;
|
|
1216
|
+
}
|
|
1217
|
+
else if(typeof r1 === 'string') {
|
|
1218
|
+
r1 = parseFloat(r1) || 0;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1137
1221
|
if(jmUtils.checkPercent(r2)) {
|
|
1138
1222
|
r2 = jmUtils.percentToNumber(r2);
|
|
1139
1223
|
r2 = d * r2;
|
|
1140
1224
|
}
|
|
1225
|
+
else if(typeof r2 === 'number' && r2 >= 0 && r2 <= 1) {
|
|
1226
|
+
r2 = r2 * d;
|
|
1227
|
+
}
|
|
1228
|
+
else if(typeof r2 === 'string') {
|
|
1229
|
+
r2 = parseFloat(r2);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
if(r2 === undefined || r2 === null || isNaN(Number(r2)) || Number(r2) <= 0) {
|
|
1233
|
+
r2 = d / 2;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
r1 = Number(r1) || 0;
|
|
1237
|
+
r2 = Number(r2) || d / 2;
|
|
1238
|
+
|
|
1141
1239
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1142
|
-
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
1143
1240
|
gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
|
|
1144
1241
|
gradient.key = this.toString();
|
|
1145
1242
|
}
|
|
1146
|
-
//offsetLine = Math.abs(r2 - r1);//二圆半径差
|
|
1147
1243
|
else if(context.createRadialGradient) {
|
|
1148
1244
|
gradient = context.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2);
|
|
1149
1245
|
}
|
|
1150
|
-
//小程序的接口特殊
|
|
1151
1246
|
else if(context.createCircularGradient) {
|
|
1152
1247
|
gradient = context.createCircularGradient(sx1, sy1, r2);
|
|
1153
1248
|
}
|
|
@@ -1170,67 +1265,539 @@ class jmGradient {
|
|
|
1170
1265
|
}
|
|
1171
1266
|
|
|
1172
1267
|
/**
|
|
1173
|
-
*
|
|
1174
|
-
*
|
|
1175
|
-
* linear-gradient
|
|
1176
|
-
*
|
|
1268
|
+
* 解析渐变字符串
|
|
1269
|
+
* 支持的格式:
|
|
1270
|
+
* - linear-gradient(180deg, #8b5cf6 0%, #6366f1 50%, #4f46e5 100%)
|
|
1271
|
+
* - linear-gradient(to top, red, blue)
|
|
1272
|
+
* - linear-gradient(to right bottom, red, blue)
|
|
1273
|
+
* - linear-gradient(45deg, red, blue)
|
|
1274
|
+
* - linear-gradient(0.5turn, red, blue)
|
|
1275
|
+
* - radial-gradient(circle, red, blue)
|
|
1276
|
+
* - radial-gradient(ellipse at top, red, blue)
|
|
1177
1277
|
*
|
|
1178
1278
|
* @method fromString
|
|
1179
1279
|
* @for jmGradient
|
|
1180
|
-
* @
|
|
1280
|
+
* @param {string} s 渐变字符串
|
|
1181
1281
|
*/
|
|
1182
1282
|
fromString(s) {
|
|
1183
|
-
if(!s)
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
const
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1283
|
+
if(!s) {
|
|
1284
|
+
console.warn('jmGradient: 渐变字符串为空');
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
// 使用 [\s\S] 匹配任意字符(包括换行符),支持多行渐变字符串
|
|
1288
|
+
const gradientMatch = s.match(/(linear|radial)-gradient\s*\(\s*([\s\S]+)\)/i);
|
|
1289
|
+
if(!gradientMatch || gradientMatch.length < 3) {
|
|
1290
|
+
console.warn('jmGradient: 无效的渐变字符串格式: "' + s + '"');
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
const type = gradientMatch[1].toLowerCase();
|
|
1295
|
+
if(type !== 'linear' && type !== 'radial') {
|
|
1296
|
+
console.warn('jmGradient: 不支持的渐变类型 "' + type + '",仅支持 linear 和 radial');
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
this.type = type;
|
|
1301
|
+
const content = jmUtils.trim(gradientMatch[2]);
|
|
1302
|
+
|
|
1303
|
+
const splitIndex = this._findSplitIndex(content);
|
|
1304
|
+
if(splitIndex < 0) {
|
|
1305
|
+
console.warn('jmGradient: 无法解析渐变内容: "' + content + '"');
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
const params = content.substring(0, splitIndex).trim();
|
|
1310
|
+
const colorPart = content.substring(splitIndex + 1).trim();
|
|
1311
|
+
|
|
1312
|
+
if(!colorPart) {
|
|
1313
|
+
console.warn('jmGradient: 未找到颜色停止点');
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
if(this.type === 'linear') {
|
|
1318
|
+
this._parseLinearParams(params);
|
|
1319
|
+
}
|
|
1320
|
+
else {
|
|
1321
|
+
this._parseRadialParams(params);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
const colorCount = this._parseColorStops(colorPart);
|
|
1325
|
+
if(colorCount === 0) {
|
|
1326
|
+
console.warn('jmGradient: 未找到有效的颜色停止点: "' + colorPart + '"');
|
|
1327
|
+
}
|
|
1328
|
+
else if(colorCount < 2) {
|
|
1329
|
+
console.warn('jmGradient: 颜色停止点至少需要2个,当前只有 ' + colorCount + ' 个');
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
/**
|
|
1334
|
+
* 找到参数和颜色的分割位置(第一个不在括号内的逗号)
|
|
1335
|
+
* @param {string} content 内容字符串
|
|
1336
|
+
* @returns {number} 分割位置索引
|
|
1337
|
+
*/
|
|
1338
|
+
_findSplitIndex(content) {
|
|
1339
|
+
let depth = 0;
|
|
1340
|
+
|
|
1341
|
+
for(let i = 0; i < content.length; i++) {
|
|
1342
|
+
const char = content[i];
|
|
1343
|
+
if(char === '(') {
|
|
1344
|
+
depth++;
|
|
1194
1345
|
}
|
|
1195
|
-
else {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1346
|
+
else if(char === ')') {
|
|
1347
|
+
depth--;
|
|
1348
|
+
}
|
|
1349
|
+
else if(char === ',' && depth === 0) {
|
|
1350
|
+
return i;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
return -1;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
/**
|
|
1358
|
+
* 验证渐变配置是否有效
|
|
1359
|
+
* @returns {boolean} 是否有效
|
|
1360
|
+
*/
|
|
1361
|
+
isValid() {
|
|
1362
|
+
if(!this.type) return false;
|
|
1363
|
+
if(this.type === 'linear') {
|
|
1364
|
+
return typeof this.x1 !== 'undefined' ||
|
|
1365
|
+
typeof this.x2 !== 'undefined' ||
|
|
1366
|
+
typeof this._angle !== 'undefined';
|
|
1367
|
+
}
|
|
1368
|
+
if(this.type === 'radial') {
|
|
1369
|
+
return this.stops && this.stops.length >= 2;
|
|
1370
|
+
}
|
|
1371
|
+
return false;
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* 解析线性渐变参数
|
|
1376
|
+
* @param {string} params 参数字符串
|
|
1377
|
+
*/
|
|
1378
|
+
_parseLinearParams(params) {
|
|
1379
|
+
const trimmed = jmUtils.trim(params);
|
|
1380
|
+
|
|
1381
|
+
if(trimmed.startsWith('to ')) {
|
|
1382
|
+
const direction = trimmed.substring(3).toLowerCase().trim();
|
|
1383
|
+
const dir = this._directionToAngle(direction);
|
|
1384
|
+
this._angle = dir.angle;
|
|
1385
|
+
this.x1 = dir.x1;
|
|
1386
|
+
this.y1 = dir.y1;
|
|
1387
|
+
this.x2 = dir.x2;
|
|
1388
|
+
this.y2 = dir.y2;
|
|
1389
|
+
}
|
|
1390
|
+
else if(this._hasAngleUnit(trimmed)) {
|
|
1391
|
+
const angle = this._parseAngle(trimmed);
|
|
1392
|
+
this._angle = angle;
|
|
1393
|
+
const coords = this._angleToCoords(angle);
|
|
1394
|
+
this.x1 = coords.x1;
|
|
1395
|
+
this.y1 = coords.y1;
|
|
1396
|
+
this.x2 = coords.x2;
|
|
1397
|
+
this.y2 = coords.y2;
|
|
1398
|
+
}
|
|
1399
|
+
else if(trimmed.startsWith('at ')) {
|
|
1400
|
+
const radialMatch = trimmed.match(/at\s+(.+)/i);
|
|
1401
|
+
if(radialMatch) {
|
|
1402
|
+
this._parseRadialParams(trimmed);
|
|
1200
1403
|
}
|
|
1201
1404
|
}
|
|
1202
|
-
//径向渐变
|
|
1203
1405
|
else {
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
this.
|
|
1207
|
-
this.
|
|
1406
|
+
const parts = trimmed.split(/\s+/);
|
|
1407
|
+
if(parts.length >= 4) {
|
|
1408
|
+
this.x1 = parts[0];
|
|
1409
|
+
this.y1 = parts[1];
|
|
1410
|
+
this.x2 = parts[2];
|
|
1411
|
+
this.y2 = parts[3];
|
|
1208
1412
|
}
|
|
1209
|
-
else {
|
|
1210
|
-
this.x1 =
|
|
1211
|
-
this.y1 =
|
|
1212
|
-
this.
|
|
1213
|
-
this.
|
|
1214
|
-
|
|
1215
|
-
|
|
1413
|
+
else if(parts.length === 2) {
|
|
1414
|
+
this.x1 = 0;
|
|
1415
|
+
this.y1 = 0;
|
|
1416
|
+
this.x2 = parts[0];
|
|
1417
|
+
this.y2 = parts[1];
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* 解析径向渐变参数
|
|
1424
|
+
* @param {string} params 参数字符串
|
|
1425
|
+
*/
|
|
1426
|
+
_parseRadialParams(params) {
|
|
1427
|
+
const trimmed = jmUtils.trim(params);
|
|
1428
|
+
|
|
1429
|
+
this.shape = 'ellipse';
|
|
1430
|
+
this.position = { x: '50%', y: '50%' };
|
|
1431
|
+
|
|
1432
|
+
const atMatch = trimmed.match(/^(.+?)\s+at\s+(.+)$/i);
|
|
1433
|
+
if(atMatch) {
|
|
1434
|
+
const shapePart = jmUtils.trim(atMatch[1]);
|
|
1435
|
+
const posPart = jmUtils.trim(atMatch[2]);
|
|
1436
|
+
this._parseRadialShape(shapePart);
|
|
1437
|
+
this._parseRadialPosition(posPart);
|
|
1438
|
+
}
|
|
1439
|
+
else if(trimmed.startsWith('circle') || trimmed.startsWith('ellipse')) {
|
|
1440
|
+
this._parseRadialShape(trimmed);
|
|
1441
|
+
this.x1 = '50%';
|
|
1442
|
+
this.y1 = '50%';
|
|
1443
|
+
this.x2 = '50%';
|
|
1444
|
+
this.y2 = '50%';
|
|
1445
|
+
}
|
|
1446
|
+
else {
|
|
1447
|
+
const parts = trimmed.split(/\s+/);
|
|
1448
|
+
if(parts.length >= 3) {
|
|
1449
|
+
this.x1 = parts[0];
|
|
1450
|
+
this.y1 = parts[1];
|
|
1451
|
+
this.r1 = parts[2];
|
|
1452
|
+
}
|
|
1453
|
+
if(parts.length >= 6) {
|
|
1454
|
+
this.x2 = parts[3];
|
|
1455
|
+
this.y2 = parts[4];
|
|
1456
|
+
this.r2 = parts[5];
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
if(this.x1 === undefined && this.y1 === undefined) {
|
|
1461
|
+
this.x1 = '50%';
|
|
1462
|
+
this.y1 = '50%';
|
|
1463
|
+
}
|
|
1464
|
+
if(this.x2 === undefined && this.y2 === undefined) {
|
|
1465
|
+
this.x2 = '50%';
|
|
1466
|
+
this.y2 = '50%';
|
|
1467
|
+
}
|
|
1468
|
+
if(this.r2 === undefined) {
|
|
1469
|
+
this.r2 = '50%';
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
/**
|
|
1474
|
+
* 解析径向渐变形状
|
|
1475
|
+
* @param {string} shapePart 形状描述
|
|
1476
|
+
*/
|
|
1477
|
+
_parseRadialShape(shapePart) {
|
|
1478
|
+
if(shapePart.startsWith('circle')) {
|
|
1479
|
+
this.shape = 'circle';
|
|
1480
|
+
const sizeMatch = shapePart.match(/circle\s*\(\s*([^)]+)\s*\)/i);
|
|
1481
|
+
if(sizeMatch) {
|
|
1482
|
+
this.r2 = jmUtils.trim(sizeMatch[1]);
|
|
1216
1483
|
}
|
|
1217
1484
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1485
|
+
else if(shapePart.startsWith('ellipse')) {
|
|
1486
|
+
this.shape = 'ellipse';
|
|
1487
|
+
const sizeMatch = shapePart.match(/ellipse\s*\(\s*([^)]+)\s*\)/i);
|
|
1488
|
+
if(sizeMatch) {
|
|
1489
|
+
const sizes = jmUtils.trim(sizeMatch[1]).split(/\s+/);
|
|
1490
|
+
if(sizes.length >= 2) {
|
|
1491
|
+
this.rx = sizes[0];
|
|
1492
|
+
this.ry = sizes[1];
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* 解析径向渐变位置
|
|
1500
|
+
* @param {string} posPart 位置描述
|
|
1501
|
+
*/
|
|
1502
|
+
_parseRadialPosition(posPart) {
|
|
1503
|
+
const parts = posPart.split(/\s+/);
|
|
1504
|
+
if(parts.length >= 2) {
|
|
1505
|
+
this.x1 = parts[0];
|
|
1506
|
+
this.y1 = parts[1];
|
|
1507
|
+
this.x2 = parts[0];
|
|
1508
|
+
this.y2 = parts[1];
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
/**
|
|
1513
|
+
* 解析颜色停止点
|
|
1514
|
+
* @param {string} colorPart 颜色部分字符串
|
|
1515
|
+
* @returns {number} 成功解析的颜色数量
|
|
1516
|
+
*/
|
|
1517
|
+
_parseColorStops(colorPart) {
|
|
1518
|
+
if(!colorPart) {
|
|
1519
|
+
return 0;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
const stops = this._splitColorStops(colorPart);
|
|
1523
|
+
let lastOffset = -1;
|
|
1524
|
+
let colorCount = 0;
|
|
1525
|
+
|
|
1526
|
+
for(let i = 0; i < stops.length; i++) {
|
|
1527
|
+
const stop = jmUtils.trim(stops[i]);
|
|
1528
|
+
if(!stop) continue;
|
|
1529
|
+
|
|
1530
|
+
const parsed = this._parseSingleColorStop(stop);
|
|
1531
|
+
if(!parsed) {
|
|
1532
|
+
continue;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
let { color, offset } = parsed;
|
|
1536
|
+
|
|
1537
|
+
if(color === 'transparent') {
|
|
1538
|
+
color = 'rgba(0,0,0,0)';
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
if(!this._isValidColor(color)) {
|
|
1542
|
+
console.warn('jmGradient: 无效的颜色格式 "' + color + '"');
|
|
1543
|
+
continue;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
const normalizedOffset = this._normalizeOffset(offset);
|
|
1547
|
+
let finalOffset = normalizedOffset;
|
|
1548
|
+
|
|
1549
|
+
if(finalOffset === null) {
|
|
1550
|
+
if(i === 0) {
|
|
1551
|
+
finalOffset = 0;
|
|
1552
|
+
}
|
|
1553
|
+
else if(i === stops.length - 1) {
|
|
1554
|
+
finalOffset = 1;
|
|
1555
|
+
}
|
|
1556
|
+
else {
|
|
1557
|
+
const nextOffset = this._findNextOffset(stops, i);
|
|
1558
|
+
if(nextOffset !== null) {
|
|
1559
|
+
finalOffset = (lastOffset + nextOffset) / 2;
|
|
1560
|
+
}
|
|
1561
|
+
else {
|
|
1562
|
+
finalOffset = Math.min(1, lastOffset + (1 - lastOffset) / (stops.length - i));
|
|
1230
1563
|
}
|
|
1231
1564
|
}
|
|
1232
1565
|
}
|
|
1566
|
+
|
|
1567
|
+
if(finalOffset !== null) {
|
|
1568
|
+
if(finalOffset < 0 || finalOffset > 1) {
|
|
1569
|
+
console.warn('jmGradient: 颜色偏移量 ' + finalOffset + ' 超出有效范围 [0, 1],将调整为有效范围');
|
|
1570
|
+
finalOffset = Math.max(0, Math.min(1, finalOffset));
|
|
1571
|
+
}
|
|
1572
|
+
lastOffset = finalOffset;
|
|
1573
|
+
this.addStop(finalOffset, color);
|
|
1574
|
+
colorCount++;
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
return colorCount;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
/**
|
|
1582
|
+
* 分割颜色停止点字符串
|
|
1583
|
+
* @param {string} colorPart 颜色部分字符串
|
|
1584
|
+
* @returns {string[]} 颜色停止点数组
|
|
1585
|
+
*/
|
|
1586
|
+
_splitColorStops(colorPart) {
|
|
1587
|
+
const stops = [];
|
|
1588
|
+
let depth = 0;
|
|
1589
|
+
let current = '';
|
|
1590
|
+
|
|
1591
|
+
for(let i = 0; i < colorPart.length; i++) {
|
|
1592
|
+
const char = colorPart[i];
|
|
1593
|
+
if(char === '(') {
|
|
1594
|
+
depth++;
|
|
1595
|
+
current += char;
|
|
1596
|
+
}
|
|
1597
|
+
else if(char === ')') {
|
|
1598
|
+
depth--;
|
|
1599
|
+
current += char;
|
|
1600
|
+
}
|
|
1601
|
+
else if(char === ',' && depth === 0) {
|
|
1602
|
+
stops.push(current.trim());
|
|
1603
|
+
current = '';
|
|
1604
|
+
}
|
|
1605
|
+
else {
|
|
1606
|
+
current += char;
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
if(current.trim()) {
|
|
1611
|
+
stops.push(current.trim());
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
return stops;
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
/**
|
|
1618
|
+
* 解析单个颜色停止点
|
|
1619
|
+
* @param {string} stop 单个颜色停止点字符串
|
|
1620
|
+
* @returns {object|null} {color, offset} 或 null
|
|
1621
|
+
*/
|
|
1622
|
+
_parseSingleColorStop(stop) {
|
|
1623
|
+
const hexMatch = stop.match(/^(#[a-fA-F0-9]{3,8})\s*(\d+(?:\.\d+)?%?)?$/i);
|
|
1624
|
+
if(hexMatch) {
|
|
1625
|
+
return { color: hexMatch[1], offset: hexMatch[2] || null };
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
const rgbaMatch = stop.match(/^(rgba?\s*\([^)]+\))\s*(\d+(?:\.\d+)?%?)?$/i);
|
|
1629
|
+
if(rgbaMatch) {
|
|
1630
|
+
return { color: rgbaMatch[1], offset: rgbaMatch[2] || null };
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
const hslaMatch = stop.match(/^(hsla?\s*\([^)]+\))\s*(\d+(?:\.\d+)?%?)?$/i);
|
|
1634
|
+
if(hslaMatch) {
|
|
1635
|
+
return { color: hslaMatch[1], offset: hslaMatch[2] || null };
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
const namedMatch = stop.match(/^([a-zA-Z]+)\s*(\d+(?:\.\d+)?%?)?$/i);
|
|
1639
|
+
if(namedMatch && this._isValidColor(namedMatch[1])) {
|
|
1640
|
+
return { color: namedMatch[1], offset: namedMatch[2] || null };
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
return null;
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
/**
|
|
1647
|
+
* 查找下一个有偏移量的颜色停止点
|
|
1648
|
+
* @param {string[]} stops 颜色停止点数组
|
|
1649
|
+
* @param {number} currentIndex 当前索引
|
|
1650
|
+
* @returns {number|null} 下一个偏移量或null
|
|
1651
|
+
*/
|
|
1652
|
+
_findNextOffset(stops, currentIndex) {
|
|
1653
|
+
for(let i = currentIndex + 1; i < stops.length; i++) {
|
|
1654
|
+
const parsed = this._parseSingleColorStop(jmUtils.trim(stops[i]));
|
|
1655
|
+
if(parsed && parsed.offset) {
|
|
1656
|
+
return this._normalizeOffset(parsed.offset);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
return null;
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
/**
|
|
1663
|
+
* 验证颜色格式是否有效
|
|
1664
|
+
* @param {string} color 颜色字符串
|
|
1665
|
+
* @returns {boolean} 是否有效
|
|
1666
|
+
*/
|
|
1667
|
+
_isValidColor(color) {
|
|
1668
|
+
if(!color) return false;
|
|
1669
|
+
|
|
1670
|
+
const hexPattern = /^#([a-fA-F0-9]{3,8})$/;
|
|
1671
|
+
if(hexPattern.test(color)) return true;
|
|
1672
|
+
|
|
1673
|
+
// 支持 rgba(r,g,b,a) 和 rgba(r, g, b, a) 等各种空格格式
|
|
1674
|
+
const rgbPattern = /^rgba?\s*\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*[\d.]+\s*)?\)$/i;
|
|
1675
|
+
if(rgbPattern.test(color)) return true;
|
|
1676
|
+
|
|
1677
|
+
const hslPattern = /^hsla?\s*\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*(,\s*[\d.]+\s*)?\)$/i;
|
|
1678
|
+
if(hslPattern.test(color)) return true;
|
|
1679
|
+
|
|
1680
|
+
// 使用 jmUtils 中的完整 CSS 颜色关键字表
|
|
1681
|
+
if(colorKeywords && colorKeywords[color.toLowerCase()]) return true;
|
|
1682
|
+
|
|
1683
|
+
// 宽松处理:符合 CSS 关键字命名规则的字符串也视为有效颜色
|
|
1684
|
+
// (纯字母,可能在运行时被浏览器或其他环境解析)
|
|
1685
|
+
if(/^[a-zA-Z]+$/.test(color)) return true;
|
|
1686
|
+
|
|
1687
|
+
return false;
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
/**
|
|
1691
|
+
* 标准化偏移值
|
|
1692
|
+
* @param {string} offset 偏移字符串
|
|
1693
|
+
* @returns {number|null} 0-1之间的数值或null
|
|
1694
|
+
*/
|
|
1695
|
+
_normalizeOffset(offset) {
|
|
1696
|
+
if(!offset) return null;
|
|
1697
|
+
offset = jmUtils.trim(offset);
|
|
1698
|
+
if(offset.endsWith('%')) {
|
|
1699
|
+
return parseFloat(offset) / 100;
|
|
1700
|
+
}
|
|
1701
|
+
const num = parseFloat(offset);
|
|
1702
|
+
if(isNaN(num)) return null;
|
|
1703
|
+
if(num > 1) {
|
|
1704
|
+
return num / 100;
|
|
1705
|
+
}
|
|
1706
|
+
return num;
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
/**
|
|
1710
|
+
* 检查字符串是否包含角度单位
|
|
1711
|
+
* @param {string} str 待检查字符串
|
|
1712
|
+
* @returns {boolean}
|
|
1713
|
+
*/
|
|
1714
|
+
_hasAngleUnit(str) {
|
|
1715
|
+
return /^-?\d+(\.\d+)?\s*(deg|rad|grad|turn)$/i.test(str);
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
/**
|
|
1719
|
+
* 解析角度值
|
|
1720
|
+
* @param {string} angleStr 角度字符串
|
|
1721
|
+
* @returns {number} 弧度值
|
|
1722
|
+
*/
|
|
1723
|
+
_parseAngle(angleStr) {
|
|
1724
|
+
angleStr = jmUtils.trim(angleStr);
|
|
1725
|
+
const match = angleStr.match(/^(-?\d+(\.\d+)?)\s*(deg|rad|grad|turn)?$/i);
|
|
1726
|
+
if(!match) return 0;
|
|
1727
|
+
|
|
1728
|
+
let value = parseFloat(match[1]);
|
|
1729
|
+
const unit = (match[3] || 'deg').toLowerCase();
|
|
1730
|
+
|
|
1731
|
+
switch(unit) {
|
|
1732
|
+
case 'deg':
|
|
1733
|
+
return value * Math.PI / 180;
|
|
1734
|
+
case 'rad':
|
|
1735
|
+
return value;
|
|
1736
|
+
case 'grad':
|
|
1737
|
+
return value * Math.PI / 200;
|
|
1738
|
+
case 'turn':
|
|
1739
|
+
return value * 2 * Math.PI;
|
|
1740
|
+
default:
|
|
1741
|
+
return value * Math.PI / 180;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
/**
|
|
1746
|
+
* 将角度转换为起点和终点坐标
|
|
1747
|
+
* @param {number} angle 弧度值
|
|
1748
|
+
* @returns {object} 坐标对象
|
|
1749
|
+
*/
|
|
1750
|
+
_angleToCoords(angle) {
|
|
1751
|
+
const x = Math.cos(angle);
|
|
1752
|
+
const y = -Math.sin(angle);
|
|
1753
|
+
|
|
1754
|
+
return {
|
|
1755
|
+
x1: Math.round((0.5 - x * 0.5) * 1000) / 1000,
|
|
1756
|
+
y1: Math.round((0.5 + y * 0.5) * 1000) / 1000,
|
|
1757
|
+
x2: Math.round((0.5 + x * 0.5) * 1000) / 1000,
|
|
1758
|
+
y2: Math.round((0.5 - y * 0.5) * 1000) / 1000
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
/**
|
|
1763
|
+
* 将方向关键词转换为角度和坐标
|
|
1764
|
+
* @param {string} direction 方向描述
|
|
1765
|
+
* @returns {object} 包含angle和坐标的对象
|
|
1766
|
+
*/
|
|
1767
|
+
_directionToAngle(direction) {
|
|
1768
|
+
const directions = {
|
|
1769
|
+
'to top': { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' },
|
|
1770
|
+
'to bottom': { angle: Math.PI, x1: '50%', y1: '0%', x2: '50%', y2: '100%' },
|
|
1771
|
+
'to left': { angle: -Math.PI / 2, x1: '100%', y1: '50%', x2: '0%', y2: '50%' },
|
|
1772
|
+
'to right': { angle: Math.PI / 2, x1: '0%', y1: '50%', x2: '100%', y2: '50%' },
|
|
1773
|
+
'to top left': { angle: -Math.PI * 3 / 4, x1: '100%', y1: '100%', x2: '0%', y2: '0%' },
|
|
1774
|
+
'to top right': { angle: -Math.PI / 4, x1: '0%', y1: '100%', x2: '100%', y2: '0%' },
|
|
1775
|
+
'to bottom left': { angle: Math.PI * 3 / 4, x1: '100%', y1: '0%', x2: '0%', y2: '100%' },
|
|
1776
|
+
'to bottom right': { angle: Math.PI / 4, x1: '0%', y1: '0%', x2: '100%', y2: '100%' },
|
|
1777
|
+
'top': { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' },
|
|
1778
|
+
'bottom': { angle: Math.PI, x1: '50%', y1: '0%', x2: '50%', y2: '100%' },
|
|
1779
|
+
'left': { angle: -Math.PI / 2, x1: '100%', y1: '50%', x2: '0%', y2: '50%' },
|
|
1780
|
+
'right': { angle: Math.PI / 2, x1: '0%', y1: '50%', x2: '100%', y2: '50%' }
|
|
1781
|
+
};
|
|
1782
|
+
|
|
1783
|
+
const dir = directions[direction];
|
|
1784
|
+
if(dir) {
|
|
1785
|
+
return dir;
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
const keywordMatch = direction.match(/to\s+(top|bottom|left|right)/i);
|
|
1789
|
+
if(keywordMatch) {
|
|
1790
|
+
const mainDir = keywordMatch[1].toLowerCase();
|
|
1791
|
+
const secDir = direction.replace(keywordMatch[0], '').trim();
|
|
1792
|
+
if(secDir) {
|
|
1793
|
+
const combined = `to ${mainDir} ${secDir}`;
|
|
1794
|
+
if(directions[combined]) {
|
|
1795
|
+
return directions[combined];
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1233
1798
|
}
|
|
1799
|
+
|
|
1800
|
+
return { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' };
|
|
1234
1801
|
}
|
|
1235
1802
|
|
|
1236
1803
|
/**
|