watermark-js-plus 0.6.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.
@@ -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,43 +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: 20,
159
- fontFamily: 'sans-serif',
160
- fontStyle: '',
161
- fontColor: '#000',
162
- fontWeight: 'normal',
163
- filter: 'none',
164
- globalAlpha: 0.5,
165
- mode: 'default',
166
- mutationObserve: true,
167
- unique: true,
168
- parent: 'body',
169
- onSuccess: function () { },
170
- onBeforeDestroy: function () { },
171
- onDestroyed: function () { }
172
- }, props);
438
+ this.props = args;
439
+ this.options = Object.assign({}, initialOptions, args);
173
440
  this.changeParentElement(this.options.parent);
174
- this.initializeOptions();
441
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
442
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
175
443
  }
176
444
  /**
177
445
  * Create an HD canvas.
@@ -189,14 +457,10 @@ var Watermark = /** @class */ (function () {
189
457
  (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
190
458
  return canvas;
191
459
  };
192
- Watermark.prototype.changeParentElement = function (parent) {
193
- if (typeof parent === 'string') {
194
- var parentElement = document.querySelector(parent);
195
- parentElement && (this.parentElement = parentElement);
196
- }
197
- else {
198
- this.parentElement = parent;
199
- }
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);
200
464
  };
201
465
  /**
202
466
  * Creating a watermark.
@@ -218,6 +482,7 @@ var Watermark = /** @class */ (function () {
218
482
  case 1:
219
483
  canvas = _c.sent();
220
484
  image = convertImage(canvas);
485
+ this.clearCanvas();
221
486
  this.watermarkDom = document.createElement('div');
222
487
  watermarkInnerDom = document.createElement('div');
223
488
  this.watermarkDom.__WATERMARK__ = 'watermark';
@@ -237,7 +502,7 @@ var Watermark = /** @class */ (function () {
237
502
  });
238
503
  };
239
504
  /**
240
- * Deleting this watermark.
505
+ * Delete this watermark.
241
506
  */
242
507
  Watermark.prototype.destroy = function () {
243
508
  var _a, _b, _c, _d, _e, _f, _g;
@@ -247,73 +512,31 @@ var Watermark = /** @class */ (function () {
247
512
  (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
248
513
  (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
249
514
  };
250
- Watermark.prototype.initializeOptions = function () {
251
- var _a, _b, _c, _d, _e, _f, _g;
252
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
253
- 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);
254
521
  }
255
- var translateX;
256
- var translateY;
257
- var textBaseline = 'middle';
258
- var textAlign = 'center';
259
- switch (this.options.translatePlacement) {
260
- case 'top':
261
- translateX = this.options.width / 2;
262
- translateY = 0;
263
- textBaseline = 'top';
264
- break;
265
- case 'top-start':
266
- translateX = 0;
267
- translateY = 0;
268
- textBaseline = 'top';
269
- textAlign = 'start';
270
- break;
271
- case 'top-end':
272
- translateX = this.options.width;
273
- translateY = 0;
274
- textBaseline = 'top';
275
- textAlign = 'end';
276
- break;
277
- case 'bottom':
278
- translateX = this.options.width / 2;
279
- translateY = this.options.height;
280
- textBaseline = 'bottom';
281
- break;
282
- case 'bottom-start':
283
- translateX = 0;
284
- translateY = this.options.height;
285
- textBaseline = 'bottom';
286
- textAlign = 'start';
287
- break;
288
- case 'bottom-end':
289
- translateX = this.options.width;
290
- translateY = this.options.height;
291
- textBaseline = 'bottom';
292
- textAlign = 'end';
293
- break;
294
- case 'left':
295
- translateX = 0;
296
- translateY = this.options.height / 2;
297
- textAlign = 'start';
298
- break;
299
- case 'right':
300
- translateX = this.options.width;
301
- translateY = this.options.height / 2;
302
- textAlign = 'end';
303
- break;
304
- case 'middle':
305
- translateX = this.options.width / 2;
306
- translateY = this.options.height / 2;
307
- break;
522
+ else {
523
+ Object.keys(args).forEach(function (key) {
524
+ _this.options[key] = args[key];
525
+ });
308
526
  }
309
- 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)) {
310
- textBaseline = 'top';
311
- textAlign = 'left';
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);
536
+ }
537
+ else {
538
+ this.parentElement = parent;
312
539
  }
313
- isUndefined((_d = this.props) === null || _d === void 0 ? void 0 : _d.translateX) && (this.options.translateX = translateX);
314
- isUndefined((_e = this.props) === null || _e === void 0 ? void 0 : _e.translateY) && (this.options.translateY = translateY);
315
- isUndefined((_f = this.props) === null || _f === void 0 ? void 0 : _f.textBaseline) && (this.options.textBaseline = textBaseline);
316
- isUndefined((_g = this.props) === null || _g === void 0 ? void 0 : _g.textAlign) && (this.options.textAlign = textAlign);
317
540
  };
318
541
  Watermark.prototype.validateUnique = function () {
319
542
  var result = true;
@@ -343,13 +566,13 @@ var Watermark = /** @class */ (function () {
343
566
  };
344
567
  Watermark.prototype.draw = function () {
345
568
  var _this = this;
346
- var canvas = Watermark.createCanvas(this.options.width, this.options.height);
347
- // document.body?.appendChild(canvas)
348
- var ctx = canvas.getContext('2d');
569
+ // const canvas = Watermark.createCanvas(this.options.width, this.options.height)
570
+ var ctx = this.canvas.getContext('2d');
349
571
  if (ctx === null) {
350
572
  throw new Error('get context error');
351
573
  }
352
574
  this.setStyle(ctx);
575
+ ctx.save();
353
576
  ctx.translate(this.options.translateX, this.options.translateY);
354
577
  ctx.rotate(this.options.rotate);
355
578
  return new Promise(function (resolve) {
@@ -370,26 +593,70 @@ var Watermark = /** @class */ (function () {
370
593
  });
371
594
  };
372
595
  Watermark.prototype.setStyle = function (ctx) {
596
+ var _a;
373
597
  var propName = 'fillStyle';
374
598
  if (this.options.textType === 'stroke') {
375
599
  propName = 'strokeStyle';
376
600
  }
377
- ctx[propName] && (ctx[propName] = this.options.fontColor);
378
- ctx.font = "".concat(this.options.fontStyle, " ").concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
379
- ctx.filter = this.options.filter;
601
+ var style = this.options.fontColor;
602
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) {
603
+ switch (this.options.advancedStyle.type) {
604
+ case 'linear':
605
+ style = this.createLinearGradient(ctx);
606
+ break;
607
+ case 'radial':
608
+ style = this.createRadialGradient(ctx);
609
+ break;
610
+ case 'conic':
611
+ style = this.createConicGradient(ctx);
612
+ break;
613
+ case 'pattern':
614
+ style = this.createPattern(ctx);
615
+ break;
616
+ }
617
+ }
618
+ ctx[propName] && style && (ctx[propName] = style);
380
619
  this.options.textAlign && (ctx.textAlign = this.options.textAlign);
381
620
  this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
382
621
  ctx.globalAlpha = this.options.globalAlpha;
383
622
  if (this.options.shadowStyle) {
384
- ctx.shadowBlur = this.options.shadowStyle.shadowBlur || 0;
385
- ctx.shadowColor = this.options.shadowStyle.shadowColor || '#00000000';
386
- ctx.shadowOffsetX = this.options.shadowStyle.shadowOffsetX || 0;
387
- ctx.shadowOffsetY = this.options.shadowStyle.shadowOffsetY || 0;
623
+ ctx.shadowBlur = getValue(this.options.shadowStyle.shadowBlur, 0);
624
+ ctx.shadowColor = getValue(this.options.shadowStyle.shadowColor, '#00000000');
625
+ ctx.shadowOffsetX = getValue(this.options.shadowStyle.shadowOffsetX, 0);
626
+ ctx.shadowOffsetY = getValue(this.options.shadowStyle.shadowOffsetY, 0);
388
627
  }
389
628
  if (isFunction(this.options.extraDrawFunc)) {
390
629
  this.options.extraDrawFunc(ctx);
391
630
  }
392
631
  };
632
+ Watermark.prototype.createLinearGradient = function (ctx) {
633
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
634
+ 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));
635
+ (_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) {
636
+ gradient.addColorStop(item.offset, item.color);
637
+ });
638
+ return gradient;
639
+ };
640
+ Watermark.prototype.createConicGradient = function (ctx) {
641
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
642
+ 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));
643
+ (_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) {
644
+ gradient.addColorStop(item.offset, item.color);
645
+ });
646
+ return gradient;
647
+ };
648
+ Watermark.prototype.createRadialGradient = function (ctx) {
649
+ 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;
650
+ 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));
651
+ (_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) {
652
+ gradient.addColorStop(item.offset, item.color);
653
+ });
654
+ return gradient;
655
+ };
656
+ Watermark.prototype.createPattern = function (ctx) {
657
+ var _a, _b, _c, _d, _e, _f, _g, _h;
658
+ 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) || '');
659
+ };
393
660
  Watermark.prototype.setText = function (ctx, params) {
394
661
  var methodName = 'fillText';
395
662
  if (this.options.textType === 'stroke') {
@@ -402,21 +669,18 @@ var Watermark = /** @class */ (function () {
402
669
  text: this.options.content,
403
670
  x: 0,
404
671
  y: 0,
405
- maxWidth: this.options.textRowMaxWidth
672
+ maxWidth: this.options.textRowMaxWidth || this.options.width
406
673
  });
407
674
  resolve(ctx.canvas);
408
675
  };
409
676
  Watermark.prototype.drawImage = function (ctx, resolve) {
410
677
  var _this = this;
411
- var image = new Image();
412
- image.setAttribute('crossOrigin', 'Anonymous');
413
- image.src = this.options.image;
414
- image.onload = function () {
678
+ loadImage(this.options.image).then(function (image) {
415
679
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
416
680
  var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
417
681
  ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
418
682
  resolve(ctx.canvas);
419
- };
683
+ });
420
684
  };
421
685
  Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
422
686
  var _this = this;
@@ -436,23 +700,8 @@ var Watermark = /** @class */ (function () {
436
700
  // )
437
701
  // resolve(canvas)
438
702
  // }
439
- var width = this.options.textRowMaxWidth || this.options.width;
440
- var lines = getMultiLineData(ctx, this.options.content, width);
441
- var yOffsetValue;
442
- switch (this.options.textBaseline) {
443
- case 'middle':
444
- yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
445
- break;
446
- case 'bottom':
447
- case 'alphabetic':
448
- case 'ideographic':
449
- yOffsetValue = (lines.length - 1) * this.options.lineHeight;
450
- break;
451
- case 'top':
452
- case 'hanging':
453
- yOffsetValue = 0;
454
- break;
455
- }
703
+ var lines = this.recommendOptions.textLine.data;
704
+ var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
456
705
  lines.forEach(function (text, index) {
457
706
  _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
458
707
  });
@@ -461,18 +710,14 @@ var Watermark = /** @class */ (function () {
461
710
  Watermark.prototype.drawRichText = function (ctx, resolve) {
462
711
  var _this = this;
463
712
  var obj = createCustomContentSVG(ctx, this.options);
464
- var image = new Image();
465
- image.width = obj.width;
466
- image.height = obj.height;
467
- image.src = convertSVGToImage(obj.element);
468
- image.onload = function () {
713
+ loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
469
714
  var imagePosition = _this.getDrawImagePosition(image.width, image.height);
470
715
  ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
471
716
  resolve(ctx.canvas);
472
- };
717
+ });
473
718
  };
474
719
  Watermark.prototype.getImageRect = function (image) {
475
- var rect = { width: this.options.imageWidth, height: this.options.imageHeight };
720
+ var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
476
721
  switch (true) {
477
722
  case rect.width !== 0 && rect.height === 0:
478
723
  rect.height = rect.width * image.height / image.width;
@@ -570,6 +815,15 @@ var Watermark = /** @class */ (function () {
570
815
  characterData: true // 节点内容或节点文本的变动。
571
816
  });
572
817
  };
818
+ Watermark.prototype.clearCanvas = function () {
819
+ var ctx = this.canvas.getContext('2d');
820
+ if (ctx === null) {
821
+ throw new Error('get context error');
822
+ }
823
+ ctx.restore();
824
+ ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
825
+ // this.canvas.width = this.options.w
826
+ };
573
827
  return Watermark;
574
828
  }());
575
829