@leafer/image 1.0.0-beta.2 → 1.0.0-beta.5
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 +2 -2
- package/src/ImageManager.ts +10 -1
- package/src/LeaferImageBase.ts +47 -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.5",
|
|
4
4
|
"description": "@leafer/image",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,6 +19,6 @@
|
|
|
19
19
|
"leaferjs"
|
|
20
20
|
],
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@leafer/interface": "1.0.0-beta.
|
|
22
|
+
"@leafer/interface": "1.0.0-beta.5"
|
|
23
23
|
}
|
|
24
24
|
}
|
package/src/ImageManager.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ILeafer, ILeaferCanvasConfig, IImageManager, ILeaferImageConfig, ILeaferImage } from '@leafer/interface'
|
|
1
|
+
import { ILeafer, ILeaferCanvasConfig, IImageManager, ILeaferImageConfig, ILeaferImage, ITaskProcessor, IFunction } from '@leafer/interface'
|
|
2
2
|
import { Creator } from '@leafer/platform'
|
|
3
|
+
import { TaskProcessor } from '@leafer/task'
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
interface ILeaferImageMap {
|
|
@@ -11,10 +12,13 @@ export class ImageManager implements IImageManager {
|
|
|
11
12
|
|
|
12
13
|
public leafer: ILeafer
|
|
13
14
|
|
|
15
|
+
public tasker: ITaskProcessor
|
|
16
|
+
|
|
14
17
|
public map: ILeaferImageMap = {}
|
|
15
18
|
|
|
16
19
|
constructor(leafer: ILeafer, _config: ILeaferCanvasConfig) {
|
|
17
20
|
this.leafer = leafer
|
|
21
|
+
this.tasker = new TaskProcessor()
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
public get(config: ILeaferImageConfig): ILeaferImage {
|
|
@@ -26,6 +30,11 @@ export class ImageManager implements IImageManager {
|
|
|
26
30
|
return image
|
|
27
31
|
}
|
|
28
32
|
|
|
33
|
+
public load(image: ILeaferImage, onSuccess: IFunction, onError: IFunction): void {
|
|
34
|
+
this.tasker.add(async () => await image.load(onSuccess, onError))
|
|
35
|
+
if (!this.tasker.running) this.tasker.start()
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
public destory(): void {
|
|
30
39
|
this.leafer = null
|
|
31
40
|
this.map = null
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ILeaferImage, ILeaferImageConfig, IFunction, IObject } from '@leafer/interface'
|
|
2
|
+
import { Platform } from '@leafer/platform'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class LeaferImageBase implements ILeaferImage {
|
|
6
|
+
|
|
7
|
+
public view: any
|
|
8
|
+
|
|
9
|
+
public width: number
|
|
10
|
+
public height: number
|
|
11
|
+
|
|
12
|
+
public ready: boolean
|
|
13
|
+
|
|
14
|
+
public options: ILeaferImageConfig
|
|
15
|
+
|
|
16
|
+
constructor(_options: ILeaferImageConfig) {
|
|
17
|
+
this.options = _options
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async load(onSuccess: IFunction, onError: IFunction): Promise<void> {
|
|
21
|
+
return Platform.origin.loadImage(this.options.url).then((img) => {
|
|
22
|
+
this.ready = true
|
|
23
|
+
this.width = img.naturalWidth || img.width
|
|
24
|
+
this.height = img.naturalHeight || img.height
|
|
25
|
+
this.view = img
|
|
26
|
+
if (onSuccess) onSuccess(this)
|
|
27
|
+
}).catch((e) => {
|
|
28
|
+
if (onError) onError(e)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public getCanvas(width: number, height: number, opacity?: number, _filters?: IObject): any {
|
|
33
|
+
width || (width = this.width)
|
|
34
|
+
height || (height = this.height)
|
|
35
|
+
const canvas = Platform.origin.createCanvas(width, height)
|
|
36
|
+
const ctx = canvas.getContext('2d')
|
|
37
|
+
if (opacity) ctx.globalAlpha = opacity
|
|
38
|
+
ctx.drawImage(this.view, 0, 0, width, height)
|
|
39
|
+
return canvas
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public destory(): void {
|
|
43
|
+
this.view = null
|
|
44
|
+
this.options = null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
1
|
+
export { LeaferImageBase } from './LeaferImageBase'
|
|
2
|
+
export { ImageManager } from './ImageManager'
|