@leafer/canvas 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/canvas
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@leafer/canvas",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/canvas",
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/canvas/canvas",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/list": "1.0.0-alpha.1"
21
+ },
22
+ "devDependencies": {
23
+ "@leafer/interface": "1.0.0-alpha.1"
24
+ }
25
+ }
@@ -0,0 +1,62 @@
1
+ import { ILeaferCanvas, IScreenSizeData, ILeafer, ICanvasManager } from '@leafer/interface'
2
+
3
+
4
+ export class CanvasManager implements ICanvasManager {
5
+
6
+ public leafer: ILeafer
7
+
8
+ public list: ILeaferCanvas[] = []
9
+
10
+ constructor(leafer: ILeafer) {
11
+ this.leafer = leafer
12
+ }
13
+
14
+ public add(canvas: ILeaferCanvas): void {
15
+ canvas.manager = this
16
+ this.list.push(canvas)
17
+ }
18
+
19
+ public get(size: IScreenSizeData): ILeaferCanvas {
20
+ let old: ILeaferCanvas
21
+ const { list } = this
22
+ for (let i = 0, len = list.length; i < len; i++) {
23
+ old = list[i]
24
+ if (old.recycled && old.isSameSize(size)) {
25
+ old.recycled = false
26
+ return old
27
+ }
28
+ }
29
+
30
+ const canvas = this.leafer.creator.canvas(size)
31
+ this.add(canvas)
32
+ return canvas
33
+ }
34
+
35
+ public recycle(old: ILeaferCanvas): void {
36
+ if (!old.recycled) {
37
+ old.clear()
38
+ old.recycled = true
39
+ }
40
+ }
41
+
42
+ public clearRecycled(): void {
43
+ let canvas: ILeaferCanvas
44
+ const filter: ILeaferCanvas[] = []
45
+ for (let i = 0, len = this.list.length; i < len; i++) {
46
+ canvas = this.list[i]
47
+ canvas.recycled ? canvas.destroy() : filter.push(canvas)
48
+ }
49
+ this.list = filter
50
+ }
51
+
52
+ public clear(): void {
53
+ this.list.forEach(item => { item.destroy() })
54
+ this.list.length = 0
55
+ }
56
+
57
+ public destory(): void {
58
+ this.clear()
59
+ this.leafer = undefined
60
+ }
61
+
62
+ }
@@ -0,0 +1,47 @@
1
+ import { IScreenSizeData, IHitCanvasManager, ILeaf, IHitCanvas, ILeaferCanvas, ILeafList } from '@leafer/interface'
2
+ import { LeafList } from '@leafer/list'
3
+
4
+ import { CanvasManager } from './CanvasManager'
5
+
6
+
7
+ export class HitCanvasManager extends CanvasManager implements IHitCanvasManager {
8
+
9
+ protected pathTypeList: ILeafList = new LeafList()
10
+ protected imageTypeList: ILeafList = new LeafList()
11
+
12
+ public get(size: IScreenSizeData): ILeaferCanvas { return undefined }
13
+
14
+ public getImageType(leaf: ILeaf, size: IScreenSizeData): IHitCanvas {
15
+ this.imageTypeList.push(leaf)
16
+ return this.leafer.creator.hitCanvas(size)
17
+ }
18
+
19
+ public getPathType(leaf: ILeaf): IHitCanvas {
20
+ this.pathTypeList.push(leaf)
21
+ return this.leafer.creator.hitCanvas()
22
+ }
23
+
24
+ public clearImageType(): void {
25
+ this.__clearLeafList(this.imageTypeList)
26
+ }
27
+
28
+ public clearPathType(): void {
29
+ this.__clearLeafList(this.pathTypeList)
30
+ }
31
+
32
+ protected __clearLeafList(leafList: ILeafList): void {
33
+ leafList.forEach(leaf => {
34
+ if (leaf.__hitCanvas) {
35
+ leaf.__hitCanvas.destroy()
36
+ leaf.__hitCanvas = undefined
37
+ }
38
+ })
39
+ leafList.reset()
40
+ }
41
+
42
+ public clear(): void {
43
+ this.clearPathType()
44
+ this.clearImageType()
45
+ }
46
+
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { CanvasManager } from './CanvasManager'
2
+ export { HitCanvasManager } from './HitCanvasManager'