@hprint/plugins 0.0.2 → 0.0.3
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/package.json +3 -3
- package/src/plugins/AlignGuidLinePlugin.ts +1152 -1152
- package/src/plugins/GroupAlignPlugin.ts +365 -365
- package/src/plugins/LockPlugin.ts +242 -242
- package/src/plugins/ResizePlugin.ts +278 -278
- package/src/plugins/RulerPlugin.ts +78 -78
- package/src/plugins/UnitPlugin.ts +348 -348
- package/src/plugins/WaterMarkPlugin.ts +257 -257
- package/src/utils/ruler/ruler.ts +936 -936
|
@@ -1,257 +1,257 @@
|
|
|
1
|
-
import { cloneDeep } from 'lodash-es';
|
|
2
|
-
import { fabric } from '@hprint/core';
|
|
3
|
-
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
4
|
-
|
|
5
|
-
type IPlugin = Pick<
|
|
6
|
-
WaterMarkPlugin,
|
|
7
|
-
'drawWaterMark' | 'clearWaterMMatk' | 'updateDrawStatus'
|
|
8
|
-
>;
|
|
9
|
-
|
|
10
|
-
declare module '@hprint/core' {
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
12
|
-
interface IEditor extends IPlugin { }
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
enum POSITION {
|
|
16
|
-
lt = 'Left_Top',
|
|
17
|
-
lb = 'Left_Right',
|
|
18
|
-
rt = 'Right_Top',
|
|
19
|
-
rb = 'Right_Bottom',
|
|
20
|
-
full = 'Full',
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type IPosition =
|
|
24
|
-
| POSITION.lt
|
|
25
|
-
| POSITION.lb
|
|
26
|
-
| POSITION.rt
|
|
27
|
-
| POSITION.rb
|
|
28
|
-
| POSITION.full; // lt 左上 lr 左上 rt 右上 rb 右下 full 平铺 后续可扩展其他功能
|
|
29
|
-
type IDrawOps = {
|
|
30
|
-
text: string;
|
|
31
|
-
size: number;
|
|
32
|
-
fontFamily: string;
|
|
33
|
-
color: string;
|
|
34
|
-
isRotate: boolean;
|
|
35
|
-
position: IPosition;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const defaultOptions: IDrawOps = {
|
|
39
|
-
text: '',
|
|
40
|
-
size: 24,
|
|
41
|
-
isRotate: false, // 是否倾斜
|
|
42
|
-
fontFamily: '汉体', // 可考虑自定义字体
|
|
43
|
-
color: '#ccc', // 可考虑自定义颜色
|
|
44
|
-
position: POSITION.lt,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
class WaterMarkPlugin implements IPluginTempl {
|
|
48
|
-
static pluginName = 'WaterMarkPlugin';
|
|
49
|
-
static apis = ['drawWaterMark', 'clearWaterMMatk', 'updateDrawStatus'];
|
|
50
|
-
private hadDraw = false;
|
|
51
|
-
private drawOps: IDrawOps = defaultOptions;
|
|
52
|
-
constructor(
|
|
53
|
-
public canvas: fabric.Canvas,
|
|
54
|
-
public editor: IEditor
|
|
55
|
-
) {
|
|
56
|
-
this.init();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private createCanvas(width: number, height: number) {
|
|
60
|
-
const waterCanvas: HTMLCanvasElement = document.createElement('canvas');
|
|
61
|
-
waterCanvas.width = width;
|
|
62
|
-
waterCanvas.height = height;
|
|
63
|
-
waterCanvas.style.position = 'fixed';
|
|
64
|
-
waterCanvas.style.opacity = '0';
|
|
65
|
-
waterCanvas.style.zIndex = '-1';
|
|
66
|
-
return waterCanvas;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 待优化
|
|
70
|
-
private drawing: Record<IPosition, (...arg: any[]) => void> = {
|
|
71
|
-
[POSITION.lt]: (
|
|
72
|
-
width: number,
|
|
73
|
-
height: number,
|
|
74
|
-
cb: (imgString: string) => void
|
|
75
|
-
) => {
|
|
76
|
-
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
77
|
-
width,
|
|
78
|
-
height
|
|
79
|
-
);
|
|
80
|
-
const w = waterCanvas.width || width;
|
|
81
|
-
let ctx: CanvasRenderingContext2D | null =
|
|
82
|
-
waterCanvas.getContext('2d')!;
|
|
83
|
-
ctx.fillStyle = this.drawOps.color;
|
|
84
|
-
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
85
|
-
ctx.fillText(this.drawOps.text, 10, this.drawOps.size + 10, w - 20);
|
|
86
|
-
cb && cb(waterCanvas.toDataURL());
|
|
87
|
-
waterCanvas = null;
|
|
88
|
-
ctx = null;
|
|
89
|
-
},
|
|
90
|
-
[POSITION.rt]: (
|
|
91
|
-
width: number,
|
|
92
|
-
height: number,
|
|
93
|
-
cb: (imgString: string) => void
|
|
94
|
-
) => {
|
|
95
|
-
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
96
|
-
width,
|
|
97
|
-
height
|
|
98
|
-
);
|
|
99
|
-
let ctx: CanvasRenderingContext2D | null =
|
|
100
|
-
waterCanvas.getContext('2d')!;
|
|
101
|
-
const w = waterCanvas.width || width;
|
|
102
|
-
ctx.fillStyle = this.drawOps.color;
|
|
103
|
-
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
104
|
-
ctx.fillText(
|
|
105
|
-
this.drawOps.text,
|
|
106
|
-
w - ctx.measureText(this.drawOps.text).width - 20,
|
|
107
|
-
this.drawOps.size + 10,
|
|
108
|
-
w - 20
|
|
109
|
-
);
|
|
110
|
-
cb && cb(waterCanvas.toDataURL());
|
|
111
|
-
waterCanvas = null;
|
|
112
|
-
ctx = null;
|
|
113
|
-
},
|
|
114
|
-
[POSITION.lb]: (
|
|
115
|
-
width: number,
|
|
116
|
-
height: number,
|
|
117
|
-
cb: (imgString: string) => void
|
|
118
|
-
) => {
|
|
119
|
-
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
120
|
-
width,
|
|
121
|
-
height
|
|
122
|
-
);
|
|
123
|
-
let ctx: CanvasRenderingContext2D | null =
|
|
124
|
-
waterCanvas.getContext('2d')!;
|
|
125
|
-
const w = waterCanvas.width || width;
|
|
126
|
-
const h = waterCanvas.height || height;
|
|
127
|
-
ctx.fillStyle = this.drawOps.color;
|
|
128
|
-
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
129
|
-
ctx.fillText(this.drawOps.text, 10, h - this.drawOps.size, w - 20);
|
|
130
|
-
cb && cb(waterCanvas.toDataURL());
|
|
131
|
-
waterCanvas = null;
|
|
132
|
-
ctx = null;
|
|
133
|
-
},
|
|
134
|
-
[POSITION.rb]: (
|
|
135
|
-
width: number,
|
|
136
|
-
height: number,
|
|
137
|
-
cb: (imgString: string) => void
|
|
138
|
-
) => {
|
|
139
|
-
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
140
|
-
width,
|
|
141
|
-
height
|
|
142
|
-
);
|
|
143
|
-
let ctx: CanvasRenderingContext2D | null =
|
|
144
|
-
waterCanvas.getContext('2d')!;
|
|
145
|
-
const w = waterCanvas.width || width;
|
|
146
|
-
ctx.fillStyle = this.drawOps.color;
|
|
147
|
-
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
148
|
-
ctx.fillText(
|
|
149
|
-
this.drawOps.text,
|
|
150
|
-
w - ctx.measureText(this.drawOps.text).width - 20,
|
|
151
|
-
height - this.drawOps.size,
|
|
152
|
-
width - 20
|
|
153
|
-
);
|
|
154
|
-
cb && cb(waterCanvas.toDataURL());
|
|
155
|
-
waterCanvas = null;
|
|
156
|
-
ctx = null;
|
|
157
|
-
},
|
|
158
|
-
[POSITION.full]: (
|
|
159
|
-
width: number,
|
|
160
|
-
height: number,
|
|
161
|
-
cb: (imgString: string) => void
|
|
162
|
-
) => {
|
|
163
|
-
const angle = -30; // 按逆时针30度算
|
|
164
|
-
const R = (angle * Math.PI) / 180;
|
|
165
|
-
const font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
166
|
-
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
167
|
-
width,
|
|
168
|
-
height
|
|
169
|
-
);
|
|
170
|
-
let ctx: CanvasRenderingContext2D | null =
|
|
171
|
-
waterCanvas.getContext('2d')!;
|
|
172
|
-
ctx.font = font;
|
|
173
|
-
const textW = ctx.measureText(this.drawOps.text).width + 40;
|
|
174
|
-
let patternCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
175
|
-
this.drawOps.isRotate
|
|
176
|
-
? textW * Math.abs(Math.cos(R)) + this.drawOps.size
|
|
177
|
-
: textW,
|
|
178
|
-
this.drawOps.isRotate
|
|
179
|
-
? textW * Math.abs(Math.sin(R)) + this.drawOps.size
|
|
180
|
-
: this.drawOps.size + 20
|
|
181
|
-
);
|
|
182
|
-
document.body.appendChild(patternCanvas);
|
|
183
|
-
let ctxWater: CanvasRenderingContext2D | null =
|
|
184
|
-
patternCanvas.getContext('2d')!;
|
|
185
|
-
ctxWater.textAlign = 'left';
|
|
186
|
-
ctxWater.textBaseline = 'top';
|
|
187
|
-
ctxWater.font = font;
|
|
188
|
-
ctxWater.fillStyle = `${this.drawOps.color}`;
|
|
189
|
-
if (this.drawOps.isRotate) {
|
|
190
|
-
ctxWater.translate(0, textW * Math.abs(Math.sin(R)));
|
|
191
|
-
ctxWater.rotate(R);
|
|
192
|
-
ctxWater.fillText(this.drawOps.text, 0, 0);
|
|
193
|
-
} else {
|
|
194
|
-
ctxWater.fillText(this.drawOps.text, 10, 10);
|
|
195
|
-
}
|
|
196
|
-
ctx.fillStyle = ctx.createPattern(patternCanvas, 'repeat')!;
|
|
197
|
-
ctx.fillRect(0, 0, width, height);
|
|
198
|
-
cb && cb(waterCanvas.toDataURL());
|
|
199
|
-
waterCanvas = null;
|
|
200
|
-
patternCanvas = null;
|
|
201
|
-
ctx = null;
|
|
202
|
-
ctxWater = null;
|
|
203
|
-
},
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
drawWaterMark(ops: IDrawOps) {
|
|
207
|
-
this.drawOps = Object.assign(cloneDeep(this.drawOps), ops);
|
|
208
|
-
if (!this.drawOps.text) return;
|
|
209
|
-
const workspace = this.canvas
|
|
210
|
-
.getObjects()
|
|
211
|
-
.find((item: any) => item.id === 'workspace');
|
|
212
|
-
const { width, height, left, top }: any = workspace;
|
|
213
|
-
this.drawing[this.drawOps?.position](
|
|
214
|
-
width,
|
|
215
|
-
height,
|
|
216
|
-
(imgString: string) => {
|
|
217
|
-
this.canvas.overlayImage = undefined;
|
|
218
|
-
this.hadDraw = true;
|
|
219
|
-
this.canvas.setOverlayImage(
|
|
220
|
-
imgString,
|
|
221
|
-
this.canvas.renderAll.bind(this.canvas),
|
|
222
|
-
{
|
|
223
|
-
left: left || 0,
|
|
224
|
-
top: top || 0,
|
|
225
|
-
originX: 'left',
|
|
226
|
-
originY: 'top',
|
|
227
|
-
}
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// 更新handDrow 导入json时无法知道是否绘制
|
|
234
|
-
updateDrawStatus(status: boolean) {
|
|
235
|
-
this.hadDraw = status;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
clearWaterMMatk() {
|
|
239
|
-
if (!this.hadDraw) return;
|
|
240
|
-
this.canvas.overlayImage = undefined;
|
|
241
|
-
this.canvas.renderAll();
|
|
242
|
-
this.hadDraw = false;
|
|
243
|
-
this.drawOps = defaultOptions;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
init() {
|
|
247
|
-
// TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
|
|
248
|
-
this.editor.on('sizeChange', this.drawWaterMark.bind(this));
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
destroy() {
|
|
252
|
-
// TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
|
|
253
|
-
this.editor.off('sizeChange', this.drawWaterMark);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export default WaterMarkPlugin;
|
|
1
|
+
import { cloneDeep } from 'lodash-es';
|
|
2
|
+
import { fabric } from '@hprint/core';
|
|
3
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
4
|
+
|
|
5
|
+
type IPlugin = Pick<
|
|
6
|
+
WaterMarkPlugin,
|
|
7
|
+
'drawWaterMark' | 'clearWaterMMatk' | 'updateDrawStatus'
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
declare module '@hprint/core' {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
12
|
+
interface IEditor extends IPlugin { }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
enum POSITION {
|
|
16
|
+
lt = 'Left_Top',
|
|
17
|
+
lb = 'Left_Right',
|
|
18
|
+
rt = 'Right_Top',
|
|
19
|
+
rb = 'Right_Bottom',
|
|
20
|
+
full = 'Full',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type IPosition =
|
|
24
|
+
| POSITION.lt
|
|
25
|
+
| POSITION.lb
|
|
26
|
+
| POSITION.rt
|
|
27
|
+
| POSITION.rb
|
|
28
|
+
| POSITION.full; // lt 左上 lr 左上 rt 右上 rb 右下 full 平铺 后续可扩展其他功能
|
|
29
|
+
type IDrawOps = {
|
|
30
|
+
text: string;
|
|
31
|
+
size: number;
|
|
32
|
+
fontFamily: string;
|
|
33
|
+
color: string;
|
|
34
|
+
isRotate: boolean;
|
|
35
|
+
position: IPosition;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const defaultOptions: IDrawOps = {
|
|
39
|
+
text: '',
|
|
40
|
+
size: 24,
|
|
41
|
+
isRotate: false, // 是否倾斜
|
|
42
|
+
fontFamily: '汉体', // 可考虑自定义字体
|
|
43
|
+
color: '#ccc', // 可考虑自定义颜色
|
|
44
|
+
position: POSITION.lt,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class WaterMarkPlugin implements IPluginTempl {
|
|
48
|
+
static pluginName = 'WaterMarkPlugin';
|
|
49
|
+
static apis = ['drawWaterMark', 'clearWaterMMatk', 'updateDrawStatus'];
|
|
50
|
+
private hadDraw = false;
|
|
51
|
+
private drawOps: IDrawOps = defaultOptions;
|
|
52
|
+
constructor(
|
|
53
|
+
public canvas: fabric.Canvas,
|
|
54
|
+
public editor: IEditor
|
|
55
|
+
) {
|
|
56
|
+
this.init();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private createCanvas(width: number, height: number) {
|
|
60
|
+
const waterCanvas: HTMLCanvasElement = document.createElement('canvas');
|
|
61
|
+
waterCanvas.width = width;
|
|
62
|
+
waterCanvas.height = height;
|
|
63
|
+
waterCanvas.style.position = 'fixed';
|
|
64
|
+
waterCanvas.style.opacity = '0';
|
|
65
|
+
waterCanvas.style.zIndex = '-1';
|
|
66
|
+
return waterCanvas;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 待优化
|
|
70
|
+
private drawing: Record<IPosition, (...arg: any[]) => void> = {
|
|
71
|
+
[POSITION.lt]: (
|
|
72
|
+
width: number,
|
|
73
|
+
height: number,
|
|
74
|
+
cb: (imgString: string) => void
|
|
75
|
+
) => {
|
|
76
|
+
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
77
|
+
width,
|
|
78
|
+
height
|
|
79
|
+
);
|
|
80
|
+
const w = waterCanvas.width || width;
|
|
81
|
+
let ctx: CanvasRenderingContext2D | null =
|
|
82
|
+
waterCanvas.getContext('2d')!;
|
|
83
|
+
ctx.fillStyle = this.drawOps.color;
|
|
84
|
+
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
85
|
+
ctx.fillText(this.drawOps.text, 10, this.drawOps.size + 10, w - 20);
|
|
86
|
+
cb && cb(waterCanvas.toDataURL());
|
|
87
|
+
waterCanvas = null;
|
|
88
|
+
ctx = null;
|
|
89
|
+
},
|
|
90
|
+
[POSITION.rt]: (
|
|
91
|
+
width: number,
|
|
92
|
+
height: number,
|
|
93
|
+
cb: (imgString: string) => void
|
|
94
|
+
) => {
|
|
95
|
+
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
96
|
+
width,
|
|
97
|
+
height
|
|
98
|
+
);
|
|
99
|
+
let ctx: CanvasRenderingContext2D | null =
|
|
100
|
+
waterCanvas.getContext('2d')!;
|
|
101
|
+
const w = waterCanvas.width || width;
|
|
102
|
+
ctx.fillStyle = this.drawOps.color;
|
|
103
|
+
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
104
|
+
ctx.fillText(
|
|
105
|
+
this.drawOps.text,
|
|
106
|
+
w - ctx.measureText(this.drawOps.text).width - 20,
|
|
107
|
+
this.drawOps.size + 10,
|
|
108
|
+
w - 20
|
|
109
|
+
);
|
|
110
|
+
cb && cb(waterCanvas.toDataURL());
|
|
111
|
+
waterCanvas = null;
|
|
112
|
+
ctx = null;
|
|
113
|
+
},
|
|
114
|
+
[POSITION.lb]: (
|
|
115
|
+
width: number,
|
|
116
|
+
height: number,
|
|
117
|
+
cb: (imgString: string) => void
|
|
118
|
+
) => {
|
|
119
|
+
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
120
|
+
width,
|
|
121
|
+
height
|
|
122
|
+
);
|
|
123
|
+
let ctx: CanvasRenderingContext2D | null =
|
|
124
|
+
waterCanvas.getContext('2d')!;
|
|
125
|
+
const w = waterCanvas.width || width;
|
|
126
|
+
const h = waterCanvas.height || height;
|
|
127
|
+
ctx.fillStyle = this.drawOps.color;
|
|
128
|
+
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
129
|
+
ctx.fillText(this.drawOps.text, 10, h - this.drawOps.size, w - 20);
|
|
130
|
+
cb && cb(waterCanvas.toDataURL());
|
|
131
|
+
waterCanvas = null;
|
|
132
|
+
ctx = null;
|
|
133
|
+
},
|
|
134
|
+
[POSITION.rb]: (
|
|
135
|
+
width: number,
|
|
136
|
+
height: number,
|
|
137
|
+
cb: (imgString: string) => void
|
|
138
|
+
) => {
|
|
139
|
+
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
140
|
+
width,
|
|
141
|
+
height
|
|
142
|
+
);
|
|
143
|
+
let ctx: CanvasRenderingContext2D | null =
|
|
144
|
+
waterCanvas.getContext('2d')!;
|
|
145
|
+
const w = waterCanvas.width || width;
|
|
146
|
+
ctx.fillStyle = this.drawOps.color;
|
|
147
|
+
ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
148
|
+
ctx.fillText(
|
|
149
|
+
this.drawOps.text,
|
|
150
|
+
w - ctx.measureText(this.drawOps.text).width - 20,
|
|
151
|
+
height - this.drawOps.size,
|
|
152
|
+
width - 20
|
|
153
|
+
);
|
|
154
|
+
cb && cb(waterCanvas.toDataURL());
|
|
155
|
+
waterCanvas = null;
|
|
156
|
+
ctx = null;
|
|
157
|
+
},
|
|
158
|
+
[POSITION.full]: (
|
|
159
|
+
width: number,
|
|
160
|
+
height: number,
|
|
161
|
+
cb: (imgString: string) => void
|
|
162
|
+
) => {
|
|
163
|
+
const angle = -30; // 按逆时针30度算
|
|
164
|
+
const R = (angle * Math.PI) / 180;
|
|
165
|
+
const font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
|
|
166
|
+
let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
167
|
+
width,
|
|
168
|
+
height
|
|
169
|
+
);
|
|
170
|
+
let ctx: CanvasRenderingContext2D | null =
|
|
171
|
+
waterCanvas.getContext('2d')!;
|
|
172
|
+
ctx.font = font;
|
|
173
|
+
const textW = ctx.measureText(this.drawOps.text).width + 40;
|
|
174
|
+
let patternCanvas: HTMLCanvasElement | null = this.createCanvas(
|
|
175
|
+
this.drawOps.isRotate
|
|
176
|
+
? textW * Math.abs(Math.cos(R)) + this.drawOps.size
|
|
177
|
+
: textW,
|
|
178
|
+
this.drawOps.isRotate
|
|
179
|
+
? textW * Math.abs(Math.sin(R)) + this.drawOps.size
|
|
180
|
+
: this.drawOps.size + 20
|
|
181
|
+
);
|
|
182
|
+
document.body.appendChild(patternCanvas);
|
|
183
|
+
let ctxWater: CanvasRenderingContext2D | null =
|
|
184
|
+
patternCanvas.getContext('2d')!;
|
|
185
|
+
ctxWater.textAlign = 'left';
|
|
186
|
+
ctxWater.textBaseline = 'top';
|
|
187
|
+
ctxWater.font = font;
|
|
188
|
+
ctxWater.fillStyle = `${this.drawOps.color}`;
|
|
189
|
+
if (this.drawOps.isRotate) {
|
|
190
|
+
ctxWater.translate(0, textW * Math.abs(Math.sin(R)));
|
|
191
|
+
ctxWater.rotate(R);
|
|
192
|
+
ctxWater.fillText(this.drawOps.text, 0, 0);
|
|
193
|
+
} else {
|
|
194
|
+
ctxWater.fillText(this.drawOps.text, 10, 10);
|
|
195
|
+
}
|
|
196
|
+
ctx.fillStyle = ctx.createPattern(patternCanvas, 'repeat')!;
|
|
197
|
+
ctx.fillRect(0, 0, width, height);
|
|
198
|
+
cb && cb(waterCanvas.toDataURL());
|
|
199
|
+
waterCanvas = null;
|
|
200
|
+
patternCanvas = null;
|
|
201
|
+
ctx = null;
|
|
202
|
+
ctxWater = null;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
drawWaterMark(ops: IDrawOps) {
|
|
207
|
+
this.drawOps = Object.assign(cloneDeep(this.drawOps), ops);
|
|
208
|
+
if (!this.drawOps.text) return;
|
|
209
|
+
const workspace = this.canvas
|
|
210
|
+
.getObjects()
|
|
211
|
+
.find((item: any) => item.id === 'workspace');
|
|
212
|
+
const { width, height, left, top }: any = workspace;
|
|
213
|
+
this.drawing[this.drawOps?.position](
|
|
214
|
+
width,
|
|
215
|
+
height,
|
|
216
|
+
(imgString: string) => {
|
|
217
|
+
this.canvas.overlayImage = undefined;
|
|
218
|
+
this.hadDraw = true;
|
|
219
|
+
this.canvas.setOverlayImage(
|
|
220
|
+
imgString,
|
|
221
|
+
this.canvas.renderAll.bind(this.canvas),
|
|
222
|
+
{
|
|
223
|
+
left: left || 0,
|
|
224
|
+
top: top || 0,
|
|
225
|
+
originX: 'left',
|
|
226
|
+
originY: 'top',
|
|
227
|
+
}
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// 更新handDrow 导入json时无法知道是否绘制
|
|
234
|
+
updateDrawStatus(status: boolean) {
|
|
235
|
+
this.hadDraw = status;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
clearWaterMMatk() {
|
|
239
|
+
if (!this.hadDraw) return;
|
|
240
|
+
this.canvas.overlayImage = undefined;
|
|
241
|
+
this.canvas.renderAll();
|
|
242
|
+
this.hadDraw = false;
|
|
243
|
+
this.drawOps = defaultOptions;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
init() {
|
|
247
|
+
// TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
|
|
248
|
+
this.editor.on('sizeChange', this.drawWaterMark.bind(this));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
destroy() {
|
|
252
|
+
// TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
|
|
253
|
+
this.editor.off('sizeChange', this.drawWaterMark);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export default WaterMarkPlugin;
|