watermark-js-plus 0.7.0 → 0.8.1

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