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.
@@ -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,43 +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: 20,
165
- fontFamily: 'sans-serif',
166
- fontStyle: '',
167
- fontColor: '#000',
168
- fontWeight: 'normal',
169
- filter: 'none',
170
- globalAlpha: 0.5,
171
- mode: 'default',
172
- mutationObserve: true,
173
- unique: true,
174
- parent: 'body',
175
- onSuccess: function () { },
176
- onBeforeDestroy: function () { },
177
- onDestroyed: function () { }
178
- }, props);
444
+ this.props = args;
445
+ this.options = Object.assign({}, initialOptions, args);
179
446
  this.changeParentElement(this.options.parent);
180
- this.initializeOptions();
447
+ this.canvas = Watermark.createCanvas(this.options.width, this.options.height);
448
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
181
449
  }
182
450
  /**
183
451
  * Create an HD canvas.
@@ -195,14 +463,10 @@
195
463
  (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
196
464
  return canvas;
197
465
  };
198
- Watermark.prototype.changeParentElement = function (parent) {
199
- if (typeof parent === 'string') {
200
- var parentElement = document.querySelector(parent);
201
- parentElement && (this.parentElement = parentElement);
202
- }
203
- else {
204
- this.parentElement = parent;
205
- }
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);
206
470
  };
207
471
  /**
208
472
  * Creating a watermark.
@@ -224,6 +488,7 @@
224
488
  case 1:
225
489
  canvas = _c.sent();
226
490
  image = convertImage(canvas);
491
+ this.clearCanvas();
227
492
  this.watermarkDom = document.createElement('div');
228
493
  watermarkInnerDom = document.createElement('div');
229
494
  this.watermarkDom.__WATERMARK__ = 'watermark';
@@ -243,7 +508,7 @@
243
508
  });
244
509
  };
245
510
  /**
246
- * Deleting this watermark.
511
+ * Delete this watermark.
247
512
  */
248
513
  Watermark.prototype.destroy = function () {
249
514
  var _a, _b, _c, _d, _e, _f, _g;
@@ -253,73 +518,31 @@
253
518
  (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
254
519
  (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
255
520
  };
256
- Watermark.prototype.initializeOptions = function () {
257
- var _a, _b, _c, _d, _e, _f, _g;
258
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.rotate) {
259
- 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);
260
527
  }
261
- var translateX;
262
- var translateY;
263
- var textBaseline = 'middle';
264
- var textAlign = 'center';
265
- switch (this.options.translatePlacement) {
266
- case 'top':
267
- translateX = this.options.width / 2;
268
- translateY = 0;
269
- textBaseline = 'top';
270
- break;
271
- case 'top-start':
272
- translateX = 0;
273
- translateY = 0;
274
- textBaseline = 'top';
275
- textAlign = 'start';
276
- break;
277
- case 'top-end':
278
- translateX = this.options.width;
279
- translateY = 0;
280
- textBaseline = 'top';
281
- textAlign = 'end';
282
- break;
283
- case 'bottom':
284
- translateX = this.options.width / 2;
285
- translateY = this.options.height;
286
- textBaseline = 'bottom';
287
- break;
288
- case 'bottom-start':
289
- translateX = 0;
290
- translateY = this.options.height;
291
- textBaseline = 'bottom';
292
- textAlign = 'start';
293
- break;
294
- case 'bottom-end':
295
- translateX = this.options.width;
296
- translateY = this.options.height;
297
- textBaseline = 'bottom';
298
- textAlign = 'end';
299
- break;
300
- case 'left':
301
- translateX = 0;
302
- translateY = this.options.height / 2;
303
- textAlign = 'start';
304
- break;
305
- case 'right':
306
- translateX = this.options.width;
307
- translateY = this.options.height / 2;
308
- textAlign = 'end';
309
- break;
310
- case 'middle':
311
- translateX = this.options.width / 2;
312
- translateY = this.options.height / 2;
313
- break;
528
+ else {
529
+ Object.keys(args).forEach(function (key) {
530
+ _this.options[key] = args[key];
531
+ });
314
532
  }
315
- 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)) {
316
- textBaseline = 'top';
317
- textAlign = 'left';
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);
542
+ }
543
+ else {
544
+ this.parentElement = parent;
318
545
  }
319
- isUndefined((_d = this.props) === null || _d === void 0 ? void 0 : _d.translateX) && (this.options.translateX = translateX);
320
- isUndefined((_e = this.props) === null || _e === void 0 ? void 0 : _e.translateY) && (this.options.translateY = translateY);
321
- isUndefined((_f = this.props) === null || _f === void 0 ? void 0 : _f.textBaseline) && (this.options.textBaseline = textBaseline);
322
- isUndefined((_g = this.props) === null || _g === void 0 ? void 0 : _g.textAlign) && (this.options.textAlign = textAlign);
323
546
  };
324
547
  Watermark.prototype.validateUnique = function () {
325
548
  var result = true;
@@ -349,13 +572,13 @@
349
572
  };
350
573
  Watermark.prototype.draw = function () {
351
574
  var _this = this;
352
- var canvas = Watermark.createCanvas(this.options.width, this.options.height);
353
- // document.body?.appendChild(canvas)
354
- var ctx = canvas.getContext('2d');
575
+ // const canvas = Watermark.createCanvas(this.options.width, this.options.height)
576
+ var ctx = this.canvas.getContext('2d');
355
577
  if (ctx === null) {
356
578
  throw new Error('get context error');
357
579
  }
358
580
  this.setStyle(ctx);
581
+ ctx.save();
359
582
  ctx.translate(this.options.translateX, this.options.translateY);
360
583
  ctx.rotate(this.options.rotate);
361
584
  return new Promise(function (resolve) {
@@ -376,26 +599,70 @@
376
599
  });
377
600
  };
378
601
  Watermark.prototype.setStyle = function (ctx) {
602
+ var _a;
379
603
  var propName = 'fillStyle';
380
604
  if (this.options.textType === 'stroke') {
381
605
  propName = 'strokeStyle';
382
606
  }
383
- ctx[propName] && (ctx[propName] = this.options.fontColor);
384
- ctx.font = "".concat(this.options.fontStyle, " ").concat(this.options.fontWeight, " ").concat(this.options.fontSize, "px ").concat(this.options.fontFamily);
385
- ctx.filter = this.options.filter;
607
+ var style = this.options.fontColor;
608
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) {
609
+ switch (this.options.advancedStyle.type) {
610
+ case 'linear':
611
+ style = this.createLinearGradient(ctx);
612
+ break;
613
+ case 'radial':
614
+ style = this.createRadialGradient(ctx);
615
+ break;
616
+ case 'conic':
617
+ style = this.createConicGradient(ctx);
618
+ break;
619
+ case 'pattern':
620
+ style = this.createPattern(ctx);
621
+ break;
622
+ }
623
+ }
624
+ ctx[propName] && style && (ctx[propName] = style);
386
625
  this.options.textAlign && (ctx.textAlign = this.options.textAlign);
387
626
  this.options.textBaseline && (ctx.textBaseline = this.options.textBaseline);
388
627
  ctx.globalAlpha = this.options.globalAlpha;
389
628
  if (this.options.shadowStyle) {
390
- ctx.shadowBlur = this.options.shadowStyle.shadowBlur || 0;
391
- ctx.shadowColor = this.options.shadowStyle.shadowColor || '#00000000';
392
- ctx.shadowOffsetX = this.options.shadowStyle.shadowOffsetX || 0;
393
- 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);
394
633
  }
395
634
  if (isFunction(this.options.extraDrawFunc)) {
396
635
  this.options.extraDrawFunc(ctx);
397
636
  }
398
637
  };
638
+ Watermark.prototype.createLinearGradient = function (ctx) {
639
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
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));
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) {
642
+ gradient.addColorStop(item.offset, item.color);
643
+ });
644
+ return gradient;
645
+ };
646
+ Watermark.prototype.createConicGradient = function (ctx) {
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) {
650
+ gradient.addColorStop(item.offset, item.color);
651
+ });
652
+ return gradient;
653
+ };
654
+ Watermark.prototype.createRadialGradient = function (ctx) {
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) {
658
+ gradient.addColorStop(item.offset, item.color);
659
+ });
660
+ return gradient;
661
+ };
662
+ Watermark.prototype.createPattern = function (ctx) {
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) || '');
665
+ };
399
666
  Watermark.prototype.setText = function (ctx, params) {
400
667
  var methodName = 'fillText';
401
668
  if (this.options.textType === 'stroke') {
@@ -408,21 +675,18 @@
408
675
  text: this.options.content,
409
676
  x: 0,
410
677
  y: 0,
411
- maxWidth: this.options.textRowMaxWidth
678
+ maxWidth: this.options.textRowMaxWidth || this.options.width
412
679
  });
413
680
  resolve(ctx.canvas);
414
681
  };
415
682
  Watermark.prototype.drawImage = function (ctx, resolve) {
416
683
  var _this = this;
417
- var image = new Image();
418
- image.setAttribute('crossOrigin', 'Anonymous');
419
- image.src = this.options.image;
420
- image.onload = function () {
684
+ loadImage(this.options.image).then(function (image) {
421
685
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
422
686
  var imagePosition = _this.getDrawImagePosition(imageWidth, imageHeight);
423
687
  ctx.drawImage(image, imagePosition.x, imagePosition.y, imageWidth, imageHeight);
424
688
  resolve(ctx.canvas);
425
- };
689
+ });
426
690
  };
427
691
  Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
428
692
  var _this = this;
@@ -442,23 +706,8 @@
442
706
  // )
443
707
  // resolve(canvas)
444
708
  // }
445
- var width = this.options.textRowMaxWidth || this.options.width;
446
- var lines = getMultiLineData(ctx, this.options.content, width);
447
- var yOffsetValue;
448
- switch (this.options.textBaseline) {
449
- case 'middle':
450
- yOffsetValue = (lines.length - 1) * this.options.lineHeight / 2;
451
- break;
452
- case 'bottom':
453
- case 'alphabetic':
454
- case 'ideographic':
455
- yOffsetValue = (lines.length - 1) * this.options.lineHeight;
456
- break;
457
- case 'top':
458
- case 'hanging':
459
- yOffsetValue = 0;
460
- break;
461
- }
709
+ var lines = this.recommendOptions.textLine.data;
710
+ var yOffsetValue = this.recommendOptions.textLine.yOffsetValue;
462
711
  lines.forEach(function (text, index) {
463
712
  _this.setText(ctx, { text: text, x: 0, y: _this.options.lineHeight * index - yOffsetValue });
464
713
  });
@@ -467,18 +716,14 @@
467
716
  Watermark.prototype.drawRichText = function (ctx, resolve) {
468
717
  var _this = this;
469
718
  var obj = createCustomContentSVG(ctx, this.options);
470
- var image = new Image();
471
- image.width = obj.width;
472
- image.height = obj.height;
473
- image.src = convertSVGToImage(obj.element);
474
- image.onload = function () {
719
+ loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
475
720
  var imagePosition = _this.getDrawImagePosition(image.width, image.height);
476
721
  ctx.drawImage(image, imagePosition.x, imagePosition.y, ctx.canvas.width, ctx.canvas.height);
477
722
  resolve(ctx.canvas);
478
- };
723
+ });
479
724
  };
480
725
  Watermark.prototype.getImageRect = function (image) {
481
- var rect = { width: this.options.imageWidth, height: this.options.imageHeight };
726
+ var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
482
727
  switch (true) {
483
728
  case rect.width !== 0 && rect.height === 0:
484
729
  rect.height = rect.width * image.height / image.width;
@@ -576,6 +821,15 @@
576
821
  characterData: true // 节点内容或节点文本的变动。
577
822
  });
578
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
+ };
579
833
  return Watermark;
580
834
  }());
581
835