@idraw/util 0.2.0-alpha.9 → 0.3.0-0.3.0-beta.6.0
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/LICENSE +1 -1
- package/dist/esm/index.d.ts +71 -0
- package/dist/esm/index.js +29 -0
- package/dist/esm/lib/check.d.ts +17 -0
- package/dist/esm/lib/check.js +115 -0
- package/dist/esm/lib/color.d.ts +3 -0
- package/dist/esm/lib/color.js +9 -0
- package/dist/esm/lib/context.d.ts +80 -0
- package/dist/esm/lib/context.js +194 -0
- package/dist/esm/lib/data.d.ts +1 -0
- package/dist/esm/lib/data.js +27 -0
- package/dist/esm/lib/file.d.ts +6 -0
- package/dist/esm/lib/file.js +10 -0
- package/dist/esm/lib/is.d.ts +45 -0
- package/dist/esm/lib/is.js +81 -0
- package/dist/esm/lib/istype.d.ts +13 -0
- package/dist/esm/lib/istype.js +39 -0
- package/dist/esm/lib/loader.d.ts +6 -0
- package/dist/esm/lib/loader.js +41 -0
- package/dist/esm/lib/parser.d.ts +5 -0
- package/dist/esm/lib/parser.js +41 -0
- package/dist/esm/lib/time.d.ts +5 -0
- package/dist/esm/lib/time.js +38 -0
- package/dist/esm/lib/uuid.d.ts +1 -0
- package/dist/esm/lib/uuid.js +6 -0
- package/dist/index.global.js +710 -284
- package/dist/index.global.min.js +1 -0
- package/package.json +10 -9
- package/dist/index.cjs.js +0 -285
- package/dist/index.d.ts +0 -75
- package/dist/index.es.js +0 -283
package/dist/index.global.js
CHANGED
|
@@ -1,288 +1,714 @@
|
|
|
1
|
-
var iDrawUtil =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
},
|
|
134
|
-
null: function (data) {
|
|
135
|
-
return parsePrototype(data) === 'Null';
|
|
136
|
-
},
|
|
137
|
-
promise: function (data) {
|
|
138
|
-
return parsePrototype(data) === 'Promise';
|
|
139
|
-
},
|
|
1
|
+
var iDrawUtil = function(exports) {
|
|
2
|
+
"use strict";
|
|
3
|
+
function compose(middleware) {
|
|
4
|
+
return function(context, next) {
|
|
5
|
+
return dispatch(0);
|
|
6
|
+
function dispatch(i) {
|
|
7
|
+
let fn = middleware[i];
|
|
8
|
+
if (i === middleware.length && next) {
|
|
9
|
+
fn = next;
|
|
10
|
+
}
|
|
11
|
+
if (!fn)
|
|
12
|
+
return Promise.resolve();
|
|
13
|
+
try {
|
|
14
|
+
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
|
|
15
|
+
} catch (err) {
|
|
16
|
+
return Promise.reject(err);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function delay(time) {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
resolve();
|
|
25
|
+
}, time);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function throttle(fn, timeout) {
|
|
29
|
+
let timer = -1;
|
|
30
|
+
return function(...args) {
|
|
31
|
+
if (timer > 0) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
timer = setTimeout(() => {
|
|
35
|
+
fn(...args);
|
|
36
|
+
timer = -1;
|
|
37
|
+
}, timeout);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function downloadImageFromCanvas(canvas, opts) {
|
|
41
|
+
const { filename, type = "image/jpeg" } = opts;
|
|
42
|
+
const stream = canvas.toDataURL(type);
|
|
43
|
+
const downloadLink = document.createElement("a");
|
|
44
|
+
downloadLink.href = stream;
|
|
45
|
+
downloadLink.download = filename;
|
|
46
|
+
const downloadClickEvent = document.createEvent("MouseEvents");
|
|
47
|
+
downloadClickEvent.initEvent("click", true, false);
|
|
48
|
+
downloadLink.dispatchEvent(downloadClickEvent);
|
|
49
|
+
}
|
|
50
|
+
function toColorHexNum(color2) {
|
|
51
|
+
return parseInt(color2.replace(/^\#/, "0x"));
|
|
52
|
+
}
|
|
53
|
+
function toColorHexStr(color2) {
|
|
54
|
+
return "#" + color2.toString(16);
|
|
55
|
+
}
|
|
56
|
+
function isColorStr(color2) {
|
|
57
|
+
return typeof color2 === "string" && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color2);
|
|
58
|
+
}
|
|
59
|
+
function createUUID() {
|
|
60
|
+
function str4() {
|
|
61
|
+
return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1);
|
|
62
|
+
}
|
|
63
|
+
return `${str4()}${str4()}-${str4()}-${str4()}-${str4()}-${str4()}${str4()}${str4()}`;
|
|
64
|
+
}
|
|
65
|
+
function deepClone(target) {
|
|
66
|
+
function _clone(t) {
|
|
67
|
+
const type = is$1(t);
|
|
68
|
+
if (["Null", "Number", "String", "Boolean", "Undefined"].indexOf(type) >= 0) {
|
|
69
|
+
return t;
|
|
70
|
+
} else if (type === "Array") {
|
|
71
|
+
const arr = [];
|
|
72
|
+
t.forEach((item) => {
|
|
73
|
+
arr.push(_clone(item));
|
|
74
|
+
});
|
|
75
|
+
return arr;
|
|
76
|
+
} else if (type === "Object") {
|
|
77
|
+
const obj = {};
|
|
78
|
+
const keys = Object.keys(t);
|
|
79
|
+
keys.forEach((key) => {
|
|
80
|
+
obj[key] = _clone(t[key]);
|
|
81
|
+
});
|
|
82
|
+
return obj;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return _clone(target);
|
|
86
|
+
}
|
|
87
|
+
function is$1(data) {
|
|
88
|
+
return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, "").split(" ")[1];
|
|
89
|
+
}
|
|
90
|
+
function parsePrototype(data) {
|
|
91
|
+
const typeStr = Object.prototype.toString.call(data) || "";
|
|
92
|
+
const result = typeStr.replace(/(\[object|\])/ig, "").trim();
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
const istype = {
|
|
96
|
+
type(data, lowerCase) {
|
|
97
|
+
const result = parsePrototype(data);
|
|
98
|
+
return lowerCase === true ? result.toLocaleLowerCase() : result;
|
|
99
|
+
},
|
|
100
|
+
array(data) {
|
|
101
|
+
return parsePrototype(data) === "Array";
|
|
102
|
+
},
|
|
103
|
+
json(data) {
|
|
104
|
+
return parsePrototype(data) === "Object";
|
|
105
|
+
},
|
|
106
|
+
function(data) {
|
|
107
|
+
return parsePrototype(data) === "Function";
|
|
108
|
+
},
|
|
109
|
+
asyncFunction(data) {
|
|
110
|
+
return parsePrototype(data) === "AsyncFunction";
|
|
111
|
+
},
|
|
112
|
+
string(data) {
|
|
113
|
+
return parsePrototype(data) === "String";
|
|
114
|
+
},
|
|
115
|
+
number(data) {
|
|
116
|
+
return parsePrototype(data) === "Number";
|
|
117
|
+
},
|
|
118
|
+
undefined(data) {
|
|
119
|
+
return parsePrototype(data) === "Undefined";
|
|
120
|
+
},
|
|
121
|
+
null(data) {
|
|
122
|
+
return parsePrototype(data) === "Null";
|
|
123
|
+
},
|
|
124
|
+
promise(data) {
|
|
125
|
+
return parsePrototype(data) === "Promise";
|
|
126
|
+
}
|
|
127
|
+
// nodeList (data: any): boolean {
|
|
128
|
+
// return parsePrototype(data) === 'NodeList';
|
|
129
|
+
// },
|
|
130
|
+
// imageData(data: any): boolean {
|
|
131
|
+
// return parsePrototype(data) === 'ImageData';
|
|
132
|
+
// }
|
|
140
133
|
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
134
|
+
function parseHTMLToDataURL(html2, opts) {
|
|
135
|
+
const { width, height } = opts;
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
const _svg = `
|
|
138
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${width || ""}" height = "${height || ""}">
|
|
139
|
+
<foreignObject width="100%" height="100%">
|
|
140
|
+
<div xmlns = "http://www.w3.org/1999/xhtml">
|
|
141
|
+
${html2}
|
|
142
|
+
</div>
|
|
143
|
+
</foreignObject>
|
|
144
|
+
</svg>
|
|
145
|
+
`;
|
|
146
|
+
const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
|
|
147
|
+
const reader = new FileReader();
|
|
148
|
+
reader.readAsDataURL(blob);
|
|
149
|
+
reader.onload = function(event) {
|
|
150
|
+
var _a;
|
|
151
|
+
const base64 = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result;
|
|
152
|
+
resolve(base64);
|
|
153
|
+
};
|
|
154
|
+
reader.onerror = function(err) {
|
|
155
|
+
reject(err);
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function parseSVGToDataURL(svg2) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
const _svg = svg2;
|
|
162
|
+
const blob = new Blob([_svg], { type: "image/svg+xml;charset=utf-8" });
|
|
163
|
+
const reader = new FileReader();
|
|
164
|
+
reader.readAsDataURL(blob);
|
|
165
|
+
reader.onload = function(event) {
|
|
166
|
+
var _a;
|
|
167
|
+
const base64 = (_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result;
|
|
168
|
+
resolve(base64);
|
|
169
|
+
};
|
|
170
|
+
reader.onerror = function(err) {
|
|
171
|
+
reject(err);
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const { Image } = window;
|
|
176
|
+
function loadImage(src) {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
const img = new Image();
|
|
179
|
+
img.crossOrigin = "anonymous";
|
|
180
|
+
img.onload = function() {
|
|
181
|
+
resolve(img);
|
|
182
|
+
};
|
|
183
|
+
img.onabort = reject;
|
|
184
|
+
img.onerror = reject;
|
|
185
|
+
img.src = src;
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
async function loadSVG(svg2) {
|
|
189
|
+
const dataURL = await parseSVGToDataURL(svg2);
|
|
190
|
+
const image = await loadImage(dataURL);
|
|
191
|
+
return image;
|
|
192
|
+
}
|
|
193
|
+
function filterAmpersand(str) {
|
|
194
|
+
return str.replace(/\&/ig, "&");
|
|
195
|
+
}
|
|
196
|
+
async function loadHTML(html2, opts) {
|
|
197
|
+
html2 = filterAmpersand(html2);
|
|
198
|
+
const dataURL = await parseHTMLToDataURL(html2, opts);
|
|
199
|
+
const image = await loadImage(dataURL);
|
|
200
|
+
return image;
|
|
201
|
+
}
|
|
202
|
+
class Context {
|
|
203
|
+
// private _scale: number;
|
|
204
|
+
// private _scrollX: number;
|
|
205
|
+
// private _scrollY: number;
|
|
206
|
+
constructor(ctx, opts) {
|
|
207
|
+
this._opts = opts;
|
|
208
|
+
this._ctx = ctx;
|
|
209
|
+
this._transform = {
|
|
210
|
+
scale: 1,
|
|
211
|
+
scrollX: 0,
|
|
212
|
+
scrollY: 0
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
getContext() {
|
|
216
|
+
return this._ctx;
|
|
217
|
+
}
|
|
218
|
+
resetSize(opts) {
|
|
219
|
+
this._opts = { ...this._opts, ...opts };
|
|
220
|
+
}
|
|
221
|
+
calcDeviceNum(num) {
|
|
222
|
+
return num * this._opts.devicePixelRatio;
|
|
223
|
+
}
|
|
224
|
+
calcScreenNum(num) {
|
|
225
|
+
return num / this._opts.devicePixelRatio;
|
|
226
|
+
}
|
|
227
|
+
getSize() {
|
|
228
|
+
return {
|
|
229
|
+
width: this._opts.width,
|
|
230
|
+
height: this._opts.height,
|
|
231
|
+
contextWidth: this._opts.contextWidth,
|
|
232
|
+
contextHeight: this._opts.contextHeight,
|
|
233
|
+
devicePixelRatio: this._opts.devicePixelRatio
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
setTransform(config) {
|
|
237
|
+
this._transform = { ...this._transform, ...config };
|
|
238
|
+
}
|
|
239
|
+
getTransform() {
|
|
240
|
+
return {
|
|
241
|
+
scale: this._transform.scale,
|
|
242
|
+
scrollX: this._transform.scrollX,
|
|
243
|
+
scrollY: this._transform.scrollY
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
setFillStyle(color2) {
|
|
247
|
+
this._ctx.fillStyle = color2;
|
|
248
|
+
}
|
|
249
|
+
fill(fillRule) {
|
|
250
|
+
return this._ctx.fill(fillRule || "nonzero");
|
|
251
|
+
}
|
|
252
|
+
arc(x2, y2, radius, startAngle, endAngle, anticlockwise) {
|
|
253
|
+
return this._ctx.arc(
|
|
254
|
+
this._doSize(x2),
|
|
255
|
+
this._doSize(y2),
|
|
256
|
+
this._doSize(radius),
|
|
257
|
+
startAngle,
|
|
258
|
+
endAngle,
|
|
259
|
+
anticlockwise
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
rect(x2, y2, w2, h2) {
|
|
263
|
+
return this._ctx.rect(
|
|
264
|
+
this._doSize(x2),
|
|
265
|
+
this._doSize(y2),
|
|
266
|
+
this._doSize(w2),
|
|
267
|
+
this._doSize(h2)
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
fillRect(x2, y2, w2, h2) {
|
|
271
|
+
return this._ctx.fillRect(
|
|
272
|
+
this._doSize(x2),
|
|
273
|
+
this._doSize(y2),
|
|
274
|
+
this._doSize(w2),
|
|
275
|
+
this._doSize(h2)
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
clearRect(x2, y2, w2, h2) {
|
|
279
|
+
return this._ctx.clearRect(
|
|
280
|
+
this._doSize(x2),
|
|
281
|
+
this._doSize(y2),
|
|
282
|
+
this._doSize(w2),
|
|
283
|
+
this._doSize(h2)
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
beginPath() {
|
|
287
|
+
return this._ctx.beginPath();
|
|
288
|
+
}
|
|
289
|
+
closePath() {
|
|
290
|
+
return this._ctx.closePath();
|
|
291
|
+
}
|
|
292
|
+
lineTo(x2, y2) {
|
|
293
|
+
return this._ctx.lineTo(this._doSize(x2), this._doSize(y2));
|
|
294
|
+
}
|
|
295
|
+
moveTo(x2, y2) {
|
|
296
|
+
return this._ctx.moveTo(this._doSize(x2), this._doSize(y2));
|
|
297
|
+
}
|
|
298
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
299
|
+
return this._ctx.arcTo(
|
|
300
|
+
this._doSize(x1),
|
|
301
|
+
this._doSize(y1),
|
|
302
|
+
this._doSize(x2),
|
|
303
|
+
this._doSize(y2),
|
|
304
|
+
this._doSize(radius)
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
setLineWidth(w2) {
|
|
308
|
+
return this._ctx.lineWidth = this._doSize(w2);
|
|
309
|
+
}
|
|
310
|
+
setLineDash(nums) {
|
|
311
|
+
return this._ctx.setLineDash(nums.map((n) => this._doSize(n)));
|
|
312
|
+
}
|
|
313
|
+
isPointInPath(x2, y2) {
|
|
314
|
+
return this._ctx.isPointInPath(this._doX(x2), this._doY(y2));
|
|
315
|
+
}
|
|
316
|
+
isPointInPathWithoutScroll(x2, y2) {
|
|
317
|
+
return this._ctx.isPointInPath(this._doSize(x2), this._doSize(y2));
|
|
318
|
+
}
|
|
319
|
+
setStrokeStyle(color2) {
|
|
320
|
+
this._ctx.strokeStyle = color2;
|
|
321
|
+
}
|
|
322
|
+
stroke() {
|
|
323
|
+
return this._ctx.stroke();
|
|
324
|
+
}
|
|
325
|
+
translate(x2, y2) {
|
|
326
|
+
return this._ctx.translate(this._doSize(x2), this._doSize(y2));
|
|
327
|
+
}
|
|
328
|
+
rotate(angle2) {
|
|
329
|
+
return this._ctx.rotate(angle2);
|
|
330
|
+
}
|
|
331
|
+
drawImage(...args) {
|
|
332
|
+
const image = args[0];
|
|
333
|
+
const sx = args[1];
|
|
334
|
+
const sy = args[2];
|
|
335
|
+
const sw = args[3];
|
|
336
|
+
const sh = args[4];
|
|
337
|
+
const dx = args[args.length - 4];
|
|
338
|
+
const dy = args[args.length - 3];
|
|
339
|
+
const dw = args[args.length - 2];
|
|
340
|
+
const dh = args[args.length - 1];
|
|
341
|
+
if (args.length === 9) {
|
|
342
|
+
return this._ctx.drawImage(
|
|
343
|
+
image,
|
|
344
|
+
this._doSize(sx),
|
|
345
|
+
this._doSize(sy),
|
|
346
|
+
this._doSize(sw),
|
|
347
|
+
this._doSize(sh),
|
|
348
|
+
this._doSize(dx),
|
|
349
|
+
this._doSize(dy),
|
|
350
|
+
this._doSize(dw),
|
|
351
|
+
this._doSize(dh)
|
|
352
|
+
);
|
|
353
|
+
} else {
|
|
354
|
+
return this._ctx.drawImage(
|
|
355
|
+
image,
|
|
356
|
+
this._doSize(dx),
|
|
357
|
+
this._doSize(dy),
|
|
358
|
+
this._doSize(dw),
|
|
359
|
+
this._doSize(dh)
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
createPattern(image, repetition) {
|
|
364
|
+
return this._ctx.createPattern(image, repetition);
|
|
365
|
+
}
|
|
366
|
+
measureText(text2) {
|
|
367
|
+
return this._ctx.measureText(text2);
|
|
368
|
+
}
|
|
369
|
+
setTextAlign(align) {
|
|
370
|
+
this._ctx.textAlign = align;
|
|
371
|
+
}
|
|
372
|
+
fillText(text2, x2, y2, maxWidth) {
|
|
373
|
+
if (maxWidth !== void 0) {
|
|
374
|
+
return this._ctx.fillText(
|
|
375
|
+
text2,
|
|
376
|
+
this._doSize(x2),
|
|
377
|
+
this._doSize(y2),
|
|
378
|
+
this._doSize(maxWidth)
|
|
379
|
+
);
|
|
380
|
+
} else {
|
|
381
|
+
return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2));
|
|
176
382
|
}
|
|
383
|
+
}
|
|
384
|
+
strokeText(text2, x2, y2, maxWidth) {
|
|
385
|
+
if (maxWidth !== void 0) {
|
|
386
|
+
return this._ctx.strokeText(
|
|
387
|
+
text2,
|
|
388
|
+
this._doSize(x2),
|
|
389
|
+
this._doSize(y2),
|
|
390
|
+
this._doSize(maxWidth)
|
|
391
|
+
);
|
|
392
|
+
} else {
|
|
393
|
+
return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2));
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
setFont(opts) {
|
|
397
|
+
const strList = [];
|
|
398
|
+
if (opts.fontWeight === "bold") {
|
|
399
|
+
strList.push(`${opts.fontWeight}`);
|
|
400
|
+
}
|
|
401
|
+
strList.push(`${this._doSize(opts.fontSize || 12)}px`);
|
|
402
|
+
strList.push(`${opts.fontFamily || "sans-serif"}`);
|
|
403
|
+
this._ctx.font = `${strList.join(" ")}`;
|
|
404
|
+
}
|
|
405
|
+
setTextBaseline(baseline) {
|
|
406
|
+
this._ctx.textBaseline = baseline;
|
|
407
|
+
}
|
|
408
|
+
setGlobalAlpha(alpha) {
|
|
409
|
+
this._ctx.globalAlpha = alpha;
|
|
410
|
+
}
|
|
411
|
+
save() {
|
|
412
|
+
this._ctx.save();
|
|
413
|
+
}
|
|
414
|
+
restore() {
|
|
415
|
+
this._ctx.restore();
|
|
416
|
+
}
|
|
417
|
+
scale(ratioX, ratioY) {
|
|
418
|
+
this._ctx.scale(ratioX, ratioY);
|
|
419
|
+
}
|
|
420
|
+
setShadowColor(color2) {
|
|
421
|
+
this._ctx.shadowColor = color2;
|
|
422
|
+
}
|
|
423
|
+
setShadowOffsetX(offsetX) {
|
|
424
|
+
this._ctx.shadowOffsetX = this._doSize(offsetX);
|
|
425
|
+
}
|
|
426
|
+
setShadowOffsetY(offsetY) {
|
|
427
|
+
this._ctx.shadowOffsetY = this._doSize(offsetY);
|
|
428
|
+
}
|
|
429
|
+
setShadowBlur(blur) {
|
|
430
|
+
this._ctx.shadowBlur = this._doSize(blur);
|
|
431
|
+
}
|
|
432
|
+
ellipse(x2, y2, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
433
|
+
this._ctx.ellipse(
|
|
434
|
+
this._doSize(x2),
|
|
435
|
+
this._doSize(y2),
|
|
436
|
+
this._doSize(radiusX),
|
|
437
|
+
this._doSize(radiusY),
|
|
438
|
+
rotation,
|
|
439
|
+
startAngle,
|
|
440
|
+
endAngle,
|
|
441
|
+
counterclockwise
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
_doSize(num) {
|
|
445
|
+
return this._opts.devicePixelRatio * num;
|
|
446
|
+
}
|
|
447
|
+
_doX(x2) {
|
|
448
|
+
const { scale, scrollX } = this._transform;
|
|
449
|
+
const _x = (x2 - scrollX) / scale;
|
|
450
|
+
return this._doSize(_x);
|
|
451
|
+
}
|
|
452
|
+
_doY(y2) {
|
|
453
|
+
const { scale, scrollY } = this._transform;
|
|
454
|
+
const _y = (y2 - scrollY) / scale;
|
|
455
|
+
return this._doSize(_y);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
function number(value) {
|
|
459
|
+
return typeof value === "number" && (value > 0 || value <= 0);
|
|
460
|
+
}
|
|
461
|
+
function x(value) {
|
|
462
|
+
return number(value);
|
|
463
|
+
}
|
|
464
|
+
function y(value) {
|
|
465
|
+
return number(value);
|
|
466
|
+
}
|
|
467
|
+
function w(value) {
|
|
468
|
+
return typeof value === "number" && value >= 0;
|
|
469
|
+
}
|
|
470
|
+
function h(value) {
|
|
471
|
+
return typeof value === "number" && value >= 0;
|
|
177
472
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
function
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
473
|
+
function angle(value) {
|
|
474
|
+
return typeof value === "number" && value >= -360 && value <= 360;
|
|
475
|
+
}
|
|
476
|
+
function borderWidth(value) {
|
|
477
|
+
return w(value);
|
|
478
|
+
}
|
|
479
|
+
function borderRadius(value) {
|
|
480
|
+
return number(value) && value >= 0;
|
|
481
|
+
}
|
|
482
|
+
function color(value) {
|
|
483
|
+
return isColorStr(value);
|
|
484
|
+
}
|
|
485
|
+
function imageURL(value) {
|
|
486
|
+
return typeof value === "string" && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`);
|
|
487
|
+
}
|
|
488
|
+
function imageBase64(value) {
|
|
489
|
+
return typeof value === "string" && /^(data:image\/)/.test(`${value}`);
|
|
490
|
+
}
|
|
491
|
+
function imageSrc(value) {
|
|
492
|
+
return imageBase64(value) || imageURL(value);
|
|
493
|
+
}
|
|
494
|
+
function svg(value) {
|
|
495
|
+
return typeof value === "string" && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim());
|
|
496
|
+
}
|
|
497
|
+
function html(value) {
|
|
498
|
+
let result = false;
|
|
499
|
+
if (typeof value === "string") {
|
|
500
|
+
let div = document.createElement("div");
|
|
501
|
+
div.innerHTML = value;
|
|
502
|
+
if (div.children.length > 0) {
|
|
503
|
+
result = true;
|
|
504
|
+
}
|
|
505
|
+
div = null;
|
|
506
|
+
}
|
|
507
|
+
return result;
|
|
508
|
+
}
|
|
509
|
+
function text(value) {
|
|
510
|
+
return typeof value === "string";
|
|
511
|
+
}
|
|
512
|
+
function fontSize(value) {
|
|
513
|
+
return number(value) && value > 0;
|
|
514
|
+
}
|
|
515
|
+
function lineHeight(value) {
|
|
516
|
+
return number(value) && value > 0;
|
|
517
|
+
}
|
|
518
|
+
function strokeWidth(value) {
|
|
519
|
+
return number(value) && value > 0;
|
|
520
|
+
}
|
|
521
|
+
function textAlign(value) {
|
|
522
|
+
return ["center", "left", "right"].includes(value);
|
|
523
|
+
}
|
|
524
|
+
function fontFamily(value) {
|
|
525
|
+
return typeof value === "string" && value.length > 0;
|
|
526
|
+
}
|
|
527
|
+
function fontWeight(value) {
|
|
528
|
+
return ["bold"].includes(value);
|
|
529
|
+
}
|
|
530
|
+
const is = {
|
|
531
|
+
x,
|
|
532
|
+
y,
|
|
533
|
+
w,
|
|
534
|
+
h,
|
|
535
|
+
angle,
|
|
536
|
+
number,
|
|
537
|
+
borderWidth,
|
|
538
|
+
borderRadius,
|
|
539
|
+
color,
|
|
540
|
+
imageSrc,
|
|
541
|
+
imageURL,
|
|
542
|
+
imageBase64,
|
|
543
|
+
svg,
|
|
544
|
+
html,
|
|
545
|
+
text,
|
|
546
|
+
fontSize,
|
|
547
|
+
lineHeight,
|
|
548
|
+
textAlign,
|
|
549
|
+
fontFamily,
|
|
550
|
+
fontWeight,
|
|
551
|
+
strokeWidth
|
|
552
|
+
};
|
|
553
|
+
function attrs(attrs2) {
|
|
554
|
+
const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = attrs2;
|
|
555
|
+
if (!(is.x(x2) && is.y(y2) && is.w(w2) && is.h(h2) && is.angle(angle2))) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
if (!(angle2 >= -360 && angle2 <= 360)) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
function box(desc = {}) {
|
|
564
|
+
const { borderColor, borderRadius: borderRadius2, borderWidth: borderWidth2 } = desc;
|
|
565
|
+
if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
|
|
566
|
+
return false;
|
|
567
|
+
}
|
|
568
|
+
if (desc.hasOwnProperty("borderRadius") && !is.number(borderRadius2)) {
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
function rectDesc(desc) {
|
|
577
|
+
const { bgColor } = desc;
|
|
578
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
if (!box(desc)) {
|
|
582
|
+
return false;
|
|
583
|
+
}
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
function circleDesc(desc) {
|
|
587
|
+
const { bgColor, borderColor, borderWidth: borderWidth2 } = desc;
|
|
588
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
|
|
595
|
+
return false;
|
|
596
|
+
}
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
function imageDesc(desc) {
|
|
600
|
+
const { src } = desc;
|
|
601
|
+
if (!is.imageSrc(src)) {
|
|
602
|
+
return false;
|
|
603
|
+
}
|
|
604
|
+
return true;
|
|
605
|
+
}
|
|
606
|
+
function svgDesc(desc) {
|
|
607
|
+
const { svg: svg2 } = desc;
|
|
608
|
+
if (!is.svg(svg2)) {
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
function htmlDesc(desc) {
|
|
614
|
+
const { html: html2 } = desc;
|
|
615
|
+
if (!is.html(html2)) {
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
return true;
|
|
619
|
+
}
|
|
620
|
+
function textDesc(desc) {
|
|
621
|
+
const {
|
|
622
|
+
text: text2,
|
|
623
|
+
color: color2,
|
|
624
|
+
fontSize: fontSize2,
|
|
625
|
+
lineHeight: lineHeight2,
|
|
626
|
+
fontFamily: fontFamily2,
|
|
627
|
+
textAlign: textAlign2,
|
|
628
|
+
fontWeight: fontWeight2,
|
|
629
|
+
bgColor,
|
|
630
|
+
strokeWidth: strokeWidth2,
|
|
631
|
+
strokeColor
|
|
632
|
+
} = desc;
|
|
633
|
+
if (!is.text(text2)) {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
if (!is.color(color2)) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
if (!is.fontSize(fontSize2)) {
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
if (desc.hasOwnProperty("fontWeight") && !is.fontWeight(fontWeight2)) {
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
if (desc.hasOwnProperty("lineHeight") && !is.lineHeight(lineHeight2)) {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
if (desc.hasOwnProperty("fontFamily") && !is.fontFamily(fontFamily2)) {
|
|
652
|
+
return false;
|
|
653
|
+
}
|
|
654
|
+
if (desc.hasOwnProperty("textAlign") && !is.textAlign(textAlign2)) {
|
|
655
|
+
return false;
|
|
656
|
+
}
|
|
657
|
+
if (desc.hasOwnProperty("strokeWidth") && !is.strokeWidth(strokeWidth2)) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
if (desc.hasOwnProperty("strokeColor") && !is.color(strokeColor)) {
|
|
661
|
+
return false;
|
|
662
|
+
}
|
|
663
|
+
if (!box(desc)) {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
const check = {
|
|
669
|
+
attrs,
|
|
670
|
+
textDesc,
|
|
671
|
+
rectDesc,
|
|
672
|
+
circleDesc,
|
|
673
|
+
imageDesc,
|
|
674
|
+
svgDesc,
|
|
675
|
+
htmlDesc
|
|
676
|
+
};
|
|
677
|
+
const index = {
|
|
678
|
+
is,
|
|
679
|
+
check,
|
|
680
|
+
delay,
|
|
681
|
+
compose,
|
|
682
|
+
throttle,
|
|
683
|
+
loadImage,
|
|
684
|
+
loadSVG,
|
|
685
|
+
loadHTML,
|
|
686
|
+
downloadImageFromCanvas,
|
|
687
|
+
toColorHexStr,
|
|
688
|
+
toColorHexNum,
|
|
689
|
+
isColorStr,
|
|
690
|
+
createUUID,
|
|
691
|
+
istype,
|
|
692
|
+
deepClone,
|
|
693
|
+
Context
|
|
284
694
|
};
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
695
|
+
exports.Context = Context;
|
|
696
|
+
exports.check = check;
|
|
697
|
+
exports.compose = compose;
|
|
698
|
+
exports.createUUID = createUUID;
|
|
699
|
+
exports.deepClone = deepClone;
|
|
700
|
+
exports.default = index;
|
|
701
|
+
exports.delay = delay;
|
|
702
|
+
exports.downloadImageFromCanvas = downloadImageFromCanvas;
|
|
703
|
+
exports.is = is;
|
|
704
|
+
exports.isColorStr = isColorStr;
|
|
705
|
+
exports.istype = istype;
|
|
706
|
+
exports.loadHTML = loadHTML;
|
|
707
|
+
exports.loadImage = loadImage;
|
|
708
|
+
exports.loadSVG = loadSVG;
|
|
709
|
+
exports.throttle = throttle;
|
|
710
|
+
exports.toColorHexNum = toColorHexNum;
|
|
711
|
+
exports.toColorHexStr = toColorHexStr;
|
|
712
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
713
|
+
return exports;
|
|
714
|
+
}({});
|