leafer-x-watermark 2.0.0 → 3.0.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/README.md CHANGED
@@ -129,7 +129,7 @@ app.tree.add(watermark)
129
129
  - **WatermarkAsync**: 异步生成,适用于包含外部资源的图形。
130
130
  - **WatermarkURL**: 直接使用 `tileURL` 属性设置图片 URL。
131
131
 
132
- 继承自 Leafer UI 的 [Rect](https://www.leaferjs.com/ui/display/Rect.html) 组件,拥有所有 Rect 属性,并额外支持:
132
+ 继承自 Leafer UI 的 [Rect](https://www.leaferjs.com/ui/reference/display/Rect.html) 组件,拥有所有 Rect 属性,并额外支持:
133
133
 
134
134
  | 属性 | 类型 | 默认值 | 说明 |
135
135
  |------|------|--------|------|
package/dist/index.mjs CHANGED
@@ -336,13 +336,48 @@ WatermarkURL = __decorateClass([
336
336
  const { get, scale, copy } = MatrixHelper;
337
337
  const { getFloorScale } = MathHelper;
338
338
  const { abs } = Math;
339
- const originalCreatePattern = PaintImage.createPattern.bind(PaintImage);
339
+ const ORIGINAL_CREATE_PATTERN = Symbol.for("leafer-x-watermark:PaintImage.createPattern.original");
340
+ const STAGGER_PATTERN_ID = Symbol.for("leafer-x-watermark:paint.staggerPatternId");
341
+ function ensureOriginalCreatePattern() {
342
+ const anyPaintImage = PaintImage;
343
+ if (!anyPaintImage[ORIGINAL_CREATE_PATTERN]) {
344
+ anyPaintImage[ORIGINAL_CREATE_PATTERN] = PaintImage.createPattern.bind(PaintImage);
345
+ }
346
+ return anyPaintImage[ORIGINAL_CREATE_PATTERN];
347
+ }
348
+ const staggerCanvasCache = /* @__PURE__ */ new WeakMap();
340
349
  function normalizeStagger(stagger) {
341
350
  if (typeof stagger === "number") {
342
351
  return { type: "x", offset: stagger };
343
352
  }
344
353
  return { type: stagger.type || "x", offset: stagger.offset || 0 };
345
354
  }
355
+ function isSameStaggerParams(a, b) {
356
+ return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7];
357
+ }
358
+ function getStaggerCanvas(image, width, height, xGap, yGap, stagger, opacity, smooth) {
359
+ const cached = staggerCanvasCache.get(image);
360
+ if (cached) {
361
+ const nextParams = [
362
+ width,
363
+ height,
364
+ xGap,
365
+ yGap,
366
+ stagger.type,
367
+ stagger.offset,
368
+ opacity,
369
+ smooth
370
+ ];
371
+ if (isSameStaggerParams(cached.params, nextParams))
372
+ return cached.canvas;
373
+ }
374
+ const canvas = createStaggerCanvas(image, width, height, xGap, yGap, stagger, opacity, smooth);
375
+ staggerCanvasCache.set(image, {
376
+ params: [width, height, xGap, yGap, stagger.type, stagger.offset, opacity, smooth],
377
+ canvas
378
+ });
379
+ return canvas;
380
+ }
346
381
  function createStaggerCanvas(image, imgWidth, imgHeight, xGap, yGap, stagger, opacity, smooth) {
347
382
  const unitWidth = imgWidth + xGap;
348
383
  const unitHeight = imgHeight + yGap;
@@ -378,8 +413,8 @@ function createStaggerCanvas(image, imgWidth, imgHeight, xGap, yGap, stagger, op
378
413
  return canvas;
379
414
  }
380
415
  function createPatternWithStagger(paint, ui, canvas, renderOptions) {
381
- const originPaint = paint.originPaint;
382
- const rawStagger = originPaint?.stagger;
416
+ const originalCreatePattern = ensureOriginalCreatePattern();
417
+ const rawStagger = paint.data?.stagger ?? paint.originPaint?.stagger;
383
418
  if (rawStagger === void 0 || rawStagger === 0) {
384
419
  originalCreatePattern(paint, ui, canvas, renderOptions);
385
420
  return;
@@ -390,59 +425,62 @@ function createPatternWithStagger(paint, ui, canvas, renderOptions) {
390
425
  return;
391
426
  }
392
427
  let { scaleX, scaleY } = PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
393
- const id = `${scaleX}-${scaleY}-stagger-${stagger.type}-${stagger.offset}`;
394
- if (paint.patternId !== id && !ui.destroyed) {
395
- const { image, data } = paint;
396
- const { transform, gap } = data;
397
- const fixScale = PaintImage.getPatternFixScale(paint, scaleX, scaleY);
398
- let imageMatrix;
399
- let xGap = 0;
400
- let yGap = 0;
401
- let { width, height } = image;
402
- if (fixScale) {
403
- scaleX *= fixScale;
404
- scaleY *= fixScale;
405
- }
406
- width *= scaleX;
407
- height *= scaleY;
408
- if (gap) {
409
- xGap = gap.x * scaleX / abs(data.scaleX || 1);
410
- yGap = gap.y * scaleY / abs(data.scaleY || 1);
411
- }
412
- const unitWidth = width + xGap;
413
- const unitHeight = height + yGap;
414
- const isXStagger = stagger.type === "x";
415
- const patternWidth = isXStagger ? unitWidth : unitWidth * 2;
416
- const patternHeight = isXStagger ? unitHeight * 2 : unitHeight;
417
- if (transform || scaleX !== 1 || scaleY !== 1) {
418
- const matrixScaleX = scaleX * getFloorScale(patternWidth);
419
- const matrixScaleY = scaleY * getFloorScale(patternHeight);
420
- imageMatrix = get();
421
- if (transform)
422
- copy(imageMatrix, transform);
423
- scale(imageMatrix, 1 / matrixScaleX, 1 / matrixScaleY);
424
- }
425
- const imageCanvas = createStaggerCanvas(
426
- image,
427
- width,
428
- height,
429
- xGap,
430
- yGap,
431
- stagger,
432
- data.opacity,
433
- ui.leafer?.config.smooth
434
- );
435
- const pattern = image.getPattern(
436
- imageCanvas,
437
- data.repeat || "repeat",
438
- imageMatrix,
439
- paint
440
- );
441
- paint.style = pattern;
442
- paint.patternId = id;
428
+ const baseId = `${scaleX}-${scaleY}`;
429
+ const staggerId = `${baseId}-stagger-${stagger.type}-${stagger.offset}`;
430
+ const anyPaint = paint;
431
+ if (ui.destroyed)
432
+ return;
433
+ if (paint.patternId === baseId && anyPaint[STAGGER_PATTERN_ID] === staggerId)
434
+ return;
435
+ if (Platform.image.isLarge(paint.image, scaleX, scaleY) && !paint.data.repeat)
436
+ return;
437
+ const { image, data } = paint;
438
+ const { transform, gap } = data;
439
+ const fixScale = PaintImage.getPatternFixScale(paint, scaleX, scaleY);
440
+ if (fixScale) {
441
+ scaleX *= fixScale;
442
+ scaleY *= fixScale;
443
+ }
444
+ let imageMatrix;
445
+ let xGap = 0;
446
+ let yGap = 0;
447
+ let { width, height } = image;
448
+ width *= scaleX;
449
+ height *= scaleY;
450
+ if (gap) {
451
+ xGap = gap.x * scaleX / abs(data.scaleX || 1);
452
+ yGap = gap.y * scaleY / abs(data.scaleY || 1);
443
453
  }
454
+ if (transform || scaleX !== 1 || scaleY !== 1) {
455
+ scaleX *= getFloorScale(width + (xGap || 0));
456
+ scaleY *= getFloorScale(height + (yGap || 0));
457
+ imageMatrix = get();
458
+ if (transform)
459
+ copy(imageMatrix, transform);
460
+ scale(imageMatrix, 1 / scaleX, 1 / scaleY);
461
+ }
462
+ const imageCanvas = getStaggerCanvas(
463
+ image,
464
+ width,
465
+ height,
466
+ xGap,
467
+ yGap,
468
+ stagger,
469
+ data.opacity,
470
+ ui.leafer?.config.smooth
471
+ );
472
+ const pattern = image.getPattern(
473
+ imageCanvas,
474
+ data.repeat || (Platform.origin.noRepeat || "no-repeat"),
475
+ imageMatrix,
476
+ paint
477
+ );
478
+ paint.style = pattern;
479
+ paint.patternId = baseId;
480
+ anyPaint[STAGGER_PATTERN_ID] = staggerId;
444
481
  }
445
482
  function installStaggerPattern() {
483
+ ensureOriginalCreatePattern();
446
484
  PaintImage.createPattern = createPatternWithStagger;
447
485
  }
448
486
  function processStaggerData(paint) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "leafer-x-watermark",
3
3
  "type": "module",
4
- "version": "2.0.0",
4
+ "version": "3.0.0",
5
5
  "packageManager": "pnpm@10.14.0",
6
6
  "description": "Leafer 水印插件",
7
7
  "author": "XiaDeYu <1579883916@qq.com>",