@leafer-ui/mask 1.8.0 → 1.9.1

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.8.0",
3
+ "version": "1.9.1",
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.8.0",
26
- "@leafer-ui/draw": "1.8.0"
25
+ "@leafer/core": "1.9.1",
26
+ "@leafer-ui/draw": "1.9.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.8.0",
30
- "@leafer-ui/interface": "1.8.0"
29
+ "@leafer/interface": "1.9.1",
30
+ "@leafer-ui/interface": "1.9.1"
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,30 +50,38 @@ Group.prototype.__renderMask = function (canvas: ILeaferCanvas, options: IRender
49
50
 
50
51
  }
51
52
 
52
- if (mask === 'clipping' || mask === 'clipping-path') excludeRenderBounds(child, options) || child.__render(canvas, options) // 渲染自身到原画布中,不应用遮罩
53
+ if (mask === 'clipping' || mask === 'clipping-path') excludeRenderBounds(child, options) || child.__render(canvas, options) // 剪贴蒙版,需要渲染自身到原画布中,如果应用遮罩会造成透明度减半
53
54
 
54
55
  continue
55
56
  }
56
57
 
58
+ const childBlendMode = maskOpacity === 1 && child.__.__blendMode
59
+
60
+ // 元素存在混合模式,将先前的内容绘制到原画布
61
+ if (childBlendMode) maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, undefined, false)
62
+
57
63
  excludeRenderBounds(child, options) || child.__render(contentCanvas || canvas, options)
58
64
 
65
+ // 元素存在混合模式,应用遮罩后直接与原画布混合
66
+ if (childBlendMode) maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, childBlendMode as IBlendMode, false)
67
+
59
68
  }
60
69
 
61
- maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity)
70
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity, undefined, true)
62
71
 
63
72
  }
64
73
 
65
74
 
66
- 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 {
67
76
  switch (maskMode) {
68
77
  case 'grayscale':
69
- maskCanvas.useGrayscaleAlpha(leaf.__nowWorld)
78
+ if (!usedGrayscaleAlpha) usedGrayscaleAlpha = true, maskCanvas.useGrayscaleAlpha(leaf.__nowWorld)
70
79
  case 'alpha':
71
- usePixelMask(leaf, canvas, contentCanvas, maskCanvas); break
80
+ usePixelMask(leaf, canvas, contentCanvas, maskCanvas, blendMode, recycle); break
72
81
  case 'opacity-path':
73
- copyContent(leaf, canvas, contentCanvas, maskOpacity); break
82
+ copyContent(leaf, canvas, contentCanvas, maskOpacity, blendMode, recycle); break
74
83
  case 'path':
75
- canvas.restore()
84
+ if (recycle) canvas.restore()
76
85
  }
77
86
  }
78
87
 
@@ -82,23 +91,23 @@ function getCanvas(canvas: ILeaferCanvas): ILeaferCanvas {
82
91
  }
83
92
 
84
93
 
85
- 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 {
86
95
  const realBounds = leaf.__nowWorld
87
96
  content.resetTransform()
88
97
  content.opacity = 1
89
98
  content.useMask(mask, realBounds)
90
- mask.recycle(realBounds)
99
+ if (recycle) mask.recycle(realBounds)
91
100
 
92
- copyContent(leaf, canvas, content, 1)
101
+ copyContent(leaf, canvas, content, 1, blendMode, recycle)
93
102
  }
94
103
 
95
104
 
96
- 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 {
97
106
  const realBounds = leaf.__nowWorld
98
107
 
99
108
  canvas.resetTransform()
100
109
  canvas.opacity = maskOpacity
101
- canvas.copyWorld(content, realBounds)
110
+ canvas.copyWorld(content, realBounds, undefined, blendMode)
102
111
 
103
- content.recycle(realBounds)
112
+ recycle ? content.recycle(realBounds) : content.clearWorld(realBounds, true)
104
113
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
 
2
- export { }
2
+ export { };