@leafer/canvas-web 1.0.0-beta.9 → 1.0.0-rc.10
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 +7 -9
- package/src/LeaferCanvas.ts +43 -58
- package/src/index.ts +1 -1
- package/types/index.d.ts +27 -0
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/canvas-web",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
4
|
"description": "@leafer/canvas-web",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "src/index.ts",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
8
9
|
"files": [
|
|
9
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"types",
|
|
12
|
+
"dist"
|
|
10
13
|
],
|
|
11
14
|
"repository": {
|
|
12
15
|
"type": "git",
|
|
@@ -19,14 +22,9 @@
|
|
|
19
22
|
"leaferjs"
|
|
20
23
|
],
|
|
21
24
|
"dependencies": {
|
|
22
|
-
"@leafer/
|
|
23
|
-
"@leafer/math": "1.0.0-beta.9",
|
|
24
|
-
"@leafer/data": "1.0.0-beta.9",
|
|
25
|
-
"@leafer/event": "1.0.0-beta.9",
|
|
26
|
-
"@leafer/platform": "1.0.0-beta.9",
|
|
27
|
-
"@leafer/debug": "1.0.0-beta.9"
|
|
25
|
+
"@leafer/core": "1.0.0-rc.10"
|
|
28
26
|
},
|
|
29
27
|
"devDependencies": {
|
|
30
|
-
"@leafer/interface": "1.0.0-
|
|
28
|
+
"@leafer/interface": "1.0.0-rc.10"
|
|
31
29
|
}
|
|
32
30
|
}
|
package/src/LeaferCanvas.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
|
|
2
|
-
import { LeaferCanvasBase, canvasSizeAttrs } from '@leafer/
|
|
3
|
-
import { ResizeEvent } from '@leafer/event'
|
|
4
|
-
import { DataHelper } from '@leafer/data'
|
|
5
|
-
import { Platform } from '@leafer/platform'
|
|
6
|
-
import { Debug } from '@leafer/debug'
|
|
2
|
+
import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug } from '@leafer/core'
|
|
7
3
|
|
|
8
4
|
|
|
9
5
|
const debug = Debug.get('LeaferCanvas')
|
|
10
6
|
|
|
11
7
|
export class LeaferCanvas extends LeaferCanvasBase {
|
|
12
8
|
|
|
13
|
-
public view: HTMLCanvasElement
|
|
14
|
-
public parentView: HTMLElement
|
|
9
|
+
declare public view: HTMLCanvasElement
|
|
10
|
+
declare public parentView: HTMLElement
|
|
15
11
|
|
|
16
12
|
protected resizeObserver: ResizeObserver
|
|
17
13
|
protected autoBounds: IAutoBounds
|
|
@@ -20,13 +16,15 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
20
16
|
public init(): void {
|
|
21
17
|
const { view } = this.config
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
view ? this.__createViewFrom(view) : this.__createView()
|
|
20
|
+
const { style } = this.view
|
|
21
|
+
style.display || (style.display = 'block')
|
|
22
|
+
this.parentView = this.view.parentElement
|
|
23
|
+
if (this.parentView) this.parentView.style.userSelect = 'none'
|
|
24
|
+
|
|
25
|
+
if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
|
|
26
|
+
this.view.style.display = 'none'
|
|
27
|
+
document.body.appendChild(this.view)
|
|
30
28
|
}
|
|
31
29
|
|
|
32
30
|
this.__createContext()
|
|
@@ -34,21 +32,13 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
34
32
|
if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
public set backgroundColor(color: string) {
|
|
38
|
-
public get backgroundColor(): string { return
|
|
35
|
+
public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
|
|
36
|
+
public get backgroundColor(): string { return this.view.style.backgroundColor }
|
|
39
37
|
|
|
40
|
-
public set hittable(hittable: boolean) {
|
|
41
|
-
public get hittable() { return
|
|
38
|
+
public set hittable(hittable: boolean) { this.view.style.pointerEvents = hittable ? 'auto' : 'none' }
|
|
39
|
+
public get hittable() { return this.view.style.pointerEvents !== 'none' }
|
|
42
40
|
|
|
43
41
|
protected __createView(): void {
|
|
44
|
-
if (this.offscreen) {
|
|
45
|
-
try {
|
|
46
|
-
this.view = new OffscreenCanvas(1, 1)
|
|
47
|
-
return
|
|
48
|
-
} catch (e) {
|
|
49
|
-
debug.error(e)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
42
|
this.view = document.createElement('canvas')
|
|
53
43
|
}
|
|
54
44
|
|
|
@@ -72,7 +62,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
72
62
|
}
|
|
73
63
|
|
|
74
64
|
this.__createView()
|
|
75
|
-
const view = this.view
|
|
65
|
+
const view = this.view
|
|
76
66
|
|
|
77
67
|
if (parent.hasChildNodes()) {
|
|
78
68
|
const { style } = view
|
|
@@ -92,43 +82,38 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
92
82
|
public updateViewSize(): void {
|
|
93
83
|
const { width, height, pixelRatio } = this
|
|
94
84
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
style.height = height + 'px'
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
this.view.width = width * pixelRatio
|
|
102
|
-
this.view.height = height * pixelRatio
|
|
85
|
+
const { style } = this.view
|
|
86
|
+
style.width = width + 'px'
|
|
87
|
+
style.height = height + 'px'
|
|
103
88
|
|
|
89
|
+
this.view.width = Math.ceil(width * pixelRatio)
|
|
90
|
+
this.view.height = Math.ceil(height * pixelRatio)
|
|
104
91
|
}
|
|
105
92
|
|
|
106
93
|
public updateClientBounds(): void {
|
|
107
|
-
|
|
94
|
+
this.clientBounds = this.view.getBoundingClientRect()
|
|
108
95
|
}
|
|
109
96
|
|
|
110
97
|
public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
98
|
+
this.autoBounds = autoBounds
|
|
99
|
+
this.resizeListener = listener
|
|
100
|
+
try {
|
|
101
|
+
|
|
102
|
+
this.resizeObserver = new ResizeObserver((entries) => {
|
|
103
|
+
this.updateClientBounds()
|
|
104
|
+
for (const entry of entries) this.checkAutoBounds(entry.contentRect)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const parent = this.parentView
|
|
108
|
+
if (parent) {
|
|
109
|
+
this.resizeObserver.observe(parent)
|
|
110
|
+
this.checkAutoBounds(parent.getBoundingClientRect())
|
|
111
|
+
}
|
|
126
112
|
|
|
127
|
-
|
|
113
|
+
} catch {
|
|
128
114
|
|
|
129
|
-
|
|
115
|
+
this.imitateResizeObserver()
|
|
130
116
|
|
|
131
|
-
}
|
|
132
117
|
}
|
|
133
118
|
}
|
|
134
119
|
|
|
@@ -140,7 +125,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
140
125
|
}
|
|
141
126
|
|
|
142
127
|
protected checkAutoBounds(parentSize: ISizeData): void {
|
|
143
|
-
const view = this.view
|
|
128
|
+
const view = this.view
|
|
144
129
|
const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
|
|
145
130
|
if (width !== this.width || height !== this.height) {
|
|
146
131
|
const { style } = view
|
|
@@ -166,7 +151,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
166
151
|
|
|
167
152
|
public unrealCanvas(): void { // App needs to use
|
|
168
153
|
if (!this.unreal && this.parentView) {
|
|
169
|
-
const view = this.view
|
|
154
|
+
const view = this.view
|
|
170
155
|
if (view) view.remove()
|
|
171
156
|
|
|
172
157
|
this.view = this.parentView as HTMLCanvasElement
|
|
@@ -177,8 +162,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
177
162
|
public destroy(): void {
|
|
178
163
|
if (this.view) {
|
|
179
164
|
this.stopAutoLayout()
|
|
180
|
-
if (!this.unreal
|
|
181
|
-
const view = this.view
|
|
165
|
+
if (!this.unreal) {
|
|
166
|
+
const view = this.view
|
|
182
167
|
if (view.parentElement) view.remove()
|
|
183
168
|
}
|
|
184
169
|
super.destroy()
|
package/src/index.ts
CHANGED
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IAutoBounds, IResizeEventListener, ISizeData } from '@leafer/interface';
|
|
2
|
+
import { LeaferCanvasBase } from '@leafer/core';
|
|
3
|
+
|
|
4
|
+
declare class LeaferCanvas extends LeaferCanvasBase {
|
|
5
|
+
view: HTMLCanvasElement;
|
|
6
|
+
parentView: HTMLElement;
|
|
7
|
+
protected resizeObserver: ResizeObserver;
|
|
8
|
+
protected autoBounds: IAutoBounds;
|
|
9
|
+
protected resizeListener: IResizeEventListener;
|
|
10
|
+
init(): void;
|
|
11
|
+
set backgroundColor(color: string);
|
|
12
|
+
get backgroundColor(): string;
|
|
13
|
+
set hittable(hittable: boolean);
|
|
14
|
+
get hittable(): boolean;
|
|
15
|
+
protected __createView(): void;
|
|
16
|
+
protected __createViewFrom(inputView: string | object): void;
|
|
17
|
+
updateViewSize(): void;
|
|
18
|
+
updateClientBounds(): void;
|
|
19
|
+
startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
|
|
20
|
+
protected imitateResizeObserver(): void;
|
|
21
|
+
protected checkAutoBounds(parentSize: ISizeData): void;
|
|
22
|
+
stopAutoLayout(): void;
|
|
23
|
+
unrealCanvas(): void;
|
|
24
|
+
destroy(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { LeaferCanvas };
|