@idraw/util 0.3.0-alpha.4 → 0.3.0-alpha.7
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
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { delay, compose, throttle } from './lib/time';
|
|
2
|
+
import { downloadImageFromCanvas } from './lib/file';
|
|
3
|
+
import { toColorHexStr, toColorHexNum, isColorStr } from './lib/color';
|
|
4
|
+
import { createUUID } from './lib/uuid';
|
|
5
|
+
import { deepClone } from './lib/data';
|
|
6
|
+
import istype from './lib/istype';
|
|
7
|
+
import { loadImage, loadSVG, loadHTML } from './lib/loader';
|
|
8
|
+
import Context from './lib/context';
|
|
9
|
+
import is from './lib/is';
|
|
10
|
+
import check from './lib/check';
|
|
11
|
+
export { is, check, delay, compose, throttle, loadImage, loadSVG, loadHTML, downloadImageFromCanvas, toColorHexStr, toColorHexNum, isColorStr, createUUID, istype, deepClone, Context };
|
|
12
|
+
declare const _default: {
|
|
13
|
+
is: {
|
|
14
|
+
x: (value: any) => boolean;
|
|
15
|
+
y: (value: any) => boolean;
|
|
16
|
+
w: (value: any) => boolean;
|
|
17
|
+
h: (value: any) => boolean;
|
|
18
|
+
angle: (value: any) => boolean;
|
|
19
|
+
number: (value: any) => boolean;
|
|
20
|
+
borderWidth: (value: any) => boolean;
|
|
21
|
+
borderRadius: (value: any) => boolean;
|
|
22
|
+
color: (value: any) => boolean;
|
|
23
|
+
imageSrc: (value: any) => boolean;
|
|
24
|
+
imageURL: (value: any) => boolean;
|
|
25
|
+
imageBase64: (value: any) => boolean;
|
|
26
|
+
svg: (value: any) => boolean;
|
|
27
|
+
html: (value: any) => boolean;
|
|
28
|
+
text: (value: any) => boolean;
|
|
29
|
+
fontSize: (value: any) => boolean;
|
|
30
|
+
lineHeight: (value: any) => boolean;
|
|
31
|
+
textAlign: (value: any) => boolean;
|
|
32
|
+
fontFamily: (value: any) => boolean;
|
|
33
|
+
fontWeight: (value: any) => boolean;
|
|
34
|
+
strokeWidth: (value: any) => boolean;
|
|
35
|
+
};
|
|
36
|
+
check: {
|
|
37
|
+
attrs: (attrs: any) => boolean;
|
|
38
|
+
textDesc: (desc: any) => boolean;
|
|
39
|
+
rectDesc: (desc: any) => boolean;
|
|
40
|
+
circleDesc: (desc: any) => boolean;
|
|
41
|
+
imageDesc: (desc: any) => boolean;
|
|
42
|
+
svgDesc: (desc: any) => boolean;
|
|
43
|
+
htmlDesc: (desc: any) => boolean;
|
|
44
|
+
};
|
|
45
|
+
delay: typeof delay;
|
|
46
|
+
compose: typeof compose;
|
|
47
|
+
throttle: typeof throttle;
|
|
48
|
+
loadImage: typeof loadImage;
|
|
49
|
+
loadSVG: typeof loadSVG;
|
|
50
|
+
loadHTML: typeof loadHTML;
|
|
51
|
+
downloadImageFromCanvas: typeof downloadImageFromCanvas;
|
|
52
|
+
toColorHexStr: typeof toColorHexStr;
|
|
53
|
+
toColorHexNum: typeof toColorHexNum;
|
|
54
|
+
isColorStr: typeof isColorStr;
|
|
55
|
+
createUUID: typeof createUUID;
|
|
56
|
+
istype: {
|
|
57
|
+
type(data: any, lowerCase?: boolean | undefined): string;
|
|
58
|
+
array(data: any): boolean;
|
|
59
|
+
json(data: any): boolean;
|
|
60
|
+
function(data: any): boolean;
|
|
61
|
+
asyncFunction(data: any): boolean;
|
|
62
|
+
string(data: any): boolean;
|
|
63
|
+
number(data: any): boolean;
|
|
64
|
+
undefined(data: any): boolean;
|
|
65
|
+
null(data: any): boolean;
|
|
66
|
+
promise(data: any): boolean;
|
|
67
|
+
};
|
|
68
|
+
deepClone: typeof deepClone;
|
|
69
|
+
Context: typeof Context;
|
|
70
|
+
};
|
|
71
|
+
export default _default;
|
|
@@ -8,4 +8,22 @@ import { loadImage, loadSVG, loadHTML } from './lib/loader';
|
|
|
8
8
|
import Context from './lib/context';
|
|
9
9
|
import is from './lib/is';
|
|
10
10
|
import check from './lib/check';
|
|
11
|
-
export { is, check, delay, compose, throttle, loadImage, loadSVG, loadHTML, downloadImageFromCanvas, toColorHexStr, toColorHexNum, isColorStr, createUUID, istype, deepClone, Context
|
|
11
|
+
export { is, check, delay, compose, throttle, loadImage, loadSVG, loadHTML, downloadImageFromCanvas, toColorHexStr, toColorHexNum, isColorStr, createUUID, istype, deepClone, Context };
|
|
12
|
+
export default {
|
|
13
|
+
is,
|
|
14
|
+
check,
|
|
15
|
+
delay,
|
|
16
|
+
compose,
|
|
17
|
+
throttle,
|
|
18
|
+
loadImage,
|
|
19
|
+
loadSVG,
|
|
20
|
+
loadHTML,
|
|
21
|
+
downloadImageFromCanvas,
|
|
22
|
+
toColorHexStr,
|
|
23
|
+
toColorHexNum,
|
|
24
|
+
isColorStr,
|
|
25
|
+
createUUID,
|
|
26
|
+
istype,
|
|
27
|
+
deepClone,
|
|
28
|
+
Context
|
|
29
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function attrs(attrs: any): boolean;
|
|
2
|
+
declare function rectDesc(desc: any): boolean;
|
|
3
|
+
declare function circleDesc(desc: any): boolean;
|
|
4
|
+
declare function imageDesc(desc: any): boolean;
|
|
5
|
+
declare function svgDesc(desc: any): boolean;
|
|
6
|
+
declare function htmlDesc(desc: any): boolean;
|
|
7
|
+
declare function textDesc(desc: any): boolean;
|
|
8
|
+
declare const check: {
|
|
9
|
+
attrs: typeof attrs;
|
|
10
|
+
textDesc: typeof textDesc;
|
|
11
|
+
rectDesc: typeof rectDesc;
|
|
12
|
+
circleDesc: typeof circleDesc;
|
|
13
|
+
imageDesc: typeof imageDesc;
|
|
14
|
+
svgDesc: typeof svgDesc;
|
|
15
|
+
htmlDesc: typeof htmlDesc;
|
|
16
|
+
};
|
|
17
|
+
export default check;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import is from './is';
|
|
2
|
+
function attrs(attrs) {
|
|
3
|
+
const { x, y, w, h, angle } = attrs;
|
|
4
|
+
if (!(is.x(x) && is.y(y) && is.w(w) && is.h(h) && is.angle(angle))) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
if (!(angle >= -360 && angle <= 360)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
function box(desc = {}) {
|
|
13
|
+
const { borderColor, borderRadius, borderWidth } = desc;
|
|
14
|
+
if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
function rectDesc(desc) {
|
|
26
|
+
const { bgColor } = desc;
|
|
27
|
+
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (!box(desc)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
function circleDesc(desc) {
|
|
36
|
+
const { bgColor, borderColor, borderWidth } = desc;
|
|
37
|
+
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
function imageDesc(desc) {
|
|
49
|
+
const { src } = desc;
|
|
50
|
+
if (!is.imageSrc(src)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
function svgDesc(desc) {
|
|
56
|
+
const { svg } = desc;
|
|
57
|
+
if (!is.svg(svg)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
function htmlDesc(desc) {
|
|
63
|
+
const { html } = desc;
|
|
64
|
+
if (!is.html(html)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
function textDesc(desc) {
|
|
70
|
+
const { text, color, fontSize, lineHeight, fontFamily, textAlign, fontWeight, bgColor, strokeWidth, strokeColor } = desc;
|
|
71
|
+
if (!is.text(text)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
if (!is.color(color)) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if (!is.fontSize(fontSize)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
if (desc.hasOwnProperty('fontWeight') && !is.fontWeight(fontWeight)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (desc.hasOwnProperty('strokeWidth') && !is.strokeWidth(strokeWidth)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (desc.hasOwnProperty('strokeColor') && !is.color(strokeColor)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (!box(desc)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
const check = {
|
|
107
|
+
attrs,
|
|
108
|
+
textDesc,
|
|
109
|
+
rectDesc,
|
|
110
|
+
circleDesc,
|
|
111
|
+
imageDesc,
|
|
112
|
+
svgDesc,
|
|
113
|
+
htmlDesc,
|
|
114
|
+
};
|
|
115
|
+
export default check;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function toColorHexNum(color) {
|
|
2
|
+
return parseInt(color.replace(/^\#/, '0x'));
|
|
3
|
+
}
|
|
4
|
+
export function toColorHexStr(color) {
|
|
5
|
+
return '#' + color.toString(16);
|
|
6
|
+
}
|
|
7
|
+
export function isColorStr(color) {
|
|
8
|
+
return typeof color === 'string' && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color);
|
|
9
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { TypeContext, TypeBoardSizeOptions } from '@idraw/types';
|
|
2
|
+
type Options = {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
contextWidth: number;
|
|
6
|
+
contextHeight: number;
|
|
7
|
+
devicePixelRatio: number;
|
|
8
|
+
};
|
|
9
|
+
type Transform = {
|
|
10
|
+
scale?: number;
|
|
11
|
+
scrollX?: number;
|
|
12
|
+
scrollY?: number;
|
|
13
|
+
};
|
|
14
|
+
declare class Context implements TypeContext {
|
|
15
|
+
private _opts;
|
|
16
|
+
private _ctx;
|
|
17
|
+
private _transform;
|
|
18
|
+
constructor(ctx: CanvasRenderingContext2D, opts: Options);
|
|
19
|
+
getContext(): CanvasRenderingContext2D;
|
|
20
|
+
resetSize(opts: TypeBoardSizeOptions): void;
|
|
21
|
+
calcDeviceNum(num: number): number;
|
|
22
|
+
calcScreenNum(num: number): number;
|
|
23
|
+
getSize(): {
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
contextWidth: number;
|
|
27
|
+
contextHeight: number;
|
|
28
|
+
devicePixelRatio: number;
|
|
29
|
+
};
|
|
30
|
+
setTransform(config: Transform): void;
|
|
31
|
+
getTransform(): {
|
|
32
|
+
scale: number;
|
|
33
|
+
scrollX: number;
|
|
34
|
+
scrollY: number;
|
|
35
|
+
};
|
|
36
|
+
setFillStyle(color: string | CanvasPattern | CanvasGradient): void;
|
|
37
|
+
fill(fillRule?: CanvasFillRule | undefined): void;
|
|
38
|
+
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean | undefined): void;
|
|
39
|
+
rect(x: number, y: number, w: number, h: number): void;
|
|
40
|
+
fillRect(x: number, y: number, w: number, h: number): void;
|
|
41
|
+
clearRect(x: number, y: number, w: number, h: number): void;
|
|
42
|
+
beginPath(): void;
|
|
43
|
+
closePath(): void;
|
|
44
|
+
lineTo(x: number, y: number): void;
|
|
45
|
+
moveTo(x: number, y: number): void;
|
|
46
|
+
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
|
|
47
|
+
setLineWidth(w: number): number;
|
|
48
|
+
setLineDash(nums: number[]): void;
|
|
49
|
+
isPointInPath(x: number, y: number): boolean;
|
|
50
|
+
isPointInPathWithoutScroll(x: number, y: number): boolean;
|
|
51
|
+
setStrokeStyle(color: string): void;
|
|
52
|
+
stroke(): void;
|
|
53
|
+
translate(x: number, y: number): void;
|
|
54
|
+
rotate(angle: number): void;
|
|
55
|
+
drawImage(...args: any[]): void;
|
|
56
|
+
createPattern(image: CanvasImageSource, repetition: string | null): CanvasPattern | null;
|
|
57
|
+
measureText(text: string): TextMetrics;
|
|
58
|
+
setTextAlign(align: CanvasTextAlign): void;
|
|
59
|
+
fillText(text: string, x: number, y: number, maxWidth?: number | undefined): void;
|
|
60
|
+
strokeText(text: string, x: number, y: number, maxWidth?: number | undefined): void;
|
|
61
|
+
setFont(opts: {
|
|
62
|
+
fontSize: number;
|
|
63
|
+
fontFamily?: string;
|
|
64
|
+
fontWeight?: 'bold';
|
|
65
|
+
}): void;
|
|
66
|
+
setTextBaseline(baseline: CanvasTextBaseline): void;
|
|
67
|
+
setGlobalAlpha(alpha: number): void;
|
|
68
|
+
save(): void;
|
|
69
|
+
restore(): void;
|
|
70
|
+
scale(ratioX: number, ratioY: number): void;
|
|
71
|
+
setShadowColor(color: string): void;
|
|
72
|
+
setShadowOffsetX(offsetX: number): void;
|
|
73
|
+
setShadowOffsetY(offsetY: number): void;
|
|
74
|
+
setShadowBlur(blur: number): void;
|
|
75
|
+
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean | undefined): void;
|
|
76
|
+
private _doSize;
|
|
77
|
+
private _doX;
|
|
78
|
+
private _doY;
|
|
79
|
+
}
|
|
80
|
+
export default Context;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
class Context {
|
|
2
|
+
constructor(ctx, opts) {
|
|
3
|
+
this._opts = opts;
|
|
4
|
+
this._ctx = ctx;
|
|
5
|
+
this._transform = {
|
|
6
|
+
scale: 1,
|
|
7
|
+
scrollX: 0,
|
|
8
|
+
scrollY: 0,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
getContext() {
|
|
12
|
+
return this._ctx;
|
|
13
|
+
}
|
|
14
|
+
resetSize(opts) {
|
|
15
|
+
this._opts = Object.assign(Object.assign({}, this._opts), opts);
|
|
16
|
+
}
|
|
17
|
+
calcDeviceNum(num) {
|
|
18
|
+
return num * this._opts.devicePixelRatio;
|
|
19
|
+
}
|
|
20
|
+
calcScreenNum(num) {
|
|
21
|
+
return num / this._opts.devicePixelRatio;
|
|
22
|
+
}
|
|
23
|
+
getSize() {
|
|
24
|
+
return {
|
|
25
|
+
width: this._opts.width,
|
|
26
|
+
height: this._opts.height,
|
|
27
|
+
contextWidth: this._opts.contextWidth,
|
|
28
|
+
contextHeight: this._opts.contextHeight,
|
|
29
|
+
devicePixelRatio: this._opts.devicePixelRatio,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
setTransform(config) {
|
|
33
|
+
this._transform = Object.assign(Object.assign({}, this._transform), config);
|
|
34
|
+
}
|
|
35
|
+
getTransform() {
|
|
36
|
+
return {
|
|
37
|
+
scale: this._transform.scale,
|
|
38
|
+
scrollX: this._transform.scrollX,
|
|
39
|
+
scrollY: this._transform.scrollY,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
setFillStyle(color) {
|
|
43
|
+
this._ctx.fillStyle = color;
|
|
44
|
+
}
|
|
45
|
+
fill(fillRule) {
|
|
46
|
+
return this._ctx.fill(fillRule || 'nonzero');
|
|
47
|
+
}
|
|
48
|
+
arc(x, y, radius, startAngle, endAngle, anticlockwise) {
|
|
49
|
+
return this._ctx.arc(this._doSize(x), this._doSize(y), this._doSize(radius), startAngle, endAngle, anticlockwise);
|
|
50
|
+
}
|
|
51
|
+
rect(x, y, w, h) {
|
|
52
|
+
return this._ctx.rect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
53
|
+
}
|
|
54
|
+
fillRect(x, y, w, h) {
|
|
55
|
+
return this._ctx.fillRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
56
|
+
}
|
|
57
|
+
clearRect(x, y, w, h) {
|
|
58
|
+
return this._ctx.clearRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
|
|
59
|
+
}
|
|
60
|
+
beginPath() {
|
|
61
|
+
return this._ctx.beginPath();
|
|
62
|
+
}
|
|
63
|
+
closePath() {
|
|
64
|
+
return this._ctx.closePath();
|
|
65
|
+
}
|
|
66
|
+
lineTo(x, y) {
|
|
67
|
+
return this._ctx.lineTo(this._doSize(x), this._doSize(y));
|
|
68
|
+
}
|
|
69
|
+
moveTo(x, y) {
|
|
70
|
+
return this._ctx.moveTo(this._doSize(x), this._doSize(y));
|
|
71
|
+
}
|
|
72
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
73
|
+
return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
|
|
74
|
+
}
|
|
75
|
+
setLineWidth(w) {
|
|
76
|
+
return this._ctx.lineWidth = this._doSize(w);
|
|
77
|
+
}
|
|
78
|
+
setLineDash(nums) {
|
|
79
|
+
return this._ctx.setLineDash(nums.map(n => this._doSize(n)));
|
|
80
|
+
}
|
|
81
|
+
isPointInPath(x, y) {
|
|
82
|
+
return this._ctx.isPointInPath(this._doX(x), this._doY(y));
|
|
83
|
+
}
|
|
84
|
+
isPointInPathWithoutScroll(x, y) {
|
|
85
|
+
return this._ctx.isPointInPath(this._doSize(x), this._doSize(y));
|
|
86
|
+
}
|
|
87
|
+
setStrokeStyle(color) {
|
|
88
|
+
this._ctx.strokeStyle = color;
|
|
89
|
+
}
|
|
90
|
+
stroke() {
|
|
91
|
+
return this._ctx.stroke();
|
|
92
|
+
}
|
|
93
|
+
translate(x, y) {
|
|
94
|
+
return this._ctx.translate(this._doSize(x), this._doSize(y));
|
|
95
|
+
}
|
|
96
|
+
rotate(angle) {
|
|
97
|
+
return this._ctx.rotate(angle);
|
|
98
|
+
}
|
|
99
|
+
drawImage(...args) {
|
|
100
|
+
const image = args[0];
|
|
101
|
+
const sx = args[1];
|
|
102
|
+
const sy = args[2];
|
|
103
|
+
const sw = args[3];
|
|
104
|
+
const sh = args[4];
|
|
105
|
+
const dx = args[args.length - 4];
|
|
106
|
+
const dy = args[args.length - 3];
|
|
107
|
+
const dw = args[args.length - 2];
|
|
108
|
+
const dh = args[args.length - 1];
|
|
109
|
+
if (args.length === 9) {
|
|
110
|
+
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));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
createPattern(image, repetition) {
|
|
117
|
+
return this._ctx.createPattern(image, repetition);
|
|
118
|
+
}
|
|
119
|
+
measureText(text) {
|
|
120
|
+
return this._ctx.measureText(text);
|
|
121
|
+
}
|
|
122
|
+
setTextAlign(align) {
|
|
123
|
+
this._ctx.textAlign = align;
|
|
124
|
+
}
|
|
125
|
+
fillText(text, x, y, maxWidth) {
|
|
126
|
+
if (maxWidth !== undefined) {
|
|
127
|
+
return this._ctx.fillText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return this._ctx.fillText(text, this._doSize(x), this._doSize(y));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
strokeText(text, x, y, maxWidth) {
|
|
134
|
+
if (maxWidth !== undefined) {
|
|
135
|
+
return this._ctx.strokeText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return this._ctx.strokeText(text, this._doSize(x), this._doSize(y));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
setFont(opts) {
|
|
142
|
+
const strList = [];
|
|
143
|
+
if (opts.fontWeight === 'bold') {
|
|
144
|
+
strList.push(`${opts.fontWeight}`);
|
|
145
|
+
}
|
|
146
|
+
strList.push(`${this._doSize(opts.fontSize || 12)}px`);
|
|
147
|
+
strList.push(`${opts.fontFamily || 'sans-serif'}`);
|
|
148
|
+
this._ctx.font = `${strList.join(' ')}`;
|
|
149
|
+
}
|
|
150
|
+
setTextBaseline(baseline) {
|
|
151
|
+
this._ctx.textBaseline = baseline;
|
|
152
|
+
}
|
|
153
|
+
setGlobalAlpha(alpha) {
|
|
154
|
+
this._ctx.globalAlpha = alpha;
|
|
155
|
+
}
|
|
156
|
+
save() {
|
|
157
|
+
this._ctx.save();
|
|
158
|
+
}
|
|
159
|
+
restore() {
|
|
160
|
+
this._ctx.restore();
|
|
161
|
+
}
|
|
162
|
+
scale(ratioX, ratioY) {
|
|
163
|
+
this._ctx.scale(ratioX, ratioY);
|
|
164
|
+
}
|
|
165
|
+
setShadowColor(color) {
|
|
166
|
+
this._ctx.shadowColor = color;
|
|
167
|
+
}
|
|
168
|
+
setShadowOffsetX(offsetX) {
|
|
169
|
+
this._ctx.shadowOffsetX = this._doSize(offsetX);
|
|
170
|
+
}
|
|
171
|
+
setShadowOffsetY(offsetY) {
|
|
172
|
+
this._ctx.shadowOffsetY = this._doSize(offsetY);
|
|
173
|
+
}
|
|
174
|
+
setShadowBlur(blur) {
|
|
175
|
+
this._ctx.shadowBlur = this._doSize(blur);
|
|
176
|
+
}
|
|
177
|
+
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
178
|
+
this._ctx.ellipse(this._doSize(x), this._doSize(y), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
|
|
179
|
+
}
|
|
180
|
+
_doSize(num) {
|
|
181
|
+
return this._opts.devicePixelRatio * num;
|
|
182
|
+
}
|
|
183
|
+
_doX(x) {
|
|
184
|
+
const { scale, scrollX } = this._transform;
|
|
185
|
+
const _x = (x - scrollX) / scale;
|
|
186
|
+
return this._doSize(_x);
|
|
187
|
+
}
|
|
188
|
+
_doY(y) {
|
|
189
|
+
const { scale, scrollY } = this._transform;
|
|
190
|
+
const _y = (y - scrollY) / scale;
|
|
191
|
+
return this._doSize(_y);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export default Context;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deepClone(target: any): any;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function deepClone(target) {
|
|
2
|
+
function _clone(t) {
|
|
3
|
+
const type = is(t);
|
|
4
|
+
if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
|
|
5
|
+
return t;
|
|
6
|
+
}
|
|
7
|
+
else if (type === 'Array') {
|
|
8
|
+
const arr = [];
|
|
9
|
+
t.forEach((item) => {
|
|
10
|
+
arr.push(_clone(item));
|
|
11
|
+
});
|
|
12
|
+
return arr;
|
|
13
|
+
}
|
|
14
|
+
else if (type === 'Object') {
|
|
15
|
+
const obj = {};
|
|
16
|
+
const keys = Object.keys(t);
|
|
17
|
+
keys.forEach((key) => {
|
|
18
|
+
obj[key] = _clone(t[key]);
|
|
19
|
+
});
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return _clone(target);
|
|
24
|
+
}
|
|
25
|
+
function is(data) {
|
|
26
|
+
return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function downloadImageFromCanvas(canvas, opts) {
|
|
2
|
+
const { filename, type = 'image/jpeg' } = opts;
|
|
3
|
+
const stream = canvas.toDataURL(type);
|
|
4
|
+
const downloadLink = document.createElement('a');
|
|
5
|
+
downloadLink.href = stream;
|
|
6
|
+
downloadLink.download = filename;
|
|
7
|
+
const downloadClickEvent = document.createEvent('MouseEvents');
|
|
8
|
+
downloadClickEvent.initEvent('click', true, false);
|
|
9
|
+
downloadLink.dispatchEvent(downloadClickEvent);
|
|
10
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
declare function number(value: any): boolean;
|
|
2
|
+
declare function x(value: any): boolean;
|
|
3
|
+
declare function y(value: any): boolean;
|
|
4
|
+
declare function w(value: any): boolean;
|
|
5
|
+
declare function h(value: any): boolean;
|
|
6
|
+
declare function angle(value: any): boolean;
|
|
7
|
+
declare function borderWidth(value: any): boolean;
|
|
8
|
+
declare function borderRadius(value: any): boolean;
|
|
9
|
+
declare function color(value: any): boolean;
|
|
10
|
+
declare function imageURL(value: any): boolean;
|
|
11
|
+
declare function imageBase64(value: any): boolean;
|
|
12
|
+
declare function imageSrc(value: any): boolean;
|
|
13
|
+
declare function svg(value: any): boolean;
|
|
14
|
+
declare function html(value: any): boolean;
|
|
15
|
+
declare function text(value: any): boolean;
|
|
16
|
+
declare function fontSize(value: any): boolean;
|
|
17
|
+
declare function lineHeight(value: any): boolean;
|
|
18
|
+
declare function strokeWidth(value: any): boolean;
|
|
19
|
+
declare function textAlign(value: any): boolean;
|
|
20
|
+
declare function fontFamily(value: any): boolean;
|
|
21
|
+
declare function fontWeight(value: any): boolean;
|
|
22
|
+
declare const is: {
|
|
23
|
+
x: typeof x;
|
|
24
|
+
y: typeof y;
|
|
25
|
+
w: typeof w;
|
|
26
|
+
h: typeof h;
|
|
27
|
+
angle: typeof angle;
|
|
28
|
+
number: typeof number;
|
|
29
|
+
borderWidth: typeof borderWidth;
|
|
30
|
+
borderRadius: typeof borderRadius;
|
|
31
|
+
color: typeof color;
|
|
32
|
+
imageSrc: typeof imageSrc;
|
|
33
|
+
imageURL: typeof imageURL;
|
|
34
|
+
imageBase64: typeof imageBase64;
|
|
35
|
+
svg: typeof svg;
|
|
36
|
+
html: typeof html;
|
|
37
|
+
text: typeof text;
|
|
38
|
+
fontSize: typeof fontSize;
|
|
39
|
+
lineHeight: typeof lineHeight;
|
|
40
|
+
textAlign: typeof textAlign;
|
|
41
|
+
fontFamily: typeof fontFamily;
|
|
42
|
+
fontWeight: typeof fontWeight;
|
|
43
|
+
strokeWidth: typeof strokeWidth;
|
|
44
|
+
};
|
|
45
|
+
export default is;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { isColorStr } from './color';
|
|
2
|
+
function number(value) {
|
|
3
|
+
return (typeof value === 'number' && (value > 0 || value <= 0));
|
|
4
|
+
}
|
|
5
|
+
function x(value) {
|
|
6
|
+
return number(value);
|
|
7
|
+
}
|
|
8
|
+
function y(value) {
|
|
9
|
+
return number(value);
|
|
10
|
+
}
|
|
11
|
+
function w(value) {
|
|
12
|
+
return (typeof value === 'number' && value >= 0);
|
|
13
|
+
}
|
|
14
|
+
function h(value) {
|
|
15
|
+
return (typeof value === 'number' && value >= 0);
|
|
16
|
+
}
|
|
17
|
+
function angle(value) {
|
|
18
|
+
return (typeof value === 'number' && value >= -360 && value <= 360);
|
|
19
|
+
}
|
|
20
|
+
function borderWidth(value) {
|
|
21
|
+
return w(value);
|
|
22
|
+
}
|
|
23
|
+
function borderRadius(value) {
|
|
24
|
+
return number(value) && value >= 0;
|
|
25
|
+
}
|
|
26
|
+
function color(value) {
|
|
27
|
+
return isColorStr(value);
|
|
28
|
+
}
|
|
29
|
+
function imageURL(value) {
|
|
30
|
+
return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${value}`));
|
|
31
|
+
}
|
|
32
|
+
function imageBase64(value) {
|
|
33
|
+
return (typeof value === 'string' && /^(data:image\/)/.test(`${value}`));
|
|
34
|
+
}
|
|
35
|
+
function imageSrc(value) {
|
|
36
|
+
return (imageBase64(value) || imageURL(value));
|
|
37
|
+
}
|
|
38
|
+
function svg(value) {
|
|
39
|
+
return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test(`${value}`.trim()) && /<\/[\s]{0,}svg>$/i.test(`${value}`.trim()));
|
|
40
|
+
}
|
|
41
|
+
function html(value) {
|
|
42
|
+
let result = false;
|
|
43
|
+
if (typeof value === 'string') {
|
|
44
|
+
let div = document.createElement('div');
|
|
45
|
+
div.innerHTML = value;
|
|
46
|
+
if (div.children.length > 0) {
|
|
47
|
+
result = true;
|
|
48
|
+
}
|
|
49
|
+
div = null;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function text(value) {
|
|
54
|
+
return typeof value === 'string';
|
|
55
|
+
}
|
|
56
|
+
function fontSize(value) {
|
|
57
|
+
return number(value) && value > 0;
|
|
58
|
+
}
|
|
59
|
+
function lineHeight(value) {
|
|
60
|
+
return number(value) && value > 0;
|
|
61
|
+
}
|
|
62
|
+
function strokeWidth(value) {
|
|
63
|
+
return number(value) && value > 0;
|
|
64
|
+
}
|
|
65
|
+
function textAlign(value) {
|
|
66
|
+
return ['center', 'left', 'right'].includes(value);
|
|
67
|
+
}
|
|
68
|
+
function fontFamily(value) {
|
|
69
|
+
return typeof value === 'string' && value.length > 0;
|
|
70
|
+
}
|
|
71
|
+
function fontWeight(value) {
|
|
72
|
+
return ['bold'].includes(value);
|
|
73
|
+
}
|
|
74
|
+
const is = {
|
|
75
|
+
x, y, w, h, angle, number,
|
|
76
|
+
borderWidth, borderRadius, color,
|
|
77
|
+
imageSrc, imageURL, imageBase64, svg, html,
|
|
78
|
+
text, fontSize, lineHeight, textAlign, fontFamily, fontWeight,
|
|
79
|
+
strokeWidth,
|
|
80
|
+
};
|
|
81
|
+
export default is;
|