@idraw/util 0.3.0-alpha.6 → 0.3.0-alpha.8
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/esm/index.d.ts +71 -0
- package/{esm → dist/esm}/index.js +19 -1
- 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 +655 -701
- package/dist/index.global.min.js +1 -1
- package/package.json +8 -13
- package/dist/index.cjs.js +0 -704
- package/dist/index.d.ts +0 -222
- package/dist/index.esm.js +0 -702
- package/esm/default.d.ts +0 -1
- package/esm/default.js +0 -2
- package/esm/esm.d.ts +0 -2
- package/esm/esm.js +0 -3
- package/esm/index.d.ts +0 -11
package/dist/index.global.js
CHANGED
|
@@ -1,707 +1,661 @@
|
|
|
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
|
-
|
|
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);
|
|
149
153
|
};
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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(this._doSize(x2), this._doSize(y2), this._doSize(radius), startAngle, endAngle, anticlockwise);
|
|
254
|
+
}
|
|
255
|
+
rect(x2, y2, w2, h2) {
|
|
256
|
+
return this._ctx.rect(this._doSize(x2), this._doSize(y2), this._doSize(w2), this._doSize(h2));
|
|
257
|
+
}
|
|
258
|
+
fillRect(x2, y2, w2, h2) {
|
|
259
|
+
return this._ctx.fillRect(
|
|
260
|
+
this._doSize(x2),
|
|
261
|
+
this._doSize(y2),
|
|
262
|
+
this._doSize(w2),
|
|
263
|
+
this._doSize(h2)
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
clearRect(x2, y2, w2, h2) {
|
|
267
|
+
return this._ctx.clearRect(
|
|
268
|
+
this._doSize(x2),
|
|
269
|
+
this._doSize(y2),
|
|
270
|
+
this._doSize(w2),
|
|
271
|
+
this._doSize(h2)
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
beginPath() {
|
|
275
|
+
return this._ctx.beginPath();
|
|
276
|
+
}
|
|
277
|
+
closePath() {
|
|
278
|
+
return this._ctx.closePath();
|
|
279
|
+
}
|
|
280
|
+
lineTo(x2, y2) {
|
|
281
|
+
return this._ctx.lineTo(this._doSize(x2), this._doSize(y2));
|
|
282
|
+
}
|
|
283
|
+
moveTo(x2, y2) {
|
|
284
|
+
return this._ctx.moveTo(this._doSize(x2), this._doSize(y2));
|
|
285
|
+
}
|
|
286
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
287
|
+
return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
|
|
288
|
+
}
|
|
289
|
+
setLineWidth(w2) {
|
|
290
|
+
return this._ctx.lineWidth = this._doSize(w2);
|
|
291
|
+
}
|
|
292
|
+
setLineDash(nums) {
|
|
293
|
+
return this._ctx.setLineDash(nums.map((n) => this._doSize(n)));
|
|
294
|
+
}
|
|
295
|
+
isPointInPath(x2, y2) {
|
|
296
|
+
return this._ctx.isPointInPath(this._doX(x2), this._doY(y2));
|
|
297
|
+
}
|
|
298
|
+
isPointInPathWithoutScroll(x2, y2) {
|
|
299
|
+
return this._ctx.isPointInPath(this._doSize(x2), this._doSize(y2));
|
|
300
|
+
}
|
|
301
|
+
setStrokeStyle(color2) {
|
|
302
|
+
this._ctx.strokeStyle = color2;
|
|
303
|
+
}
|
|
304
|
+
stroke() {
|
|
305
|
+
return this._ctx.stroke();
|
|
306
|
+
}
|
|
307
|
+
translate(x2, y2) {
|
|
308
|
+
return this._ctx.translate(this._doSize(x2), this._doSize(y2));
|
|
309
|
+
}
|
|
310
|
+
rotate(angle2) {
|
|
311
|
+
return this._ctx.rotate(angle2);
|
|
312
|
+
}
|
|
313
|
+
drawImage(...args) {
|
|
314
|
+
const image = args[0];
|
|
315
|
+
const sx = args[1];
|
|
316
|
+
const sy = args[2];
|
|
317
|
+
const sw = args[3];
|
|
318
|
+
const sh = args[4];
|
|
319
|
+
const dx = args[args.length - 4];
|
|
320
|
+
const dy = args[args.length - 3];
|
|
321
|
+
const dw = args[args.length - 2];
|
|
322
|
+
const dh = args[args.length - 1];
|
|
323
|
+
if (args.length === 9) {
|
|
324
|
+
return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
|
|
325
|
+
} else {
|
|
326
|
+
return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
createPattern(image, repetition) {
|
|
330
|
+
return this._ctx.createPattern(image, repetition);
|
|
331
|
+
}
|
|
332
|
+
measureText(text2) {
|
|
333
|
+
return this._ctx.measureText(text2);
|
|
334
|
+
}
|
|
335
|
+
setTextAlign(align) {
|
|
336
|
+
this._ctx.textAlign = align;
|
|
337
|
+
}
|
|
338
|
+
fillText(text2, x2, y2, maxWidth) {
|
|
339
|
+
if (maxWidth !== void 0) {
|
|
340
|
+
return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
|
|
341
|
+
} else {
|
|
342
|
+
return this._ctx.fillText(text2, this._doSize(x2), this._doSize(y2));
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
strokeText(text2, x2, y2, maxWidth) {
|
|
346
|
+
if (maxWidth !== void 0) {
|
|
347
|
+
return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2), this._doSize(maxWidth));
|
|
348
|
+
} else {
|
|
349
|
+
return this._ctx.strokeText(text2, this._doSize(x2), this._doSize(y2));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
setFont(opts) {
|
|
353
|
+
const strList = [];
|
|
354
|
+
if (opts.fontWeight === "bold") {
|
|
355
|
+
strList.push(`${opts.fontWeight}`);
|
|
186
356
|
}
|
|
357
|
+
strList.push(`${this._doSize(opts.fontSize || 12)}px`);
|
|
358
|
+
strList.push(`${opts.fontFamily || "sans-serif"}`);
|
|
359
|
+
this._ctx.font = `${strList.join(" ")}`;
|
|
360
|
+
}
|
|
361
|
+
setTextBaseline(baseline) {
|
|
362
|
+
this._ctx.textBaseline = baseline;
|
|
363
|
+
}
|
|
364
|
+
setGlobalAlpha(alpha) {
|
|
365
|
+
this._ctx.globalAlpha = alpha;
|
|
366
|
+
}
|
|
367
|
+
save() {
|
|
368
|
+
this._ctx.save();
|
|
369
|
+
}
|
|
370
|
+
restore() {
|
|
371
|
+
this._ctx.restore();
|
|
372
|
+
}
|
|
373
|
+
scale(ratioX, ratioY) {
|
|
374
|
+
this._ctx.scale(ratioX, ratioY);
|
|
375
|
+
}
|
|
376
|
+
setShadowColor(color2) {
|
|
377
|
+
this._ctx.shadowColor = color2;
|
|
378
|
+
}
|
|
379
|
+
setShadowOffsetX(offsetX) {
|
|
380
|
+
this._ctx.shadowOffsetX = this._doSize(offsetX);
|
|
381
|
+
}
|
|
382
|
+
setShadowOffsetY(offsetY) {
|
|
383
|
+
this._ctx.shadowOffsetY = this._doSize(offsetY);
|
|
384
|
+
}
|
|
385
|
+
setShadowBlur(blur) {
|
|
386
|
+
this._ctx.shadowBlur = this._doSize(blur);
|
|
387
|
+
}
|
|
388
|
+
ellipse(x2, y2, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
389
|
+
this._ctx.ellipse(this._doSize(x2), this._doSize(y2), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
|
|
390
|
+
}
|
|
391
|
+
_doSize(num) {
|
|
392
|
+
return this._opts.devicePixelRatio * num;
|
|
393
|
+
}
|
|
394
|
+
_doX(x2) {
|
|
395
|
+
const { scale, scrollX } = this._transform;
|
|
396
|
+
const _x = (x2 - scrollX) / scale;
|
|
397
|
+
return this._doSize(_x);
|
|
398
|
+
}
|
|
399
|
+
_doY(y2) {
|
|
400
|
+
const { scale, scrollY } = this._transform;
|
|
401
|
+
const _y = (y2 - scrollY) / scale;
|
|
402
|
+
return this._doSize(_y);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function number(value) {
|
|
406
|
+
return typeof value === "number" && (value > 0 || value <= 0);
|
|
407
|
+
}
|
|
408
|
+
function x(value) {
|
|
409
|
+
return number(value);
|
|
410
|
+
}
|
|
411
|
+
function y(value) {
|
|
412
|
+
return number(value);
|
|
413
|
+
}
|
|
414
|
+
function w(value) {
|
|
415
|
+
return typeof value === "number" && value >= 0;
|
|
416
|
+
}
|
|
417
|
+
function h(value) {
|
|
418
|
+
return typeof value === "number" && value >= 0;
|
|
419
|
+
}
|
|
420
|
+
function angle(value) {
|
|
421
|
+
return typeof value === "number" && value >= -360 && value <= 360;
|
|
422
|
+
}
|
|
423
|
+
function borderWidth(value) {
|
|
424
|
+
return w(value);
|
|
425
|
+
}
|
|
426
|
+
function borderRadius(value) {
|
|
427
|
+
return number(value) && value >= 0;
|
|
428
|
+
}
|
|
429
|
+
function color(value) {
|
|
430
|
+
return isColorStr(value);
|
|
431
|
+
}
|
|
432
|
+
function imageURL(value) {
|
|
433
|
+
return typeof value === "string" && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`);
|
|
434
|
+
}
|
|
435
|
+
function imageBase64(value) {
|
|
436
|
+
return typeof value === "string" && /^(data:image\/)/.test(`${value}`);
|
|
437
|
+
}
|
|
438
|
+
function imageSrc(value) {
|
|
439
|
+
return imageBase64(value) || imageURL(value);
|
|
440
|
+
}
|
|
441
|
+
function svg(value) {
|
|
442
|
+
return typeof value === "string" && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim());
|
|
443
|
+
}
|
|
444
|
+
function html(value) {
|
|
445
|
+
let result = false;
|
|
446
|
+
if (typeof value === "string") {
|
|
447
|
+
let div = document.createElement("div");
|
|
448
|
+
div.innerHTML = value;
|
|
449
|
+
if (div.children.length > 0) {
|
|
450
|
+
result = true;
|
|
451
|
+
}
|
|
452
|
+
div = null;
|
|
453
|
+
}
|
|
454
|
+
return result;
|
|
455
|
+
}
|
|
456
|
+
function text(value) {
|
|
457
|
+
return typeof value === "string";
|
|
458
|
+
}
|
|
459
|
+
function fontSize(value) {
|
|
460
|
+
return number(value) && value > 0;
|
|
461
|
+
}
|
|
462
|
+
function lineHeight(value) {
|
|
463
|
+
return number(value) && value > 0;
|
|
464
|
+
}
|
|
465
|
+
function strokeWidth(value) {
|
|
466
|
+
return number(value) && value > 0;
|
|
467
|
+
}
|
|
468
|
+
function textAlign(value) {
|
|
469
|
+
return ["center", "left", "right"].includes(value);
|
|
470
|
+
}
|
|
471
|
+
function fontFamily(value) {
|
|
472
|
+
return typeof value === "string" && value.length > 0;
|
|
473
|
+
}
|
|
474
|
+
function fontWeight(value) {
|
|
475
|
+
return ["bold"].includes(value);
|
|
476
|
+
}
|
|
477
|
+
const is = {
|
|
478
|
+
x,
|
|
479
|
+
y,
|
|
480
|
+
w,
|
|
481
|
+
h,
|
|
482
|
+
angle,
|
|
483
|
+
number,
|
|
484
|
+
borderWidth,
|
|
485
|
+
borderRadius,
|
|
486
|
+
color,
|
|
487
|
+
imageSrc,
|
|
488
|
+
imageURL,
|
|
489
|
+
imageBase64,
|
|
490
|
+
svg,
|
|
491
|
+
html,
|
|
492
|
+
text,
|
|
493
|
+
fontSize,
|
|
494
|
+
lineHeight,
|
|
495
|
+
textAlign,
|
|
496
|
+
fontFamily,
|
|
497
|
+
fontWeight,
|
|
498
|
+
strokeWidth
|
|
499
|
+
};
|
|
500
|
+
function attrs(attrs2) {
|
|
501
|
+
const { x: x2, y: y2, w: w2, h: h2, angle: angle2 } = attrs2;
|
|
502
|
+
if (!(is.x(x2) && is.y(y2) && is.w(w2) && is.h(h2) && is.angle(angle2))) {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
if (!(angle2 >= -360 && angle2 <= 360)) {
|
|
506
|
+
return false;
|
|
507
|
+
}
|
|
508
|
+
return true;
|
|
509
|
+
}
|
|
510
|
+
function box(desc = {}) {
|
|
511
|
+
const { borderColor, borderRadius: borderRadius2, borderWidth: borderWidth2 } = desc;
|
|
512
|
+
if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
if (desc.hasOwnProperty("borderRadius") && !is.number(borderRadius2)) {
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
function rectDesc(desc) {
|
|
524
|
+
const { bgColor } = desc;
|
|
525
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
526
|
+
return false;
|
|
527
|
+
}
|
|
528
|
+
if (!box(desc)) {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
function circleDesc(desc) {
|
|
534
|
+
const { bgColor, borderColor, borderWidth: borderWidth2 } = desc;
|
|
535
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
if (desc.hasOwnProperty("borderColor") && !is.color(borderColor)) {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
if (desc.hasOwnProperty("borderWidth") && !is.number(borderWidth2)) {
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
return true;
|
|
545
|
+
}
|
|
546
|
+
function imageDesc(desc) {
|
|
547
|
+
const { src } = desc;
|
|
548
|
+
if (!is.imageSrc(src)) {
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
function svgDesc(desc) {
|
|
554
|
+
const { svg: svg2 } = desc;
|
|
555
|
+
if (!is.svg(svg2)) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
function htmlDesc(desc) {
|
|
561
|
+
const { html: html2 } = desc;
|
|
562
|
+
if (!is.html(html2)) {
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
function textDesc(desc) {
|
|
568
|
+
const {
|
|
569
|
+
text: text2,
|
|
570
|
+
color: color2,
|
|
571
|
+
fontSize: fontSize2,
|
|
572
|
+
lineHeight: lineHeight2,
|
|
573
|
+
fontFamily: fontFamily2,
|
|
574
|
+
textAlign: textAlign2,
|
|
575
|
+
fontWeight: fontWeight2,
|
|
576
|
+
bgColor,
|
|
577
|
+
strokeWidth: strokeWidth2,
|
|
578
|
+
strokeColor
|
|
579
|
+
} = desc;
|
|
580
|
+
if (!is.text(text2)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
if (!is.color(color2)) {
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
if (!is.fontSize(fontSize2)) {
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
if (desc.hasOwnProperty("bgColor") && !is.color(bgColor)) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
if (desc.hasOwnProperty("fontWeight") && !is.fontWeight(fontWeight2)) {
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
if (desc.hasOwnProperty("lineHeight") && !is.lineHeight(lineHeight2)) {
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
if (desc.hasOwnProperty("fontFamily") && !is.fontFamily(fontFamily2)) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
if (desc.hasOwnProperty("textAlign") && !is.textAlign(textAlign2)) {
|
|
602
|
+
return false;
|
|
603
|
+
}
|
|
604
|
+
if (desc.hasOwnProperty("strokeWidth") && !is.strokeWidth(strokeWidth2)) {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
if (desc.hasOwnProperty("strokeColor") && !is.color(strokeColor)) {
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
if (!box(desc)) {
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
return true;
|
|
187
614
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
reader.onload = function (event) {
|
|
197
|
-
var _a;
|
|
198
|
-
var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
199
|
-
resolve(base64);
|
|
200
|
-
};
|
|
201
|
-
reader.onerror = function (err) {
|
|
202
|
-
reject(err);
|
|
203
|
-
};
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
function parseSVGToDataURL(svg) {
|
|
207
|
-
return new Promise(function (resolve, reject) {
|
|
208
|
-
var _svg = svg;
|
|
209
|
-
var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
|
|
210
|
-
var reader = new FileReader();
|
|
211
|
-
reader.readAsDataURL(blob);
|
|
212
|
-
reader.onload = function (event) {
|
|
213
|
-
var _a;
|
|
214
|
-
var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
|
|
215
|
-
resolve(base64);
|
|
216
|
-
};
|
|
217
|
-
reader.onerror = function (err) {
|
|
218
|
-
reject(err);
|
|
219
|
-
};
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
var Image = window.Image;
|
|
224
|
-
function loadImage(src) {
|
|
225
|
-
return new Promise(function (resolve, reject) {
|
|
226
|
-
var img = new Image;
|
|
227
|
-
img.crossOrigin = 'anonymous';
|
|
228
|
-
img.onload = function () {
|
|
229
|
-
resolve(img);
|
|
230
|
-
};
|
|
231
|
-
img.onabort = reject;
|
|
232
|
-
img.onerror = reject;
|
|
233
|
-
img.src = src;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
function loadSVG(svg) {
|
|
237
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
238
|
-
var dataURL, image;
|
|
239
|
-
return __generator(this, function (_a) {
|
|
240
|
-
switch (_a.label) {
|
|
241
|
-
case 0: return [4, parseSVGToDataURL(svg)];
|
|
242
|
-
case 1:
|
|
243
|
-
dataURL = _a.sent();
|
|
244
|
-
return [4, loadImage(dataURL)];
|
|
245
|
-
case 2:
|
|
246
|
-
image = _a.sent();
|
|
247
|
-
return [2, image];
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
function filterAmpersand(str) {
|
|
253
|
-
return str.replace(/\&/ig, '&');
|
|
254
|
-
}
|
|
255
|
-
function loadHTML(html, opts) {
|
|
256
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
257
|
-
var dataURL, image;
|
|
258
|
-
return __generator(this, function (_a) {
|
|
259
|
-
switch (_a.label) {
|
|
260
|
-
case 0:
|
|
261
|
-
html = filterAmpersand(html);
|
|
262
|
-
return [4, parseHTMLToDataURL(html, opts)];
|
|
263
|
-
case 1:
|
|
264
|
-
dataURL = _a.sent();
|
|
265
|
-
return [4, loadImage(dataURL)];
|
|
266
|
-
case 2:
|
|
267
|
-
image = _a.sent();
|
|
268
|
-
return [2, image];
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
var Context = (function () {
|
|
275
|
-
function Context(ctx, opts) {
|
|
276
|
-
this._opts = opts;
|
|
277
|
-
this._ctx = ctx;
|
|
278
|
-
this._transform = {
|
|
279
|
-
scale: 1,
|
|
280
|
-
scrollX: 0,
|
|
281
|
-
scrollY: 0,
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
Context.prototype.getContext = function () {
|
|
285
|
-
return this._ctx;
|
|
286
|
-
};
|
|
287
|
-
Context.prototype.resetSize = function (opts) {
|
|
288
|
-
this._opts = __assign(__assign({}, this._opts), opts);
|
|
289
|
-
};
|
|
290
|
-
Context.prototype.calcDeviceNum = function (num) {
|
|
291
|
-
return num * this._opts.devicePixelRatio;
|
|
292
|
-
};
|
|
293
|
-
Context.prototype.calcScreenNum = function (num) {
|
|
294
|
-
return num / this._opts.devicePixelRatio;
|
|
295
|
-
};
|
|
296
|
-
Context.prototype.getSize = function () {
|
|
297
|
-
return {
|
|
298
|
-
width: this._opts.width,
|
|
299
|
-
height: this._opts.height,
|
|
300
|
-
contextWidth: this._opts.contextWidth,
|
|
301
|
-
contextHeight: this._opts.contextHeight,
|
|
302
|
-
devicePixelRatio: this._opts.devicePixelRatio,
|
|
303
|
-
};
|
|
304
|
-
};
|
|
305
|
-
Context.prototype.setTransform = function (config) {
|
|
306
|
-
this._transform = __assign(__assign({}, this._transform), config);
|
|
307
|
-
};
|
|
308
|
-
Context.prototype.getTransform = function () {
|
|
309
|
-
return {
|
|
310
|
-
scale: this._transform.scale,
|
|
311
|
-
scrollX: this._transform.scrollX,
|
|
312
|
-
scrollY: this._transform.scrollY,
|
|
313
|
-
};
|
|
314
|
-
};
|
|
315
|
-
Context.prototype.setFillStyle = function (color) {
|
|
316
|
-
this._ctx.fillStyle = color;
|
|
317
|
-
};
|
|
318
|
-
Context.prototype.fill = function (fillRule) {
|
|
319
|
-
return this._ctx.fill(fillRule || 'nonzero');
|
|
320
|
-
};
|
|
321
|
-
Context.prototype.arc = function (x, y, radius, startAngle, endAngle, anticlockwise) {
|
|
322
|
-
return this._ctx.arc(this._doSize(x), this._doSize(y), this._doSize(radius), startAngle, endAngle, anticlockwise);
|
|
323
|
-
};
|
|
324
|
-
Context.prototype.rect = function (x, y, w, h) {
|
|
325
|
-
return this._ctx.rect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
326
|
-
};
|
|
327
|
-
Context.prototype.fillRect = function (x, y, w, h) {
|
|
328
|
-
return this._ctx.fillRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
329
|
-
};
|
|
330
|
-
Context.prototype.clearRect = function (x, y, w, h) {
|
|
331
|
-
return this._ctx.clearRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
332
|
-
};
|
|
333
|
-
Context.prototype.beginPath = function () {
|
|
334
|
-
return this._ctx.beginPath();
|
|
335
|
-
};
|
|
336
|
-
Context.prototype.closePath = function () {
|
|
337
|
-
return this._ctx.closePath();
|
|
338
|
-
};
|
|
339
|
-
Context.prototype.lineTo = function (x, y) {
|
|
340
|
-
return this._ctx.lineTo(this._doSize(x), this._doSize(y));
|
|
341
|
-
};
|
|
342
|
-
Context.prototype.moveTo = function (x, y) {
|
|
343
|
-
return this._ctx.moveTo(this._doSize(x), this._doSize(y));
|
|
344
|
-
};
|
|
345
|
-
Context.prototype.arcTo = function (x1, y1, x2, y2, radius) {
|
|
346
|
-
return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
|
|
347
|
-
};
|
|
348
|
-
Context.prototype.setLineWidth = function (w) {
|
|
349
|
-
return this._ctx.lineWidth = this._doSize(w);
|
|
350
|
-
};
|
|
351
|
-
Context.prototype.setLineDash = function (nums) {
|
|
352
|
-
var _this = this;
|
|
353
|
-
return this._ctx.setLineDash(nums.map(function (n) { return _this._doSize(n); }));
|
|
354
|
-
};
|
|
355
|
-
Context.prototype.isPointInPath = function (x, y) {
|
|
356
|
-
return this._ctx.isPointInPath(this._doX(x), this._doY(y));
|
|
357
|
-
};
|
|
358
|
-
Context.prototype.isPointInPathWithoutScroll = function (x, y) {
|
|
359
|
-
return this._ctx.isPointInPath(this._doSize(x), this._doSize(y));
|
|
360
|
-
};
|
|
361
|
-
Context.prototype.setStrokeStyle = function (color) {
|
|
362
|
-
this._ctx.strokeStyle = color;
|
|
363
|
-
};
|
|
364
|
-
Context.prototype.stroke = function () {
|
|
365
|
-
return this._ctx.stroke();
|
|
366
|
-
};
|
|
367
|
-
Context.prototype.translate = function (x, y) {
|
|
368
|
-
return this._ctx.translate(this._doSize(x), this._doSize(y));
|
|
369
|
-
};
|
|
370
|
-
Context.prototype.rotate = function (angle) {
|
|
371
|
-
return this._ctx.rotate(angle);
|
|
372
|
-
};
|
|
373
|
-
Context.prototype.drawImage = function () {
|
|
374
|
-
var args = [];
|
|
375
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
376
|
-
args[_i] = arguments[_i];
|
|
377
|
-
}
|
|
378
|
-
var image = args[0];
|
|
379
|
-
var sx = args[1];
|
|
380
|
-
var sy = args[2];
|
|
381
|
-
var sw = args[3];
|
|
382
|
-
var sh = args[4];
|
|
383
|
-
var dx = args[args.length - 4];
|
|
384
|
-
var dy = args[args.length - 3];
|
|
385
|
-
var dw = args[args.length - 2];
|
|
386
|
-
var dh = args[args.length - 1];
|
|
387
|
-
if (args.length === 9) {
|
|
388
|
-
return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
|
|
389
|
-
}
|
|
390
|
-
else {
|
|
391
|
-
return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
Context.prototype.createPattern = function (image, repetition) {
|
|
395
|
-
return this._ctx.createPattern(image, repetition);
|
|
396
|
-
};
|
|
397
|
-
Context.prototype.measureText = function (text) {
|
|
398
|
-
return this._ctx.measureText(text);
|
|
399
|
-
};
|
|
400
|
-
Context.prototype.setTextAlign = function (align) {
|
|
401
|
-
this._ctx.textAlign = align;
|
|
402
|
-
};
|
|
403
|
-
Context.prototype.fillText = function (text, x, y, maxWidth) {
|
|
404
|
-
if (maxWidth !== undefined) {
|
|
405
|
-
return this._ctx.fillText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
|
|
406
|
-
}
|
|
407
|
-
else {
|
|
408
|
-
return this._ctx.fillText(text, this._doSize(x), this._doSize(y));
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
Context.prototype.strokeText = function (text, x, y, maxWidth) {
|
|
412
|
-
if (maxWidth !== undefined) {
|
|
413
|
-
return this._ctx.strokeText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
|
|
414
|
-
}
|
|
415
|
-
else {
|
|
416
|
-
return this._ctx.strokeText(text, this._doSize(x), this._doSize(y));
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
Context.prototype.setFont = function (opts) {
|
|
420
|
-
var strList = [];
|
|
421
|
-
if (opts.fontWeight === 'bold') {
|
|
422
|
-
strList.push("".concat(opts.fontWeight));
|
|
423
|
-
}
|
|
424
|
-
strList.push("".concat(this._doSize(opts.fontSize || 12), "px"));
|
|
425
|
-
strList.push("".concat(opts.fontFamily || 'sans-serif'));
|
|
426
|
-
this._ctx.font = "".concat(strList.join(' '));
|
|
427
|
-
};
|
|
428
|
-
Context.prototype.setTextBaseline = function (baseline) {
|
|
429
|
-
this._ctx.textBaseline = baseline;
|
|
430
|
-
};
|
|
431
|
-
Context.prototype.setGlobalAlpha = function (alpha) {
|
|
432
|
-
this._ctx.globalAlpha = alpha;
|
|
433
|
-
};
|
|
434
|
-
Context.prototype.save = function () {
|
|
435
|
-
this._ctx.save();
|
|
436
|
-
};
|
|
437
|
-
Context.prototype.restore = function () {
|
|
438
|
-
this._ctx.restore();
|
|
439
|
-
};
|
|
440
|
-
Context.prototype.scale = function (ratioX, ratioY) {
|
|
441
|
-
this._ctx.scale(ratioX, ratioY);
|
|
442
|
-
};
|
|
443
|
-
Context.prototype.setShadowColor = function (color) {
|
|
444
|
-
this._ctx.shadowColor = color;
|
|
445
|
-
};
|
|
446
|
-
Context.prototype.setShadowOffsetX = function (offsetX) {
|
|
447
|
-
this._ctx.shadowOffsetX = this._doSize(offsetX);
|
|
448
|
-
};
|
|
449
|
-
Context.prototype.setShadowOffsetY = function (offsetY) {
|
|
450
|
-
this._ctx.shadowOffsetY = this._doSize(offsetY);
|
|
451
|
-
};
|
|
452
|
-
Context.prototype.setShadowBlur = function (blur) {
|
|
453
|
-
this._ctx.shadowBlur = this._doSize(blur);
|
|
454
|
-
};
|
|
455
|
-
Context.prototype.ellipse = function (x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
456
|
-
this._ctx.ellipse(this._doSize(x), this._doSize(y), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
|
|
457
|
-
};
|
|
458
|
-
Context.prototype._doSize = function (num) {
|
|
459
|
-
return this._opts.devicePixelRatio * num;
|
|
460
|
-
};
|
|
461
|
-
Context.prototype._doX = function (x) {
|
|
462
|
-
var _a = this._transform, scale = _a.scale, scrollX = _a.scrollX;
|
|
463
|
-
var _x = (x - scrollX) / scale;
|
|
464
|
-
return this._doSize(_x);
|
|
465
|
-
};
|
|
466
|
-
Context.prototype._doY = function (y) {
|
|
467
|
-
var _a = this._transform, scale = _a.scale, scrollY = _a.scrollY;
|
|
468
|
-
var _y = (y - scrollY) / scale;
|
|
469
|
-
return this._doSize(_y);
|
|
470
|
-
};
|
|
471
|
-
return Context;
|
|
472
|
-
}());
|
|
473
|
-
|
|
474
|
-
function number(value) {
|
|
475
|
-
return (typeof value === 'number' && (value > 0 || value <= 0));
|
|
476
|
-
}
|
|
477
|
-
function x(value) {
|
|
478
|
-
return number(value);
|
|
479
|
-
}
|
|
480
|
-
function y(value) {
|
|
481
|
-
return number(value);
|
|
482
|
-
}
|
|
483
|
-
function w(value) {
|
|
484
|
-
return (typeof value === 'number' && value >= 0);
|
|
485
|
-
}
|
|
486
|
-
function h(value) {
|
|
487
|
-
return (typeof value === 'number' && value >= 0);
|
|
488
|
-
}
|
|
489
|
-
function angle(value) {
|
|
490
|
-
return (typeof value === 'number' && value >= -360 && value <= 360);
|
|
491
|
-
}
|
|
492
|
-
function borderWidth(value) {
|
|
493
|
-
return w(value);
|
|
494
|
-
}
|
|
495
|
-
function borderRadius(value) {
|
|
496
|
-
return number(value) && value >= 0;
|
|
497
|
-
}
|
|
498
|
-
function color(value) {
|
|
499
|
-
return isColorStr(value);
|
|
500
|
-
}
|
|
501
|
-
function imageURL(value) {
|
|
502
|
-
return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test("".concat(value)));
|
|
503
|
-
}
|
|
504
|
-
function imageBase64(value) {
|
|
505
|
-
return (typeof value === 'string' && /^(data:image\/)/.test("".concat(value)));
|
|
506
|
-
}
|
|
507
|
-
function imageSrc(value) {
|
|
508
|
-
return (imageBase64(value) || imageURL(value));
|
|
509
|
-
}
|
|
510
|
-
function svg(value) {
|
|
511
|
-
return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test("".concat(value).trim()) && /<\/[\s]{0,}svg>$/i.test("".concat(value).trim()));
|
|
512
|
-
}
|
|
513
|
-
function html(value) {
|
|
514
|
-
var result = false;
|
|
515
|
-
if (typeof value === 'string') {
|
|
516
|
-
var div = document.createElement('div');
|
|
517
|
-
div.innerHTML = value;
|
|
518
|
-
if (div.children.length > 0) {
|
|
519
|
-
result = true;
|
|
520
|
-
}
|
|
521
|
-
div = null;
|
|
522
|
-
}
|
|
523
|
-
return result;
|
|
524
|
-
}
|
|
525
|
-
function text(value) {
|
|
526
|
-
return typeof value === 'string';
|
|
527
|
-
}
|
|
528
|
-
function fontSize(value) {
|
|
529
|
-
return number(value) && value > 0;
|
|
530
|
-
}
|
|
531
|
-
function lineHeight(value) {
|
|
532
|
-
return number(value) && value > 0;
|
|
533
|
-
}
|
|
534
|
-
function strokeWidth(value) {
|
|
535
|
-
return number(value) && value > 0;
|
|
536
|
-
}
|
|
537
|
-
function textAlign(value) {
|
|
538
|
-
return ['center', 'left', 'right'].includes(value);
|
|
539
|
-
}
|
|
540
|
-
function fontFamily(value) {
|
|
541
|
-
return typeof value === 'string' && value.length > 0;
|
|
542
|
-
}
|
|
543
|
-
function fontWeight(value) {
|
|
544
|
-
return ['bold'].includes(value);
|
|
545
|
-
}
|
|
546
|
-
var is = {
|
|
547
|
-
x: x,
|
|
548
|
-
y: y,
|
|
549
|
-
w: w,
|
|
550
|
-
h: h,
|
|
551
|
-
angle: angle,
|
|
552
|
-
number: number,
|
|
553
|
-
borderWidth: borderWidth,
|
|
554
|
-
borderRadius: borderRadius,
|
|
555
|
-
color: color,
|
|
556
|
-
imageSrc: imageSrc,
|
|
557
|
-
imageURL: imageURL,
|
|
558
|
-
imageBase64: imageBase64,
|
|
559
|
-
svg: svg,
|
|
560
|
-
html: html,
|
|
561
|
-
text: text,
|
|
562
|
-
fontSize: fontSize,
|
|
563
|
-
lineHeight: lineHeight,
|
|
564
|
-
textAlign: textAlign,
|
|
565
|
-
fontFamily: fontFamily,
|
|
566
|
-
fontWeight: fontWeight,
|
|
567
|
-
strokeWidth: strokeWidth,
|
|
615
|
+
const check = {
|
|
616
|
+
attrs,
|
|
617
|
+
textDesc,
|
|
618
|
+
rectDesc,
|
|
619
|
+
circleDesc,
|
|
620
|
+
imageDesc,
|
|
621
|
+
svgDesc,
|
|
622
|
+
htmlDesc
|
|
568
623
|
};
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
|
|
587
|
-
return false;
|
|
588
|
-
}
|
|
589
|
-
if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
|
|
590
|
-
return false;
|
|
591
|
-
}
|
|
592
|
-
return true;
|
|
593
|
-
}
|
|
594
|
-
function rectDesc(desc) {
|
|
595
|
-
var bgColor = desc.bgColor;
|
|
596
|
-
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
597
|
-
return false;
|
|
598
|
-
}
|
|
599
|
-
if (!box(desc)) {
|
|
600
|
-
return false;
|
|
601
|
-
}
|
|
602
|
-
return true;
|
|
603
|
-
}
|
|
604
|
-
function circleDesc(desc) {
|
|
605
|
-
var bgColor = desc.bgColor, borderColor = desc.borderColor, borderWidth = desc.borderWidth;
|
|
606
|
-
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
607
|
-
return false;
|
|
608
|
-
}
|
|
609
|
-
if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
|
|
610
|
-
return false;
|
|
611
|
-
}
|
|
612
|
-
if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
|
|
613
|
-
return false;
|
|
614
|
-
}
|
|
615
|
-
return true;
|
|
616
|
-
}
|
|
617
|
-
function imageDesc(desc) {
|
|
618
|
-
var src = desc.src;
|
|
619
|
-
if (!is.imageSrc(src)) {
|
|
620
|
-
return false;
|
|
621
|
-
}
|
|
622
|
-
return true;
|
|
623
|
-
}
|
|
624
|
-
function svgDesc(desc) {
|
|
625
|
-
var svg = desc.svg;
|
|
626
|
-
if (!is.svg(svg)) {
|
|
627
|
-
return false;
|
|
628
|
-
}
|
|
629
|
-
return true;
|
|
630
|
-
}
|
|
631
|
-
function htmlDesc(desc) {
|
|
632
|
-
var html = desc.html;
|
|
633
|
-
if (!is.html(html)) {
|
|
634
|
-
return false;
|
|
635
|
-
}
|
|
636
|
-
return true;
|
|
637
|
-
}
|
|
638
|
-
function textDesc(desc) {
|
|
639
|
-
var text = desc.text, color = desc.color, fontSize = desc.fontSize, lineHeight = desc.lineHeight, fontFamily = desc.fontFamily, textAlign = desc.textAlign, fontWeight = desc.fontWeight, bgColor = desc.bgColor, strokeWidth = desc.strokeWidth, strokeColor = desc.strokeColor;
|
|
640
|
-
if (!is.text(text)) {
|
|
641
|
-
return false;
|
|
642
|
-
}
|
|
643
|
-
if (!is.color(color)) {
|
|
644
|
-
return false;
|
|
645
|
-
}
|
|
646
|
-
if (!is.fontSize(fontSize)) {
|
|
647
|
-
return false;
|
|
648
|
-
}
|
|
649
|
-
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
650
|
-
return false;
|
|
651
|
-
}
|
|
652
|
-
if (desc.hasOwnProperty('fontWeight') && !is.fontWeight(fontWeight)) {
|
|
653
|
-
return false;
|
|
654
|
-
}
|
|
655
|
-
if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)) {
|
|
656
|
-
return false;
|
|
657
|
-
}
|
|
658
|
-
if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)) {
|
|
659
|
-
return false;
|
|
660
|
-
}
|
|
661
|
-
if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)) {
|
|
662
|
-
return false;
|
|
663
|
-
}
|
|
664
|
-
if (desc.hasOwnProperty('strokeWidth') && !is.strokeWidth(strokeWidth)) {
|
|
665
|
-
return false;
|
|
666
|
-
}
|
|
667
|
-
if (desc.hasOwnProperty('strokeColor') && !is.color(strokeColor)) {
|
|
668
|
-
return false;
|
|
669
|
-
}
|
|
670
|
-
if (!box(desc)) {
|
|
671
|
-
return false;
|
|
672
|
-
}
|
|
673
|
-
return true;
|
|
674
|
-
}
|
|
675
|
-
var check = {
|
|
676
|
-
attrs: attrs,
|
|
677
|
-
textDesc: textDesc,
|
|
678
|
-
rectDesc: rectDesc,
|
|
679
|
-
circleDesc: circleDesc,
|
|
680
|
-
imageDesc: imageDesc,
|
|
681
|
-
svgDesc: svgDesc,
|
|
682
|
-
htmlDesc: htmlDesc,
|
|
624
|
+
const index = {
|
|
625
|
+
is,
|
|
626
|
+
check,
|
|
627
|
+
delay,
|
|
628
|
+
compose,
|
|
629
|
+
throttle,
|
|
630
|
+
loadImage,
|
|
631
|
+
loadSVG,
|
|
632
|
+
loadHTML,
|
|
633
|
+
downloadImageFromCanvas,
|
|
634
|
+
toColorHexStr,
|
|
635
|
+
toColorHexNum,
|
|
636
|
+
isColorStr,
|
|
637
|
+
createUUID,
|
|
638
|
+
istype,
|
|
639
|
+
deepClone,
|
|
640
|
+
Context
|
|
683
641
|
};
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
return default_1;
|
|
706
|
-
|
|
707
|
-
})();
|
|
642
|
+
exports.Context = Context;
|
|
643
|
+
exports.check = check;
|
|
644
|
+
exports.compose = compose;
|
|
645
|
+
exports.createUUID = createUUID;
|
|
646
|
+
exports.deepClone = deepClone;
|
|
647
|
+
exports.default = index;
|
|
648
|
+
exports.delay = delay;
|
|
649
|
+
exports.downloadImageFromCanvas = downloadImageFromCanvas;
|
|
650
|
+
exports.is = is;
|
|
651
|
+
exports.isColorStr = isColorStr;
|
|
652
|
+
exports.istype = istype;
|
|
653
|
+
exports.loadHTML = loadHTML;
|
|
654
|
+
exports.loadImage = loadImage;
|
|
655
|
+
exports.loadSVG = loadSVG;
|
|
656
|
+
exports.throttle = throttle;
|
|
657
|
+
exports.toColorHexNum = toColorHexNum;
|
|
658
|
+
exports.toColorHexStr = toColorHexStr;
|
|
659
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
660
|
+
return exports;
|
|
661
|
+
}({});
|