@leafer/image 1.0.0-rc.9 → 1.0.0
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 +5 -5
- package/src/ImageManager.ts +3 -2
- package/src/LeaferImage.ts +28 -1
- package/types/index.d.ts +4 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/image",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "@leafer/image",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leafer/task": "1.0.0
|
|
26
|
-
"@leafer/file": "1.0.0
|
|
27
|
-
"@leafer/platform": "1.0.0
|
|
25
|
+
"@leafer/task": "1.0.0",
|
|
26
|
+
"@leafer/file": "1.0.0",
|
|
27
|
+
"@leafer/platform": "1.0.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@leafer/interface": "1.0.0
|
|
30
|
+
"@leafer/interface": "1.0.0"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/src/ImageManager.ts
CHANGED
|
@@ -33,7 +33,7 @@ export const ImageManager: IImageManager = {
|
|
|
33
33
|
|
|
34
34
|
clearRecycled(): void {
|
|
35
35
|
const list = I.recycledList
|
|
36
|
-
if (list.length) {
|
|
36
|
+
if (list.length > 100) { // cache 100
|
|
37
37
|
list.forEach(image => {
|
|
38
38
|
if (!image.use && image.url) {
|
|
39
39
|
delete I.map[image.url]
|
|
@@ -44,7 +44,7 @@ export const ImageManager: IImageManager = {
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
hasOpacityPixel(config: ILeaferImageConfig): boolean {
|
|
48
48
|
return FileHelper.opacityTypes.some(item => I.isFormat(item, config))
|
|
49
49
|
},
|
|
50
50
|
|
|
@@ -61,6 +61,7 @@ export const ImageManager: IImageManager = {
|
|
|
61
61
|
|
|
62
62
|
destroy(): void {
|
|
63
63
|
I.map = {}
|
|
64
|
+
I.recycledList = []
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
}
|
package/src/LeaferImage.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ILeaferImage, ILeaferImageConfig, IFunction, IObject, InnerId } from '@leafer/interface'
|
|
1
|
+
import { ILeaferImage, ILeaferImageConfig, IFunction, IObject, InnerId, IMatrixData, ICanvasPattern, ILeaferImageCacheCanvas, ILeaferImagePatternPaint } from '@leafer/interface'
|
|
2
2
|
import { Platform } from '@leafer/platform'
|
|
3
3
|
import { IncrementId } from '@leafer/math'
|
|
4
4
|
|
|
@@ -18,6 +18,7 @@ export class LeaferImage implements ILeaferImage {
|
|
|
18
18
|
public height: number
|
|
19
19
|
|
|
20
20
|
public isSVG: boolean
|
|
21
|
+
public hasOpacityPixel: boolean // check png / svg / webp
|
|
21
22
|
|
|
22
23
|
public get completed() { return this.ready || !!this.error }
|
|
23
24
|
|
|
@@ -31,10 +32,13 @@ export class LeaferImage implements ILeaferImage {
|
|
|
31
32
|
|
|
32
33
|
protected waitComplete: IFunction[] = []
|
|
33
34
|
|
|
35
|
+
protected cache: ILeaferImageCacheCanvas
|
|
36
|
+
|
|
34
37
|
constructor(config: ILeaferImageConfig) {
|
|
35
38
|
this.innerId = create(IMAGE)
|
|
36
39
|
this.config = config || { url: '' }
|
|
37
40
|
this.isSVG = ImageManager.isFormat('svg', config)
|
|
41
|
+
this.hasOpacityPixel = ImageManager.hasOpacityPixel(config)
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
public load(onSuccess: IFunction, onError: IFunction): number {
|
|
@@ -83,15 +87,38 @@ export class LeaferImage implements ILeaferImage {
|
|
|
83
87
|
public getCanvas(width: number, height: number, opacity?: number, _filters?: IObject): any {
|
|
84
88
|
width || (width = this.width)
|
|
85
89
|
height || (height = this.height)
|
|
90
|
+
|
|
91
|
+
if (this.cache) { // when use > 1, check cache
|
|
92
|
+
let { params, data } = this.cache
|
|
93
|
+
for (let i in params) { if (params[i] !== arguments[i]) { data = null; break } }
|
|
94
|
+
if (data) return data
|
|
95
|
+
}
|
|
96
|
+
|
|
86
97
|
const canvas = Platform.origin.createCanvas(width, height)
|
|
87
98
|
const ctx = canvas.getContext('2d')
|
|
88
99
|
if (opacity) ctx.globalAlpha = opacity
|
|
89
100
|
ctx.drawImage(this.view, 0, 0, width, height)
|
|
101
|
+
|
|
102
|
+
this.cache = this.use > 1 ? { data: canvas, params: arguments } : null
|
|
103
|
+
|
|
90
104
|
return canvas
|
|
91
105
|
}
|
|
92
106
|
|
|
107
|
+
public getPattern(canvas: any, repeat: string | null, transform?: IMatrixData, paint?: ILeaferImagePatternPaint): ICanvasPattern {
|
|
108
|
+
const pattern = Platform.canvas.createPattern(canvas, repeat)
|
|
109
|
+
try {
|
|
110
|
+
if (transform && pattern.setTransform) {
|
|
111
|
+
pattern.setTransform(transform) // maybe error
|
|
112
|
+
transform = null
|
|
113
|
+
}
|
|
114
|
+
} catch { }
|
|
115
|
+
if (paint) paint.transform = transform
|
|
116
|
+
return pattern
|
|
117
|
+
}
|
|
118
|
+
|
|
93
119
|
public destroy(): void {
|
|
94
120
|
this.config = { url: '' }
|
|
121
|
+
this.cache = null
|
|
95
122
|
this.waitComplete.length = 0
|
|
96
123
|
}
|
|
97
124
|
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ILeaferImage, InnerId, IObject, ILeaferImageConfig, IFunction, IImageManager } from '@leafer/interface';
|
|
1
|
+
import { ILeaferImage, InnerId, IObject, ILeaferImageConfig, IFunction, ILeaferImageCacheCanvas, IMatrixData, ILeaferImagePatternPaint, ICanvasPattern, IImageManager } from '@leafer/interface';
|
|
2
2
|
|
|
3
3
|
declare class LeaferImage implements ILeaferImage {
|
|
4
4
|
readonly innerId: InnerId;
|
|
@@ -7,6 +7,7 @@ declare class LeaferImage implements ILeaferImage {
|
|
|
7
7
|
width: number;
|
|
8
8
|
height: number;
|
|
9
9
|
isSVG: boolean;
|
|
10
|
+
hasOpacityPixel: boolean;
|
|
10
11
|
get completed(): boolean;
|
|
11
12
|
ready: boolean;
|
|
12
13
|
error: IObject;
|
|
@@ -14,11 +15,13 @@ declare class LeaferImage implements ILeaferImage {
|
|
|
14
15
|
use: number;
|
|
15
16
|
config: ILeaferImageConfig;
|
|
16
17
|
protected waitComplete: IFunction[];
|
|
18
|
+
protected cache: ILeaferImageCacheCanvas;
|
|
17
19
|
constructor(config: ILeaferImageConfig);
|
|
18
20
|
load(onSuccess: IFunction, onError: IFunction): number;
|
|
19
21
|
unload(index: number, stopEvent?: boolean): void;
|
|
20
22
|
protected onComplete(isSuccess: boolean): void;
|
|
21
23
|
getCanvas(width: number, height: number, opacity?: number, _filters?: IObject): any;
|
|
24
|
+
getPattern(canvas: any, repeat: string | null, transform?: IMatrixData, paint?: ILeaferImagePatternPaint): ICanvasPattern;
|
|
22
25
|
destroy(): void;
|
|
23
26
|
}
|
|
24
27
|
|