@leafer/image 1.0.0-rc.2 → 1.0.0-rc.21

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/image",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.21",
4
4
  "description": "@leafer/image",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/task": "1.0.0-rc.2",
26
- "@leafer/platform": "1.0.0-rc.2"
25
+ "@leafer/task": "1.0.0-rc.21",
26
+ "@leafer/file": "1.0.0-rc.21",
27
+ "@leafer/platform": "1.0.0-rc.21"
27
28
  },
28
29
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.2"
30
+ "@leafer/interface": "1.0.0-rc.21"
30
31
  }
31
32
  }
@@ -1,7 +1,9 @@
1
- import { IImageManager, ILeaferImageConfig, ILeaferImage } from '@leafer/interface'
1
+ import { IImageManager, ILeaferImageConfig, ILeaferImage, IExportFileType } from '@leafer/interface'
2
2
  import { Creator } from '@leafer/platform'
3
+ import { FileHelper } from '@leafer/file'
3
4
  import { TaskProcessor } from '@leafer/task'
4
5
 
6
+
5
7
  export const ImageManager: IImageManager = {
6
8
 
7
9
  map: {},
@@ -12,7 +14,7 @@ export const ImageManager: IImageManager = {
12
14
 
13
15
  patternTasker: new TaskProcessor(),
14
16
 
15
- get isComplete() { return I.tasker.isComplete && I.patternTasker.isComplete },
17
+ get isComplete() { return I.tasker.isComplete },
16
18
 
17
19
  get(config: ILeaferImageConfig): ILeaferImage {
18
20
  let image = I.map[config.url]
@@ -31,7 +33,7 @@ export const ImageManager: IImageManager = {
31
33
 
32
34
  clearRecycled(): void {
33
35
  const list = I.recycledList
34
- if (list.length) {
36
+ if (list.length > 100) { // cache 100
35
37
  list.forEach(image => {
36
38
  if (!image.use && image.url) {
37
39
  delete I.map[image.url]
@@ -42,8 +44,24 @@ export const ImageManager: IImageManager = {
42
44
  }
43
45
  },
44
46
 
47
+ hasOpacityPixel(config: ILeaferImageConfig): boolean {
48
+ return FileHelper.opacityTypes.some(item => I.isFormat(item, config))
49
+ },
50
+
51
+ isFormat(format: IExportFileType, config: ILeaferImageConfig): boolean {
52
+ if (config.format === format) return true
53
+ const { url } = config
54
+ if (url.startsWith('data:')) {
55
+ if (url.startsWith('data:' + FileHelper.mineType(format))) return true
56
+ } else {
57
+ if (url.includes('.' + format) || url.includes('.' + FileHelper.upperCaseTypeMap[format])) return true
58
+ }
59
+ return false
60
+ },
61
+
45
62
  destroy(): void {
46
63
  I.map = {}
64
+ I.recycledList = []
47
65
  }
48
66
 
49
67
  }
@@ -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,16 +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
- const { url } = config
38
- if (url.startsWith('data:')) {
39
- if (url.startsWith('data:image/svg')) this.isSVG = true
40
- } else {
41
- if (url.includes('.svg')) this.isSVG = true
42
- }
43
- if (this.config.format === 'svg') this.isSVG = true
40
+ this.isSVG = ImageManager.isFormat('svg', config)
41
+ this.hasOpacityPixel = ImageManager.hasOpacityPixel(config)
44
42
  }
45
43
 
46
44
  public load(onSuccess: IFunction, onError: IFunction): number {
@@ -89,15 +87,38 @@ export class LeaferImage implements ILeaferImage {
89
87
  public getCanvas(width: number, height: number, opacity?: number, _filters?: IObject): any {
90
88
  width || (width = this.width)
91
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
+
92
97
  const canvas = Platform.origin.createCanvas(width, height)
93
98
  const ctx = canvas.getContext('2d')
94
99
  if (opacity) ctx.globalAlpha = opacity
95
100
  ctx.drawImage(this.view, 0, 0, width, height)
101
+
102
+ this.cache = this.use > 1 ? { data: canvas, params: arguments } : null
103
+
96
104
  return canvas
97
105
  }
98
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
+
99
119
  public destroy(): void {
100
120
  this.config = { url: '' }
121
+ this.cache = null
101
122
  this.waitComplete.length = 0
102
123
  }
103
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