leafer-x-watermark 1.1.1 → 1.2.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.
- package/dist/index.mjs +120 -121
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,123 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const { get, scale, copy } = MatrixHelper;
|
|
4
|
-
const { getFloorScale } = MathHelper;
|
|
5
|
-
const { abs } = Math;
|
|
6
|
-
const originalCreatePattern = PaintImage.createPattern.bind(PaintImage);
|
|
7
|
-
function normalizeStagger(stagger) {
|
|
8
|
-
if (typeof stagger === "number") {
|
|
9
|
-
return { type: "x", offset: stagger };
|
|
10
|
-
}
|
|
11
|
-
return { type: stagger.type || "x", offset: stagger.offset || 0 };
|
|
12
|
-
}
|
|
13
|
-
function createStaggerCanvas(image, imgWidth, imgHeight, xGap, yGap, stagger, opacity, smooth) {
|
|
14
|
-
const unitWidth = imgWidth + xGap;
|
|
15
|
-
const unitHeight = imgHeight + yGap;
|
|
16
|
-
const isXStagger = stagger.type === "x";
|
|
17
|
-
const patternWidth = isXStagger ? unitWidth : unitWidth * 2;
|
|
18
|
-
const patternHeight = isXStagger ? unitHeight * 2 : unitHeight;
|
|
19
|
-
const canvas = Platform.origin.createCanvas(
|
|
20
|
-
Math.max(Math.floor(patternWidth), 1),
|
|
21
|
-
Math.max(Math.floor(patternHeight), 1)
|
|
22
|
-
);
|
|
23
|
-
const ctx = canvas.getContext("2d");
|
|
24
|
-
if (opacity && opacity < 1)
|
|
25
|
-
ctx.globalAlpha = opacity;
|
|
26
|
-
ctx.imageSmoothingEnabled = smooth !== false;
|
|
27
|
-
const imgView = image.view;
|
|
28
|
-
const drawImg = (x, y) => {
|
|
29
|
-
ctx.drawImage(imgView, 0, 0, image.width, image.height, x, y, imgWidth, imgHeight);
|
|
30
|
-
};
|
|
31
|
-
const offset = stagger.offset / 100 * (isXStagger ? unitWidth : unitHeight);
|
|
32
|
-
if (isXStagger) {
|
|
33
|
-
drawImg(0, 0);
|
|
34
|
-
drawImg(offset, unitHeight);
|
|
35
|
-
if (offset + imgWidth > unitWidth) {
|
|
36
|
-
drawImg(offset - unitWidth, unitHeight);
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
drawImg(0, 0);
|
|
40
|
-
drawImg(unitWidth, offset);
|
|
41
|
-
if (offset + imgHeight > unitHeight) {
|
|
42
|
-
drawImg(unitWidth, offset - unitHeight);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return canvas;
|
|
46
|
-
}
|
|
47
|
-
function createPatternWithStagger(paint, ui, canvas, renderOptions) {
|
|
48
|
-
const originPaint = paint.originPaint;
|
|
49
|
-
const rawStagger = originPaint?.stagger;
|
|
50
|
-
if (rawStagger === void 0 || rawStagger === 0) {
|
|
51
|
-
originalCreatePattern(paint, ui, canvas, renderOptions);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const stagger = normalizeStagger(rawStagger);
|
|
55
|
-
if (stagger.offset === 0) {
|
|
56
|
-
originalCreatePattern(paint, ui, canvas, renderOptions);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
let { scaleX, scaleY } = PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
|
|
60
|
-
const id = `${scaleX}-${scaleY}-stagger-${stagger.type}-${stagger.offset}`;
|
|
61
|
-
if (paint.patternId !== id && !ui.destroyed) {
|
|
62
|
-
const { image, data } = paint;
|
|
63
|
-
const { transform, gap } = data;
|
|
64
|
-
const fixScale = PaintImage.getPatternFixScale(paint, scaleX, scaleY);
|
|
65
|
-
let imageMatrix;
|
|
66
|
-
let xGap = 0;
|
|
67
|
-
let yGap = 0;
|
|
68
|
-
let { width, height } = image;
|
|
69
|
-
if (fixScale) {
|
|
70
|
-
scaleX *= fixScale;
|
|
71
|
-
scaleY *= fixScale;
|
|
72
|
-
}
|
|
73
|
-
width *= scaleX;
|
|
74
|
-
height *= scaleY;
|
|
75
|
-
if (gap) {
|
|
76
|
-
xGap = gap.x * scaleX / abs(data.scaleX || 1);
|
|
77
|
-
yGap = gap.y * scaleY / abs(data.scaleY || 1);
|
|
78
|
-
}
|
|
79
|
-
const unitWidth = width + xGap;
|
|
80
|
-
const unitHeight = height + yGap;
|
|
81
|
-
const isXStagger = stagger.type === "x";
|
|
82
|
-
const patternWidth = isXStagger ? unitWidth : unitWidth * 2;
|
|
83
|
-
const patternHeight = isXStagger ? unitHeight * 2 : unitHeight;
|
|
84
|
-
if (transform || scaleX !== 1 || scaleY !== 1) {
|
|
85
|
-
const matrixScaleX = scaleX * getFloorScale(patternWidth);
|
|
86
|
-
const matrixScaleY = scaleY * getFloorScale(patternHeight);
|
|
87
|
-
imageMatrix = get();
|
|
88
|
-
if (transform)
|
|
89
|
-
copy(imageMatrix, transform);
|
|
90
|
-
scale(imageMatrix, 1 / matrixScaleX, 1 / matrixScaleY);
|
|
91
|
-
}
|
|
92
|
-
const imageCanvas = createStaggerCanvas(
|
|
93
|
-
image,
|
|
94
|
-
width,
|
|
95
|
-
height,
|
|
96
|
-
xGap,
|
|
97
|
-
yGap,
|
|
98
|
-
stagger,
|
|
99
|
-
data.opacity,
|
|
100
|
-
ui.leafer?.config.smooth
|
|
101
|
-
);
|
|
102
|
-
const pattern = image.getPattern(
|
|
103
|
-
imageCanvas,
|
|
104
|
-
data.repeat || "repeat",
|
|
105
|
-
imageMatrix,
|
|
106
|
-
paint
|
|
107
|
-
);
|
|
108
|
-
paint.style = pattern;
|
|
109
|
-
paint.patternId = id;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function installStaggerPattern() {
|
|
113
|
-
PaintImage.createPattern = createPatternWithStagger;
|
|
114
|
-
}
|
|
115
|
-
function processStaggerData(paint) {
|
|
116
|
-
if (paint.stagger !== void 0) {
|
|
117
|
-
return normalizeStagger(paint.stagger);
|
|
118
|
-
}
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
1
|
+
import { Debug, dataProcessor, boundsType, registerUI, Plugin, RectData, isObject, UICreator, Rect, MatrixHelper, MathHelper, PaintImage, Platform } from '@leafer-ui/core';
|
|
121
2
|
|
|
122
3
|
var __defProp = Object.defineProperty;
|
|
123
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -129,7 +10,6 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
129
10
|
if (kind && result) __defProp(target, key, result);
|
|
130
11
|
return result;
|
|
131
12
|
};
|
|
132
|
-
installStaggerPattern();
|
|
133
13
|
const debug = Debug.get("leafer-x-watermark");
|
|
134
14
|
class ProcessorData extends RectData {
|
|
135
15
|
_tileContent = "";
|
|
@@ -296,4 +176,123 @@ Watermark = __decorateClass([
|
|
|
296
176
|
], Watermark);
|
|
297
177
|
Plugin.add("leafer-x-watermark");
|
|
298
178
|
|
|
179
|
+
const { get, scale, copy } = MatrixHelper;
|
|
180
|
+
const { getFloorScale } = MathHelper;
|
|
181
|
+
const { abs } = Math;
|
|
182
|
+
const originalCreatePattern = PaintImage.createPattern.bind(PaintImage);
|
|
183
|
+
function normalizeStagger(stagger) {
|
|
184
|
+
if (typeof stagger === "number") {
|
|
185
|
+
return { type: "x", offset: stagger };
|
|
186
|
+
}
|
|
187
|
+
return { type: stagger.type || "x", offset: stagger.offset || 0 };
|
|
188
|
+
}
|
|
189
|
+
function createStaggerCanvas(image, imgWidth, imgHeight, xGap, yGap, stagger, opacity, smooth) {
|
|
190
|
+
const unitWidth = imgWidth + xGap;
|
|
191
|
+
const unitHeight = imgHeight + yGap;
|
|
192
|
+
const isXStagger = stagger.type === "x";
|
|
193
|
+
const patternWidth = isXStagger ? unitWidth : unitWidth * 2;
|
|
194
|
+
const patternHeight = isXStagger ? unitHeight * 2 : unitHeight;
|
|
195
|
+
const canvas = Platform.origin.createCanvas(
|
|
196
|
+
Math.max(Math.floor(patternWidth), 1),
|
|
197
|
+
Math.max(Math.floor(patternHeight), 1)
|
|
198
|
+
);
|
|
199
|
+
const ctx = canvas.getContext("2d");
|
|
200
|
+
if (opacity && opacity < 1)
|
|
201
|
+
ctx.globalAlpha = opacity;
|
|
202
|
+
ctx.imageSmoothingEnabled = smooth !== false;
|
|
203
|
+
const imgView = image.view;
|
|
204
|
+
const drawImg = (x, y) => {
|
|
205
|
+
ctx.drawImage(imgView, 0, 0, image.width, image.height, x, y, imgWidth, imgHeight);
|
|
206
|
+
};
|
|
207
|
+
const offset = stagger.offset / 100 * (isXStagger ? unitWidth : unitHeight);
|
|
208
|
+
if (isXStagger) {
|
|
209
|
+
drawImg(0, 0);
|
|
210
|
+
drawImg(offset, unitHeight);
|
|
211
|
+
if (offset + imgWidth > unitWidth) {
|
|
212
|
+
drawImg(offset - unitWidth, unitHeight);
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
drawImg(0, 0);
|
|
216
|
+
drawImg(unitWidth, offset);
|
|
217
|
+
if (offset + imgHeight > unitHeight) {
|
|
218
|
+
drawImg(unitWidth, offset - unitHeight);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return canvas;
|
|
222
|
+
}
|
|
223
|
+
function createPatternWithStagger(paint, ui, canvas, renderOptions) {
|
|
224
|
+
const originPaint = paint.originPaint;
|
|
225
|
+
const rawStagger = originPaint?.stagger;
|
|
226
|
+
if (rawStagger === void 0 || rawStagger === 0) {
|
|
227
|
+
originalCreatePattern(paint, ui, canvas, renderOptions);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const stagger = normalizeStagger(rawStagger);
|
|
231
|
+
if (stagger.offset === 0) {
|
|
232
|
+
originalCreatePattern(paint, ui, canvas, renderOptions);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
let { scaleX, scaleY } = PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
|
|
236
|
+
const id = `${scaleX}-${scaleY}-stagger-${stagger.type}-${stagger.offset}`;
|
|
237
|
+
if (paint.patternId !== id && !ui.destroyed) {
|
|
238
|
+
const { image, data } = paint;
|
|
239
|
+
const { transform, gap } = data;
|
|
240
|
+
const fixScale = PaintImage.getPatternFixScale(paint, scaleX, scaleY);
|
|
241
|
+
let imageMatrix;
|
|
242
|
+
let xGap = 0;
|
|
243
|
+
let yGap = 0;
|
|
244
|
+
let { width, height } = image;
|
|
245
|
+
if (fixScale) {
|
|
246
|
+
scaleX *= fixScale;
|
|
247
|
+
scaleY *= fixScale;
|
|
248
|
+
}
|
|
249
|
+
width *= scaleX;
|
|
250
|
+
height *= scaleY;
|
|
251
|
+
if (gap) {
|
|
252
|
+
xGap = gap.x * scaleX / abs(data.scaleX || 1);
|
|
253
|
+
yGap = gap.y * scaleY / abs(data.scaleY || 1);
|
|
254
|
+
}
|
|
255
|
+
const unitWidth = width + xGap;
|
|
256
|
+
const unitHeight = height + yGap;
|
|
257
|
+
const isXStagger = stagger.type === "x";
|
|
258
|
+
const patternWidth = isXStagger ? unitWidth : unitWidth * 2;
|
|
259
|
+
const patternHeight = isXStagger ? unitHeight * 2 : unitHeight;
|
|
260
|
+
if (transform || scaleX !== 1 || scaleY !== 1) {
|
|
261
|
+
const matrixScaleX = scaleX * getFloorScale(patternWidth);
|
|
262
|
+
const matrixScaleY = scaleY * getFloorScale(patternHeight);
|
|
263
|
+
imageMatrix = get();
|
|
264
|
+
if (transform)
|
|
265
|
+
copy(imageMatrix, transform);
|
|
266
|
+
scale(imageMatrix, 1 / matrixScaleX, 1 / matrixScaleY);
|
|
267
|
+
}
|
|
268
|
+
const imageCanvas = createStaggerCanvas(
|
|
269
|
+
image,
|
|
270
|
+
width,
|
|
271
|
+
height,
|
|
272
|
+
xGap,
|
|
273
|
+
yGap,
|
|
274
|
+
stagger,
|
|
275
|
+
data.opacity,
|
|
276
|
+
ui.leafer?.config.smooth
|
|
277
|
+
);
|
|
278
|
+
const pattern = image.getPattern(
|
|
279
|
+
imageCanvas,
|
|
280
|
+
data.repeat || "repeat",
|
|
281
|
+
imageMatrix,
|
|
282
|
+
paint
|
|
283
|
+
);
|
|
284
|
+
paint.style = pattern;
|
|
285
|
+
paint.patternId = id;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function installStaggerPattern() {
|
|
289
|
+
PaintImage.createPattern = createPatternWithStagger;
|
|
290
|
+
}
|
|
291
|
+
function processStaggerData(paint) {
|
|
292
|
+
if (paint.stagger !== void 0) {
|
|
293
|
+
return normalizeStagger(paint.stagger);
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
299
298
|
export { ProcessorData, Watermark, installStaggerPattern, normalizeStagger, processStaggerData };
|