@leafer/image 1.0.0-beta → 1.0.0-beta.10
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 +6 -2
- package/src/ImageManager.ts +37 -19
- package/src/LeaferImageBase.ts +101 -0
- package/src/index.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/image",
|
|
3
|
-
"version": "1.0.0-beta",
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"description": "@leafer/image",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,7 +18,11 @@
|
|
|
18
18
|
"leafer",
|
|
19
19
|
"leaferjs"
|
|
20
20
|
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@leafer/task": "1.0.0-beta.10",
|
|
23
|
+
"@leafer/platform": "1.0.0-beta.10"
|
|
24
|
+
},
|
|
21
25
|
"devDependencies": {
|
|
22
|
-
"@leafer/interface": "1.0.0-beta"
|
|
26
|
+
"@leafer/interface": "1.0.0-beta.10"
|
|
23
27
|
}
|
|
24
28
|
}
|
package/src/ImageManager.ts
CHANGED
|
@@ -1,34 +1,52 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IImageManager, ILeaferImageConfig, ILeaferImage } from '@leafer/interface'
|
|
2
2
|
import { Creator } from '@leafer/platform'
|
|
3
|
+
import { TaskProcessor } from '@leafer/task'
|
|
3
4
|
|
|
5
|
+
export const ImageManager: IImageManager = {
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
[name: string]: ILeaferImage
|
|
7
|
-
}
|
|
8
|
-
|
|
7
|
+
map: {},
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
recycledList: [],
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
tasker: new TaskProcessor(),
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
patternTasker: new TaskProcessor(),
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
this.leafer = leafer
|
|
18
|
-
}
|
|
15
|
+
get isComplete() { return I.tasker.isComplete && I.patternTasker.isComplete },
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
let image =
|
|
17
|
+
get(config: ILeaferImageConfig): ILeaferImage {
|
|
18
|
+
let image = I.map[config.url]
|
|
22
19
|
if (!image) {
|
|
23
20
|
image = Creator.image(config)
|
|
24
|
-
|
|
21
|
+
I.map[config.url] = image
|
|
25
22
|
}
|
|
23
|
+
image.use++
|
|
26
24
|
return image
|
|
27
|
-
}
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
recycle(image: ILeaferImage): void {
|
|
28
|
+
image.use--
|
|
29
|
+
setTimeout(() => { if (!image.use) I.recycledList.push(image) }, 0)
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
clearRecycled(): void {
|
|
33
|
+
const list = I.recycledList
|
|
34
|
+
if (list.length) {
|
|
35
|
+
list.forEach(image => {
|
|
36
|
+
if (!image.use) {
|
|
37
|
+
delete I.map[image.url]
|
|
38
|
+
image.destroy()
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
list.length = 0
|
|
42
|
+
}
|
|
43
|
+
},
|
|
28
44
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
45
|
+
destroy(): void {
|
|
46
|
+
I.map = {}
|
|
47
|
+
I.tasker = null
|
|
32
48
|
}
|
|
33
49
|
|
|
34
|
-
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const I = ImageManager
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { ILeaferImage, ILeaferImageConfig, IFunction, IObject, InnerId } from '@leafer/interface'
|
|
2
|
+
import { Platform } from '@leafer/platform'
|
|
3
|
+
import { IncrementId } from '@leafer/math'
|
|
4
|
+
import { ImageManager } from './ImageManager'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const { IMAGE, create } = IncrementId
|
|
8
|
+
|
|
9
|
+
export class LeaferImageBase implements ILeaferImage {
|
|
10
|
+
|
|
11
|
+
public readonly innerId: InnerId
|
|
12
|
+
public get url() { return this.config.url }
|
|
13
|
+
|
|
14
|
+
public view: any
|
|
15
|
+
|
|
16
|
+
public width: number
|
|
17
|
+
public height: number
|
|
18
|
+
|
|
19
|
+
public isSVG: boolean
|
|
20
|
+
|
|
21
|
+
public get completed() { return this.ready || !!this.error }
|
|
22
|
+
|
|
23
|
+
public ready: boolean
|
|
24
|
+
public error: IObject
|
|
25
|
+
public loading: boolean
|
|
26
|
+
|
|
27
|
+
public use = 0
|
|
28
|
+
|
|
29
|
+
public config: ILeaferImageConfig
|
|
30
|
+
|
|
31
|
+
protected waitComplete: IFunction[] = []
|
|
32
|
+
|
|
33
|
+
constructor(config: ILeaferImageConfig) {
|
|
34
|
+
this.innerId = create(IMAGE)
|
|
35
|
+
this.config = config
|
|
36
|
+
const { url } = config
|
|
37
|
+
if (url.startsWith('data:')) {
|
|
38
|
+
if (url.startsWith('data:image/svg')) this.isSVG = true
|
|
39
|
+
} else {
|
|
40
|
+
if (url.includes('.svg')) this.isSVG = true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public load(onSuccess: IFunction, onError: IFunction): number {
|
|
45
|
+
if (!this.loading) {
|
|
46
|
+
this.loading = true
|
|
47
|
+
ImageManager.tasker.addParallel(async () => await Platform.origin.loadImage(this.config.url).then((img) => {
|
|
48
|
+
this.ready = true
|
|
49
|
+
this.width = img.naturalWidth || img.width
|
|
50
|
+
this.height = img.naturalHeight || img.height
|
|
51
|
+
this.view = img
|
|
52
|
+
this.onComplete(true)
|
|
53
|
+
}).catch((e) => {
|
|
54
|
+
this.error = e
|
|
55
|
+
this.onComplete(false)
|
|
56
|
+
}), null, true)
|
|
57
|
+
}
|
|
58
|
+
this.waitComplete.push(onSuccess, onError)
|
|
59
|
+
return this.waitComplete.length - 2
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public unload(index: number): void {
|
|
63
|
+
const l = this.waitComplete
|
|
64
|
+
const error = l[index + 1]
|
|
65
|
+
if (error) error({ type: 'stop' })
|
|
66
|
+
l[index] = l[index + 1] = undefined
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
protected onComplete(isSuccess: boolean): void {
|
|
70
|
+
let odd: number
|
|
71
|
+
this.waitComplete.forEach((item, index) => {
|
|
72
|
+
odd = index % 2
|
|
73
|
+
if (item) {
|
|
74
|
+
if (isSuccess) {
|
|
75
|
+
if (!odd) item(this)
|
|
76
|
+
} else {
|
|
77
|
+
if (odd) item(this.error)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
this.waitComplete.length = 0
|
|
82
|
+
this.loading = false
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public getCanvas(width: number, height: number, opacity?: number, _filters?: IObject): any {
|
|
86
|
+
width || (width = this.width)
|
|
87
|
+
height || (height = this.height)
|
|
88
|
+
const canvas = Platform.origin.createCanvas(width, height)
|
|
89
|
+
const ctx = canvas.getContext('2d')
|
|
90
|
+
if (opacity) ctx.globalAlpha = opacity
|
|
91
|
+
ctx.drawImage(this.view, 0, 0, width, height)
|
|
92
|
+
return canvas
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public destroy(): void {
|
|
96
|
+
this.view = null
|
|
97
|
+
this.config = null
|
|
98
|
+
this.waitComplete.length = 0
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
1
|
+
export { LeaferImageBase } from './LeaferImageBase'
|
|
2
|
+
export { ImageManager } from './ImageManager'
|