1 |
|
2 | 'use strict';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const getPixelRatio = (context) => {
|
10 | if (!context) {
|
11 | return 1;
|
12 | }
|
13 | const backingStore = context.backingStorePixelRatio ||
|
14 | context.webkitBackingStorePixelRatio ||
|
15 | context.mozBackingStorePixelRatio ||
|
16 | context.msBackingStorePixelRatio ||
|
17 | context.oBackingStorePixelRatio ||
|
18 | context.backingStorePixelRatio ||
|
19 | 1;
|
20 | return (window.devicePixelRatio || 1) / backingStore;
|
21 | };
|
22 | class Watermark {
|
23 | constructor(options) {
|
24 | this.option = {
|
25 | gapX: 212,
|
26 | gapY: 222,
|
27 | width: 120,
|
28 | height: 64,
|
29 | rotate: -22,
|
30 | fontStyle: 'normal',
|
31 | fontWeight: 'normal',
|
32 | fontColor: 'rgba(0,0,0,.15)',
|
33 | fontSize: 16,
|
34 | fontFamily: 'sans-serif',
|
35 | };
|
36 | this.option = Object.assign(Object.assign({}, this.option), options);
|
37 | }
|
38 | async create() {
|
39 | const { image = '', content = '', gapX = 212, gapY = 222, width = 120, height = 64, rotate = -22, fontStyle = 'normal', fontWeight = 'normal', fontColor = 'rgba(0,0,0,.15)', fontSize = 16, fontFamily = 'sans-serif', offsetLeft, offsetTop, } = this.option;
|
40 | const canvas = document.createElement('canvas');
|
41 | const ctx = canvas.getContext('2d');
|
42 | const ratio = getPixelRatio(ctx);
|
43 | const canvasWidth = `${(gapX + width) * ratio}px`;
|
44 | const canvasHeight = `${(gapY + height) * ratio}px`;
|
45 | const canvasOffsetLeft = offsetLeft || gapX / 2;
|
46 | const canvasOffsetTop = offsetTop || gapY / 2;
|
47 | canvas.setAttribute('width', canvasWidth);
|
48 | canvas.setAttribute('height', canvasHeight);
|
49 | return new Promise(async (resolve, reject) => {
|
50 | if (ctx) {
|
51 | ctx.translate(canvasOffsetLeft * ratio, canvasOffsetTop * ratio);
|
52 | ctx.rotate((Math.PI / 180) * Number(rotate));
|
53 | const markWidth = width * ratio;
|
54 | const markHeight = height * ratio;
|
55 | if (image) {
|
56 | const img = new Image();
|
57 | img.crossOrigin = 'anonymous';
|
58 | img.referrerPolicy = 'no-referrer';
|
59 | img.src = image;
|
60 | img.onload = async () => {
|
61 | ctx.drawImage(img, 0, 0, markWidth, markHeight);
|
62 | return resolve(canvas.toDataURL());
|
63 | };
|
64 | img.onerror = (error) => {
|
65 | return reject(error);
|
66 | };
|
67 | }
|
68 | else if (content) {
|
69 | const markSize = Number(fontSize) * ratio;
|
70 | ctx.font = `${fontStyle} normal ${fontWeight} ${markSize}px/${markHeight}px ${fontFamily}`;
|
71 | ctx.fillStyle = fontColor;
|
72 | if (Array.isArray(content)) {
|
73 | content === null || content === void 0 ? void 0 : content.forEach((item, index) => ctx.fillText(item, 0, index * 50));
|
74 | }
|
75 | else {
|
76 | ctx.fillText(content, 0, 0);
|
77 | }
|
78 | return resolve(canvas.toDataURL());
|
79 | }
|
80 | }
|
81 | else {
|
82 | return reject('Error: Canvas is not supported in the current environment');
|
83 | }
|
84 | });
|
85 | }
|
86 | }
|
87 |
|
88 | module.exports = Watermark;
|
89 |
|