@leafer/canvas-web 1.0.0-alpha.30 → 1.0.0-alpha.31

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.
Files changed (2) hide show
  1. package/package.json +5 -5
  2. package/src/LeaferCanvas.ts +53 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/canvas-web",
3
- "version": "1.0.0-alpha.30",
3
+ "version": "1.0.0-alpha.31",
4
4
  "description": "@leafer/canvas-web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,11 +19,11 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/math": "1.0.0-alpha.30",
23
- "@leafer/event": "1.0.0-alpha.30",
24
- "@leafer/debug": "1.0.0-alpha.30"
22
+ "@leafer/math": "1.0.0-alpha.31",
23
+ "@leafer/event": "1.0.0-alpha.31",
24
+ "@leafer/debug": "1.0.0-alpha.31"
25
25
  },
26
26
  "devDependencies": {
27
- "@leafer/interface": "1.0.0-alpha.30"
27
+ "@leafer/interface": "1.0.0-alpha.31"
28
28
  }
29
29
  }
@@ -1,6 +1,7 @@
1
1
  import { ICanvasContext2D, IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
2
2
  import { ResizeEvent } from '@leafer/event'
3
- import { LeaferCanvasBase } from '@leafer/canvas'
3
+ import { LeaferCanvasBase, canvasSizeAttrs } from '@leafer/canvas'
4
+ import { DataHelper } from '@leafer/data'
4
5
  import { Debug } from '@leafer/debug'
5
6
 
6
7
 
@@ -12,6 +13,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
12
13
  public parentView: HTMLElement
13
14
 
14
15
  protected resizeObserver: ResizeObserver
16
+ protected autoBounds: IAutoBounds
17
+ protected resizeListener: IResizeEventListener
15
18
 
16
19
  public init(): void {
17
20
  const { view } = this.config
@@ -46,7 +49,15 @@ export class LeaferCanvas extends LeaferCanvasBase {
46
49
  }
47
50
 
48
51
  protected __createView(): void {
49
- this.view = this.offscreen ? new OffscreenCanvas(1, 1) : document.createElement('canvas')
52
+ if (this.offscreen) {
53
+ try {
54
+ this.view = new OffscreenCanvas(1, 1)
55
+ return
56
+ } catch (e) {
57
+ debug.error(e)
58
+ }
59
+ }
60
+ this.view = document.createElement('canvas')
50
61
  }
51
62
 
52
63
  protected __createViewFrom(inputView: string | object): void {
@@ -101,41 +112,57 @@ export class LeaferCanvas extends LeaferCanvasBase {
101
112
 
102
113
  public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
103
114
  if (!this.offscreen) {
104
- const view = this.view as HTMLCanvasElement
105
- const check = (parentSize: ISizeData) => {
106
- const { x, y, width, height } = autoBounds.getBoundsFrom(parentSize)
107
- const { style } = view
108
- style.marginLeft = x + 'px'
109
- style.marginTop = y + 'px'
110
-
111
- if (width !== this.width || height !== this.height) {
112
- const { pixelRatio } = this
113
- const size = { width, height, pixelRatio }
114
- const oldSize = { width: this.width, height: this.height, pixelRatio: this.pixelRatio }
115
- this.resize(size)
116
- if (this.width !== undefined) listener(new ResizeEvent(size, oldSize))
115
+ this.autoBounds = autoBounds
116
+ this.resizeListener = listener
117
+ try {
118
+
119
+ this.resizeObserver = new ResizeObserver((entries) => {
120
+ for (const entry of entries) this.checkAutoBounds(entry.contentRect)
121
+ })
122
+
123
+ const parent = this.parentView
124
+ if (parent) {
125
+ this.resizeObserver.observe(parent)
126
+ this.checkAutoBounds(parent.getBoundingClientRect())
117
127
  }
118
- }
119
128
 
120
- this.resizeObserver = new ResizeObserver((entries) => {
121
- for (const entry of entries) {
122
- check(entry.contentRect)
123
- }
124
- })
129
+ } catch (e) {
130
+
131
+ this.imitateResizeObserver()
125
132
 
126
- const parent = this.parentView
127
- if (parent) {
128
- this.resizeObserver.observe(parent)
129
- check(parent.getBoundingClientRect())
130
133
  }
131
134
  }
132
135
  }
133
136
 
137
+ protected imitateResizeObserver(): void {
138
+ if (this.autoLayout) {
139
+ if (this.parentView) this.checkAutoBounds(this.parentView.getBoundingClientRect())
140
+ window.requestAnimationFrame(this.imitateResizeObserver.bind(this))
141
+ }
142
+ }
143
+
144
+ protected checkAutoBounds(parentSize: ISizeData): void {
145
+ const view = this.view as HTMLCanvasElement
146
+ const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
147
+ if (width !== this.width || height !== this.height) {
148
+ const { style } = view
149
+ const { pixelRatio } = this
150
+ style.marginLeft = x + 'px'
151
+ style.marginTop = y + 'px'
152
+ const size = { width, height, pixelRatio }
153
+ const oldSize = {} as IScreenSizeData
154
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
155
+ this.resize(size)
156
+ if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
157
+ }
158
+ }
159
+
134
160
  public stopAutoLayout(): void {
161
+ this.autoLayout = false
162
+ this.resizeListener = null
135
163
  if (this.resizeObserver) {
136
164
  this.resizeObserver.disconnect()
137
165
  this.resizeObserver = null
138
- this.autoLayout = false
139
166
  }
140
167
  }
141
168