@leafer/image 1.0.0-beta.12 → 1.0.0-beta.15

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