@leafer/canvas-web 1.9.3 → 1.9.5
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 +3 -3
- package/src/LeaferCanvas.ts +55 -19
- package/types/index.d.ts +4 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/canvas-web",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.5",
|
|
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.9.
|
|
25
|
+
"@leafer/core": "1.9.5"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@leafer/interface": "1.9.
|
|
28
|
+
"@leafer/interface": "1.9.5"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/src/LeaferCanvas.ts
CHANGED
|
@@ -28,6 +28,10 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// CSS 原始自动宽高值
|
|
32
|
+
protected autoWidthStr: string
|
|
33
|
+
protected autoHeightStr: string
|
|
34
|
+
|
|
31
35
|
protected resizeObserver: ResizeObserver
|
|
32
36
|
protected autoBounds: IAutoBounds
|
|
33
37
|
protected resizeListener: IResizeEventListener
|
|
@@ -110,15 +114,33 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
110
114
|
|
|
111
115
|
public updateViewSize(): void {
|
|
112
116
|
const { width, height, pixelRatio } = this
|
|
113
|
-
|
|
114
117
|
const { style } = this.view
|
|
115
|
-
style.width = width + 'px'
|
|
116
|
-
style.height = height + 'px'
|
|
117
118
|
|
|
118
|
-
this.view
|
|
119
|
-
|
|
119
|
+
if (this.unreal) { // app 的 view 为 div 的情况
|
|
120
|
+
|
|
121
|
+
const { config, autoWidthStr, autoHeightStr } = this
|
|
122
|
+
if (config.width) {
|
|
123
|
+
if (isUndefined(autoWidthStr)) this.autoWidthStr = style.width || ''
|
|
124
|
+
style.width = config.width + 'px'
|
|
125
|
+
} else if (!isUndefined(autoWidthStr)) style.width = autoWidthStr
|
|
126
|
+
|
|
127
|
+
if (config.height) {
|
|
128
|
+
if (isUndefined(autoHeightStr)) this.autoHeightStr = style.height || ''
|
|
129
|
+
style.height = config.height + 'px'
|
|
130
|
+
} else if (!isUndefined(autoHeightStr)) style.height = autoHeightStr
|
|
131
|
+
|
|
132
|
+
} else {
|
|
133
|
+
|
|
134
|
+
style.width = width + 'px'
|
|
135
|
+
style.height = height + 'px'
|
|
136
|
+
|
|
137
|
+
this.view.width = Math.ceil(width * pixelRatio)
|
|
138
|
+
this.view.height = Math.ceil(height * pixelRatio)
|
|
139
|
+
|
|
140
|
+
}
|
|
120
141
|
}
|
|
121
142
|
|
|
143
|
+
|
|
122
144
|
public updateClientBounds(): void {
|
|
123
145
|
if (this.view.parentElement) this.clientBounds = this.view.getBoundingClientRect()
|
|
124
146
|
}
|
|
@@ -126,10 +148,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
126
148
|
public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
|
|
127
149
|
this.resizeListener = listener
|
|
128
150
|
|
|
129
|
-
if (autoBounds) {
|
|
151
|
+
if (autoBounds) { // check auto layout
|
|
130
152
|
|
|
131
|
-
// check auto layout
|
|
132
153
|
this.autoBounds = autoBounds
|
|
154
|
+
|
|
155
|
+
if (this.resizeObserver) return
|
|
156
|
+
|
|
133
157
|
try {
|
|
134
158
|
|
|
135
159
|
this.resizeObserver = new ResizeObserver((entries) => {
|
|
@@ -152,16 +176,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
152
176
|
|
|
153
177
|
}
|
|
154
178
|
|
|
179
|
+
this.stopListenPixelRatio()
|
|
180
|
+
|
|
155
181
|
} else {
|
|
156
182
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const pixelRatio = Platform.devicePixelRatio
|
|
160
|
-
if (!this.config.pixelRatio && this.pixelRatio !== pixelRatio) {
|
|
161
|
-
const { width, height } = this
|
|
162
|
-
this.emitResize({ width, height, pixelRatio })
|
|
163
|
-
}
|
|
164
|
-
})
|
|
183
|
+
this.listenPixelRatio()
|
|
184
|
+
if (this.unreal) this.updateViewSize() // must update
|
|
165
185
|
|
|
166
186
|
}
|
|
167
187
|
|
|
@@ -174,6 +194,25 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
174
194
|
}
|
|
175
195
|
}
|
|
176
196
|
|
|
197
|
+
// check devicePixelRatio change
|
|
198
|
+
|
|
199
|
+
protected listenPixelRatio() {
|
|
200
|
+
if (!this.windowListener) window.addEventListener('resize', this.windowListener = () => {
|
|
201
|
+
const pixelRatio = Platform.devicePixelRatio
|
|
202
|
+
if (!this.config.pixelRatio && this.pixelRatio !== pixelRatio) {
|
|
203
|
+
const { width, height } = this
|
|
204
|
+
this.emitResize({ width, height, pixelRatio })
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
protected stopListenPixelRatio() {
|
|
210
|
+
if (this.windowListener) {
|
|
211
|
+
window.removeEventListener('resize', this.windowListener)
|
|
212
|
+
this.windowListener = null
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
177
216
|
protected checkAutoBounds(parentSize: ISizeData): void {
|
|
178
217
|
const view = this.view
|
|
179
218
|
const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
|
|
@@ -213,10 +252,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
213
252
|
public destroy(): void {
|
|
214
253
|
if (this.view) {
|
|
215
254
|
this.stopAutoLayout()
|
|
216
|
-
|
|
217
|
-
window.removeEventListener('resize', this.windowListener)
|
|
218
|
-
this.windowListener = null
|
|
219
|
-
}
|
|
255
|
+
this.stopListenPixelRatio()
|
|
220
256
|
if (!this.unreal) {
|
|
221
257
|
const view = this.view
|
|
222
258
|
if (view.parentElement) view.remove()
|
package/types/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ declare class LeaferCanvas extends LeaferCanvasBase {
|
|
|
6
6
|
parentView: HTMLElement;
|
|
7
7
|
set zIndex(zIndex: number);
|
|
8
8
|
set childIndex(index: number);
|
|
9
|
+
protected autoWidthStr: string;
|
|
10
|
+
protected autoHeightStr: string;
|
|
9
11
|
protected resizeObserver: ResizeObserver;
|
|
10
12
|
protected autoBounds: IAutoBounds;
|
|
11
13
|
protected resizeListener: IResizeEventListener;
|
|
@@ -22,6 +24,8 @@ declare class LeaferCanvas extends LeaferCanvasBase {
|
|
|
22
24
|
updateClientBounds(): void;
|
|
23
25
|
startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
|
|
24
26
|
protected imitateResizeObserver(): void;
|
|
27
|
+
protected listenPixelRatio(): void;
|
|
28
|
+
protected stopListenPixelRatio(): void;
|
|
25
29
|
protected checkAutoBounds(parentSize: ISizeData): void;
|
|
26
30
|
stopAutoLayout(): void;
|
|
27
31
|
protected emitResize(size: IScreenSizeData): void;
|