watermark-js-plus 0.7.0 → 0.8.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.
@@ -51,7 +51,7 @@
51
51
  function verb(n) { return function (v) { return step([n, v]); }; }
52
52
  function step(op) {
53
53
  if (f) throw new TypeError("Generator is already executing.");
54
- while (_) try {
54
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
55
55
  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;
56
56
  if (y = 0, t) op = [op[0] & 2, t.value];
57
57
  switch (op[0]) {
@@ -134,6 +134,301 @@
134
134
  var richContent = svg.outerHTML.replace(/\n/g, '').replace(/\t/g, '').replace(/#/g, '%23');
135
135
  return "data:image/svg+xml;charset=utf-8,".concat(richContent);
136
136
  };
137
+ var getValue = function (v1, v2) {
138
+ if (isUndefined(v1)) {
139
+ return v2;
140
+ }
141
+ else {
142
+ return v1;
143
+ }
144
+ };
145
+ var loadImage = function (url, width, height) {
146
+ if (width === void 0) { width = undefined; }
147
+ if (height === void 0) { height = undefined; }
148
+ var image = new Image();
149
+ image.setAttribute('crossOrigin', 'Anonymous');
150
+ !isUndefined(width) && (image.width = width);
151
+ !isUndefined(height) && (image.height = height);
152
+ image.src = url;
153
+ return new Promise(function (resolve) {
154
+ image.onload = function () {
155
+ resolve(image);
156
+ };
157
+ });
158
+ };
159
+
160
+ var initialOptions = {
161
+ width: 300,
162
+ height: 300,
163
+ rotate: 45,
164
+ auxiliaryLine: false,
165
+ translatePlacement: 'middle',
166
+ contentType: 'text',
167
+ content: 'hello watermark-js-plus',
168
+ textType: 'fill',
169
+ imageWidth: 0,
170
+ imageHeight: 0,
171
+ lineHeight: 30,
172
+ zIndex: 10000,
173
+ backgroundPosition: '0 0, 0 0',
174
+ backgroundRepeat: 'repeat',
175
+ fontSize: '20px',
176
+ fontFamily: 'sans-serif',
177
+ fontStyle: '',
178
+ fontVariant: '',
179
+ fontColor: '#000',
180
+ fontWeight: 'normal',
181
+ filter: 'none',
182
+ globalAlpha: 0.5,
183
+ mode: 'default',
184
+ mutationObserve: true,
185
+ unique: true,
186
+ parent: 'body',
187
+ onSuccess: function () { },
188
+ onBeforeDestroy: function () { },
189
+ onDestroyed: function () { }
190
+ };
191
+ var generateRecommendOptions = function (canvas, options, args) {
192
+ var ctx = canvas.getContext('2d');
193
+ if (ctx === null) {
194
+ throw new Error('get context error');
195
+ }
196
+ ctx.font = "".concat(options.fontStyle, " ").concat(options.fontVariant, " ").concat(options.fontWeight, " ").concat(options.fontSize, " ").concat(options.fontFamily);
197
+ ctx.filter = options.filter;
198
+ if (options === null || options === void 0 ? void 0 : options.rotate) {
199
+ options.rotate = (360 - options.rotate % 360) * (Math.PI / 180);
200
+ }
201
+ if (isUndefined(args.textRowMaxWidth)) {
202
+ options.textRowMaxWidth = options.width;
203
+ }
204
+ var result = {
205
+ image: {
206
+ rect: {
207
+ width: options.imageWidth,
208
+ height: options.imageHeight
209
+ },
210
+ position: {
211
+ x: 0,
212
+ y: 0
213
+ }
214
+ },
215
+ textLine: {
216
+ data: [],
217
+ yOffsetValue: 0
218
+ },
219
+ advancedStyleParams: {
220
+ linear: {
221
+ x0: 0,
222
+ x1: 0
223
+ },
224
+ radial: {
225
+ x0: 0,
226
+ y0: 0,
227
+ r0: 0,
228
+ x1: 0,
229
+ y1: 0,
230
+ r1: 0
231
+ },
232
+ conic: {
233
+ x: 0,
234
+ y: 0,
235
+ startAngle: 0
236
+ },
237
+ pattern: {}
238
+ }
239
+ };
240
+ switch (options.contentType) {
241
+ case 'text':
242
+ result.textLine.data = [options.content];
243
+ break;
244
+ // case 'image':
245
+ // break
246
+ case 'multi-line-text':
247
+ result.textLine.data = getMultiLineData(ctx, options.content, options.textRowMaxWidth);
248
+ break;
249
+ // case 'rich-text':
250
+ // break
251
+ }
252
+ var translateX = options.width / 2;
253
+ var translateY = options.height / 2;
254
+ var textBaseline = 'middle';
255
+ var textAlign = 'center';
256
+ if (!isUndefined(args === null || args === void 0 ? void 0 : args.translateX) && !isUndefined(args === null || args === void 0 ? void 0 : args.translateY)) {
257
+ translateX = args === null || args === void 0 ? void 0 : args.translateX;
258
+ translateY = args === null || args === void 0 ? void 0 : args.translateY;
259
+ textBaseline = 'top';
260
+ textAlign = 'left';
261
+ }
262
+ else {
263
+ // default middle
264
+ // translateX = options.width / 2
265
+ // translateY = options.height / 2
266
+ // TextBaselineType = 'middle'
267
+ // textAlign = 'center'
268
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
269
+ result.advancedStyleParams.linear.x1 = options.width / 2;
270
+ // result.advancedStyleParams.radial.x0 = 0
271
+ // result.advancedStyleParams.radial.y0 = 0
272
+ result.advancedStyleParams.radial.r0 = 0;
273
+ // result.advancedStyleParams.radial.x1 = 0
274
+ // result.advancedStyleParams.radial.y1 = 0
275
+ result.advancedStyleParams.radial.r1 = options.width / 2;
276
+ // result.advancedStyleParams.conic.x = 0
277
+ // result.advancedStyleParams.conic.y = 0
278
+ }
279
+ switch (args.translatePlacement) {
280
+ case 'top':
281
+ translateX = options.width / 2;
282
+ translateY = 0;
283
+ textBaseline = 'top';
284
+ // textAlign = 'center'
285
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
286
+ result.advancedStyleParams.linear.x1 = options.width / 2;
287
+ // result.advancedStyleParams.radial.x0 = 0
288
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
289
+ // result.advancedStyleParams.radial.r0 = 0
290
+ // result.advancedStyleParams.radial.x1 = 0
291
+ // result.advancedStyleParams.radial.y1 = 0
292
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
293
+ // result.advancedStyleParams.radial.r1 = options.width / 2
294
+ // result.advancedStyleParams.conic.x = 0
295
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
296
+ break;
297
+ case 'top-start':
298
+ translateX = 0;
299
+ translateY = 0;
300
+ textBaseline = 'top';
301
+ textAlign = 'start';
302
+ result.advancedStyleParams.linear.x0 = 0;
303
+ result.advancedStyleParams.linear.x1 = options.width;
304
+ result.advancedStyleParams.radial.x0 = options.width / 2;
305
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
306
+ // result.advancedStyleParams.radial.r0 = 0
307
+ result.advancedStyleParams.radial.x1 = options.width / 2;
308
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
309
+ // result.advancedStyleParams.radial.r1 = options.width / 2
310
+ result.advancedStyleParams.conic.x = options.width / 2;
311
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
312
+ break;
313
+ case 'top-end':
314
+ translateX = options.width;
315
+ translateY = 0;
316
+ textBaseline = 'top';
317
+ textAlign = 'end';
318
+ result.advancedStyleParams.linear.x0 = 0;
319
+ result.advancedStyleParams.linear.x1 = -options.width;
320
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
321
+ result.advancedStyleParams.radial.y0 = result.textLine.data.length * options.lineHeight / 2;
322
+ // result.advancedStyleParams.radial.r0 = 0
323
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
324
+ result.advancedStyleParams.radial.y1 = result.textLine.data.length * options.lineHeight / 2;
325
+ // result.advancedStyleParams.radial.r1 = options.width / 2
326
+ result.advancedStyleParams.conic.x = -options.width / 2;
327
+ result.advancedStyleParams.conic.y = result.textLine.data.length * options.lineHeight / 2;
328
+ break;
329
+ case 'bottom':
330
+ translateX = options.width / 2;
331
+ translateY = options.height;
332
+ textBaseline = 'bottom';
333
+ // textAlign = 'center'
334
+ result.advancedStyleParams.linear.x0 = -options.width / 2;
335
+ result.advancedStyleParams.linear.x1 = options.width / 2;
336
+ // result.advancedStyleParams.radial.x0 = 0
337
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
338
+ // result.advancedStyleParams.radial.r0 = 0
339
+ // result.advancedStyleParams.radial.x1 = 0
340
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
341
+ // result.advancedStyleParams.radial.r1 = options.width / 2
342
+ result.advancedStyleParams.conic.x = 0;
343
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
344
+ break;
345
+ case 'bottom-start':
346
+ translateX = 0;
347
+ translateY = options.height;
348
+ textBaseline = 'bottom';
349
+ textAlign = 'start';
350
+ result.advancedStyleParams.linear.x0 = 0;
351
+ result.advancedStyleParams.linear.x1 = options.width;
352
+ result.advancedStyleParams.radial.x0 = options.width / 2;
353
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
354
+ // result.advancedStyleParams.radial.r0 = 0
355
+ result.advancedStyleParams.radial.x1 = options.width / 2;
356
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
357
+ // result.advancedStyleParams.radial.r1 = options.width / 2
358
+ result.advancedStyleParams.conic.x = options.width / 2;
359
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
360
+ break;
361
+ case 'bottom-end':
362
+ translateX = options.width;
363
+ translateY = options.height;
364
+ textBaseline = 'bottom';
365
+ textAlign = 'end';
366
+ result.advancedStyleParams.linear.x0 = 0;
367
+ result.advancedStyleParams.linear.x1 = -options.width;
368
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
369
+ result.advancedStyleParams.radial.y0 = -result.textLine.data.length * options.lineHeight / 2;
370
+ // result.advancedStyleParams.radial.r0 = 0
371
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
372
+ result.advancedStyleParams.radial.y1 = -result.textLine.data.length * options.lineHeight / 2;
373
+ // result.advancedStyleParams.radial.r1 = options.width / 2
374
+ result.advancedStyleParams.conic.x = -options.width / 2;
375
+ result.advancedStyleParams.conic.y = -result.textLine.data.length * options.lineHeight / 2;
376
+ break;
377
+ case 'left':
378
+ translateX = 0;
379
+ translateY = options.height / 2;
380
+ // TextBaselineType = 'middle'
381
+ textAlign = 'start';
382
+ result.advancedStyleParams.linear.x0 = 0;
383
+ result.advancedStyleParams.linear.x1 = options.width;
384
+ result.advancedStyleParams.radial.x0 = options.width / 2;
385
+ // result.advancedStyleParams.radial.y0 = 0
386
+ // result.advancedStyleParams.radial.r0 = 0
387
+ result.advancedStyleParams.radial.x1 = options.width / 2;
388
+ // result.advancedStyleParams.radial.y1 = 0
389
+ // result.advancedStyleParams.radial.r1 = options.width / 2
390
+ result.advancedStyleParams.conic.x = options.width / 2;
391
+ result.advancedStyleParams.conic.y = 0;
392
+ break;
393
+ case 'right':
394
+ translateX = options.width;
395
+ translateY = options.height / 2;
396
+ // TextBaselineType = 'middle'
397
+ textAlign = 'end';
398
+ result.advancedStyleParams.linear.x0 = 0;
399
+ result.advancedStyleParams.linear.x1 = -options.width;
400
+ result.advancedStyleParams.radial.x0 = -options.width / 2;
401
+ // result.advancedStyleParams.radial.y0 = 0
402
+ // result.advancedStyleParams.radial.r0 = 0
403
+ result.advancedStyleParams.radial.x1 = -options.width / 2;
404
+ // result.advancedStyleParams.radial.y1 = 0
405
+ // result.advancedStyleParams.radial.r1 = options.width / 2
406
+ result.advancedStyleParams.conic.x = -options.width / 2;
407
+ result.advancedStyleParams.conic.y = 0;
408
+ break;
409
+ }
410
+ options.translateX = translateX;
411
+ options.translateY = translateY;
412
+ isUndefined(args === null || args === void 0 ? void 0 : args.textBaseline) && (options.textBaseline = textBaseline);
413
+ isUndefined(args === null || args === void 0 ? void 0 : args.textAlign) && (options.textAlign = textAlign);
414
+ if (options.contentType === 'multi-line-text') {
415
+ switch (options.textBaseline) {
416
+ case 'middle':
417
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight / 2;
418
+ break;
419
+ case 'bottom':
420
+ case 'alphabetic':
421
+ case 'ideographic':
422
+ result.textLine.yOffsetValue = (result.textLine.data.length - 1) * options.lineHeight;
423
+ break;
424
+ case 'top':
425
+ case 'hanging':
426
+ result.textLine.yOffsetValue = 0;
427
+ break;
428
+ }
429
+ }
430
+ return result;
431
+ };
137
432
 
138
433
  /**
139
434
  * Watermark class
@@ -141,44 +436,16 @@
141
436
  var Watermark = /** @class */ (function () {
142
437
  /**
143
438
  * Watermark constructor
144
- * @param props - watermark options
439
+ * @param args - watermark args
145
440
  */
146
- function Watermark(props) {
147
- if (props === void 0) { props = {}; }
441
+ function Watermark(args) {
442
+ if (args === void 0) { args = {}; }
148
443
  this.parentElement = document.body;
149
- this.props = props;
150
- this.options = Object.assign({
151
- width: 300,
152
- height: 300,
153
- rotate: 45,
154
- translatePlacement: 'middle',
155
- contentType: 'text',
156
- content: 'hello watermark-js-plus',
157
- textType: 'fill',
158
- imageWidth: 0,
159
- imageHeight: 0,
160
- lineHeight: 30,
161
- zIndex: 10000,
162
- backgroundPosition: '0 0, 0 0',
163
- backgroundRepeat: 'repeat',
164
- fontSize: '20px',
165
- fontFamily: 'sans-serif',
166
- fontStyle: '',
167
- fontVariant: '',
168
- fontColor: '#000',
169
- fontWeight: 'normal',
170
- filter: 'none',
171
- globalAlpha: 0.5,
172
- mode: 'default',
173
- mutationObserve: true,
174
- unique: true,
175
- parent: 'body',
176
- onSuccess: function () { },
177
- onBeforeDestroy: function () { },
178
- onDestroyed: function () { }
179
- }, props);
444
+ this.props = args;
445
+ this.options = Object.assign({}, initialOptions, args);
180
446
  this.changeParentElement(this.options.parent);
181
- this.initializeOptions();
447
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
448
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
182
449
  }
183
450
  /**
184
451
  * Create an HD canvas.
@@ -196,22 +463,10 @@
196
463
  (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
197
464
  return canvas;
198
465
  };
199
- Watermark.prototype.changeOptions = function (props) {
200
- var _this = this;
201
- if (props === void 0) { props = {}; }
202
- Object.keys(props).forEach(function (key) {
203
- var _a;
204
- ((_a = _this.options) === null || _a === void 0 ? void 0 : _a[key]) && (_this.options[key] = props[key]);
205
- });
206
- };
207
- Watermark.prototype.changeParentElement = function (parent) {
208
- if (typeof parent === 'string') {
209
- var parentElement = document.querySelector(parent);
210
- parentElement && (this.parentElement = parentElement);
211
- }
212
- else {
213
- this.parentElement = parent;
214
- }
466
+ Watermark.prototype.changeOptions = function (args, mode) {
467
+ if (args === void 0) { args = {}; }
468
+ if (mode === void 0) { mode = 'overwrite'; }
469
+ this.initConfigData(args, mode);
215
470
  };
216
471
  /**
217
472
  * Creating a watermark.
@@ -233,6 +488,7 @@
233
488
  case 1:
234
489
  canvas = _c.sent();
235
490
  image = convertImage(canvas);
491
+ this.clearCanvas();
236
492
  this.watermarkDom = document.createElement('div');
237
493
  watermarkInnerDom = document.createElement('div');
238
494
  this.watermarkDom.__WATERMARK__ = 'watermark';
@@ -252,7 +508,7 @@
252
508
  });
253
509
  };
254
510
  /**
255
- * Deleting this watermark.
511
+ * Delete this watermark.
256
512
  */
257
513
  Watermark.prototype.destroy = function () {
258
514
  var _a, _b, _c, _d, _e, _f, _g;
@@ -262,77 +518,31 @@
262
518
  (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
263
519
  (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
264
520
  };
265
- Watermark.prototype.initializeOptions = function () {
266
- var _a, _b, _c, _d, _e;
267
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
268
- this.options.rotate = (360 - this.options.rotate % 360) * (Math.PI / 180);
521
+ Watermark.prototype.initConfigData = function (args, mode) {
522
+ var _this = this;
523
+ if (mode === void 0) { mode = 'overwrite'; }
524
+ this.props = args;
525
+ if (mode === 'overwrite') {
526
+ this.options = Object.assign({}, initialOptions, args);
269
527
  }
270
- var translateX;
271
- var translateY;
272
- var textBaseline = 'middle';
273
- var textAlign = 'center';
274
- switch (this.options.translatePlacement) {
275
- case 'top':
276
- translateX = this.options.width / 2;
277
- translateY = 0;
278
- textBaseline = 'top';
279
- break;
280
- case 'top-start':
281
- translateX = 0;
282
- translateY = 0;
283
- textBaseline = 'top';
284
- textAlign = 'start';
285
- break;
286
- case 'top-end':
287
- translateX = this.options.width;
288
- translateY = 0;
289
- textBaseline = 'top';
290
- textAlign = 'end';
291
- break;
292
- case 'bottom':
293
- translateX = this.options.width / 2;
294
- translateY = this.options.height;
295
- textBaseline = 'bottom';
296
- break;
297
- case 'bottom-start':
298
- translateX = 0;
299
- translateY = this.options.height;
300
- textBaseline = 'bottom';
301
- textAlign = 'start';
302
- break;
303
- case 'bottom-end':
304
- translateX = this.options.width;
305
- translateY = this.options.height;
306
- textBaseline = 'bottom';
307
- textAlign = 'end';
308
- break;
309
- case 'left':
310
- translateX = 0;
311
- translateY = this.options.height / 2;
312
- textAlign = 'start';
313
- break;
314
- case 'right':
315
- translateX = this.options.width;
316
- translateY = this.options.height / 2;
317
- textAlign = 'end';
318
- break;
319
- case 'middle':
320
- translateX = this.options.width / 2;
321
- translateY = this.options.height / 2;
322
- break;
528
+ else {
529
+ Object.keys(args).forEach(function (key) {
530
+ _this.options[key] = args[key];
531
+ });
323
532
  }
324
- if (isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateX) || isUndefined((_c = this.props) === null || _c === void 0 ? void 0 : _c.translateY)) {
325
- this.options.translateX = translateX;
326
- this.options.translateY = translateY;
533
+ this.changeParentElement(this.options.parent);
534
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
535
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
536
+ // document.body?.appendChild(this.canvas)
537
+ };
538
+ Watermark.prototype.changeParentElement = function (parent) {
539
+ if (typeof parent === 'string') {
540
+ var parentElement = document.querySelector(parent);
541
+ parentElement && (this.parentElement = parentElement);
327
542
  }
328
543
  else {
329
- textBaseline = 'top';
330
- textAlign = 'left';
331
- this.defaultAdvancedStyleParams.x0 = 0;
332
- this.defaultAdvancedStyleParams.x1 = this.options.textRowMaxWidth || this.options.width;
544
+ this.parentElement = parent;
333
545
  }
334
- isUndefined((_d = this.props) === null || _d === void 0 ? void 0 : _d.textBaseline) && (this.options.textBaseline = textBaseline);
335
- isUndefined((_e = this.props) === null || _e === void 0 ? void 0 : _e.textAlign) && (this.options.textAlign = textAlign);
336
546
  };
337
547
  Watermark.prototype.validateUnique = function () {
338
548
  var result = true;
@@ -362,13 +572,13 @@
362
572
  };
363
573
  Watermark.prototype.draw = function () {
364
574
  var _this = this;
365
- var canvas = Watermark.createCanvas(this.options.width, this.options.height);
366
- // document.body?.appendChild(canvas)
367
- var ctx = canvas.getContext('2d');
575
+ // const canvas = Watermark.createCanvas(this.options.width, this.options.height)
576
+ var ctx = this.canvas.getContext('2d');
368
577
  if (ctx === null) {
369
578
  throw new Error('get context error');
370
579
  }
371
580
  this.setStyle(ctx);
581
+ ctx.save();
372
582
  ctx.translate(this.options.translateX, this.options.translateY);
373
583
  ctx.rotate(this.options.rotate);
374
584
  return new Promise(function (resolve) {
@@ -412,16 +622,14 @@
412
622
  }
413
623
  }
414
624
  ctx[propName] && style && (ctx[propName] = style);
415
- ctx.font = "".concat(this.options.fontStyle, " ").concat(this.options.fontVariant, " ").concat(this.options.fontWeight, " ").concat(this.options.fontSize, " ").concat(this.options.fontFamily);
416
- ctx.filter = this.options.filter;
417
625
  this.options.textAlign && (ctx.textAlign = this.options.textAlign);
418
626
  this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
419
627
  ctx.globalAlpha = this.options.globalAlpha;
420
628
  if (this.options.shadowStyle) {
421
- ctx.shadowBlur = this.options.shadowStyle.shadowBlur || 0;
422
- ctx.shadowColor = this.options.shadowStyle.shadowColor || '#00000000';
423
- ctx.shadowOffsetX = this.options.shadowStyle.shadowOffsetX || 0;
424
- ctx.shadowOffsetY = this.options.shadowStyle.shadowOffsetY || 0;
629
+ ctx.shadowBlur = getValue(this.options.shadowStyle.shadowBlur, 0);
630
+ ctx.shadowColor = getValue(this.options.shadowStyle.shadowColor, '#00000000');
631
+ ctx.shadowOffsetX = getValue(this.options.shadowStyle.shadowOffsetX, 0);
632
+ ctx.shadowOffsetY = getValue(this.options.shadowStyle.shadowOffsetY, 0);
425
633
  }
426
634
  if (isFunction(this.options.extraDrawFunc)) {
427
635
  this.options.extraDrawFunc(ctx);
@@ -429,31 +637,31 @@
429
637
  };
430
638
  Watermark.prototype.createLinearGradient = function (ctx) {
431
639
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
432
- var gradient = ctx.createLinearGradient((_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.x0, (_f = (_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.advancedStyle) === null || _e === void 0 ? void 0 : _e.params) === null || _f === void 0 ? void 0 : _f.y0, (_j = (_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.advancedStyle) === null || _h === void 0 ? void 0 : _h.params) === null || _j === void 0 ? void 0 : _j.x1, (_m = (_l = (_k = this.options) === null || _k === void 0 ? void 0 : _k.advancedStyle) === null || _l === void 0 ? void 0 : _l.params) === null || _m === void 0 ? void 0 : _m.y1);
640
+ var gradient = ctx.createLinearGradient(getValue((_c = (_b = (_a = this.options.advancedStyle) === null || _a === void 0 ? void 0 : _a.params) === null || _b === void 0 ? void 0 : _b.linear) === null || _c === void 0 ? void 0 : _c.x0, this.recommendOptions.advancedStyleParams.linear.x0), getValue((_f = (_e = (_d = this.options.advancedStyle) === null || _d === void 0 ? void 0 : _d.params) === null || _e === void 0 ? void 0 : _e.linear) === null || _f === void 0 ? void 0 : _f.y0, 0), getValue((_j = (_h = (_g = this.options.advancedStyle) === null || _g === void 0 ? void 0 : _g.params) === null || _h === void 0 ? void 0 : _h.linear) === null || _j === void 0 ? void 0 : _j.x1, this.recommendOptions.advancedStyleParams.linear.x1), getValue((_m = (_l = (_k = this.options.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.linear) === null || _m === void 0 ? void 0 : _m.y1, 0));
433
641
  (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
434
642
  gradient.addColorStop(item.offset, item.color);
435
643
  });
436
644
  return gradient;
437
645
  };
438
646
  Watermark.prototype.createConicGradient = function (ctx) {
439
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
440
- var gradient = ctx.createConicGradient((_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.startAngle, (_f = (_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.advancedStyle) === null || _e === void 0 ? void 0 : _e.params) === null || _f === void 0 ? void 0 : _f.x, (_j = (_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.advancedStyle) === null || _h === void 0 ? void 0 : _h.params) === null || _j === void 0 ? void 0 : _j.y);
441
- (_m = (_l = (_k = this.options) === null || _k === void 0 ? void 0 : _k.advancedStyle) === null || _l === void 0 ? void 0 : _l.colorStops) === null || _m === void 0 ? void 0 : _m.forEach(function (item) {
647
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
648
+ var gradient = ctx.createConicGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.conic) === null || _d === void 0 ? void 0 : _d.startAngle, 0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.conic) === null || _h === void 0 ? void 0 : _h.x, this.recommendOptions.advancedStyleParams.conic.x), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.conic) === null || _m === void 0 ? void 0 : _m.y, this.recommendOptions.advancedStyleParams.conic.y));
649
+ (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
442
650
  gradient.addColorStop(item.offset, item.color);
443
651
  });
444
652
  return gradient;
445
653
  };
446
654
  Watermark.prototype.createRadialGradient = function (ctx) {
447
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
448
- var gradient = ctx.createRadialGradient((_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.x0, (_f = (_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.advancedStyle) === null || _e === void 0 ? void 0 : _e.params) === null || _f === void 0 ? void 0 : _f.y0, (_j = (_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.advancedStyle) === null || _h === void 0 ? void 0 : _h.params) === null || _j === void 0 ? void 0 : _j.r0, (_m = (_l = (_k = this.options) === null || _k === void 0 ? void 0 : _k.advancedStyle) === null || _l === void 0 ? void 0 : _l.params) === null || _m === void 0 ? void 0 : _m.x1, (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.params) === null || _q === void 0 ? void 0 : _q.y1, (_t = (_s = (_r = this.options) === null || _r === void 0 ? void 0 : _r.advancedStyle) === null || _s === void 0 ? void 0 : _s.params) === null || _t === void 0 ? void 0 : _t.r1);
449
- (_w = (_v = (_u = this.options) === null || _u === void 0 ? void 0 : _u.advancedStyle) === null || _v === void 0 ? void 0 : _v.colorStops) === null || _w === void 0 ? void 0 : _w.forEach(function (item) {
655
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
656
+ var gradient = ctx.createRadialGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.radial) === null || _d === void 0 ? void 0 : _d.x0, this.recommendOptions.advancedStyleParams.radial.x0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.radial) === null || _h === void 0 ? void 0 : _h.y0, this.recommendOptions.advancedStyleParams.radial.y0), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.radial) === null || _m === void 0 ? void 0 : _m.r0, this.recommendOptions.advancedStyleParams.radial.r0), getValue((_r = (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.params) === null || _q === void 0 ? void 0 : _q.radial) === null || _r === void 0 ? void 0 : _r.x1, this.recommendOptions.advancedStyleParams.radial.x1), getValue((_v = (_u = (_t = (_s = this.options) === null || _s === void 0 ? void 0 : _s.advancedStyle) === null || _t === void 0 ? void 0 : _t.params) === null || _u === void 0 ? void 0 : _u.radial) === null || _v === void 0 ? void 0 : _v.y1, this.recommendOptions.advancedStyleParams.radial.y1), getValue((_z = (_y = (_x = (_w = this.options) === null || _w === void 0 ? void 0 : _w.advancedStyle) === null || _x === void 0 ? void 0 : _x.params) === null || _y === void 0 ? void 0 : _y.radial) === null || _z === void 0 ? void 0 : _z.r1, this.recommendOptions.advancedStyleParams.radial.r1));
657
+ (_2 = (_1 = (_0 = this.options) === null || _0 === void 0 ? void 0 : _0.advancedStyle) === null || _1 === void 0 ? void 0 : _1.colorStops) === null || _2 === void 0 ? void 0 : _2.forEach(function (item) {
450
658
  gradient.addColorStop(item.offset, item.color);
451
659
  });
452
660
  return gradient;
453
661
  };
454
662
  Watermark.prototype.createPattern = function (ctx) {
455
- var _a, _b, _c, _d, _e, _f;
456
- return ctx.createPattern((_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.image, ((_f = (_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.advancedStyle) === null || _e === void 0 ? void 0 : _e.params) === null || _f === void 0 ? void 0 : _f.repetition) || '');
663
+ var _a, _b, _c, _d, _e, _f, _g, _h;
664
+ return ctx.createPattern((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.pattern) === null || _d === void 0 ? void 0 : _d.image, ((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.pattern) === null || _h === void 0 ? void 0 : _h.repetition) || '');
457
665
  };
458
666
  Watermark.prototype.setText = function (ctx, params) {
459
667
  var methodName = 'fillText';
@@ -467,21 +675,18 @@
467
675
  text: this.options.content,
468
676
  x: 0,
469
677
  y: 0,
470
- maxWidth: this.options.textRowMaxWidth
678
+ maxWidth: this.options.textRowMaxWidth || this.options.width
471
679
  });
472
680
  resolve(ctx.canvas);
473
681
  };
474
682
  Watermark.prototype.drawImage = function (ctx, resolve) {
475
683
  var _this = this;
476
- var image = new Image();
477
- image.setAttribute('crossOrigin', 'Anonymous');
478
- image.src = this.options.image;
479
- image.onload = function () {
684
+ loadImage(this.options.image).then(function (image) {
480
685
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
481
686
  var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
482
687
  ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
483
688
  resolve(ctx.canvas);
484
- };
689
+ });
485
690
  };
486
691
  Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
487
692
  var _this = this;
@@ -501,23 +706,8 @@
501
706
  // )
502
707
  // resolve(canvas)
503
708
  // }
504
- var width = this.options.textRowMaxWidth || this.options.width;
505
- var lines = getMultiLineData(ctx, this.options.content, width);
506
- var yOffsetValue;
507
- switch (this.options.textBaseline) {
508
- case 'middle':
509
- yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
510
- break;
511
- case 'bottom':
512
- case 'alphabetic':
513
- case 'ideographic':
514
- yOffsetValue = (lines.length - 1) * this.options.lineHeight;
515
- break;
516
- case 'top':
517
- case 'hanging':
518
- yOffsetValue = 0;
519
- break;
520
- }
709
+ var lines = this.recommendOptions.textLine.data;
710
+ var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
521
711
  lines.forEach(function (text, index) {
522
712
  _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
523
713
  });
@@ -526,18 +716,14 @@
526
716
  Watermark.prototype.drawRichText = function (ctx, resolve) {
527
717
  var _this = this;
528
718
  var obj = createCustomContentSVG(ctx, this.options);
529
- var image = new Image();
530
- image.width = obj.width;
531
- image.height = obj.height;
532
- image.src = convertSVGToImage(obj.element);
533
- image.onload = function () {
719
+ loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
534
720
  var imagePosition = _this.getDrawImagePosition(image.width, image.height);
535
721
  ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
536
722
  resolve(ctx.canvas);
537
- };
723
+ });
538
724
  };
539
725
  Watermark.prototype.getImageRect = function (image) {
540
- var rect = { width: this.options.imageWidth, height: this.options.imageHeight };
726
+ var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
541
727
  switch (true) {
542
728
  case rect.width !== 0 && rect.height === 0:
543
729
  rect.height = rect.width * image.height / image.width;
@@ -635,6 +821,15 @@
635
821
  characterData: true // 节点内容或节点文本的变动。
636
822
  });
637
823
  };
824
+ Watermark.prototype.clearCanvas = function () {
825
+ var ctx = this.canvas.getContext('2d');
826
+ if (ctx === null) {
827
+ throw new Error('get context error');
828
+ }
829
+ ctx.restore();
830
+ ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
831
+ // this.canvas.width = this.options.w
832
+ };
638
833
  return Watermark;
639
834
  }());
640
835