@leafer/canvas-miniapp 1.0.0 → 1.0.2

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/canvas-miniapp",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "@leafer/canvas-miniapp",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,13 +22,13 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/canvas": "1.0.0",
26
- "@leafer/data": "1.0.0",
27
- "@leafer/event": "1.0.0",
28
- "@leafer/platform": "1.0.0",
29
- "@leafer/debug": "1.0.0"
25
+ "@leafer/canvas": "1.0.2",
26
+ "@leafer/data": "1.0.2",
27
+ "@leafer/event": "1.0.2",
28
+ "@leafer/platform": "1.0.2",
29
+ "@leafer/debug": "1.0.2"
30
30
  },
31
31
  "devDependencies": {
32
- "@leafer/interface": "1.0.0"
32
+ "@leafer/interface": "1.0.2"
33
33
  }
34
34
  }
@@ -1,4 +1,4 @@
1
- import { IResizeEventListener, IAutoBounds, IScreenSizeData, IFunction, IMiniappSelect, IObject } from '@leafer/interface'
1
+ import { IResizeEventListener, IAutoBounds, IScreenSizeData, IFunction, IMiniappSelect, IObject, ICanvasContext2D } from '@leafer/interface'
2
2
  import { LeaferCanvasBase, canvasPatch, canvasSizeAttrs, Platform, DataHelper, ResizeEvent } from '@leafer/core'
3
3
 
4
4
 
@@ -10,10 +10,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
10
10
  public resizeListener: IResizeEventListener
11
11
 
12
12
  public testView: any
13
- public testContext: any
13
+ public testContext: ICanvasContext2D
14
14
 
15
15
  public init(): void {
16
- let { view } = this.config
16
+ const { config } = this
17
+ let view = config.view || config.canvas
18
+
17
19
  if (view) {
18
20
  if (typeof view === 'string') {
19
21
  if (view[0] !== '#') view = '#' + view
@@ -39,19 +41,25 @@ export class LeaferCanvas extends LeaferCanvasBase {
39
41
  } else {
40
42
  this.view = view.view || view
41
43
  }
42
- this.__createContext()
44
+
45
+ this.view.getContext ? this.__createContext() : this.unrealCanvas()
46
+
43
47
  const { width, height, pixelRatio } = this.config
44
48
  const size = { width: width || view.width, height: height || view.height, pixelRatio }
45
49
  this.resize(size)
46
50
 
47
- // fix roundRect
48
- if (this.context.roundRect) {
49
- this.roundRect = function (x: number, y: number, width: number, height: number, radius?: number | number[]): void {
50
- this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius)
51
+ if (this.context) {
52
+
53
+ if (this.viewSelect) Platform.renderCanvas = this
54
+
55
+ // fix roundRect
56
+ if (this.context.roundRect) {
57
+ this.roundRect = function (x: number, y: number, width: number, height: number, radius?: number | number[]): void {
58
+ this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius)
59
+ }
51
60
  }
61
+ canvasPatch((this.context as IObject).__proto__)
52
62
  }
53
- canvasPatch((this.context as IObject).__proto__)
54
-
55
63
  }
56
64
 
57
65
  protected __createView(): void {
@@ -73,10 +81,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
73
81
  }
74
82
 
75
83
 
76
- public startAutoLayout(_autoBounds: IAutoBounds, listener: IResizeEventListener): void {
84
+ public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
77
85
  this.resizeListener = listener
78
- this.checkSize = this.checkSize.bind(this)
79
- Platform.miniapp.onWindowResize(this.checkSize)
86
+ if (autoBounds) {
87
+ this.checkSize = this.checkSize.bind(this)
88
+ Platform.miniapp.onWindowResize(this.checkSize)
89
+ }
80
90
  }
81
91
 
82
92
  public checkSize(): void {
@@ -86,12 +96,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
86
96
  const { width, height } = this.clientBounds
87
97
  const { pixelRatio } = this
88
98
  const size = { width, height, pixelRatio }
89
- if (!this.isSameSize(size)) {
90
- const oldSize = {} as IScreenSizeData
91
- DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
92
- this.resize(size)
93
- if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
94
- }
99
+ if (!this.isSameSize(size)) this.emitResize(size)
95
100
  })
96
101
  }, 500)
97
102
  }
@@ -103,4 +108,15 @@ export class LeaferCanvas extends LeaferCanvasBase {
103
108
  Platform.miniapp.offWindowResize(this.checkSize)
104
109
  }
105
110
 
111
+ public unrealCanvas(): void { // App needs to use
112
+ this.unreal = true
113
+ }
114
+
115
+ protected emitResize(size: IScreenSizeData): void {
116
+ const oldSize = {} as IScreenSizeData
117
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
118
+ this.resize(size)
119
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
120
+ }
121
+
106
122
  }
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IMiniappSelect, IResizeEventListener, IObject, IFunction, IAutoBounds } from '@leafer/interface';
1
+ import { IMiniappSelect, IResizeEventListener, ICanvasContext2D, IObject, IFunction, IAutoBounds, IScreenSizeData } from '@leafer/interface';
2
2
  import { LeaferCanvasBase } from '@leafer/core';
3
3
 
4
4
  declare class LeaferCanvas extends LeaferCanvasBase {
@@ -6,15 +6,17 @@ declare class LeaferCanvas extends LeaferCanvasBase {
6
6
  viewSelect: IMiniappSelect;
7
7
  resizeListener: IResizeEventListener;
8
8
  testView: any;
9
- testContext: any;
9
+ testContext: ICanvasContext2D;
10
10
  init(): void;
11
11
  protected initView(view?: IObject): void;
12
12
  protected __createView(): void;
13
13
  updateViewSize(): void;
14
14
  updateClientBounds(callback?: IFunction): void;
15
- startAutoLayout(_autoBounds: IAutoBounds, listener: IResizeEventListener): void;
15
+ startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
16
16
  checkSize(): void;
17
17
  stopAutoLayout(): void;
18
+ unrealCanvas(): void;
19
+ protected emitResize(size: IScreenSizeData): void;
18
20
  }
19
21
 
20
22
  export { LeaferCanvas };