watermark-js-plus 1.6.4 → 1.6.6
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.
- package/dist/es/src/core/blind-decode.js +16 -0
- package/dist/es/src/core/blind.js +9 -5
- package/dist/es/src/core/watermark.js +49 -46
- package/dist/ie/es/src/compat/ie/blind-decode.js +155 -0
- package/dist/ie/es/src/core/blind-decode.js +28 -0
- package/dist/ie/es/src/core/blind.js +9 -5
- package/dist/ie/es/src/core/watermark.js +49 -46
- package/dist/ie/es/src/index.ie.js +4 -0
- package/dist/ie/index.cjs.js +241 -52
- package/dist/ie/index.cjs.js.map +1 -1
- package/dist/ie/index.cjs.min.js +1 -1
- package/dist/ie/index.esm.js +241 -52
- package/dist/ie/index.esm.js.map +1 -1
- package/dist/ie/index.esm.min.js +1 -1
- package/dist/ie/index.iife.js +241 -52
- package/dist/ie/index.iife.js.map +1 -1
- package/dist/ie/index.iife.min.js +1 -1
- package/dist/ie/index.umd.js +241 -52
- package/dist/ie/index.umd.js.map +1 -1
- package/dist/ie/index.umd.min.js +1 -1
- package/dist/index.cjs.js +73 -52
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.esm.js +73 -52
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.iife.js +73 -52
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.umd.js +73 -52
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/types/compat/ie/blind-decode.d.ts +3 -0
- package/dist/types/core/blind-decode.d.ts +15 -0
- package/dist/types/core/watermark.d.ts +3 -2
- package/package.json +20 -20
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var blindDecodeFallback;
|
|
2
|
+
var applyBlindDecodeComposite = function (_a) {
|
|
3
|
+
var ctx = _a.ctx, width = _a.width, height = _a.height, fillColor = _a.fillColor, compositeOperation = _a.compositeOperation, compositeTimes = _a.compositeTimes;
|
|
4
|
+
if (!(compositeTimes > 0)) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
ctx.globalCompositeOperation;
|
|
8
|
+
ctx.globalCompositeOperation = compositeOperation;
|
|
9
|
+
if (ctx.globalCompositeOperation !== compositeOperation && blindDecodeFallback) ;
|
|
10
|
+
ctx.fillStyle = fillColor;
|
|
11
|
+
for (var i = 0; i < compositeTimes; i++) {
|
|
12
|
+
ctx.fillRect(0, 0, width, height);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { applyBlindDecodeComposite };
|
|
@@ -2,6 +2,7 @@ import { __extends, __assign, __awaiter, __generator } from '../../node_modules/
|
|
|
2
2
|
import { convertImage, isFunction } from '../utils/index.js';
|
|
3
3
|
import { Watermark } from './watermark.js';
|
|
4
4
|
import { WatermarkCanvas } from './canvas.js';
|
|
5
|
+
import { applyBlindDecodeComposite } from './blind-decode.js';
|
|
5
6
|
import protection from '../utils/protection.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -64,11 +65,14 @@ var BlindWatermark = /** @class */ (function (_super) {
|
|
|
64
65
|
throw new Error('get context error');
|
|
65
66
|
}
|
|
66
67
|
ctx.drawImage(img_1, 0, 0, width, height);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
applyBlindDecodeComposite({
|
|
69
|
+
ctx: ctx,
|
|
70
|
+
width: width,
|
|
71
|
+
height: height,
|
|
72
|
+
fillColor: fillColor,
|
|
73
|
+
compositeOperation: compositeOperation,
|
|
74
|
+
compositeTimes: compositeTimes,
|
|
75
|
+
});
|
|
72
76
|
var resultImage = convertImage(canvas);
|
|
73
77
|
if (isFunction(onSuccess)) {
|
|
74
78
|
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(resultImage);
|
|
@@ -64,17 +64,18 @@ var Watermark = /** @class */ (function () {
|
|
|
64
64
|
return [2 /*return*/];
|
|
65
65
|
}
|
|
66
66
|
this.isCreating = true;
|
|
67
|
+
_h.label = 1;
|
|
68
|
+
case 1:
|
|
69
|
+
_h.trys.push([1, , 3, 4]);
|
|
67
70
|
if (!this.validateUnique()) {
|
|
68
|
-
this.isCreating = false;
|
|
69
71
|
return [2 /*return*/];
|
|
70
72
|
}
|
|
71
73
|
if (!this.validateContent()) {
|
|
72
|
-
this.isCreating = false;
|
|
73
74
|
return [2 /*return*/];
|
|
74
75
|
}
|
|
75
76
|
firstDraw = isUndefined(this.watermarkDom);
|
|
76
77
|
return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
|
|
77
|
-
case
|
|
78
|
+
case 2:
|
|
78
79
|
_h.sent();
|
|
79
80
|
this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
|
|
80
81
|
image = convertImage(this.layoutCanvas);
|
|
@@ -84,22 +85,25 @@ var Watermark = /** @class */ (function () {
|
|
|
84
85
|
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
85
86
|
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
86
87
|
parentElementType = this.checkParentElementType();
|
|
87
|
-
this.watermarkDom.style.cssText = "\n
|
|
88
|
+
this.watermarkDom.style.cssText = "\n z-index:".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
|
|
88
89
|
backgroundSize = generateBackgroundSize(this.options);
|
|
89
|
-
watermarkInnerDom.style.cssText = "\n
|
|
90
|
+
watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, ";\n ").concat(generateAnimationStyle(this.options.movable, this.options.backgroundRepeat), "\n ");
|
|
90
91
|
this.watermarkDom.appendChild(watermarkInnerDom);
|
|
91
92
|
this.parentElement.appendChild(this.watermarkDom);
|
|
92
93
|
if (this.options.mutationObserve) {
|
|
93
94
|
try {
|
|
94
|
-
this.
|
|
95
|
+
this.bindMutationObserver();
|
|
95
96
|
}
|
|
96
97
|
catch (_j) {
|
|
97
98
|
(_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
|
|
102
|
+
return [3 /*break*/, 4];
|
|
103
|
+
case 3:
|
|
101
104
|
this.isCreating = false;
|
|
102
|
-
return [
|
|
105
|
+
return [7 /*endfinally*/];
|
|
106
|
+
case 4: return [2 /*return*/];
|
|
103
107
|
}
|
|
104
108
|
});
|
|
105
109
|
});
|
|
@@ -122,7 +126,7 @@ var Watermark = /** @class */ (function () {
|
|
|
122
126
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
123
127
|
(_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
124
128
|
(_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
125
|
-
(_d = this.
|
|
129
|
+
(_d = this.parentObserver) === null || _d === void 0 ? void 0 : _d.disconnect();
|
|
126
130
|
(_f = (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.parentNode) === null || _f === void 0 ? void 0 : _f.removeChild(this.watermarkDom);
|
|
127
131
|
(_h = (_g = this.options).onDestroyed) === null || _h === void 0 ? void 0 : _h.call(_g);
|
|
128
132
|
};
|
|
@@ -182,56 +186,55 @@ var Watermark = /** @class */ (function () {
|
|
|
182
186
|
}
|
|
183
187
|
return 'custom';
|
|
184
188
|
};
|
|
185
|
-
Watermark.prototype.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
return
|
|
189
|
-
|
|
190
|
-
this.observer = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
|
|
191
|
-
return __generator(this, function (_a) {
|
|
192
|
-
switch (_a.label) {
|
|
189
|
+
Watermark.prototype.recreateOnMutation = function () {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
191
|
+
var _b, _c;
|
|
192
|
+
return __generator(this, function (_d) {
|
|
193
|
+
switch (_d.label) {
|
|
193
194
|
case 0:
|
|
194
|
-
|
|
195
|
+
_d.trys.push([0, 2, , 3]);
|
|
195
196
|
this.remove();
|
|
196
197
|
return [4 /*yield*/, this.create()];
|
|
197
198
|
case 1:
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
case 2:
|
|
199
|
+
_d.sent();
|
|
200
|
+
return [3 /*break*/, 3];
|
|
201
|
+
case 2:
|
|
202
|
+
_d.sent();
|
|
203
|
+
(_c = (_b = this.options).onObserveError) === null || _c === void 0 ? void 0 : _c.call(_b);
|
|
204
|
+
return [3 /*break*/, 3];
|
|
205
|
+
case 3: return [2 /*return*/];
|
|
201
206
|
}
|
|
202
207
|
});
|
|
203
|
-
});
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
Watermark.prototype.bindMutationObserver = function () {
|
|
211
|
+
var _this = this;
|
|
212
|
+
if (!this.watermarkDom) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.observer = new MutationObserver(function (mutationsList) {
|
|
216
|
+
if (mutationsList.length > 0) {
|
|
217
|
+
void _this.recreateOnMutation();
|
|
218
|
+
}
|
|
219
|
+
});
|
|
204
220
|
this.observer.observe(this.watermarkDom, {
|
|
205
221
|
attributes: true, // 属性的变动
|
|
222
|
+
attributeFilter: ['style', 'class', 'hidden', 'id'], // 仅监听会影响水印展示或标识的属性
|
|
206
223
|
childList: true, // 子节点的变动(指新增,删除或者更改)
|
|
207
224
|
subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
|
|
208
225
|
characterData: true, // 节点内容或节点文本的变动。
|
|
209
226
|
});
|
|
210
|
-
this.
|
|
211
|
-
var
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (!watermarkRemoved) return [3 /*break*/, 3];
|
|
222
|
-
this.remove();
|
|
223
|
-
return [4 /*yield*/, this.create()];
|
|
224
|
-
case 2:
|
|
225
|
-
_a.sent();
|
|
226
|
-
_a.label = 3;
|
|
227
|
-
case 3:
|
|
228
|
-
_i++;
|
|
229
|
-
return [3 /*break*/, 1];
|
|
230
|
-
case 4: return [2 /*return*/];
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
}); });
|
|
234
|
-
this.parentObserve.observe(this.parentElement, {
|
|
227
|
+
this.parentObserver = new MutationObserver(function (mutationsList) {
|
|
228
|
+
var watermarkDom = _this.watermarkDom;
|
|
229
|
+
if (!watermarkDom) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
var watermarkRemoved = mutationsList.some(function (item) { return Array.from(item.removedNodes).includes(watermarkDom); });
|
|
233
|
+
if (watermarkRemoved) {
|
|
234
|
+
void _this.recreateOnMutation();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
this.parentObserver.observe(this.parentElement, {
|
|
235
238
|
childList: true, // 子节点的变动(指新增,删除或者更改)
|
|
236
239
|
});
|
|
237
240
|
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var MAX_CHANNEL_VALUE = 255;
|
|
2
|
+
var overlay = function (backdrop, source) {
|
|
3
|
+
if (backdrop <= 0.5) {
|
|
4
|
+
return 2 * backdrop * source;
|
|
5
|
+
}
|
|
6
|
+
return 1 - 2 * (1 - backdrop) * (1 - source);
|
|
7
|
+
};
|
|
8
|
+
var colorBurn = function (backdrop, source) {
|
|
9
|
+
if (backdrop === 1) {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
if (source === 0) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
return 1 - Math.min(1, (1 - backdrop) / source);
|
|
16
|
+
};
|
|
17
|
+
var getBlendFunction = function (compositeOperation) {
|
|
18
|
+
switch (compositeOperation) {
|
|
19
|
+
case 'overlay':
|
|
20
|
+
return overlay;
|
|
21
|
+
case 'color-burn':
|
|
22
|
+
return colorBurn;
|
|
23
|
+
default:
|
|
24
|
+
throw new Error("Unsupported blind watermark composite operation: ".concat(compositeOperation));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var parseNormalizedColor = function (fillStyle) {
|
|
28
|
+
var hexColor = /^#([\da-f]{6})$/i.exec(fillStyle);
|
|
29
|
+
if (hexColor) {
|
|
30
|
+
return {
|
|
31
|
+
color: [
|
|
32
|
+
parseInt(hexColor[1].slice(0, 2), 16),
|
|
33
|
+
parseInt(hexColor[1].slice(2, 4), 16),
|
|
34
|
+
parseInt(hexColor[1].slice(4, 6), 16),
|
|
35
|
+
],
|
|
36
|
+
alpha: 1,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
var rgbColor = /^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)$/i.exec(fillStyle);
|
|
40
|
+
if (rgbColor) {
|
|
41
|
+
return {
|
|
42
|
+
color: [Number(rgbColor[1]), Number(rgbColor[2]), Number(rgbColor[3])],
|
|
43
|
+
alpha: rgbColor[4] === undefined ? 1 : Math.max(0, Math.min(1, Number(rgbColor[4]))),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
};
|
|
48
|
+
var getFillColor = function (ctx, fillColor) {
|
|
49
|
+
var colorCanvas = (ctx.canvas.ownerDocument || document).createElement('canvas');
|
|
50
|
+
colorCanvas.width = 1;
|
|
51
|
+
colorCanvas.height = 1;
|
|
52
|
+
var colorContext = colorCanvas.getContext('2d');
|
|
53
|
+
if (!colorContext) {
|
|
54
|
+
throw new Error('get context error');
|
|
55
|
+
}
|
|
56
|
+
colorContext.fillStyle = fillColor;
|
|
57
|
+
if (typeof colorContext.fillStyle === 'string') {
|
|
58
|
+
var parsedColor = parseNormalizedColor(colorContext.fillStyle);
|
|
59
|
+
if (parsedColor) {
|
|
60
|
+
return parsedColor;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
colorContext.fillRect(0, 0, 1, 1);
|
|
64
|
+
var color = colorContext.getImageData(0, 0, 1, 1).data;
|
|
65
|
+
return {
|
|
66
|
+
color: [color[0], color[1], color[2]],
|
|
67
|
+
alpha: color[3] / MAX_CHANNEL_VALUE,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
var isOpaque = function (data) {
|
|
71
|
+
for (var i = 3; i < data.length; i += 4) {
|
|
72
|
+
if (data[i] !== MAX_CHANNEL_VALUE) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
var applyCommonOpaqueBlend = function (data, sourceColor, compositeOperation, compositeTimes) {
|
|
79
|
+
var isBlack = sourceColor[0] === 0 && sourceColor[1] === 0 && sourceColor[2] === 0;
|
|
80
|
+
var isWhite = sourceColor[0] === 1 && sourceColor[1] === 1 && sourceColor[2] === 1;
|
|
81
|
+
if (compositeOperation === 'color-burn' && (isBlack || isWhite)) {
|
|
82
|
+
if (isBlack) {
|
|
83
|
+
for (var offset = 0; offset < data.length; offset += 4) {
|
|
84
|
+
for (var channel = 0; channel < 3; channel++) {
|
|
85
|
+
data[offset + channel] = data[offset + channel] === MAX_CHANNEL_VALUE ? MAX_CHANNEL_VALUE : 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (compositeOperation !== 'overlay' || (!isBlack && !isWhite)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
var iterations = Math.ceil(compositeTimes);
|
|
95
|
+
var multiplier = Math.pow(2, Math.min(iterations, 8));
|
|
96
|
+
for (var offset = 0; offset < data.length; offset += 4) {
|
|
97
|
+
for (var channel = 0; channel < 3; channel++) {
|
|
98
|
+
var value = data[offset + channel];
|
|
99
|
+
data[offset + channel] = isBlack
|
|
100
|
+
? Math.max(0, multiplier * value - MAX_CHANNEL_VALUE * (multiplier - 1))
|
|
101
|
+
: Math.min(MAX_CHANNEL_VALUE, multiplier * value);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
};
|
|
106
|
+
var applyOpaqueBlend = function (data, sourceColor, compositeTimes, blend) {
|
|
107
|
+
for (var i = 0; i < compositeTimes; i++) {
|
|
108
|
+
for (var offset = 0; offset < data.length; offset += 4) {
|
|
109
|
+
for (var channel = 0; channel < 3; channel++) {
|
|
110
|
+
data[offset + channel] =
|
|
111
|
+
blend(data[offset + channel] / MAX_CHANNEL_VALUE, sourceColor[channel]) * MAX_CHANNEL_VALUE;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var applyAlphaBlend = function (data, sourceColor, sourceAlpha, compositeTimes, blend) {
|
|
117
|
+
for (var i = 0; i < compositeTimes; i++) {
|
|
118
|
+
for (var offset = 0; offset < data.length; offset += 4) {
|
|
119
|
+
var backdropAlpha = data[offset + 3] / MAX_CHANNEL_VALUE;
|
|
120
|
+
var outputAlpha = sourceAlpha + backdropAlpha * (1 - sourceAlpha);
|
|
121
|
+
for (var channel = 0; channel < 3; channel++) {
|
|
122
|
+
var backdrop = data[offset + channel] / MAX_CHANNEL_VALUE;
|
|
123
|
+
var blendedSource = (1 - backdropAlpha) * sourceColor[channel] + backdropAlpha * blend(backdrop, sourceColor[channel]);
|
|
124
|
+
var premultipliedOutput = sourceAlpha * blendedSource + backdropAlpha * (1 - sourceAlpha) * backdrop;
|
|
125
|
+
data[offset + channel] = outputAlpha === 0 ? 0 : (premultipliedOutput / outputAlpha) * MAX_CHANNEL_VALUE;
|
|
126
|
+
}
|
|
127
|
+
data[offset + 3] = outputAlpha * MAX_CHANNEL_VALUE;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var softwareBlindDecodeFallback = function (_a) {
|
|
132
|
+
var ctx = _a.ctx, fillColor = _a.fillColor, compositeOperation = _a.compositeOperation, compositeTimes = _a.compositeTimes;
|
|
133
|
+
var blend = getBlendFunction(compositeOperation);
|
|
134
|
+
var _b = getFillColor(ctx, fillColor), color = _b.color, alpha = _b.alpha;
|
|
135
|
+
if (alpha === 0 || !(compositeTimes > 0)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
var imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
139
|
+
var sourceColor = [
|
|
140
|
+
color[0] / MAX_CHANNEL_VALUE,
|
|
141
|
+
color[1] / MAX_CHANNEL_VALUE,
|
|
142
|
+
color[2] / MAX_CHANNEL_VALUE,
|
|
143
|
+
];
|
|
144
|
+
if (alpha === 1 && isOpaque(imageData.data)) {
|
|
145
|
+
if (!applyCommonOpaqueBlend(imageData.data, sourceColor, compositeOperation, compositeTimes)) {
|
|
146
|
+
applyOpaqueBlend(imageData.data, sourceColor, compositeTimes, blend);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
applyAlphaBlend(imageData.data, sourceColor, alpha, compositeTimes, blend);
|
|
151
|
+
}
|
|
152
|
+
ctx.putImageData(imageData, 0, 0);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export { softwareBlindDecodeFallback };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var blindDecodeFallback;
|
|
2
|
+
var registerBlindDecodeFallback = function (fallback) {
|
|
3
|
+
blindDecodeFallback = fallback;
|
|
4
|
+
};
|
|
5
|
+
var applyBlindDecodeComposite = function (_a) {
|
|
6
|
+
var ctx = _a.ctx, width = _a.width, height = _a.height, fillColor = _a.fillColor, compositeOperation = _a.compositeOperation, compositeTimes = _a.compositeTimes;
|
|
7
|
+
if (!(compositeTimes > 0)) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
var previousCompositeOperation = ctx.globalCompositeOperation;
|
|
11
|
+
ctx.globalCompositeOperation = compositeOperation;
|
|
12
|
+
if (ctx.globalCompositeOperation !== compositeOperation && blindDecodeFallback) {
|
|
13
|
+
ctx.globalCompositeOperation = previousCompositeOperation;
|
|
14
|
+
blindDecodeFallback({
|
|
15
|
+
ctx: ctx,
|
|
16
|
+
fillColor: fillColor,
|
|
17
|
+
compositeOperation: compositeOperation,
|
|
18
|
+
compositeTimes: compositeTimes,
|
|
19
|
+
});
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
ctx.fillStyle = fillColor;
|
|
23
|
+
for (var i = 0; i < compositeTimes; i++) {
|
|
24
|
+
ctx.fillRect(0, 0, width, height);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { applyBlindDecodeComposite, registerBlindDecodeFallback };
|
|
@@ -2,6 +2,7 @@ import { __extends, __assign, __awaiter, __generator } from '../../node_modules/
|
|
|
2
2
|
import { convertImage, isFunction } from '../utils/index.js';
|
|
3
3
|
import { Watermark } from './watermark.js';
|
|
4
4
|
import { WatermarkCanvas } from './canvas.js';
|
|
5
|
+
import { applyBlindDecodeComposite } from './blind-decode.js';
|
|
5
6
|
import protection from '../utils/protection.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -64,11 +65,14 @@ var BlindWatermark = /** @class */ (function (_super) {
|
|
|
64
65
|
throw new Error('get context error');
|
|
65
66
|
}
|
|
66
67
|
ctx.drawImage(img_1, 0, 0, width, height);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
applyBlindDecodeComposite({
|
|
69
|
+
ctx: ctx,
|
|
70
|
+
width: width,
|
|
71
|
+
height: height,
|
|
72
|
+
fillColor: fillColor,
|
|
73
|
+
compositeOperation: compositeOperation,
|
|
74
|
+
compositeTimes: compositeTimes,
|
|
75
|
+
});
|
|
72
76
|
var resultImage = convertImage(canvas);
|
|
73
77
|
if (isFunction(onSuccess)) {
|
|
74
78
|
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(resultImage);
|
|
@@ -64,17 +64,18 @@ var Watermark = /** @class */ (function () {
|
|
|
64
64
|
return [2 /*return*/];
|
|
65
65
|
}
|
|
66
66
|
this.isCreating = true;
|
|
67
|
+
_h.label = 1;
|
|
68
|
+
case 1:
|
|
69
|
+
_h.trys.push([1, , 3, 4]);
|
|
67
70
|
if (!this.validateUnique()) {
|
|
68
|
-
this.isCreating = false;
|
|
69
71
|
return [2 /*return*/];
|
|
70
72
|
}
|
|
71
73
|
if (!this.validateContent()) {
|
|
72
|
-
this.isCreating = false;
|
|
73
74
|
return [2 /*return*/];
|
|
74
75
|
}
|
|
75
76
|
firstDraw = isUndefined(this.watermarkDom);
|
|
76
77
|
return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
|
|
77
|
-
case
|
|
78
|
+
case 2:
|
|
78
79
|
_h.sent();
|
|
79
80
|
this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
|
|
80
81
|
image = convertImage(this.layoutCanvas);
|
|
@@ -84,22 +85,25 @@ var Watermark = /** @class */ (function () {
|
|
|
84
85
|
this.watermarkDom.__WATERMARK__ = 'watermark';
|
|
85
86
|
this.watermarkDom.__WATERMARK__INSTANCE__ = this;
|
|
86
87
|
parentElementType = this.checkParentElementType();
|
|
87
|
-
this.watermarkDom.style.cssText = "\n
|
|
88
|
+
this.watermarkDom.style.cssText = "\n z-index:".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
|
|
88
89
|
backgroundSize = generateBackgroundSize(this.options);
|
|
89
|
-
watermarkInnerDom.style.cssText = "\n
|
|
90
|
+
watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, ";\n ").concat(generateAnimationStyle(this.options.movable, this.options.backgroundRepeat), "\n ");
|
|
90
91
|
this.watermarkDom.appendChild(watermarkInnerDom);
|
|
91
92
|
this.parentElement.appendChild(this.watermarkDom);
|
|
92
93
|
if (this.options.mutationObserve) {
|
|
93
94
|
try {
|
|
94
|
-
this.
|
|
95
|
+
this.bindMutationObserver();
|
|
95
96
|
}
|
|
96
97
|
catch (_j) {
|
|
97
98
|
(_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
|
|
102
|
+
return [3 /*break*/, 4];
|
|
103
|
+
case 3:
|
|
101
104
|
this.isCreating = false;
|
|
102
|
-
return [
|
|
105
|
+
return [7 /*endfinally*/];
|
|
106
|
+
case 4: return [2 /*return*/];
|
|
103
107
|
}
|
|
104
108
|
});
|
|
105
109
|
});
|
|
@@ -122,7 +126,7 @@ var Watermark = /** @class */ (function () {
|
|
|
122
126
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
123
127
|
(_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
124
128
|
(_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
125
|
-
(_d = this.
|
|
129
|
+
(_d = this.parentObserver) === null || _d === void 0 ? void 0 : _d.disconnect();
|
|
126
130
|
(_f = (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.parentNode) === null || _f === void 0 ? void 0 : _f.removeChild(this.watermarkDom);
|
|
127
131
|
(_h = (_g = this.options).onDestroyed) === null || _h === void 0 ? void 0 : _h.call(_g);
|
|
128
132
|
};
|
|
@@ -182,56 +186,55 @@ var Watermark = /** @class */ (function () {
|
|
|
182
186
|
}
|
|
183
187
|
return 'custom';
|
|
184
188
|
};
|
|
185
|
-
Watermark.prototype.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
return
|
|
189
|
-
|
|
190
|
-
this.observer = new MutationObserver(function (mutationsList) { return __awaiter(_this, void 0, void 0, function () {
|
|
191
|
-
return __generator(this, function (_a) {
|
|
192
|
-
switch (_a.label) {
|
|
189
|
+
Watermark.prototype.recreateOnMutation = function () {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
191
|
+
var _b, _c;
|
|
192
|
+
return __generator(this, function (_d) {
|
|
193
|
+
switch (_d.label) {
|
|
193
194
|
case 0:
|
|
194
|
-
|
|
195
|
+
_d.trys.push([0, 2, , 3]);
|
|
195
196
|
this.remove();
|
|
196
197
|
return [4 /*yield*/, this.create()];
|
|
197
198
|
case 1:
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
case 2:
|
|
199
|
+
_d.sent();
|
|
200
|
+
return [3 /*break*/, 3];
|
|
201
|
+
case 2:
|
|
202
|
+
_d.sent();
|
|
203
|
+
(_c = (_b = this.options).onObserveError) === null || _c === void 0 ? void 0 : _c.call(_b);
|
|
204
|
+
return [3 /*break*/, 3];
|
|
205
|
+
case 3: return [2 /*return*/];
|
|
201
206
|
}
|
|
202
207
|
});
|
|
203
|
-
});
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
Watermark.prototype.bindMutationObserver = function () {
|
|
211
|
+
var _this = this;
|
|
212
|
+
if (!this.watermarkDom) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.observer = new MutationObserver(function (mutationsList) {
|
|
216
|
+
if (mutationsList.length > 0) {
|
|
217
|
+
void _this.recreateOnMutation();
|
|
218
|
+
}
|
|
219
|
+
});
|
|
204
220
|
this.observer.observe(this.watermarkDom, {
|
|
205
221
|
attributes: true, // 属性的变动
|
|
222
|
+
attributeFilter: ['style', 'class', 'hidden', 'id'], // 仅监听会影响水印展示或标识的属性
|
|
206
223
|
childList: true, // 子节点的变动(指新增,删除或者更改)
|
|
207
224
|
subtree: true, // 布尔值,表示是否将该观察器应用于该节点的所有后代节点。
|
|
208
225
|
characterData: true, // 节点内容或节点文本的变动。
|
|
209
226
|
});
|
|
210
|
-
this.
|
|
211
|
-
var
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (!watermarkRemoved) return [3 /*break*/, 3];
|
|
222
|
-
this.remove();
|
|
223
|
-
return [4 /*yield*/, this.create()];
|
|
224
|
-
case 2:
|
|
225
|
-
_a.sent();
|
|
226
|
-
_a.label = 3;
|
|
227
|
-
case 3:
|
|
228
|
-
_i++;
|
|
229
|
-
return [3 /*break*/, 1];
|
|
230
|
-
case 4: return [2 /*return*/];
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
}); });
|
|
234
|
-
this.parentObserve.observe(this.parentElement, {
|
|
227
|
+
this.parentObserver = new MutationObserver(function (mutationsList) {
|
|
228
|
+
var watermarkDom = _this.watermarkDom;
|
|
229
|
+
if (!watermarkDom) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
var watermarkRemoved = mutationsList.some(function (item) { return Array.from(item.removedNodes).includes(watermarkDom); });
|
|
233
|
+
if (watermarkRemoved) {
|
|
234
|
+
void _this.recreateOnMutation();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
this.parentObserver.observe(this.parentElement, {
|
|
235
238
|
childList: true, // 子节点的变动(指新增,删除或者更改)
|
|
236
239
|
});
|
|
237
240
|
};
|
|
@@ -4,6 +4,10 @@ import '../_virtual/es.array.includes.js';
|
|
|
4
4
|
import '../_virtual/es.array.fill.js';
|
|
5
5
|
import '../node_modules/whatwg-fetch/fetch.js';
|
|
6
6
|
import './utils/polyfill.js';
|
|
7
|
+
import { registerBlindDecodeFallback } from './core/blind-decode.js';
|
|
8
|
+
import { softwareBlindDecodeFallback } from './compat/ie/blind-decode.js';
|
|
7
9
|
export { Watermark } from './core/watermark.js';
|
|
8
10
|
export { BlindWatermark } from './core/blind.js';
|
|
9
11
|
export { ImageWatermark } from './core/image.js';
|
|
12
|
+
|
|
13
|
+
registerBlindDecodeFallback(softwareBlindDecodeFallback);
|