@leafer-ui/effect 1.9.7 → 1.9.9

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/effect",
3
- "version": "1.9.7",
3
+ "version": "1.9.9",
4
4
  "description": "@leafer-ui/effect",
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.9.7",
26
- "@leafer-ui/draw": "1.9.7"
25
+ "@leafer/core": "1.9.9",
26
+ "@leafer-ui/draw": "1.9.9"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.9.7",
30
- "@leafer-ui/interface": "1.9.7"
29
+ "@leafer/interface": "1.9.9",
30
+ "@leafer-ui/interface": "1.9.9"
31
31
  }
32
32
  }
@@ -1,10 +1,10 @@
1
- import { IBoundsData, ILeaferCanvas, IOffsetBoundsData } from '@leafer/interface'
1
+ import { IBoundsData, IMatrixData, ILeaferCanvas, IOffsetBoundsData } from '@leafer/interface'
2
2
  import { BoundsHelper, LeafHelper } from '@leafer/core'
3
3
 
4
4
  import { IUI, ICachedShape } from '@leafer-ui/interface'
5
- import { ColorConvert } from '@leafer-ui/draw'
5
+ import { ColorConvert, Effect } from '@leafer-ui/draw'
6
6
 
7
- import { drawWorldShadow } from './Shadow'
7
+ import { drawWorldShadow, getShadowRenderSpread } from './Shadow'
8
8
 
9
9
 
10
10
  const { toOffsetOutBounds } = BoundsHelper
@@ -12,16 +12,16 @@ const offsetOutBounds = {} as IOffsetBoundsData
12
12
 
13
13
  export function innerShadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape): void {
14
14
 
15
- let copyBounds: IBoundsData, spreadScale: number
15
+ let copyBounds: IBoundsData, transform: IMatrixData
16
16
 
17
- const { __nowWorld: nowWorld, __layout } = ui
17
+ const { __nowWorld: nowWorld } = ui
18
18
  const { innerShadow } = ui.__
19
- const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape
19
+ const { worldCanvas, bounds, renderBounds, shapeBounds, scaleX, scaleY } = shape
20
20
 
21
21
  const other = current.getSameCanvas()
22
22
  const end = innerShadow.length - 1
23
23
 
24
- toOffsetOutBounds(bounds, offsetOutBounds)
24
+ toOffsetOutBounds(bounds, offsetOutBounds, renderBounds)
25
25
 
26
26
  innerShadow.forEach((item, index) => {
27
27
 
@@ -34,21 +34,22 @@ export function innerShadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape
34
34
 
35
35
  other.save()
36
36
 
37
- other.setWorldShadow((offsetOutBounds.offsetX + item.x * scaleX * otherScale), (offsetOutBounds.offsetY + item.y * scaleY * otherScale), item.blur * scaleX * otherScale)
37
+ other.setWorldShadow((offsetOutBounds.offsetX + (item.x || 0) * scaleX * otherScale), (offsetOutBounds.offsetY + (item.y || 0) * scaleY * otherScale), (item.blur || 0) * scaleX * otherScale)
38
38
 
39
- spreadScale = item.spread ? 1 - item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) * otherScale : 0
39
+ transform = Effect.getShadowTransform(ui, other, shape, item, offsetOutBounds, otherScale, true)
40
+ if (transform) other.setTransform(transform)
40
41
 
41
- drawWorldShadow(other, offsetOutBounds, spreadScale, shape)
42
+ drawWorldShadow(other, offsetOutBounds, shape)
42
43
 
43
44
  other.restore()
44
45
 
45
46
  if (worldCanvas) {
46
- other.copyWorld(other, bounds, nowWorld, 'copy')
47
+ other.copyWorld(other, renderBounds, nowWorld, 'copy')
47
48
  other.copyWorld(worldCanvas, nowWorld, nowWorld, 'source-out')
48
49
  copyBounds = nowWorld
49
50
  } else {
50
51
  other.copyWorld(shape.canvas, shapeBounds, bounds, 'source-out')
51
- copyBounds = bounds
52
+ copyBounds = renderBounds
52
53
  }
53
54
 
54
55
  other.fillWorld(copyBounds, ColorConvert.string(item.color), 'source-in')
@@ -63,4 +64,4 @@ export function innerShadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape
63
64
 
64
65
  }
65
66
 
66
-
67
+ export const getInnerShadowSpread = getShadowRenderSpread
package/src/Shadow.ts CHANGED
@@ -1,26 +1,26 @@
1
- import { IBoundsData, ILeaferCanvas, IOffsetBoundsData } from '@leafer/interface'
2
- import { BoundsHelper, LeafHelper, Platform } from '@leafer/core'
1
+ import { IBoundsData, IFourNumber, ILeaferCanvas, IMatrixData, IOffsetBoundsData } from '@leafer/interface'
2
+ import { BoundsHelper, LeafHelper, Matrix, Platform } from '@leafer/core'
3
3
 
4
4
  import { IUI, ICachedShape, ILeafShadowEffect } from '@leafer-ui/interface'
5
5
  import { ColorConvert, Effect } from '@leafer-ui/draw'
6
6
 
7
7
 
8
- const { copy, toOffsetOutBounds } = BoundsHelper
9
- const tempBounds = {} as IBoundsData
8
+ const { copy, move, toOffsetOutBounds } = BoundsHelper, { max } = Math
9
+ const tempBounds = {} as IBoundsData, tempMatrix = new Matrix()
10
10
  const offsetOutBounds = {} as IOffsetBoundsData
11
11
 
12
12
  export function shadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape): void {
13
13
 
14
- let copyBounds: IBoundsData, spreadScale: number
14
+ let copyBounds: IBoundsData, transform: IMatrixData
15
15
 
16
- const { __nowWorld: nowWorld, __layout } = ui
16
+ const { __nowWorld: nowWorld } = ui
17
17
  const { shadow } = ui.__
18
- const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape
18
+ const { worldCanvas, bounds, renderBounds, shapeBounds, scaleX, scaleY } = shape
19
19
 
20
20
  const other = current.getSameCanvas()
21
21
  const end = shadow.length - 1
22
22
 
23
- toOffsetOutBounds(bounds, offsetOutBounds)
23
+ toOffsetOutBounds(bounds, offsetOutBounds, renderBounds)
24
24
 
25
25
  shadow.forEach((item, index) => {
26
26
 
@@ -31,13 +31,16 @@ export function shadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape): vo
31
31
  if (sx > 1) otherScale = 1 / sx
32
32
  }
33
33
 
34
- other.setWorldShadow((offsetOutBounds.offsetX + item.x * scaleX * otherScale), (offsetOutBounds.offsetY + item.y * scaleY * otherScale), item.blur * scaleX * otherScale, ColorConvert.string(item.color))
34
+ other.setWorldShadow((offsetOutBounds.offsetX + (item.x || 0) * scaleX * otherScale), (offsetOutBounds.offsetY + (item.y || 0) * scaleY * otherScale), (item.blur || 0) * scaleX * otherScale, ColorConvert.string(item.color))
35
35
 
36
- spreadScale = item.spread ? 1 + item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) * otherScale : 0
36
+ transform = Effect.getShadowTransform(ui, other, shape, item, offsetOutBounds, otherScale)
37
+ if (transform) other.setTransform(transform)
37
38
 
38
- drawWorldShadow(other, offsetOutBounds, spreadScale, shape)
39
+ drawWorldShadow(other, offsetOutBounds, shape)
39
40
 
40
- copyBounds = bounds
41
+ if (transform) other.resetTransform()
42
+
43
+ copyBounds = renderBounds
41
44
 
42
45
  if (item.box) {
43
46
 
@@ -45,15 +48,14 @@ export function shadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape): vo
45
48
  other.save()
46
49
 
47
50
  if (worldCanvas) {
48
- other.copyWorld(other, bounds, nowWorld, 'copy')
51
+ other.copyWorld(other, renderBounds, nowWorld, 'copy')
49
52
  copyBounds = nowWorld
50
53
  }
51
54
 
52
55
  worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out')
53
56
  }
54
57
 
55
- if (Effect.isTransformShadow(item)) Effect.renderTransformShadow(ui, current, other, copyBounds, item)
56
- else LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode)
58
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode)
57
59
 
58
60
  if (end && index < end) other.clearWorld(copyBounds)
59
61
  })
@@ -62,45 +64,39 @@ export function shadow(ui: IUI, current: ILeaferCanvas, shape: ICachedShape): vo
62
64
 
63
65
  }
64
66
 
65
-
66
- export function getShadowSpread(_ui: IUI, shadow: ILeafShadowEffect[]): number {
67
- let width = 0
68
- shadow.forEach(item => width = Math.max(width, Math.max(Math.abs(item.y), Math.abs(item.x)) + (item.spread > 0 ? item.spread : 0) + item.blur * 1.5))
69
- return width
67
+ export function getShadowRenderSpread(_ui: IUI, shadow: ILeafShadowEffect[]): IFourNumber {
68
+ let top = 0, right = 0, bottom = 0, left = 0, x: number, y: number, spread: number, blur: number
69
+ shadow.forEach(item => {
70
+ x = item.x || 0, y = item.y || 0, spread = item.spread || 0, blur = (item.blur || 0) * 1.5
71
+ top = max(top, spread + blur - y)
72
+ right = max(right, spread + blur + x)
73
+ bottom = max(bottom, spread + blur + y)
74
+ left = max(left, spread + blur - x)
75
+ })
76
+ return (top === right && right === bottom && bottom === left) ? top : [top, right, bottom, left]
70
77
  }
71
78
 
79
+ export function getShadowTransform(ui: IUI, canvas: ILeaferCanvas, _shape: ICachedShape, shadow: ILeafShadowEffect, outBounds: IBoundsData, otherScale: number, isInnerShaodw?: boolean): IMatrixData {
80
+ if (shadow.spread) {
81
+ const spreadScale = 1 + shadow.spread * 2 / ui.__layout.strokeBounds.width * otherScale * (isInnerShaodw ? -1 : 1)
82
+ tempMatrix.set().scaleOfOuter({ x: (outBounds.x + outBounds.width / 2) * canvas.pixelRatio, y: (outBounds.y + outBounds.height / 2) * canvas.pixelRatio }, spreadScale)
83
+ return tempMatrix
84
+ }
85
+ return undefined
86
+ }
72
87
 
73
- export function drawWorldShadow(canvas: ILeaferCanvas, outBounds: IBoundsData, spreadScale: number, shape: ICachedShape): void {
88
+ export function drawWorldShadow(canvas: ILeaferCanvas, outBounds: IBoundsData, shape: ICachedShape): void {
74
89
 
75
- const { bounds, shapeBounds } = shape
90
+ const { shapeBounds } = shape
91
+ let from: IBoundsData, to: IBoundsData
76
92
 
77
93
  if (Platform.fullImageShadow) { // Safari
78
-
79
94
  copy(tempBounds, canvas.bounds)
80
- tempBounds.x += (outBounds.x - shapeBounds.x)
81
- tempBounds.y += (outBounds.y - shapeBounds.y)
82
-
83
- if (spreadScale) {
84
- const { fitMatrix } = shape
85
- tempBounds.x -= (bounds.x + (fitMatrix ? fitMatrix.e : 0) + bounds.width / 2) * (spreadScale - 1)
86
- tempBounds.y -= (bounds.y + (fitMatrix ? fitMatrix.f : 0) + bounds.height / 2) * (spreadScale - 1)
87
- tempBounds.width *= spreadScale
88
- tempBounds.height *= spreadScale
89
- }
90
-
91
- canvas.copyWorld(shape.canvas, canvas.bounds, tempBounds)
92
-
95
+ move(tempBounds, outBounds.x - shapeBounds.x, outBounds.y - shapeBounds.y)
96
+ from = canvas.bounds, to = tempBounds
93
97
  } else {
94
-
95
- if (spreadScale) {
96
- copy(tempBounds, outBounds)
97
- tempBounds.x -= (outBounds.width / 2) * (spreadScale - 1)
98
- tempBounds.y -= (outBounds.height / 2) * (spreadScale - 1)
99
- tempBounds.width *= spreadScale
100
- tempBounds.height *= spreadScale
101
- }
102
-
103
- canvas.copyWorld(shape.canvas, shapeBounds, spreadScale ? tempBounds : outBounds)
98
+ from = shapeBounds, to = outBounds
104
99
  }
105
100
 
101
+ canvas.copyWorld(shape.canvas, from, to)
106
102
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { IEffectModule, ILeafShadowEffect } from '@leafer-ui/interface'
2
2
 
3
- import { shadow, getShadowSpread } from './Shadow'
4
- import { innerShadow } from './InnerShadow'
3
+ import { shadow, getShadowRenderSpread, getShadowTransform } from './Shadow'
4
+ import { innerShadow, getInnerShadowSpread } from './InnerShadow'
5
5
  import { blur } from './Blur'
6
6
  import { backgroundBlur } from './BackgroundBlur'
7
7
 
@@ -13,6 +13,9 @@ export const EffectModule: IEffectModule = {
13
13
  backgroundBlur,
14
14
 
15
15
  // @leafer-in/shadow will rewrite
16
- getShadowSpread,
17
- isTransformShadow(_shadow: ILeafShadowEffect): boolean { return undefined }
16
+ getShadowRenderSpread,
17
+ getShadowTransform,
18
+ isTransformShadow(_shadow: ILeafShadowEffect): boolean { return undefined },
19
+
20
+ getInnerShadowSpread
18
21
  }