@leafer/display 1.0.0-alpha.21 → 1.0.0-alpha.23

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.
Files changed (3) hide show
  1. package/package.json +9 -9
  2. package/src/Branch.ts +65 -29
  3. package/src/Leaf.ts +92 -43
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/display",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "@leafer/display",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,15 +19,15 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/math": "1.0.0-alpha.21",
23
- "@leafer/data": "1.0.0-alpha.21",
24
- "@leafer/layout": "1.0.0-alpha.21",
25
- "@leafer/display-module": "1.0.0-alpha.21",
26
- "@leafer/event": "1.0.0-alpha.21",
27
- "@leafer/decorator": "1.0.0-alpha.21",
28
- "@leafer/helper": "1.0.0-alpha.21"
22
+ "@leafer/math": "1.0.0-alpha.23",
23
+ "@leafer/data": "1.0.0-alpha.23",
24
+ "@leafer/layout": "1.0.0-alpha.23",
25
+ "@leafer/display-module": "1.0.0-alpha.23",
26
+ "@leafer/event": "1.0.0-alpha.23",
27
+ "@leafer/decorator": "1.0.0-alpha.23",
28
+ "@leafer/helper": "1.0.0-alpha.23"
29
29
  },
30
30
  "devDependencies": {
31
- "@leafer/interface": "1.0.0-alpha.21"
31
+ "@leafer/interface": "1.0.0-alpha.23"
32
32
  }
33
33
  }
package/src/Branch.ts CHANGED
@@ -8,7 +8,7 @@ import { Leaf } from './Leaf'
8
8
 
9
9
  const { setByListWithHandle } = BoundsHelper
10
10
  const { sort } = BranchHelper
11
- const { relativeBoxBounds: localBoxBounds, relativeEventBounds: localEventBounds, relativeRenderBounds: localRenderBounds } = LeafBoundsHelper
11
+ const { localBoxBounds, localEventBounds, localRenderBounds, maskLocalBoxBounds, maskLocalEventBounds, maskLocalRenderBounds } = LeafBoundsHelper
12
12
 
13
13
  export class Branch extends Leaf {
14
14
 
@@ -20,10 +20,10 @@ export class Branch extends Leaf {
20
20
 
21
21
  // overwrite
22
22
 
23
- public __updateEventBoundsSpreadWidth(): number {
23
+ public __updateStrokeBoundsSpreadWidth(): number {
24
24
  const { children } = this
25
25
  for (let i = 0, len = children.length; i < len; i++) {
26
- if (children[i].__layout.eventBoundsSpreadWidth) return 1
26
+ if (children[i].__layout.strokeBoundsSpreadWidth) return 1
27
27
  }
28
28
  return 0
29
29
  }
@@ -38,15 +38,15 @@ export class Branch extends Leaf {
38
38
 
39
39
 
40
40
  public __updateBoxBounds(): void {
41
- setByListWithHandle(this.__layout.boxBounds, this.children, localBoxBounds)
41
+ setByListWithHandle(this.__layout.boxBounds, this.children, this.__hasMask ? maskLocalBoxBounds : localBoxBounds)
42
42
  }
43
43
 
44
- public __updateEventBounds(): void {
45
- setByListWithHandle(this.__layout.eventBounds, this.children, localEventBounds)
44
+ public __updateStrokeBounds(): void {
45
+ setByListWithHandle(this.__layout.strokeBounds, this.children, this.__hasMask ? maskLocalEventBounds : localEventBounds)
46
46
  }
47
47
 
48
48
  public __updateRenderBounds(): void {
49
- setByListWithHandle(this.__layout.renderBounds, this.children, localRenderBounds)
49
+ setByListWithHandle(this.__layout.renderBounds, this.children, this.__hasMask ? maskLocalRenderBounds : localRenderBounds)
50
50
  }
51
51
 
52
52
  public __updateChange(): void {
@@ -74,12 +74,46 @@ export class Branch extends Leaf {
74
74
 
75
75
  if (this.__worldOpacity) {
76
76
  let child: ILeaf
77
- const { bounds, hideBounds } = options, { children } = this
78
- for (let i = 0, len = children.length; i < len; i++) {
79
- child = children[i]
80
- if (bounds && !bounds.hit(child.__world, options.matrix)) continue
81
- if (hideBounds && hideBounds.includes(child.__world)) continue
82
- child.__render(canvas, options)
77
+ const { children } = this
78
+ if (this.__hasMask) {
79
+
80
+ let oldCanvas: ILeaferCanvas = canvas, maskCanvas: ILeaferCanvas
81
+
82
+ for (let i = 0, len = children.length; i < len; i++) {
83
+ child = children[i]
84
+
85
+ if (child.isMask) {
86
+ // if (mask) { } // 多个遮罩的情况
87
+ canvas = canvas.getSameCanvas()
88
+ maskCanvas = canvas.getSameCanvas()
89
+ child.__render(maskCanvas, options)
90
+ //mask = child
91
+ continue
92
+ }
93
+
94
+ child.__render(canvas, options)
95
+ }
96
+
97
+ if (maskCanvas) {
98
+
99
+ maskCanvas.resetTransform()
100
+ maskCanvas.copyWorld(canvas, this.__world, null, 'source-in')
101
+
102
+ oldCanvas.resetTransform()
103
+ oldCanvas.copyWorld(maskCanvas, this.__world)
104
+
105
+ canvas.recycle()
106
+ maskCanvas.recycle()
107
+ }
108
+
109
+ } else {
110
+ const { bounds, hideBounds } = options
111
+ for (let i = 0, len = children.length; i < len; i++) {
112
+ child = children[i]
113
+ if (bounds && !bounds.hit(child.__world, options.matrix)) continue
114
+ if (hideBounds && hideBounds.includes(child.__world)) continue
115
+ child.__render(canvas, options)
116
+ }
83
117
  }
84
118
  }
85
119
 
@@ -87,22 +121,21 @@ export class Branch extends Leaf {
87
121
 
88
122
  public add(child: ILeaf, index?: number): void {
89
123
 
90
- if (child.parent) {
91
- if (child.parent !== this) console.warn('child had other parent, can not add to this, child innerId:' + child.innerId)
92
- return
93
- }
124
+ if (child.parent) child.parent.remove(child)
94
125
 
95
126
  child.parent = this
96
127
 
97
128
  index === undefined ? this.children.push(child) : this.children.splice(index, 0, child)
98
- if (child.__isBranch) this.__.__childBranchNumber ? this.__.__childBranchNumber++ : this.__.__childBranchNumber = 1
129
+ if (child.__isBranch) this.__.__childBranchNumber = (this.__.__childBranchNumber || 0) + 1
99
130
 
100
- if (this.root) {
101
- child.__bindRoot(this.root)
131
+ if (this.leafer) {
132
+ child.__bindLeafer(this.leafer)
102
133
 
103
- const event = new ChildEvent(ChildEvent.ADD, child, this)
104
- if (this.hasEvent(ChildEvent.ADD)) this.emitEvent(event)
105
- this.root.emitEvent(event)
134
+ if (this.leafer.ready) {
135
+ const event = new ChildEvent(ChildEvent.ADD, child, this)
136
+ if (this.hasEvent(ChildEvent.ADD)) this.emitEvent(event)
137
+ this.leafer.emitEvent(event)
138
+ }
106
139
  }
107
140
 
108
141
  if (child.__parentWait) child.__runParentWait()
@@ -115,13 +148,16 @@ export class Branch extends Leaf {
115
148
  if (index > -1) {
116
149
  this.children.splice(index, 1)
117
150
 
118
- if (child.__isBranch) this.__.__childBranchNumber > 1 ? this.__.__childBranchNumber-- : this.__.__childBranchNumber = 0
151
+ if (child.__isBranch) this.__.__childBranchNumber = (this.__.__childBranchNumber || 1) - 1
152
+
153
+ if (this.leafer) {
154
+ if (this.leafer.ready) {
155
+ const event = new ChildEvent(ChildEvent.REMOVE, child, this)
156
+ if (this.hasEvent(ChildEvent.REMOVE)) this.emitEvent(event)
157
+ this.leafer.emitEvent(event)
158
+ }
119
159
 
120
- if (this.root) {
121
- const event = new ChildEvent(ChildEvent.REMOVE, child, this)
122
- if (this.hasEvent(ChildEvent.REMOVE)) this.emitEvent(event)
123
- this.root.emitEvent(event)
124
- child.root = null
160
+ child.__bindLeafer(null)
125
161
  }
126
162
 
127
163
  child.parent = null
package/src/Leaf.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { ILeafer, ILeaf, ILeafInputData, ILeafData, ILeaferCanvas, IRenderOptions, IMatrixWithBoundsData, __Number, __Boolean, ILeafLayout, InnerId, IHitCanvas, IRadiusPointData, IEventListenerMap, IEventListener, IEventListenerOptions, IEventListenerId, IEvent, IObject, IFunction, __String } from '@leafer/interface'
2
- import { IncrementId } from '@leafer/math'
1
+ import { ILeafer, ILeaf, ILeafInputData, ILeafData, ILeaferCanvas, IRenderOptions, IMatrixWithBoundsData, __Number, __Boolean, __Value, ILeafLayout, InnerId, IHitCanvas, IRadiusPointData, IEventListenerMap, IEventListener, IEventListenerOptions, IEventListenerId, IEvent, IObject, IFunction, __String, IPointData, IMatrixDecompositionAttr, ILayoutBoundsType, ILayoutLocationType, IBoundsData, IMatrixData } from '@leafer/interface'
2
+ import { IncrementId, MatrixHelper, PointHelper } from '@leafer/math'
3
3
  import { LeafData } from '@leafer/data'
4
4
  import { LeafLayout } from '@leafer/layout'
5
5
  import { LeafDataProxy, LeafMatrix, LeafBounds, LeafHit, LeafEventer, LeafRender } from '@leafer/display-module'
@@ -16,31 +16,44 @@ const { LEAF, create } = IncrementId
16
16
  @useModule(LeafRender)
17
17
  export class Leaf implements ILeaf {
18
18
 
19
- public get tag(): string { return this.constructor.name }
19
+ public get tag(): string { return this.__tag }
20
+ public get __tag(): string { return 'Leaf' }
21
+
20
22
  public readonly innerId: InnerId // 内部唯一标识
23
+
21
24
  public get __DataProcessor() { return LeafData }
22
25
  public get __LayoutProcessor() { return LeafLayout }
23
26
 
24
27
  public leafer?: ILeafer
25
- public root?: ILeaf
26
28
  public parent?: ILeaf
27
29
 
28
- public __isRoot: boolean
30
+ public isLeafer: boolean
29
31
  public __isBranch: boolean
30
32
  public __isBranchLeaf: boolean
31
33
 
32
34
  public __: ILeafData
33
35
  public __layout: ILeafLayout
34
36
 
35
- public __relative: IMatrixWithBoundsData
37
+ public __local: IMatrixWithBoundsData
36
38
  public __world: IMatrixWithBoundsData
37
-
38
39
  public __worldOpacity: number
39
- public __renderTime: number // μs 1000微秒 = 1毫秒
40
+
41
+ // now transform
42
+ public get worldTransform(): IMatrixData { return this.__layout.getTransform('world') }
43
+ public get localTransform(): IMatrixData { return this.__layout.getTransform('local') }
44
+
45
+ // now bounds
46
+ public get worldBoxBounds(): IBoundsData { return this.getBounds('box') }
47
+ public get worldStrokeBounds(): IBoundsData { return this.getBounds('stroke') }
48
+ public get worldRenderBounds(): IBoundsData { return this.getBounds('render') }
49
+
50
+ // now opacity
51
+ public get worldOpacity(): number { this.__layout.update(); return this.__worldOpacity }
40
52
 
41
53
  public __level: number // 所在层级 0 -> 高
42
54
  public __tempNumber: number // 用于排序,记录最后一次在parent中的排序索引,可用于移除之后回退
43
55
 
56
+ public __hasMask?: boolean
44
57
  public __hitCanvas?: IHitCanvas
45
58
 
46
59
  // event
@@ -49,7 +62,6 @@ export class Leaf implements ILeaf {
49
62
 
50
63
  public __parentWait?: IFunction[]
51
64
 
52
-
53
65
  // branch
54
66
  public children?: ILeaf[]
55
67
 
@@ -57,11 +69,10 @@ export class Leaf implements ILeaf {
57
69
 
58
70
  this.innerId = create(LEAF)
59
71
 
60
- this.__relative = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
72
+ this.__local = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
61
73
  this.__world = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
62
74
 
63
75
  this.__worldOpacity = 1
64
- this.__renderTime = 2
65
76
 
66
77
  this.__ = new this.__DataProcessor(this)
67
78
  this.__layout = new this.__LayoutProcessor(this)
@@ -82,72 +93,69 @@ export class Leaf implements ILeaf {
82
93
  this.__parentWait = null
83
94
  }
84
95
 
85
- public __setAsLeafer(): void {
86
- this.leafer = this as unknown as ILeafer
87
- }
88
-
89
- public __setAsRoot(): void {
90
- this.__bindRoot(this)
91
- this.__isRoot = true
92
- }
93
-
94
- public __bindRoot(root: ILeaf): void {
95
- if (this.__isRoot) return
96
+ public __bindLeafer(leafer: ILeafer): void {
97
+ if (this.isLeafer) leafer = this as unknown as ILeafer
96
98
 
97
- this.root = root
98
- this.leafer = root.leafer
99
+ this.leafer = leafer
99
100
  this.__level = this.parent ? this.parent.__level + 1 : 1
100
101
 
101
102
  if (this.__isBranch) {
102
103
  const { children } = this
103
104
  for (let i = 0, len = children.length; i < len; i++) {
104
- children[i].__bindRoot(root)
105
+ children[i].__bindLeafer(leafer)
105
106
  }
106
107
  }
107
108
  }
108
109
 
110
+ public set(_data: IObject): void { }
111
+
112
+ public get(_attrNames?: string[]): IObject { return undefined }
113
+
109
114
 
110
115
  // LeafDataProxy rewrite
111
116
 
112
- public __set(_attrName: string, _newValue: unknown): void { }
117
+ public __setAttr(_attrName: string, _newValue: __Value): void { }
113
118
 
114
- public __get(_attrName: string): unknown { return undefined }
119
+ public __getAttr(_attrName: string): __Value { return undefined }
115
120
 
116
- public __updateAttr(_attrName: string): void { }
121
+ public __updateAttr(_attrName?: string, _value?: __Value): void { }
117
122
 
118
123
  // ---
119
124
 
125
+ public forceUpdate(): void {
126
+ this.__updateAttr('x')
127
+ }
128
+
120
129
 
121
130
  // LeafMatrix rewrite
122
131
 
123
132
  public __updateWorldMatrix(): void { }
124
133
 
125
- public __updateRelativeMatrix(): void { }
134
+ public __updateLocalMatrix(): void { }
126
135
 
127
136
  // ---
128
137
 
129
-
130
138
  // LeafBounds rewrite
131
139
 
132
140
  public __updateWorldBounds(): void { }
133
141
 
134
142
 
135
- public __updateRelativeBoxBounds(): void { }
143
+ public __updateLocalBoxBounds(): void { }
136
144
 
137
- public __updateRelativeEventBounds(): void { }
145
+ public __updateLocalStrokeBounds(): void { }
138
146
 
139
- public __updateRelativeRenderBounds(): void { }
147
+ public __updateLocalRenderBounds(): void { }
140
148
 
141
149
  // box
142
150
 
143
151
  public __updateBoxBounds(): void { }
144
152
 
145
- public __updateEventBounds(): void { }
153
+ public __updateStrokeBounds(): void { }
146
154
 
147
155
  public __updateRenderBounds(): void { }
148
156
 
149
157
 
150
- public __updateEventBoundsSpreadWidth(): number { return 0 }
158
+ public __updateStrokeBoundsSpreadWidth(): number { return 0 }
151
159
 
152
160
  public __updateRenderBoundsSpreadWidth(): number { return 0 }
153
161
 
@@ -157,12 +165,50 @@ export class Leaf implements ILeaf {
157
165
  // ---
158
166
 
159
167
 
168
+ // convert
169
+
170
+ public getWorld(attrName: IMatrixDecompositionAttr): number {
171
+ return this.__layout.getMatrixDecompositionData('world')[attrName]
172
+ }
173
+
174
+ public getBounds(type: ILayoutBoundsType, locationType: ILayoutLocationType = 'world'): IBoundsData {
175
+ return this.__layout.getBounds(type, locationType)
176
+ }
177
+
178
+
179
+ public worldToLocal(world: IPointData, to?: IPointData, isMovePoint?: boolean): void {
180
+ if (this.parent) {
181
+ MatrixHelper.toInnerPoint(this.parent.worldTransform, world, to, isMovePoint)
182
+ } else {
183
+ if (to) PointHelper.copy(to, world)
184
+ }
185
+ }
186
+
187
+ public localToWorld(local: IPointData, to?: IPointData, isMovePoint?: boolean): void {
188
+ if (this.parent) {
189
+ MatrixHelper.toOuterPoint(this.parent.worldTransform, local, to, isMovePoint)
190
+ } else {
191
+ if (to) PointHelper.copy(to, local)
192
+ }
193
+ }
194
+
195
+ public worldToInner(world: IPointData, to?: IPointData, isMovePoint?: boolean): void {
196
+ MatrixHelper.toInnerPoint(this.worldTransform, world, to, isMovePoint)
197
+ }
198
+
199
+ public innerToWorld(inner: IPointData, to?: IPointData, isMovePoint?: boolean): void {
200
+ MatrixHelper.toOuterPoint(this.worldTransform, inner, to, isMovePoint)
201
+ }
202
+
203
+
160
204
  // LeafHit rewrite
161
205
 
162
206
  public __hitWorld(_point: IRadiusPointData): boolean { return true }
163
207
 
164
208
  public __hit(_local: IRadiusPointData): boolean { return true }
165
209
 
210
+ public __drawHitPath(_canvas: ILeaferCanvas): void { }
211
+
166
212
  public __updateHitCanvas(): void { }
167
213
 
168
214
  // ---
@@ -176,6 +222,9 @@ export class Leaf implements ILeaf {
176
222
 
177
223
  public __draw(_canvas: ILeaferCanvas, _options: IRenderOptions): void { }
178
224
 
225
+ public __renderShape(_canvas: ILeaferCanvas, _options: IRenderOptions): void { }
226
+
227
+
179
228
  public __updateWorldOpacity(): void { }
180
229
 
181
230
  public __updateRenderTime(): void { }
@@ -202,7 +251,9 @@ export class Leaf implements ILeaf {
202
251
 
203
252
  public add(_child: ILeaf, _index?: number): void { }
204
253
 
205
- public remove(_child?: ILeaf): void { }
254
+ public remove(_child?: ILeaf): void {
255
+ if (this.parent) this.parent.remove(this)
256
+ }
206
257
 
207
258
  // ---
208
259
 
@@ -229,19 +280,12 @@ export class Leaf implements ILeaf {
229
280
 
230
281
  public destroy(): void {
231
282
  if (this.__) {
232
-
233
- if (this.__isBranch) {
234
- this.children.forEach(child => { child.destroy() })
235
- this.children = null
236
- }
237
-
238
283
  if (this.__hitCanvas) {
239
284
  this.__hitCanvas.destroy()
240
285
  this.__hitCanvas = null
241
286
  }
242
287
 
243
288
  this.leafer = null
244
- this.root = null
245
289
  this.parent = null
246
290
 
247
291
  this.__.destroy()
@@ -251,6 +295,11 @@ export class Leaf implements ILeaf {
251
295
 
252
296
  this.__captureMap = null
253
297
  this.__bubbleMap = null
298
+
299
+ if (this.__isBranch) {
300
+ this.children.forEach(child => { child.destroy() })
301
+ this.children.length = 0
302
+ }
254
303
  }
255
304
  }
256
305