@leafer-ui/mask 1.7.0 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/mask",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "@leafer-ui/mask",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,11 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.7.0",
26
- "@leafer-ui/draw": "1.7.0"
25
+ "@leafer/core": "1.9.0",
26
+ "@leafer-ui/draw": "1.9.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.7.0",
30
- "@leafer-ui/interface": "1.7.0"
29
+ "@leafer/interface": "1.9.0",
30
+ "@leafer-ui/interface": "1.9.0"
31
31
  }
32
32
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ILeaf, ILeaferCanvas, IMaskType, IRenderOptions } from '@leafer/interface'
1
+ import { IBlendMode, ILeaf, ILeaferCanvas, IMaskType, IRenderOptions } from '@leafer/interface'
2
2
  import { LeafBoundsHelper } from '@leafer/core'
3
3
 
4
4
  import { Group } from '@leafer-ui/draw'
@@ -6,6 +6,7 @@ import { Group } from '@leafer-ui/draw'
6
6
 
7
7
  type IMaskMode = 'path' | 'alpha' | 'grayscale' | 'opacity-path'
8
8
  const { excludeRenderBounds } = LeafBoundsHelper
9
+ let usedGrayscaleAlpha: boolean
9
10
 
10
11
  Group.prototype.__renderMask = function (canvas: ILeaferCanvas, options: IRenderOptions): void {
11
12
 
@@ -18,21 +19,21 @@ Group.prototype.__renderMask = function (canvas: ILeaferCanvas, options: IRender
18
19
  if (mask) {
19
20
 
20
21
  if (currentMask) {
21
- maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity)
22
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, undefined, true)
22
23
  maskCanvas = contentCanvas = null
23
24
  }
24
25
 
25
26
  // mask start
27
+ maskOpacity = child.__.opacity
28
+ usedGrayscaleAlpha = false
26
29
 
27
30
  if (mask === 'path' || mask === 'clipping-path') {
28
31
 
29
- if (child.opacity < 1) {
32
+ if (maskOpacity < 1) {
30
33
 
31
34
  currentMask = 'opacity-path'
32
- maskOpacity = child.opacity
33
35
  if (!contentCanvas) contentCanvas = getCanvas(canvas)
34
36
 
35
-
36
37
  } else {
37
38
  currentMask = 'path'
38
39
  canvas.save()
@@ -49,28 +50,38 @@ Group.prototype.__renderMask = function (canvas: ILeaferCanvas, options: IRender
49
50
 
50
51
  }
51
52
 
52
- if (!(mask === 'clipping' || mask === 'clipping-path')) continue
53
+ if (mask === 'clipping' || mask === 'clipping-path') excludeRenderBounds(child, options) || child.__render(canvas, options) // 剪贴蒙版,需要渲染自身到原画布中,如果应用遮罩会造成透明度减半
54
+
55
+ continue
53
56
  }
54
57
 
55
- if (excludeRenderBounds(child, options)) continue
56
- child.__render(contentCanvas || canvas, options)
58
+ const childBlendMode = maskOpacity === 1 && child.__.__blendMode
59
+
60
+ // 元素存在混合模式,将先前的内容绘制到原画布
61
+ if (childBlendMode) maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, undefined, false)
62
+
63
+ excludeRenderBounds(child, options) || child.__render(contentCanvas || canvas, options)
64
+
65
+ // 元素存在混合模式,应用遮罩后直接与原画布混合
66
+ if (childBlendMode) maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, childBlendMode as IBlendMode, false)
67
+
57
68
  }
58
69
 
59
- maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity)
70
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, undefined, true)
60
71
 
61
72
  }
62
73
 
63
74
 
64
- function maskEnd(leaf: ILeaf, maskMode: IMaskMode, canvas: ILeaferCanvas, contentCanvas: ILeaferCanvas, maskCanvas: ILeaferCanvas, maskOpacity: number): void {
75
+ function maskEnd(leaf: ILeaf, maskMode: IMaskMode, canvas: ILeaferCanvas, contentCanvas: ILeaferCanvas, maskCanvas: ILeaferCanvas, maskOpacity: number, blendMode: IBlendMode, recycle: boolean): void {
65
76
  switch (maskMode) {
66
77
  case 'grayscale':
67
- maskCanvas.useGrayscaleAlpha(leaf.__nowWorld)
78
+ if (!usedGrayscaleAlpha) usedGrayscaleAlpha = true, maskCanvas.useGrayscaleAlpha(leaf.__nowWorld)
68
79
  case 'alpha':
69
- usePixelMask(leaf, canvas, contentCanvas, maskCanvas); break
80
+ usePixelMask(leaf, canvas, contentCanvas, maskCanvas, blendMode, recycle); break
70
81
  case 'opacity-path':
71
- copyContent(leaf, canvas, contentCanvas, maskOpacity); break
82
+ copyContent(leaf, canvas, contentCanvas, maskOpacity, blendMode, recycle); break
72
83
  case 'path':
73
- canvas.restore()
84
+ if (recycle) canvas.restore()
74
85
  }
75
86
  }
76
87
 
@@ -80,23 +91,23 @@ function getCanvas(canvas: ILeaferCanvas): ILeaferCanvas {
80
91
  }
81
92
 
82
93
 
83
- function usePixelMask(leaf: ILeaf, canvas: ILeaferCanvas, content: ILeaferCanvas, mask: ILeaferCanvas): void {
94
+ function usePixelMask(leaf: ILeaf, canvas: ILeaferCanvas, content: ILeaferCanvas, mask: ILeaferCanvas, blendMode: IBlendMode, recycle: boolean): void {
84
95
  const realBounds = leaf.__nowWorld
85
96
  content.resetTransform()
86
97
  content.opacity = 1
87
98
  content.useMask(mask, realBounds)
88
- mask.recycle(realBounds)
99
+ if (recycle) mask.recycle(realBounds)
89
100
 
90
- copyContent(leaf, canvas, content, 1)
101
+ copyContent(leaf, canvas, content, 1, blendMode, recycle)
91
102
  }
92
103
 
93
104
 
94
- function copyContent(leaf: ILeaf, canvas: ILeaferCanvas, content: ILeaferCanvas, maskOpacity: number): void {
105
+ function copyContent(leaf: ILeaf, canvas: ILeaferCanvas, content: ILeaferCanvas, maskOpacity: number, blendMode: IBlendMode, recycle: boolean): void {
95
106
  const realBounds = leaf.__nowWorld
96
107
 
97
108
  canvas.resetTransform()
98
109
  canvas.opacity = maskOpacity
99
- canvas.copyWorld(content, realBounds)
110
+ canvas.copyWorld(content, realBounds, undefined, blendMode)
100
111
 
101
- content.recycle(realBounds)
112
+ recycle ? content.recycle(realBounds) : content.clearWorld(realBounds, true)
102
113
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
 
2
- export { }
2
+ export { };