@leafer/canvas-miniapp 1.0.0-rc.9 → 1.0.1

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-rc.9",
3
+ "version": "1.0.1",
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-rc.9",
26
- "@leafer/data": "1.0.0-rc.9",
27
- "@leafer/event": "1.0.0-rc.9",
28
- "@leafer/platform": "1.0.0-rc.9",
29
- "@leafer/debug": "1.0.0-rc.9"
25
+ "@leafer/canvas": "1.0.1",
26
+ "@leafer/data": "1.0.1",
27
+ "@leafer/event": "1.0.1",
28
+ "@leafer/platform": "1.0.1",
29
+ "@leafer/debug": "1.0.1"
30
30
  },
31
31
  "devDependencies": {
32
- "@leafer/interface": "1.0.0-rc.9"
32
+ "@leafer/interface": "1.0.1"
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,22 @@ 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
+ // fix roundRect
53
+ if (this.context.roundRect) {
54
+ this.roundRect = function (x: number, y: number, width: number, height: number, radius?: number | number[]): void {
55
+ this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius)
56
+ }
51
57
  }
58
+ canvasPatch((this.context as IObject).__proto__)
52
59
  }
53
- canvasPatch((this.context as IObject).__proto__)
54
-
55
60
  }
56
61
 
57
62
  protected __createView(): void {
@@ -61,8 +66,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
61
66
  public updateViewSize(): void {
62
67
  const { width, height, pixelRatio } = this
63
68
 
64
- this.view.width = width * pixelRatio
65
- this.view.height = height * pixelRatio
69
+ this.view.width = Math.ceil(width * pixelRatio)
70
+ this.view.height = Math.ceil(height * pixelRatio)
66
71
  }
67
72
 
68
73
  public updateClientBounds(callback?: IFunction): void {
@@ -73,10 +78,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
73
78
  }
74
79
 
75
80
 
76
- public startAutoLayout(_autoBounds: IAutoBounds, listener: IResizeEventListener): void {
81
+ public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
77
82
  this.resizeListener = listener
78
- this.checkSize = this.checkSize.bind(this)
79
- Platform.miniapp.onWindowResize(this.checkSize)
83
+ if (autoBounds) {
84
+ this.checkSize = this.checkSize.bind(this)
85
+ Platform.miniapp.onWindowResize(this.checkSize)
86
+ }
80
87
  }
81
88
 
82
89
  public checkSize(): void {
@@ -86,12 +93,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
86
93
  const { width, height } = this.clientBounds
87
94
  const { pixelRatio } = this
88
95
  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
- }
96
+ if (!this.isSameSize(size)) this.emitResize(size)
95
97
  })
96
98
  }, 500)
97
99
  }
@@ -103,4 +105,15 @@ export class LeaferCanvas extends LeaferCanvasBase {
103
105
  Platform.miniapp.offWindowResize(this.checkSize)
104
106
  }
105
107
 
108
+ public unrealCanvas(): void { // App needs to use
109
+ this.unreal = true
110
+ }
111
+
112
+ protected emitResize(size: IScreenSizeData): void {
113
+ const oldSize = {} as IScreenSizeData
114
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
115
+ this.resize(size)
116
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
117
+ }
118
+
106
119
  }
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 };