watermark-js-plus 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -3
- package/dist/core/blind.d.ts +17 -0
- package/dist/core/watermark.d.ts +40 -0
- package/dist/index.d.ts +3 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/watermark.esm.js +489 -0
- package/dist/watermark.umd.js +498 -0
- package/dist/watermark.umd.min.js +1 -0
- package/package.json +14 -20
- package/dist/index.js +0 -400
- package/dist/index.min.js +0 -1
package/dist/index.js
DELETED
|
@@ -1,400 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.watermark = {}));
|
|
5
|
-
})(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
const convertImage = (canvas) => {
|
|
8
|
-
return canvas.toDataURL('image/png', 1);
|
|
9
|
-
};
|
|
10
|
-
const isFunction = (value) => {
|
|
11
|
-
return typeof value === 'function';
|
|
12
|
-
};
|
|
13
|
-
const createSVGElement = (tagName, attrs = {}, namespaceURI = 'http://www.w3.org/2000/svg') => {
|
|
14
|
-
const element = document.createElementNS(namespaceURI, tagName);
|
|
15
|
-
for (const attr in attrs) {
|
|
16
|
-
element.setAttribute(attr, attrs[attr]);
|
|
17
|
-
}
|
|
18
|
-
return element;
|
|
19
|
-
};
|
|
20
|
-
const getMultiLineData = (ctx, text, maxWidth) => {
|
|
21
|
-
const result = [];
|
|
22
|
-
let str = '';
|
|
23
|
-
for (let i = 0, len = text.length; i < len; i++) {
|
|
24
|
-
str += text.charAt(i);
|
|
25
|
-
if (ctx.measureText(str).width > maxWidth) {
|
|
26
|
-
result.push(str.substring(0, str.length - 1));
|
|
27
|
-
str = '';
|
|
28
|
-
i--;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
result.push(str);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
const createCustomContentSVG = (ctx, options) => {
|
|
35
|
-
const svgElement = createSVGElement('svg', {
|
|
36
|
-
xmlns: 'http://www.w3.org/2000/svg'
|
|
37
|
-
});
|
|
38
|
-
const foreignObjectElement = createSVGElement('foreignObject', {
|
|
39
|
-
width: options.width.toString(),
|
|
40
|
-
height: options.height.toString()
|
|
41
|
-
});
|
|
42
|
-
const bodyElement = document.createElement('div');
|
|
43
|
-
bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
44
|
-
bodyElement.style.cssText = `
|
|
45
|
-
text-align: center;
|
|
46
|
-
display: flex;
|
|
47
|
-
align-items: center;
|
|
48
|
-
justify-content: center;
|
|
49
|
-
width: 100%;
|
|
50
|
-
height: 100%;
|
|
51
|
-
font: ${ctx.font};
|
|
52
|
-
color: ${options.fontColor};
|
|
53
|
-
`;
|
|
54
|
-
bodyElement.innerHTML = options.content;
|
|
55
|
-
foreignObjectElement.appendChild(bodyElement);
|
|
56
|
-
svgElement.appendChild(foreignObjectElement);
|
|
57
|
-
return svgElement;
|
|
58
|
-
};
|
|
59
|
-
const convertSVGToImage = (svg) => {
|
|
60
|
-
const richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
|
|
61
|
-
return `data:image/svg+xml;charset=utf-8,${richContent}`;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
exports.ContentTypeEnum = void 0;
|
|
65
|
-
(function (ContentTypeEnum) {
|
|
66
|
-
ContentTypeEnum["text"] = "text";
|
|
67
|
-
ContentTypeEnum["image"] = "image";
|
|
68
|
-
ContentTypeEnum["multiLineText"] = "multi-line-text";
|
|
69
|
-
ContentTypeEnum["richText"] = "rich-text";
|
|
70
|
-
})(exports.ContentTypeEnum || (exports.ContentTypeEnum = {}));
|
|
71
|
-
exports.TextAlignEnum = void 0;
|
|
72
|
-
(function (TextAlignEnum) {
|
|
73
|
-
TextAlignEnum["center"] = "center";
|
|
74
|
-
TextAlignEnum["left"] = "left";
|
|
75
|
-
TextAlignEnum["right"] = "right";
|
|
76
|
-
})(exports.TextAlignEnum || (exports.TextAlignEnum = {}));
|
|
77
|
-
exports.TextBaselineEnum = void 0;
|
|
78
|
-
(function (TextBaselineEnum) {
|
|
79
|
-
TextBaselineEnum["top"] = "top";
|
|
80
|
-
TextBaselineEnum["bottom"] = "bottom";
|
|
81
|
-
TextBaselineEnum["middle"] = "middle";
|
|
82
|
-
})(exports.TextBaselineEnum || (exports.TextBaselineEnum = {}));
|
|
83
|
-
exports.CreateWatermarkModeEnum = void 0;
|
|
84
|
-
(function (CreateWatermarkModeEnum) {
|
|
85
|
-
CreateWatermarkModeEnum["default"] = "default";
|
|
86
|
-
CreateWatermarkModeEnum["blind"] = "blind";
|
|
87
|
-
})(exports.CreateWatermarkModeEnum || (exports.CreateWatermarkModeEnum = {}));
|
|
88
|
-
exports.DecodeBlindWatermarkModeEnum = void 0;
|
|
89
|
-
(function (DecodeBlindWatermarkModeEnum) {
|
|
90
|
-
DecodeBlindWatermarkModeEnum["canvas"] = "canvas";
|
|
91
|
-
DecodeBlindWatermarkModeEnum["html"] = "html";
|
|
92
|
-
DecodeBlindWatermarkModeEnum["svg"] = "svg";
|
|
93
|
-
})(exports.DecodeBlindWatermarkModeEnum || (exports.DecodeBlindWatermarkModeEnum = {}));
|
|
94
|
-
|
|
95
|
-
class Watermark {
|
|
96
|
-
options;
|
|
97
|
-
parentElement = document.body;
|
|
98
|
-
observer;
|
|
99
|
-
parentObserve;
|
|
100
|
-
watermarkDom;
|
|
101
|
-
constructor(props = {}) {
|
|
102
|
-
this.options = Object.assign({
|
|
103
|
-
width: 300,
|
|
104
|
-
height: 300,
|
|
105
|
-
rotate: 45,
|
|
106
|
-
contentType: exports.ContentTypeEnum.text,
|
|
107
|
-
content: 'hello watermark-js-plus',
|
|
108
|
-
imageWidth: 0,
|
|
109
|
-
imageHeight: 0,
|
|
110
|
-
lineHeight: 30,
|
|
111
|
-
zIndex: 10000,
|
|
112
|
-
backgroundPosition: '0 0, 0 0',
|
|
113
|
-
fontSize: 20,
|
|
114
|
-
fontFamily: 'sans-serif',
|
|
115
|
-
textAlign: exports.TextAlignEnum.center,
|
|
116
|
-
textBaseline: exports.TextBaselineEnum.middle,
|
|
117
|
-
fontColor: '#000',
|
|
118
|
-
globalAlpha: 0.5,
|
|
119
|
-
fontWeight: 'normal',
|
|
120
|
-
mode: exports.CreateWatermarkModeEnum.default,
|
|
121
|
-
mutationObserve: true,
|
|
122
|
-
unique: true,
|
|
123
|
-
parent: 'body',
|
|
124
|
-
onSuccess: () => { },
|
|
125
|
-
onBeforeDestroy: () => { },
|
|
126
|
-
onDestroyed: () => { }
|
|
127
|
-
}, props);
|
|
128
|
-
if (this.options?.rotate) {
|
|
129
|
-
this.options.rotate = (360 - this.options.rotate % 360) * (Math.PI / 180);
|
|
130
|
-
}
|
|
131
|
-
this.changeParentElement(this.options.parent);
|
|
132
|
-
}
|
|
133
|
-
static createCanvas(width, height) {
|
|
134
|
-
const ratio = window.devicePixelRatio || 1;
|
|
135
|
-
const canvas = document.createElement('canvas');
|
|
136
|
-
canvas.width = width * ratio; // 实际渲染像素
|
|
137
|
-
canvas.height = height * ratio; // 实际渲染像素
|
|
138
|
-
canvas.style.width = `${width}px`; // 控制显示大小
|
|
139
|
-
canvas.style.height = `${height}px`; // 控制显示大小
|
|
140
|
-
canvas.getContext('2d')?.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
141
|
-
return canvas;
|
|
142
|
-
}
|
|
143
|
-
changeParentElement(parent) {
|
|
144
|
-
if (typeof parent === 'string') {
|
|
145
|
-
const parentElement = document.querySelector(parent);
|
|
146
|
-
parentElement && (this.parentElement = parentElement);
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
this.parentElement = parent;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
async create() {
|
|
153
|
-
if (!this.validateUnique()) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (!this.validateContent()) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
const canvas = await this.draw();
|
|
160
|
-
const image = convertImage(canvas);
|
|
161
|
-
this.watermarkDom = document.createElement('div');
|
|
162
|
-
const watermarkInnerDom = document.createElement('div');
|
|
163
|
-
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
164
|
-
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
165
|
-
this.watermarkDom.style.cssText = `
|
|
166
|
-
z-index: ${this.options.zIndex};
|
|
167
|
-
position: relative;
|
|
168
|
-
`;
|
|
169
|
-
watermarkInnerDom.style.cssText = `
|
|
170
|
-
position: fixed;
|
|
171
|
-
z-index: ${this.options.zIndex};
|
|
172
|
-
pointer-events: none;
|
|
173
|
-
top: 0;
|
|
174
|
-
bottom: 0;
|
|
175
|
-
left: 0;
|
|
176
|
-
right: 0;
|
|
177
|
-
background-image: url(${image});
|
|
178
|
-
background-repeat: repeat;
|
|
179
|
-
background-size: ${this.options.width}px ${this.options.height}px;
|
|
180
|
-
background-position: ${this.options.backgroundPosition};
|
|
181
|
-
-webkit-print-color-adjust: exact;
|
|
182
|
-
`;
|
|
183
|
-
this.watermarkDom.append(watermarkInnerDom);
|
|
184
|
-
this.parentElement.appendChild(this.watermarkDom);
|
|
185
|
-
if (this.options.mutationObserve) {
|
|
186
|
-
this.bindMutationObserve();
|
|
187
|
-
}
|
|
188
|
-
this.options.onSuccess?.();
|
|
189
|
-
}
|
|
190
|
-
destroy() {
|
|
191
|
-
this.options.onBeforeDestroy?.();
|
|
192
|
-
this.observer?.disconnect();
|
|
193
|
-
this.parentObserve?.disconnect();
|
|
194
|
-
this.watermarkDom?.remove();
|
|
195
|
-
this.options.onDestroyed?.();
|
|
196
|
-
}
|
|
197
|
-
validateUnique() {
|
|
198
|
-
let result = true;
|
|
199
|
-
if (this.options.unique) {
|
|
200
|
-
this.parentElement.childNodes.forEach(node => {
|
|
201
|
-
if (!result) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
|
|
205
|
-
result = false;
|
|
206
|
-
// throw new Error('duplicate watermark error')
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
return result;
|
|
211
|
-
}
|
|
212
|
-
validateContent() {
|
|
213
|
-
switch (this.options.contentType) {
|
|
214
|
-
case exports.ContentTypeEnum.image:
|
|
215
|
-
return Object.hasOwnProperty.call(this.options, 'image');
|
|
216
|
-
case exports.ContentTypeEnum.multiLineText:
|
|
217
|
-
case exports.ContentTypeEnum.richText:
|
|
218
|
-
case exports.ContentTypeEnum.text:
|
|
219
|
-
return this.options.content.length > 0;
|
|
220
|
-
}
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
draw() {
|
|
224
|
-
const canvas = Watermark.createCanvas(this.options.width, this.options.height);
|
|
225
|
-
// document.body?.appendChild(canvas)
|
|
226
|
-
const ctx = canvas.getContext('2d');
|
|
227
|
-
if (ctx === null) {
|
|
228
|
-
throw new Error('get context error');
|
|
229
|
-
}
|
|
230
|
-
ctx.font = `${this.options.fontWeight} ${this.options.fontSize}px ${this.options.fontFamily}`;
|
|
231
|
-
ctx.textAlign = this.options.textAlign;
|
|
232
|
-
ctx.textBaseline = this.options.textBaseline;
|
|
233
|
-
ctx.fillStyle = this.options.fontColor;
|
|
234
|
-
ctx.globalAlpha = this.options.globalAlpha;
|
|
235
|
-
return new Promise((resolve) => {
|
|
236
|
-
switch (this.options.contentType) {
|
|
237
|
-
case exports.ContentTypeEnum.text:
|
|
238
|
-
this.drawText(ctx, resolve);
|
|
239
|
-
break;
|
|
240
|
-
case exports.ContentTypeEnum.image:
|
|
241
|
-
this.drawImage(ctx, resolve);
|
|
242
|
-
break;
|
|
243
|
-
case exports.ContentTypeEnum.multiLineText:
|
|
244
|
-
this.drawMultiLineText(ctx, resolve);
|
|
245
|
-
break;
|
|
246
|
-
case exports.ContentTypeEnum.richText:
|
|
247
|
-
this.drawRichText(ctx, resolve);
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
drawText(ctx, resolve) {
|
|
253
|
-
ctx.translate(this.options.width / 2, this.options.height / 2);
|
|
254
|
-
ctx.rotate(this.options.rotate);
|
|
255
|
-
ctx.fillText(this.options.content, 0, 0);
|
|
256
|
-
resolve(ctx.canvas);
|
|
257
|
-
}
|
|
258
|
-
drawImage(ctx, resolve) {
|
|
259
|
-
const image = new Image();
|
|
260
|
-
image.setAttribute('crossOrigin', 'Anonymous');
|
|
261
|
-
image.src = this.options.image;
|
|
262
|
-
image.onload = () => {
|
|
263
|
-
ctx.translate(this.options.width / 2, this.options.height / 2);
|
|
264
|
-
ctx.rotate(this.options.rotate);
|
|
265
|
-
const { width: imageWidth, height: imageHeight } = this.getImageRect(image);
|
|
266
|
-
ctx.drawImage(image, 0 - imageWidth / 2, 0 - imageHeight / 2, imageWidth, imageHeight);
|
|
267
|
-
resolve(ctx.canvas);
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
drawMultiLineText(ctx, resolve) {
|
|
271
|
-
// image.width = this.options.width
|
|
272
|
-
// image.height = this.options.height
|
|
273
|
-
// const element = createCustomContentSvg(context, this.options)
|
|
274
|
-
// image.src = convertSVGToImage(element)
|
|
275
|
-
// image.onload = () => {
|
|
276
|
-
// context.translate(this.options.width / 2, this.options.height / 2)
|
|
277
|
-
// context.rotate(this.options.rotate)
|
|
278
|
-
// context.drawImage(
|
|
279
|
-
// image,
|
|
280
|
-
// -this.options.width / 2,
|
|
281
|
-
// -this.options.height / 2,
|
|
282
|
-
// context.canvas.width,
|
|
283
|
-
// context.canvas.height
|
|
284
|
-
// )
|
|
285
|
-
// resolve(canvas)
|
|
286
|
-
// }
|
|
287
|
-
const lines = getMultiLineData(ctx, this.options.content, this.options.width);
|
|
288
|
-
ctx.translate(this.options.width / 2, this.options.height / 2);
|
|
289
|
-
ctx.rotate(this.options.rotate);
|
|
290
|
-
const yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
|
|
291
|
-
lines.forEach((txt, index) => {
|
|
292
|
-
ctx.fillText(txt, 0, this.options.lineHeight * index - yOffsetValue);
|
|
293
|
-
});
|
|
294
|
-
resolve(ctx.canvas);
|
|
295
|
-
}
|
|
296
|
-
drawRichText(ctx, resolve) {
|
|
297
|
-
const image = new Image();
|
|
298
|
-
image.width = this.options.width;
|
|
299
|
-
image.height = this.options.height;
|
|
300
|
-
const element = createCustomContentSVG(ctx, this.options);
|
|
301
|
-
image.src = convertSVGToImage(element);
|
|
302
|
-
image.onload = () => {
|
|
303
|
-
ctx.translate(this.options.width / 2, this.options.height / 2);
|
|
304
|
-
ctx.rotate(this.options.rotate);
|
|
305
|
-
ctx.drawImage(image, -this.options.width / 2, -this.options.height / 2, ctx.canvas.width, ctx.canvas.height);
|
|
306
|
-
resolve(ctx.canvas);
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
getImageRect(image) {
|
|
310
|
-
const rect = { width: this.options.imageWidth, height: this.options.imageHeight };
|
|
311
|
-
switch (true) {
|
|
312
|
-
case rect.width !== 0 && rect.height === 0:
|
|
313
|
-
rect.height = rect.width * image.height / image.width;
|
|
314
|
-
break;
|
|
315
|
-
case rect.width === 0 && rect.height !== 0:
|
|
316
|
-
rect.width = rect.height * image.width / image.height;
|
|
317
|
-
break;
|
|
318
|
-
case rect.width === 0 && rect.height === 0:
|
|
319
|
-
rect.width = image.width;
|
|
320
|
-
rect.height = image.height;
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
return rect;
|
|
324
|
-
}
|
|
325
|
-
bindMutationObserve() {
|
|
326
|
-
if (!this.watermarkDom) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
this.observer = new MutationObserver((mutationsList) => {
|
|
330
|
-
if (mutationsList.length > 0) {
|
|
331
|
-
this.destroy();
|
|
332
|
-
this.create();
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
this.observer.observe(this.watermarkDom, {
|
|
336
|
-
attributes: true,
|
|
337
|
-
childList: true,
|
|
338
|
-
subtree: true,
|
|
339
|
-
characterData: true // 节点内容或节点文本的变动。
|
|
340
|
-
});
|
|
341
|
-
this.parentObserve = new MutationObserver((mutationsList) => {
|
|
342
|
-
mutationsList.forEach(item => {
|
|
343
|
-
if (item?.target === this.watermarkDom || item?.removedNodes?.[0] === this.watermarkDom) {
|
|
344
|
-
this.destroy();
|
|
345
|
-
this.create();
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
this.parentObserve.observe(this.parentElement, {
|
|
350
|
-
attributes: true,
|
|
351
|
-
childList: true,
|
|
352
|
-
subtree: true,
|
|
353
|
-
characterData: true // 节点内容或节点文本的变动。
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
class BlindWatermark extends Watermark {
|
|
359
|
-
constructor(props = {}) {
|
|
360
|
-
props.globalAlpha = 0.005;
|
|
361
|
-
props.mode = exports.CreateWatermarkModeEnum.blind;
|
|
362
|
-
super(props);
|
|
363
|
-
}
|
|
364
|
-
static decode(props) {
|
|
365
|
-
const options = Object.assign({
|
|
366
|
-
url: '',
|
|
367
|
-
fillColor: '#000',
|
|
368
|
-
compositeOperation: 'color-burn',
|
|
369
|
-
mode: exports.DecodeBlindWatermarkModeEnum.canvas
|
|
370
|
-
}, props);
|
|
371
|
-
if (!options.url) {
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
if (options.mode === exports.DecodeBlindWatermarkModeEnum.canvas) {
|
|
375
|
-
const img = new Image();
|
|
376
|
-
img.src = options.url;
|
|
377
|
-
img.onload = () => {
|
|
378
|
-
const { width, height } = img;
|
|
379
|
-
const canvas = Watermark.createCanvas(width, height);
|
|
380
|
-
const ctx = canvas.getContext('2d');
|
|
381
|
-
if (ctx === null) {
|
|
382
|
-
throw new Error('get context error');
|
|
383
|
-
}
|
|
384
|
-
ctx.drawImage(img, 0, 0, width, height);
|
|
385
|
-
ctx.globalCompositeOperation = options.compositeOperation;
|
|
386
|
-
ctx.fillStyle = options.fillColor;
|
|
387
|
-
ctx.fillRect(0, 0, width, height);
|
|
388
|
-
const resultImage = convertImage(canvas);
|
|
389
|
-
if (options.onSuccess && isFunction(options.onSuccess)) {
|
|
390
|
-
options.onSuccess?.(resultImage);
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
exports.BlindWatermark = BlindWatermark;
|
|
398
|
-
exports.Watermark = Watermark;
|
|
399
|
-
|
|
400
|
-
}));
|
package/dist/index.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).watermark={})}(this,(function(t){"use strict";const e=t=>t.toDataURL("image/png",1),n=(t,e={},n="http://www.w3.org/2000/svg")=>{const i=document.createElementNS(n,t);for(const t in e)i.setAttribute(t,e[t]);return i};var i,o,s,a,r;t.ContentTypeEnum=void 0,(i=t.ContentTypeEnum||(t.ContentTypeEnum={})).text="text",i.image="image",i.multiLineText="multi-line-text",i.richText="rich-text",t.TextAlignEnum=void 0,(o=t.TextAlignEnum||(t.TextAlignEnum={})).center="center",o.left="left",o.right="right",t.TextBaselineEnum=void 0,(s=t.TextBaselineEnum||(t.TextBaselineEnum={})).top="top",s.bottom="bottom",s.middle="middle",t.CreateWatermarkModeEnum=void 0,(a=t.CreateWatermarkModeEnum||(t.CreateWatermarkModeEnum={})).default="default",a.blind="blind",t.DecodeBlindWatermarkModeEnum=void 0,(r=t.DecodeBlindWatermarkModeEnum||(t.DecodeBlindWatermarkModeEnum={})).canvas="canvas",r.html="html",r.svg="svg";class h{options;parentElement=document.body;observer;parentObserve;watermarkDom;constructor(e={}){this.options=Object.assign({width:300,height:300,rotate:45,contentType:t.ContentTypeEnum.text,content:"hello watermark-js-plus",imageWidth:0,imageHeight:0,lineHeight:30,zIndex:1e4,backgroundPosition:"0 0, 0 0",fontSize:20,fontFamily:"sans-serif",textAlign:t.TextAlignEnum.center,textBaseline:t.TextBaselineEnum.middle,fontColor:"#000",globalAlpha:.5,fontWeight:"normal",mode:t.CreateWatermarkModeEnum.default,mutationObserve:!0,unique:!0,parent:"body",onSuccess:()=>{},onBeforeDestroy:()=>{},onDestroyed:()=>{}},e),this.options?.rotate&&(this.options.rotate=(360-this.options.rotate%360)*(Math.PI/180)),this.changeParentElement(this.options.parent)}static createCanvas(t,e){const n=window.devicePixelRatio||1,i=document.createElement("canvas");return i.width=t*n,i.height=e*n,i.style.width=`${t}px`,i.style.height=`${e}px`,i.getContext("2d")?.setTransform(n,0,0,n,0,0),i}changeParentElement(t){if("string"==typeof t){const e=document.querySelector(t);e&&(this.parentElement=e)}else this.parentElement=t}async create(){if(!this.validateUnique())return;if(!this.validateContent())return;const t=await this.draw(),n=e(t);this.watermarkDom=document.createElement("div");const i=document.createElement("div");this.watermarkDom.__WATERMARK__="watermark",this.watermarkDom.__WATERMARK__INSTANCE__=this,this.watermarkDom.style.cssText=`\n z-index: ${this.options.zIndex};\n position: relative;\n `,i.style.cssText=`\n position: fixed;\n z-index: ${this.options.zIndex};\n pointer-events: none;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n background-image: url(${n});\n background-repeat: repeat;\n background-size: ${this.options.width}px ${this.options.height}px;\n background-position: ${this.options.backgroundPosition};\n -webkit-print-color-adjust: exact;\n `,this.watermarkDom.append(i),this.parentElement.appendChild(this.watermarkDom),this.options.mutationObserve&&this.bindMutationObserve(),this.options.onSuccess?.()}destroy(){this.options.onBeforeDestroy?.(),this.observer?.disconnect(),this.parentObserve?.disconnect(),this.watermarkDom?.remove(),this.options.onDestroyed?.()}validateUnique(){let t=!0;return this.options.unique&&this.parentElement.childNodes.forEach((e=>{t&&Object.hasOwnProperty.call(e,"__WATERMARK__")&&(t=!1)})),t}validateContent(){switch(this.options.contentType){case t.ContentTypeEnum.image:return Object.hasOwnProperty.call(this.options,"image");case t.ContentTypeEnum.multiLineText:case t.ContentTypeEnum.richText:case t.ContentTypeEnum.text:return this.options.content.length>0}return!1}draw(){const e=h.createCanvas(this.options.width,this.options.height).getContext("2d");if(null===e)throw new Error("get context error");return e.font=`${this.options.fontWeight} ${this.options.fontSize}px ${this.options.fontFamily}`,e.textAlign=this.options.textAlign,e.textBaseline=this.options.textBaseline,e.fillStyle=this.options.fontColor,e.globalAlpha=this.options.globalAlpha,new Promise((n=>{switch(this.options.contentType){case t.ContentTypeEnum.text:this.drawText(e,n);break;case t.ContentTypeEnum.image:this.drawImage(e,n);break;case t.ContentTypeEnum.multiLineText:this.drawMultiLineText(e,n);break;case t.ContentTypeEnum.richText:this.drawRichText(e,n)}}))}drawText(t,e){t.translate(this.options.width/2,this.options.height/2),t.rotate(this.options.rotate),t.fillText(this.options.content,0,0),e(t.canvas)}drawImage(t,e){const n=new Image;n.setAttribute("crossOrigin","Anonymous"),n.src=this.options.image,n.onload=()=>{t.translate(this.options.width/2,this.options.height/2),t.rotate(this.options.rotate);const{width:i,height:o}=this.getImageRect(n);t.drawImage(n,0-i/2,0-o/2,i,o),e(t.canvas)}}drawMultiLineText(t,e){const n=((t,e,n)=>{const i=[];let o="";for(let s=0,a=e.length;s<a;s++)o+=e.charAt(s),t.measureText(o).width>n&&(i.push(o.substring(0,o.length-1)),o="",s--);return i.push(o),i})(t,this.options.content,this.options.width);t.translate(this.options.width/2,this.options.height/2),t.rotate(this.options.rotate);const i=(n.length-1)*this.options.lineHeight/2;n.forEach(((e,n)=>{t.fillText(e,0,this.options.lineHeight*n-i)})),e(t.canvas)}drawRichText(t,e){const i=new Image;i.width=this.options.width,i.height=this.options.height;const o=((t,e)=>{const i=n("svg",{xmlns:"http://www.w3.org/2000/svg"}),o=n("foreignObject",{width:e.width.toString(),height:e.height.toString()}),s=document.createElement("div");return s.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),s.style.cssText=`\n text-align: center;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 100%;\n height: 100%;\n font: ${t.font};\n color: ${e.fontColor};\n `,s.innerHTML=e.content,o.appendChild(s),i.appendChild(o),i})(t,this.options);i.src=`data:image/svg+xml;charset=utf-8,${o.outerHTML.replace(/\n/g,"").replace(/\t/g,"").replace(/#/g,"%23")}`,i.onload=()=>{t.translate(this.options.width/2,this.options.height/2),t.rotate(this.options.rotate),t.drawImage(i,-this.options.width/2,-this.options.height/2,t.canvas.width,t.canvas.height),e(t.canvas)}}getImageRect(t){const e={width:this.options.imageWidth,height:this.options.imageHeight};switch(!0){case 0!==e.width&&0===e.height:e.height=e.width*t.height/t.width;break;case 0===e.width&&0!==e.height:e.width=e.height*t.width/t.height;break;case 0===e.width&&0===e.height:e.width=t.width,e.height=t.height}return e}bindMutationObserve(){this.watermarkDom&&(this.observer=new MutationObserver((t=>{t.length>0&&(this.destroy(),this.create())})),this.observer.observe(this.watermarkDom,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),this.parentObserve=new MutationObserver((t=>{t.forEach((t=>{t?.target!==this.watermarkDom&&t?.removedNodes?.[0]!==this.watermarkDom||(this.destroy(),this.create())}))})),this.parentObserve.observe(this.parentElement,{attributes:!0,childList:!0,subtree:!0,characterData:!0}))}}t.BlindWatermark=class extends h{constructor(e={}){e.globalAlpha=.005,e.mode=t.CreateWatermarkModeEnum.blind,super(e)}static decode(n){const i=Object.assign({url:"",fillColor:"#000",compositeOperation:"color-burn",mode:t.DecodeBlindWatermarkModeEnum.canvas},n);if(i.url&&i.mode===t.DecodeBlindWatermarkModeEnum.canvas){const t=new Image;t.src=i.url,t.onload=()=>{const{width:n,height:o}=t,s=h.createCanvas(n,o),a=s.getContext("2d");if(null===a)throw new Error("get context error");a.drawImage(t,0,0,n,o),a.globalCompositeOperation=i.compositeOperation,a.fillStyle=i.fillColor,a.fillRect(0,0,n,o);const r=e(s);i.onSuccess&&"function"==typeof i.onSuccess&&i.onSuccess?.(r)}}}},t.Watermark=h}));
|