@leafer/renderer 1.0.0-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/renderer
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@leafer/renderer",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/renderer",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/renderer",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/event": "1.0.0-alpha.1",
21
+ "@leafer/math": "1.0.0-alpha.1",
22
+ "@leafer/data": "1.0.0-alpha.1",
23
+ "@leafer/platform": "1.0.0-alpha.1",
24
+ "@leafer/debug": "1.0.0-alpha.1"
25
+ },
26
+ "devDependencies": {
27
+ "@leafer/interface": "1.0.0-alpha.1"
28
+ }
29
+ }
@@ -0,0 +1,213 @@
1
+ import { ILeaf, ILeaferCanvas, IRenderer, IRendererConfig, IEventListenerId, IBounds, IFunction, ILayoutBlockData } from '@leafer/interface'
2
+ import { LayoutEvent, RenderEvent, ResizeEvent } from '@leafer/event'
3
+ import { Bounds } from '@leafer/math'
4
+ import { DataHelper } from '@leafer/data'
5
+ import { Platform } from '@leafer/platform'
6
+ import { Debug, Run } from '@leafer/debug'
7
+
8
+
9
+ export class Renderer implements IRenderer {
10
+
11
+ public target: ILeaf
12
+ public canvas: ILeaferCanvas
13
+ public layoutedBlocks: ILayoutBlockData[]
14
+
15
+ public totalTimes = 0
16
+ public times: number = 0
17
+
18
+ public FPS = 60
19
+
20
+ public config: IRendererConfig = {
21
+ maxFPS: 60
22
+ }
23
+
24
+ public running: boolean
25
+
26
+ private _changed = false
27
+ set changed(value: boolean) {
28
+ if (value) {
29
+ if (!this._changed) {
30
+ this._changed = true
31
+ this.requestRender()
32
+ }
33
+ } else {
34
+ this._changed = false
35
+ }
36
+ }
37
+ get changed(): boolean { return this._changed }
38
+
39
+ protected eventIds: IEventListenerId[]
40
+
41
+ constructor(target: ILeaf, canvas: ILeaferCanvas, userConfig: IRendererConfig) {
42
+ this.target = target
43
+ this.canvas = canvas
44
+ if (userConfig) this.config = DataHelper.default(userConfig, this.config)
45
+ this.listenEvents()
46
+ }
47
+
48
+ public start(): void {
49
+ this.running = true
50
+ this.changed = true
51
+ }
52
+
53
+ public stop(): void {
54
+ this.running = false
55
+ }
56
+
57
+ private listenEvents(): void {
58
+ const { target } = this
59
+ this.eventIds = [
60
+ target.on__(RenderEvent.REQUEST, this.onRequest, this),
61
+ target.on__(LayoutEvent.END, this.onLayoutEnd, this),
62
+ target.on__(RenderEvent.AGAIN, this.renderOnce, this),
63
+ target.on__(ResizeEvent.RESIZE, this.onResize, this)
64
+ ]
65
+ }
66
+
67
+ private removeListenEvents(): void {
68
+ this.target.off__(this.eventIds)
69
+ }
70
+
71
+ protected onResize(e: ResizeEvent): void {
72
+ if (e.bigger || !e.samePixelRatio) {
73
+ const { width, height } = e.old
74
+ const bounds = new Bounds(0, 0, width, height)
75
+ if (!bounds.includes(this.target.__world)) {
76
+ this.target.__updateAttr('fill')
77
+ this.changed = true
78
+ }
79
+ }
80
+ }
81
+
82
+ protected onRequest(): void {
83
+ this.changed = true
84
+ }
85
+
86
+ public requestLayout(): void {
87
+ this.target.emit(LayoutEvent.REQUEST)
88
+ }
89
+
90
+ public onLayoutEnd(event: LayoutEvent): void {
91
+ this.layoutedBlocks = event.data
92
+ }
93
+
94
+ public render(callback?: IFunction): void {
95
+ const { target } = this
96
+ const { START, RENDER, END } = RenderEvent
97
+ this.times = 0
98
+ target.emit(START)
99
+ this.renderOnce(callback)
100
+ target.emit(RENDER)
101
+ target.emit(END)
102
+ }
103
+
104
+ public renderOnce(callback?: IFunction): void {
105
+ const { target } = this
106
+ const { BEFORE_ONCE, ONCE, AFTER_ONCE } = RenderEvent
107
+
108
+ this.times++
109
+ this.totalTimes++
110
+ this.changed = false
111
+
112
+ if (callback) {
113
+
114
+ target.emit(BEFORE_ONCE)
115
+
116
+ callback()
117
+
118
+ } else {
119
+
120
+ this.requestLayout()
121
+
122
+ target.emit(BEFORE_ONCE)
123
+
124
+ if (this.totalTimes > 1) {
125
+ if (this.layoutedBlocks) this.partRender()
126
+ } else {
127
+ this.fullRender()
128
+ }
129
+
130
+ }
131
+
132
+ target.emit(ONCE)
133
+ target.emit(AFTER_ONCE)
134
+
135
+
136
+ if (this.layoutedBlocks) {
137
+ this.layoutedBlocks.forEach(item => { item.destroy() })
138
+ this.layoutedBlocks = undefined
139
+ }
140
+
141
+ this.__checkAgain()
142
+ }
143
+
144
+ protected __checkAgain(): void {
145
+ if (this.changed && this.times < 3) this.target.emit(RenderEvent.AGAIN)
146
+ }
147
+
148
+
149
+ protected partRender(): void {
150
+ const { canvas, layoutedBlocks } = this
151
+
152
+ if (layoutedBlocks.some(block => block.updatedBounds.includes(this.target.__world))) {
153
+ this.fullRender(canvas.bounds)
154
+ } else {
155
+ let bounds: IBounds
156
+ layoutedBlocks.forEach(block => {
157
+ bounds = block.updatedBounds
158
+ if (canvas.bounds.hit(bounds) && !bounds.isEmpty()) this.clipRender(bounds.getIntersect(canvas.bounds))
159
+ })
160
+ }
161
+ }
162
+
163
+ public clipRender(bounds: IBounds): void {
164
+ const t = Run.start('part render')
165
+ const { canvas, target } = this
166
+
167
+ canvas.save()
168
+ canvas.clearBounds(bounds)
169
+ if (Debug.showRepaint) canvas.strokeBounds(bounds, 'red')
170
+ canvas.clipBounds(bounds)
171
+ target.__render(canvas, { bounds })
172
+ canvas.restore()
173
+
174
+ Run.end(t)
175
+ }
176
+
177
+ public fullRender(bounds?: IBounds): void {
178
+ const { canvas, target } = this
179
+ Renderer.fullRender(target, canvas, bounds)
180
+ if (Debug.showRepaint) canvas.strokeBounds(bounds || canvas.bounds, 'red')
181
+ }
182
+
183
+ static fullRender(target: ILeaf, canvas: ILeaferCanvas, bounds?: IBounds): void {
184
+ const t = Run.start('full render')
185
+ if (!bounds) bounds = canvas.bounds
186
+
187
+ canvas.save()
188
+ canvas.clear()
189
+ target.__render(canvas, canvas.bounds.includes(target.__world) ? {} : { bounds })
190
+ canvas.restore()
191
+
192
+ Run.end(t)
193
+ }
194
+
195
+ private requestRender(): void {
196
+ const startTime = Date.now()
197
+ Platform.requestRender(() => {
198
+ if (this.changed) {
199
+ this.FPS = Math.min(60, Math.ceil(1000 / (Date.now() - startTime)))
200
+ if (this.running) this.render()
201
+ }
202
+ })
203
+ }
204
+
205
+ public destroy(): void {
206
+ if (this.target) {
207
+ this.removeListenEvents()
208
+ this.target = undefined
209
+ this.canvas = undefined
210
+ this.config = undefined
211
+ }
212
+ }
213
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Renderer } from './Renderer'