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 CHANGED
@@ -4,9 +4,7 @@
4
4
  </a>
5
5
  </p>
6
6
  <p align="center">
7
- <a href="https://npmjs.com/package/pinia"><img src="https://badgen.net/npm/v/pinia" alt="npm package"></a>
8
- <a href="https://github.com/vuejs/pinia/actions/workflows/test.yml?query=branch%3Av2"><img src="https://github.com/vuejs/pinia/workflows/test/badge.svg?branch=v2" alt="build status"></a>
9
- <a href="https://codecov.io/github/vuejs/pinia"><img src="https://badgen.net/codecov/c/github/vuejs/pinia/v2" alt="code coverage"></a>
7
+ <a href="https://npmjs.com/package/watermark-js-plus"><img src="https://badgen.net/npm/v/watermark-js-plus" alt="npm package"></a>
10
8
  </p>
11
9
 
12
10
  # Watermark
@@ -0,0 +1,17 @@
1
+ import { DecodeBlindWatermarkOptions, WatermarkOptions } from '../types';
2
+ import Watermark from './watermark';
3
+ /**
4
+ * BlindWatermark class
5
+ */
6
+ export default class BlindWatermark extends Watermark {
7
+ /**
8
+ * BlindWatermark constructor
9
+ * @param props - blind watermark options
10
+ */
11
+ constructor(props?: Partial<WatermarkOptions>);
12
+ /**
13
+ * Decode blind watermark.
14
+ * @param props - decode options
15
+ */
16
+ static decode(props: Partial<DecodeBlindWatermarkOptions>): void;
17
+ }
@@ -0,0 +1,40 @@
1
+ import { WatermarkOptions } from '../types';
2
+ /**
3
+ * Watermark class
4
+ */
5
+ export default class Watermark {
6
+ private readonly options;
7
+ private parentElement;
8
+ private observer?;
9
+ private parentObserve?;
10
+ private watermarkDom?;
11
+ /**
12
+ * Watermark constructor
13
+ * @param props - watermark options
14
+ */
15
+ constructor(props?: Partial<WatermarkOptions>);
16
+ /**
17
+ * Create an HD canvas.
18
+ * @param width - width of canvas
19
+ * @param height - height of canvas
20
+ */
21
+ static createCanvas(width: number, height: number): HTMLCanvasElement;
22
+ changeParentElement(parent: Element | string): void;
23
+ /**
24
+ * Creating a watermark.
25
+ */
26
+ create(): Promise<void>;
27
+ /**
28
+ * Deleting this watermark.
29
+ */
30
+ destroy(): void;
31
+ private validateUnique;
32
+ private validateContent;
33
+ private draw;
34
+ private drawText;
35
+ private drawImage;
36
+ private drawMultiLineText;
37
+ private drawRichText;
38
+ private getImageRect;
39
+ private bindMutationObserve;
40
+ }
@@ -0,0 +1,3 @@
1
+ import Watermark from './core/watermark';
2
+ import BlindWatermark from './core/blind';
3
+ export { Watermark, BlindWatermark };
@@ -0,0 +1,63 @@
1
+ export declare enum ContentTypeEnum {
2
+ text = "text",
3
+ image = "image",
4
+ multiLineText = "multi-line-text",
5
+ richText = "rich-text"
6
+ }
7
+ export declare enum TextAlignEnum {
8
+ center = "center",
9
+ left = "left",
10
+ right = "right"
11
+ }
12
+ export declare enum TextBaselineEnum {
13
+ top = "top",
14
+ bottom = "bottom",
15
+ middle = "middle"
16
+ }
17
+ export declare enum CreateWatermarkModeEnum {
18
+ default = "default",
19
+ blind = "blind"
20
+ }
21
+ export declare enum DecodeBlindWatermarkModeEnum {
22
+ canvas = "canvas",
23
+ html = "html",
24
+ svg = "svg"
25
+ }
26
+ export interface WatermarkDom extends HTMLDivElement {
27
+ __WATERMARK__?: string;
28
+ __WATERMARK__INSTANCE__?: any;
29
+ }
30
+ export interface WatermarkOptions {
31
+ width: number;
32
+ height: number;
33
+ rotate: number;
34
+ contentType: ContentTypeEnum;
35
+ content: string;
36
+ image?: string;
37
+ imageWidth: number;
38
+ imageHeight: number;
39
+ lineHeight: number;
40
+ zIndex: number;
41
+ backgroundPosition: string;
42
+ fontSize: number;
43
+ fontFamily: string;
44
+ textAlign: TextAlignEnum;
45
+ textBaseline: TextBaselineEnum;
46
+ fontColor: string;
47
+ globalAlpha: number;
48
+ fontWeight: string;
49
+ mode: CreateWatermarkModeEnum;
50
+ mutationObserve: boolean;
51
+ unique: boolean;
52
+ parent: Element | string;
53
+ onSuccess: Function;
54
+ onBeforeDestroy: Function;
55
+ onDestroyed: Function;
56
+ }
57
+ export interface DecodeBlindWatermarkOptions {
58
+ url: string;
59
+ fillColor: string;
60
+ compositeOperation: string;
61
+ mode: DecodeBlindWatermarkModeEnum;
62
+ onSuccess: Function;
63
+ }
@@ -0,0 +1,9 @@
1
+ import { WatermarkOptions } from '../types';
2
+ export declare const convertImage: (canvas: HTMLCanvasElement) => string;
3
+ export declare const isFunction: (value: Function) => boolean;
4
+ export declare const createSVGElement: (tagName: string, attrs?: {
5
+ [key: string]: string;
6
+ }, namespaceURI?: string) => Element;
7
+ export declare const getMultiLineData: (ctx: CanvasRenderingContext2D, text: string, maxWidth: number) => string[];
8
+ export declare const createCustomContentSVG: (ctx: CanvasRenderingContext2D, options: WatermarkOptions) => Element;
9
+ export declare const convertSVGToImage: (svg: Element) => string;
@@ -0,0 +1,489 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise */
16
+
17
+ var extendStatics = function(d, b) {
18
+ extendStatics = Object.setPrototypeOf ||
19
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
20
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
21
+ return extendStatics(d, b);
22
+ };
23
+
24
+ function __extends(d, b) {
25
+ if (typeof b !== "function" && b !== null)
26
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
27
+ extendStatics(d, b);
28
+ function __() { this.constructor = d; }
29
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
30
+ }
31
+
32
+ function __awaiter(thisArg, _arguments, P, generator) {
33
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
+ return new (P || (P = Promise))(function (resolve, reject) {
35
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
36
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
37
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
38
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
39
+ });
40
+ }
41
+
42
+ function __generator(thisArg, body) {
43
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
44
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
45
+ function verb(n) { return function (v) { return step([n, v]); }; }
46
+ function step(op) {
47
+ if (f) throw new TypeError("Generator is already executing.");
48
+ while (_) try {
49
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
50
+ if (y = 0, t) op = [op[0] & 2, t.value];
51
+ switch (op[0]) {
52
+ case 0: case 1: t = op; break;
53
+ case 4: _.label++; return { value: op[1], done: false };
54
+ case 5: _.label++; y = op[1]; op = [0]; continue;
55
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
56
+ default:
57
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
58
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
59
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
60
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
61
+ if (t[2]) _.ops.pop();
62
+ _.trys.pop(); continue;
63
+ }
64
+ op = body.call(thisArg, _);
65
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
66
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
67
+ }
68
+ }
69
+
70
+ var convertImage = function (canvas) {
71
+ return canvas.toDataURL('image/png', 1);
72
+ };
73
+ var isFunction = function (value) {
74
+ return typeof value === 'function';
75
+ };
76
+ var createSVGElement = function (tagName, attrs, namespaceURI) {
77
+ if (attrs === void 0) { attrs = {}; }
78
+ if (namespaceURI === void 0) { namespaceURI = 'http://www.w3.org/2000/svg'; }
79
+ var element = document.createElementNS(namespaceURI, tagName);
80
+ for (var attr in attrs) {
81
+ element.setAttribute(attr, attrs[attr]);
82
+ }
83
+ return element;
84
+ };
85
+ var getMultiLineData = function (ctx, text, maxWidth) {
86
+ var result = [];
87
+ var str = '';
88
+ for (var i = 0, len = text.length; i < len; i++) {
89
+ str += text.charAt(i);
90
+ if (ctx.measureText(str).width > maxWidth) {
91
+ result.push(str.substring(0, str.length - 1));
92
+ str = '';
93
+ i--;
94
+ }
95
+ }
96
+ result.push(str);
97
+ return result;
98
+ };
99
+ var createCustomContentSVG = function (ctx, options) {
100
+ var svgElement = createSVGElement('svg', {
101
+ xmlns: 'http://www.w3.org/2000/svg'
102
+ });
103
+ var foreignObjectElement = createSVGElement('foreignObject', {
104
+ width: options.width.toString(),
105
+ height: options.height.toString()
106
+ });
107
+ var bodyElement = document.createElement('div');
108
+ bodyElement.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
109
+ bodyElement.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: ".concat(ctx.font, ";\n color: ").concat(options.fontColor, ";\n ");
110
+ bodyElement.innerHTML = options.content;
111
+ foreignObjectElement.appendChild(bodyElement);
112
+ svgElement.appendChild(foreignObjectElement);
113
+ return svgElement;
114
+ };
115
+ var convertSVGToImage = function (svg) {
116
+ var richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
117
+ return "data:image/svg+xml;charset=utf-8,".concat(richContent);
118
+ };
119
+
120
+ var ContentTypeEnum;
121
+ (function (ContentTypeEnum) {
122
+ ContentTypeEnum["text"] = "text";
123
+ ContentTypeEnum["image"] = "image";
124
+ ContentTypeEnum["multiLineText"] = "multi-line-text";
125
+ ContentTypeEnum["richText"] = "rich-text";
126
+ })(ContentTypeEnum || (ContentTypeEnum = {}));
127
+ var TextAlignEnum;
128
+ (function (TextAlignEnum) {
129
+ TextAlignEnum["center"] = "center";
130
+ TextAlignEnum["left"] = "left";
131
+ TextAlignEnum["right"] = "right";
132
+ })(TextAlignEnum || (TextAlignEnum = {}));
133
+ var TextBaselineEnum;
134
+ (function (TextBaselineEnum) {
135
+ TextBaselineEnum["top"] = "top";
136
+ TextBaselineEnum["bottom"] = "bottom";
137
+ TextBaselineEnum["middle"] = "middle";
138
+ })(TextBaselineEnum || (TextBaselineEnum = {}));
139
+ var CreateWatermarkModeEnum;
140
+ (function (CreateWatermarkModeEnum) {
141
+ CreateWatermarkModeEnum["default"] = "default";
142
+ CreateWatermarkModeEnum["blind"] = "blind";
143
+ })(CreateWatermarkModeEnum || (CreateWatermarkModeEnum = {}));
144
+ var DecodeBlindWatermarkModeEnum;
145
+ (function (DecodeBlindWatermarkModeEnum) {
146
+ DecodeBlindWatermarkModeEnum["canvas"] = "canvas";
147
+ DecodeBlindWatermarkModeEnum["html"] = "html";
148
+ DecodeBlindWatermarkModeEnum["svg"] = "svg";
149
+ })(DecodeBlindWatermarkModeEnum || (DecodeBlindWatermarkModeEnum = {}));
150
+
151
+ /**
152
+ * Watermark class
153
+ */
154
+ var Watermark = /** @class */ (function () {
155
+ /**
156
+ * Watermark constructor
157
+ * @param props - watermark options
158
+ */
159
+ function Watermark(props) {
160
+ if (props === void 0) { props = {}; }
161
+ var _a;
162
+ this.parentElement = document.body;
163
+ this.options = Object.assign({
164
+ width: 300,
165
+ height: 300,
166
+ rotate: 45,
167
+ contentType: ContentTypeEnum.text,
168
+ content: 'hello watermark-js-plus',
169
+ imageWidth: 0,
170
+ imageHeight: 0,
171
+ lineHeight: 30,
172
+ zIndex: 10000,
173
+ backgroundPosition: '0 0, 0 0',
174
+ fontSize: 20,
175
+ fontFamily: 'sans-serif',
176
+ textAlign: TextAlignEnum.center,
177
+ textBaseline: TextBaselineEnum.middle,
178
+ fontColor: '#000',
179
+ globalAlpha: 0.5,
180
+ fontWeight: 'normal',
181
+ mode: CreateWatermarkModeEnum.default,
182
+ mutationObserve: true,
183
+ unique: true,
184
+ parent: 'body',
185
+ onSuccess: function () { },
186
+ onBeforeDestroy: function () { },
187
+ onDestroyed: function () { }
188
+ }, props);
189
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
190
+ this.options.rotate = (360 - this.options.rotate % 360) * (Math.PI / 180);
191
+ }
192
+ this.changeParentElement(this.options.parent);
193
+ }
194
+ /**
195
+ * Create an HD canvas.
196
+ * @param width - width of canvas
197
+ * @param height - height of canvas
198
+ */
199
+ Watermark.createCanvas = function (width, height) {
200
+ var _a;
201
+ var ratio = window.devicePixelRatio || 1;
202
+ var canvas = document.createElement('canvas');
203
+ canvas.width = width * ratio; // actual rendered pixel
204
+ canvas.height = height * ratio; // actual rendered pixel
205
+ canvas.style.width = "".concat(width, "px"); // control display size
206
+ canvas.style.height = "".concat(height, "px"); // control display size
207
+ (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
208
+ return canvas;
209
+ };
210
+ Watermark.prototype.changeParentElement = function (parent) {
211
+ if (typeof parent === 'string') {
212
+ var parentElement = document.querySelector(parent);
213
+ parentElement && (this.parentElement = parentElement);
214
+ }
215
+ else {
216
+ this.parentElement = parent;
217
+ }
218
+ };
219
+ /**
220
+ * Creating a watermark.
221
+ */
222
+ Watermark.prototype.create = function () {
223
+ var _a, _b;
224
+ return __awaiter(this, void 0, void 0, function () {
225
+ var canvas, image, watermarkInnerDom;
226
+ return __generator(this, function (_c) {
227
+ switch (_c.label) {
228
+ case 0:
229
+ if (!this.validateUnique()) {
230
+ return [2 /*return*/];
231
+ }
232
+ if (!this.validateContent()) {
233
+ return [2 /*return*/];
234
+ }
235
+ return [4 /*yield*/, this.draw()];
236
+ case 1:
237
+ canvas = _c.sent();
238
+ image = convertImage(canvas);
239
+ this.watermarkDom = document.createElement('div');
240
+ watermarkInnerDom = document.createElement('div');
241
+ this.watermarkDom.__WATERMARK__ = 'watermark';
242
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
243
+ this.watermarkDom.style.cssText = "\n z-index: ".concat(this.options.zIndex, ";\n position: relative;\n ");
244
+ watermarkInnerDom.style.cssText = "\n position: fixed;\n z-index: ".concat(this.options.zIndex, ";\n pointer-events: none;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n background-image: url(").concat(image, ");\n background-repeat: repeat;\n background-size: ").concat(this.options.width, "px ").concat(this.options.height, "px;\n background-position: ").concat(this.options.backgroundPosition, ";\n -webkit-print-color-adjust: exact;\n ");
245
+ this.watermarkDom.append(watermarkInnerDom);
246
+ this.parentElement.appendChild(this.watermarkDom);
247
+ if (this.options.mutationObserve) {
248
+ this.bindMutationObserve();
249
+ }
250
+ (_b = (_a = this.options).onSuccess) === null || _b === void 0 ? void 0 : _b.call(_a);
251
+ return [2 /*return*/];
252
+ }
253
+ });
254
+ });
255
+ };
256
+ /**
257
+ * Deleting this watermark.
258
+ */
259
+ Watermark.prototype.destroy = function () {
260
+ var _a, _b, _c, _d, _e, _f, _g;
261
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
262
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
263
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
264
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
265
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
266
+ };
267
+ Watermark.prototype.validateUnique = function () {
268
+ var result = true;
269
+ if (this.options.unique) {
270
+ this.parentElement.childNodes.forEach(function (node) {
271
+ if (!result) {
272
+ return;
273
+ }
274
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
275
+ result = false;
276
+ // throw new Error('duplicate watermark error')
277
+ }
278
+ });
279
+ }
280
+ return result;
281
+ };
282
+ Watermark.prototype.validateContent = function () {
283
+ switch (this.options.contentType) {
284
+ case ContentTypeEnum.image:
285
+ return Object.hasOwnProperty.call(this.options, 'image');
286
+ case ContentTypeEnum.multiLineText:
287
+ case ContentTypeEnum.richText:
288
+ case ContentTypeEnum.text:
289
+ return this.options.content.length > 0;
290
+ }
291
+ return false;
292
+ };
293
+ Watermark.prototype.draw = function () {
294
+ var _this = this;
295
+ var canvas = Watermark.createCanvas(this.options.width, this.options.height);
296
+ // document.body?.appendChild(canvas)
297
+ var ctx = canvas.getContext('2d');
298
+ if (ctx === null) {
299
+ throw new Error('get context error');
300
+ }
301
+ ctx.font = "".concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
302
+ ctx.textAlign = this.options.textAlign;
303
+ ctx.textBaseline = this.options.textBaseline;
304
+ ctx.fillStyle = this.options.fontColor;
305
+ ctx.globalAlpha = this.options.globalAlpha;
306
+ return new Promise(function (resolve) {
307
+ switch (_this.options.contentType) {
308
+ case ContentTypeEnum.text:
309
+ _this.drawText(ctx, resolve);
310
+ break;
311
+ case ContentTypeEnum.image:
312
+ _this.drawImage(ctx, resolve);
313
+ break;
314
+ case ContentTypeEnum.multiLineText:
315
+ _this.drawMultiLineText(ctx, resolve);
316
+ break;
317
+ case ContentTypeEnum.richText:
318
+ _this.drawRichText(ctx, resolve);
319
+ break;
320
+ }
321
+ });
322
+ };
323
+ Watermark.prototype.drawText = function (ctx, resolve) {
324
+ ctx.translate(this.options.width / 2, this.options.height / 2);
325
+ ctx.rotate(this.options.rotate);
326
+ ctx.fillText(this.options.content, 0, 0);
327
+ resolve(ctx.canvas);
328
+ };
329
+ Watermark.prototype.drawImage = function (ctx, resolve) {
330
+ var _this = this;
331
+ var image = new Image();
332
+ image.setAttribute('crossOrigin', 'Anonymous');
333
+ image.src = this.options.image;
334
+ image.onload = function () {
335
+ ctx.translate(_this.options.width / 2, _this.options.height / 2);
336
+ ctx.rotate(_this.options.rotate);
337
+ var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
338
+ ctx.drawImage(image, 0 - imageWidth / 2, 0 - imageHeight / 2, imageWidth, imageHeight);
339
+ resolve(ctx.canvas);
340
+ };
341
+ };
342
+ Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
343
+ var _this = this;
344
+ // image.width = this.options.width
345
+ // image.height = this.options.height
346
+ // const element = createCustomContentSvg(context, this.options)
347
+ // image.src = convertSVGToImage(element)
348
+ // image.onload = () => {
349
+ // context.translate(this.options.width / 2, this.options.height / 2)
350
+ // context.rotate(this.options.rotate)
351
+ // context.drawImage(
352
+ // image,
353
+ // -this.options.width / 2,
354
+ // -this.options.height / 2,
355
+ // context.canvas.width,
356
+ // context.canvas.height
357
+ // )
358
+ // resolve(canvas)
359
+ // }
360
+ var lines = getMultiLineData(ctx, this.options.content, this.options.width);
361
+ ctx.translate(this.options.width / 2, this.options.height / 2);
362
+ ctx.rotate(this.options.rotate);
363
+ var yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
364
+ lines.forEach(function (txt, index) {
365
+ ctx.fillText(txt, 0, _this.options.lineHeight * index - yOffsetValue);
366
+ });
367
+ resolve(ctx.canvas);
368
+ };
369
+ Watermark.prototype.drawRichText = function (ctx, resolve) {
370
+ var _this = this;
371
+ var image = new Image();
372
+ image.width = this.options.width;
373
+ image.height = this.options.height;
374
+ var element = createCustomContentSVG(ctx, this.options);
375
+ image.src = convertSVGToImage(element);
376
+ image.onload = function () {
377
+ ctx.translate(_this.options.width / 2, _this.options.height / 2);
378
+ ctx.rotate(_this.options.rotate);
379
+ ctx.drawImage(image, -_this.options.width / 2, -_this.options.height / 2, ctx.canvas.width, ctx.canvas.height);
380
+ resolve(ctx.canvas);
381
+ };
382
+ };
383
+ Watermark.prototype.getImageRect = function (image) {
384
+ var rect = { width: this.options.imageWidth, height: this.options.imageHeight };
385
+ switch (true) {
386
+ case rect.width !== 0 && rect.height === 0:
387
+ rect.height = rect.width * image.height / image.width;
388
+ break;
389
+ case rect.width === 0 && rect.height !== 0:
390
+ rect.width = rect.height * image.width / image.height;
391
+ break;
392
+ case rect.width === 0 && rect.height === 0:
393
+ rect.width = image.width;
394
+ rect.height = image.height;
395
+ break;
396
+ }
397
+ return rect;
398
+ };
399
+ Watermark.prototype.bindMutationObserve = function () {
400
+ var _this = this;
401
+ if (!this.watermarkDom) {
402
+ return;
403
+ }
404
+ this.observer = new MutationObserver(function (mutationsList) {
405
+ if (mutationsList.length > 0) {
406
+ _this.destroy();
407
+ _this.create();
408
+ }
409
+ });
410
+ this.observer.observe(this.watermarkDom, {
411
+ attributes: true,
412
+ childList: true,
413
+ subtree: true,
414
+ characterData: true // 节点内容或节点文本的变动。
415
+ });
416
+ this.parentObserve = new MutationObserver(function (mutationsList) {
417
+ mutationsList.forEach(function (item) {
418
+ var _a;
419
+ if ((item === null || item === void 0 ? void 0 : item.target) === _this.watermarkDom || ((_a = item === null || item === void 0 ? void 0 : item.removedNodes) === null || _a === void 0 ? void 0 : _a[0]) === _this.watermarkDom) {
420
+ _this.destroy();
421
+ _this.create();
422
+ }
423
+ });
424
+ });
425
+ this.parentObserve.observe(this.parentElement, {
426
+ attributes: true,
427
+ childList: true,
428
+ subtree: true,
429
+ characterData: true // 节点内容或节点文本的变动。
430
+ });
431
+ };
432
+ return Watermark;
433
+ }());
434
+
435
+ /**
436
+ * BlindWatermark class
437
+ */
438
+ var BlindWatermark = /** @class */ (function (_super) {
439
+ __extends(BlindWatermark, _super);
440
+ /**
441
+ * BlindWatermark constructor
442
+ * @param props - blind watermark options
443
+ */
444
+ function BlindWatermark(props) {
445
+ if (props === void 0) { props = {}; }
446
+ props.globalAlpha = 0.005;
447
+ props.mode = CreateWatermarkModeEnum.blind;
448
+ return _super.call(this, props) || this;
449
+ }
450
+ /**
451
+ * Decode blind watermark.
452
+ * @param props - decode options
453
+ */
454
+ BlindWatermark.decode = function (props) {
455
+ var options = Object.assign({
456
+ url: '',
457
+ fillColor: '#000',
458
+ compositeOperation: 'color-burn',
459
+ mode: DecodeBlindWatermarkModeEnum.canvas
460
+ }, props);
461
+ if (!options.url) {
462
+ return;
463
+ }
464
+ if (options.mode === DecodeBlindWatermarkModeEnum.canvas) {
465
+ var img_1 = new Image();
466
+ img_1.src = options.url;
467
+ img_1.onload = function () {
468
+ var _a;
469
+ var width = img_1.width, height = img_1.height;
470
+ var canvas = Watermark.createCanvas(width, height);
471
+ var ctx = canvas.getContext('2d');
472
+ if (ctx === null) {
473
+ throw new Error('get context error');
474
+ }
475
+ ctx.drawImage(img_1, 0, 0, width, height);
476
+ ctx.globalCompositeOperation = options.compositeOperation;
477
+ ctx.fillStyle = options.fillColor;
478
+ ctx.fillRect(0, 0, width, height);
479
+ var resultImage = convertImage(canvas);
480
+ if (options.onSuccess && isFunction(options.onSuccess)) {
481
+ (_a = options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options, resultImage);
482
+ }
483
+ };
484
+ }
485
+ };
486
+ return BlindWatermark;
487
+ }(Watermark));
488
+
489
+ export { BlindWatermark, Watermark };