@leafer/canvas-web 1.0.0-beta.12 → 1.0.0-beta.16
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 -21
- package/src/index.ts +1 -1
- package/types/index.d.ts +29 -0
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/canvas-web",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.16",
|
|
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.12",
|
|
24
|
-
"@leafer/data": "1.0.0-beta.12",
|
|
25
|
-
"@leafer/event": "1.0.0-beta.12",
|
|
26
|
-
"@leafer/platform": "1.0.0-beta.12",
|
|
27
|
-
"@leafer/debug": "1.0.0-beta.12"
|
|
25
|
+
"@leafer/core": "1.0.0-beta.16"
|
|
28
26
|
},
|
|
29
27
|
"devDependencies": {
|
|
30
|
-
"@leafer/interface": "1.0.0-beta.
|
|
28
|
+
"@leafer/interface": "1.0.0-beta.16"
|
|
31
29
|
}
|
|
32
30
|
}
|
package/src/LeaferCanvas.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
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'
|
|
1
|
+
import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener, ICursorType } from '@leafer/interface'
|
|
2
|
+
import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug, Cursor } 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
|
|
@@ -21,25 +17,51 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
21
17
|
const { view } = this.config
|
|
22
18
|
|
|
23
19
|
view ? this.__createViewFrom(view) : this.__createView()
|
|
24
|
-
const { style } = this.view
|
|
20
|
+
const { style } = this.view
|
|
25
21
|
style.display || (style.display = 'block')
|
|
26
|
-
this.parentView =
|
|
22
|
+
this.parentView = this.view.parentElement
|
|
23
|
+
|
|
24
|
+
if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
|
|
25
|
+
this.view.style.display = 'none'
|
|
26
|
+
document.body.appendChild(this.view)
|
|
27
|
+
}
|
|
27
28
|
|
|
28
29
|
this.__createContext()
|
|
29
30
|
|
|
30
31
|
if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
public set backgroundColor(color: string) {
|
|
34
|
-
public get backgroundColor(): string { return
|
|
34
|
+
public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
|
|
35
|
+
public get backgroundColor(): string { return this.view.style.backgroundColor }
|
|
35
36
|
|
|
36
|
-
public set hittable(hittable: boolean) {
|
|
37
|
-
public get hittable() { return
|
|
37
|
+
public set hittable(hittable: boolean) { this.view.style.pointerEvents = hittable ? 'auto' : 'none' }
|
|
38
|
+
public get hittable() { return this.view.style.pointerEvents !== 'none' }
|
|
38
39
|
|
|
39
40
|
protected __createView(): void {
|
|
40
41
|
this.view = document.createElement('canvas')
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
public setCursor(cursor: ICursorType | ICursorType[]): void {
|
|
45
|
+
const list: ICursorType[] = []
|
|
46
|
+
this.eachCursor(cursor, list)
|
|
47
|
+
if (typeof list[list.length - 1] === 'object') list.push('default')
|
|
48
|
+
this.view.style.cursor = list.map(item => (typeof item === 'object') ? `url(${item.url}) ${item.x || 0} ${item.y || 0}` : item).join(',')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level = 0): void {
|
|
52
|
+
level++
|
|
53
|
+
if (cursor instanceof Array) {
|
|
54
|
+
cursor.forEach(item => this.eachCursor(item, list, level))
|
|
55
|
+
} else {
|
|
56
|
+
const custom = typeof cursor === 'string' && Cursor.get(cursor)
|
|
57
|
+
if (custom && level < 2) {
|
|
58
|
+
this.eachCursor(custom, list, level)
|
|
59
|
+
} else {
|
|
60
|
+
list.push(cursor)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
43
65
|
protected __createViewFrom(inputView: string | object): void {
|
|
44
66
|
let find: unknown = (typeof inputView === 'string') ? document.getElementById(inputView) : inputView as HTMLElement
|
|
45
67
|
if (find) {
|
|
@@ -60,7 +82,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
60
82
|
}
|
|
61
83
|
|
|
62
84
|
this.__createView()
|
|
63
|
-
const view = this.view
|
|
85
|
+
const view = this.view
|
|
64
86
|
|
|
65
87
|
if (parent.hasChildNodes()) {
|
|
66
88
|
const { style } = view
|
|
@@ -80,7 +102,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
80
102
|
public updateViewSize(): void {
|
|
81
103
|
const { width, height, pixelRatio } = this
|
|
82
104
|
|
|
83
|
-
const { style } = this.view
|
|
105
|
+
const { style } = this.view
|
|
84
106
|
style.width = width + 'px'
|
|
85
107
|
style.height = height + 'px'
|
|
86
108
|
|
|
@@ -90,7 +112,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
90
112
|
}
|
|
91
113
|
|
|
92
114
|
public updateClientBounds(): void {
|
|
93
|
-
this.clientBounds =
|
|
115
|
+
this.clientBounds = this.view.getBoundingClientRect()
|
|
94
116
|
}
|
|
95
117
|
|
|
96
118
|
public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
|
|
@@ -109,7 +131,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
109
131
|
this.checkAutoBounds(parent.getBoundingClientRect())
|
|
110
132
|
}
|
|
111
133
|
|
|
112
|
-
} catch
|
|
134
|
+
} catch {
|
|
113
135
|
|
|
114
136
|
this.imitateResizeObserver()
|
|
115
137
|
|
|
@@ -124,7 +146,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
124
146
|
}
|
|
125
147
|
|
|
126
148
|
protected checkAutoBounds(parentSize: ISizeData): void {
|
|
127
|
-
const view = this.view
|
|
149
|
+
const view = this.view
|
|
128
150
|
const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
|
|
129
151
|
if (width !== this.width || height !== this.height) {
|
|
130
152
|
const { style } = view
|
|
@@ -150,7 +172,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
150
172
|
|
|
151
173
|
public unrealCanvas(): void { // App needs to use
|
|
152
174
|
if (!this.unreal && this.parentView) {
|
|
153
|
-
const view = this.view
|
|
175
|
+
const view = this.view
|
|
154
176
|
if (view) view.remove()
|
|
155
177
|
|
|
156
178
|
this.view = this.parentView as HTMLCanvasElement
|
|
@@ -162,7 +184,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
162
184
|
if (this.view) {
|
|
163
185
|
this.stopAutoLayout()
|
|
164
186
|
if (!this.unreal) {
|
|
165
|
-
const view = this.view
|
|
187
|
+
const view = this.view
|
|
166
188
|
if (view.parentElement) view.remove()
|
|
167
189
|
}
|
|
168
190
|
super.destroy()
|
package/src/index.ts
CHANGED
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { IAutoBounds, IResizeEventListener, ICursorType, 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
|
+
setCursor(cursor: ICursorType | ICursorType[]): void;
|
|
17
|
+
protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level?: number): void;
|
|
18
|
+
protected __createViewFrom(inputView: string | object): void;
|
|
19
|
+
updateViewSize(): void;
|
|
20
|
+
updateClientBounds(): void;
|
|
21
|
+
startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
|
|
22
|
+
protected imitateResizeObserver(): void;
|
|
23
|
+
protected checkAutoBounds(parentSize: ISizeData): void;
|
|
24
|
+
stopAutoLayout(): void;
|
|
25
|
+
unrealCanvas(): void;
|
|
26
|
+
destroy(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { LeaferCanvas };
|