@leafer/canvas-web 1.0.0 → 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-web",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "@leafer/canvas-web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.0.0"
25
+ "@leafer/core": "1.0.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.0.0"
28
+ "@leafer/interface": "1.0.1"
29
29
  }
30
30
  }
@@ -33,7 +33,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
33
33
  protected resizeListener: IResizeEventListener
34
34
 
35
35
  public init(): void {
36
- const { view } = this.config
36
+ const { config } = this
37
+ const view = config.view || config.canvas
37
38
 
38
39
  view ? this.__createViewFrom(view) : this.__createView()
39
40
  const { style } = this.view
@@ -52,7 +53,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
52
53
 
53
54
  this.__createContext()
54
55
 
55
- if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
56
+ if (!this.autoLayout) this.resize(config as IScreenSizeData)
56
57
  }
57
58
 
58
59
  public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
@@ -122,29 +123,47 @@ export class LeaferCanvas extends LeaferCanvasBase {
122
123
  }
123
124
 
124
125
  public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
125
- this.autoBounds = autoBounds
126
126
  this.resizeListener = listener
127
- try {
128
127
 
129
- this.resizeObserver = new ResizeObserver((entries) => {
130
- this.updateClientBounds()
131
- for (const entry of entries) this.checkAutoBounds(entry.contentRect)
132
- })
128
+ if (autoBounds) {
129
+
130
+ // check auto layout
131
+ this.autoBounds = autoBounds
132
+ try {
133
+
134
+ this.resizeObserver = new ResizeObserver((entries) => {
135
+ this.updateClientBounds()
136
+ for (const entry of entries) this.checkAutoBounds(entry.contentRect)
137
+ })
138
+
139
+ const parent = this.parentView
140
+ if (parent) {
141
+ this.resizeObserver.observe(parent)
142
+ this.checkAutoBounds(parent.getBoundingClientRect())
143
+ } else {
144
+ this.checkAutoBounds(this.view)
145
+ debug.warn('no parent')
146
+ }
147
+
148
+ } catch {
149
+
150
+ this.imitateResizeObserver()
133
151
 
134
- const parent = this.parentView
135
- if (parent) {
136
- this.resizeObserver.observe(parent)
137
- this.checkAutoBounds(parent.getBoundingClientRect())
138
- } else {
139
- this.checkAutoBounds(this.view)
140
- debug.warn('no parent')
141
152
  }
142
153
 
143
- } catch {
154
+ } else {
144
155
 
145
- this.imitateResizeObserver()
156
+ // check devicePixelRatio change
157
+ window.addEventListener('resize', () => {
158
+ const pixelRatio = Platform.devicePixelRatio
159
+ if (this.pixelRatio !== pixelRatio) {
160
+ const { width, height } = this
161
+ this.emitResize({ width, height, pixelRatio })
162
+ }
163
+ })
146
164
 
147
165
  }
166
+
148
167
  }
149
168
 
150
169
  protected imitateResizeObserver(): void {
@@ -157,16 +176,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
157
176
  protected checkAutoBounds(parentSize: ISizeData): void {
158
177
  const view = this.view
159
178
  const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
160
- if (width !== this.width || height !== this.height) {
179
+ const size = { width, height, pixelRatio: Platform.devicePixelRatio } as IScreenSizeData
180
+ if (!this.isSameSize(size)) {
161
181
  const { style } = view
162
- const { pixelRatio } = this
163
182
  style.marginLeft = x + 'px'
164
183
  style.marginTop = y + 'px'
165
- const size = { width, height, pixelRatio }
166
- const oldSize = {} as IScreenSizeData
167
- DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
168
- this.resize(size)
169
- if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
184
+ this.emitResize(size)
170
185
  }
171
186
  }
172
187
 
@@ -179,6 +194,14 @@ export class LeaferCanvas extends LeaferCanvasBase {
179
194
  }
180
195
  }
181
196
 
197
+ protected emitResize(size: IScreenSizeData): void {
198
+ const oldSize = {} as IScreenSizeData
199
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
200
+ this.resize(size)
201
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
202
+ }
203
+
204
+
182
205
  public unrealCanvas(): void { // App needs to use
183
206
  if (!this.unreal && this.parentView) {
184
207
  const view = this.view
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IAutoBounds, IResizeEventListener, ISizeData } from '@leafer/interface';
1
+ import { IAutoBounds, IResizeEventListener, ISizeData, IScreenSizeData } from '@leafer/interface';
2
2
  import { LeaferCanvasBase } from '@leafer/core';
3
3
 
4
4
  declare class LeaferCanvas extends LeaferCanvasBase {
@@ -23,6 +23,7 @@ declare class LeaferCanvas extends LeaferCanvasBase {
23
23
  protected imitateResizeObserver(): void;
24
24
  protected checkAutoBounds(parentSize: ISizeData): void;
25
25
  stopAutoLayout(): void;
26
+ protected emitResize(size: IScreenSizeData): void;
26
27
  unrealCanvas(): void;
27
28
  destroy(): void;
28
29
  }