@leafer/canvas-web 1.0.0-rc.9 → 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 +3 -3
- package/src/LeaferCanvas.ts +83 -51
- package/types/index.d.ts +5 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/canvas-web",
|
|
3
|
-
"version": "1.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.
|
|
25
|
+
"@leafer/core": "1.0.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@leafer/interface": "1.0.
|
|
28
|
+
"@leafer/interface": "1.0.1"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/src/LeaferCanvas.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener
|
|
2
|
-
import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug
|
|
1
|
+
import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
|
|
2
|
+
import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug } from '@leafer/core'
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
const debug = Debug.get('LeaferCanvas')
|
|
@@ -9,27 +9,51 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
9
9
|
declare public view: HTMLCanvasElement
|
|
10
10
|
declare public parentView: HTMLElement
|
|
11
11
|
|
|
12
|
+
public set zIndex(zIndex: number) {
|
|
13
|
+
const { style } = this.view
|
|
14
|
+
style.zIndex = zIndex as unknown as string
|
|
15
|
+
this.setAbsolute(this.view)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public set childIndex(index: number) {
|
|
19
|
+
const { view, parentView } = this
|
|
20
|
+
if (view && parentView) {
|
|
21
|
+
const beforeNode = parentView.children[index]
|
|
22
|
+
if (beforeNode) {
|
|
23
|
+
this.setAbsolute(beforeNode as HTMLCanvasElement)
|
|
24
|
+
parentView.insertBefore(view, beforeNode)
|
|
25
|
+
} else {
|
|
26
|
+
parentView.appendChild(beforeNode)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
12
31
|
protected resizeObserver: ResizeObserver
|
|
13
32
|
protected autoBounds: IAutoBounds
|
|
14
33
|
protected resizeListener: IResizeEventListener
|
|
15
34
|
|
|
16
35
|
public init(): void {
|
|
17
|
-
const {
|
|
36
|
+
const { config } = this
|
|
37
|
+
const view = config.view || config.canvas
|
|
18
38
|
|
|
19
39
|
view ? this.__createViewFrom(view) : this.__createView()
|
|
20
40
|
const { style } = this.view
|
|
21
41
|
style.display || (style.display = 'block')
|
|
22
42
|
this.parentView = this.view.parentElement
|
|
23
|
-
|
|
43
|
+
|
|
44
|
+
if (this.parentView) {
|
|
45
|
+
const pStyle = this.parentView.style
|
|
46
|
+
pStyle.webkitUserSelect = pStyle.userSelect = 'none' // fix safari: use webkitUserSelect
|
|
47
|
+
}
|
|
24
48
|
|
|
25
49
|
if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
|
|
26
|
-
|
|
50
|
+
style.display = 'none'
|
|
27
51
|
document.body.appendChild(this.view)
|
|
28
52
|
}
|
|
29
53
|
|
|
30
54
|
this.__createContext()
|
|
31
55
|
|
|
32
|
-
if (!this.autoLayout) this.resize(
|
|
56
|
+
if (!this.autoLayout) this.resize(config as IScreenSizeData)
|
|
33
57
|
}
|
|
34
58
|
|
|
35
59
|
public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
|
|
@@ -42,27 +66,6 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
42
66
|
this.view = document.createElement('canvas')
|
|
43
67
|
}
|
|
44
68
|
|
|
45
|
-
public setCursor(cursor: ICursorType | ICursorType[]): void {
|
|
46
|
-
const list: ICursorType[] = []
|
|
47
|
-
this.eachCursor(cursor, list)
|
|
48
|
-
if (typeof list[list.length - 1] === 'object') list.push('default')
|
|
49
|
-
this.view.style.cursor = list.map(item => (typeof item === 'object') ? `url(${item.url}) ${item.x || 0} ${item.y || 0}` : item).join(',')
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level = 0): void {
|
|
53
|
-
level++
|
|
54
|
-
if (cursor instanceof Array) {
|
|
55
|
-
cursor.forEach(item => this.eachCursor(item, list, level))
|
|
56
|
-
} else {
|
|
57
|
-
const custom = typeof cursor === 'string' && Cursor.get(cursor)
|
|
58
|
-
if (custom && level < 2) {
|
|
59
|
-
this.eachCursor(custom, list, level)
|
|
60
|
-
} else {
|
|
61
|
-
list.push(cursor)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
69
|
protected __createViewFrom(inputView: string | object): void {
|
|
67
70
|
let find: unknown = (typeof inputView === 'string') ? document.getElementById(inputView) : inputView as HTMLElement
|
|
68
71
|
if (find) {
|
|
@@ -86,9 +89,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
86
89
|
const view = this.view
|
|
87
90
|
|
|
88
91
|
if (parent.hasChildNodes()) {
|
|
89
|
-
|
|
90
|
-
style.position = 'absolute'
|
|
91
|
-
style.top = style.left = '0px'
|
|
92
|
+
this.setAbsolute(view)
|
|
92
93
|
parent.style.position || (parent.style.position = 'relative')
|
|
93
94
|
}
|
|
94
95
|
|
|
@@ -100,6 +101,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
protected setAbsolute(view: HTMLCanvasElement): void {
|
|
105
|
+
const { style } = view
|
|
106
|
+
style.position = 'absolute'
|
|
107
|
+
style.top = style.left = '0px'
|
|
108
|
+
}
|
|
109
|
+
|
|
103
110
|
public updateViewSize(): void {
|
|
104
111
|
const { width, height, pixelRatio } = this
|
|
105
112
|
|
|
@@ -107,8 +114,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
107
114
|
style.width = width + 'px'
|
|
108
115
|
style.height = height + 'px'
|
|
109
116
|
|
|
110
|
-
this.view.width = width * pixelRatio
|
|
111
|
-
this.view.height = height * pixelRatio
|
|
117
|
+
this.view.width = Math.ceil(width * pixelRatio)
|
|
118
|
+
this.view.height = Math.ceil(height * pixelRatio)
|
|
112
119
|
}
|
|
113
120
|
|
|
114
121
|
public updateClientBounds(): void {
|
|
@@ -116,26 +123,47 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
116
123
|
}
|
|
117
124
|
|
|
118
125
|
public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
|
|
119
|
-
this.autoBounds = autoBounds
|
|
120
126
|
this.resizeListener = listener
|
|
121
|
-
try {
|
|
122
127
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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()
|
|
127
151
|
|
|
128
|
-
const parent = this.parentView
|
|
129
|
-
if (parent) {
|
|
130
|
-
this.resizeObserver.observe(parent)
|
|
131
|
-
this.checkAutoBounds(parent.getBoundingClientRect())
|
|
132
152
|
}
|
|
133
153
|
|
|
134
|
-
}
|
|
154
|
+
} else {
|
|
135
155
|
|
|
136
|
-
|
|
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
|
+
})
|
|
137
164
|
|
|
138
165
|
}
|
|
166
|
+
|
|
139
167
|
}
|
|
140
168
|
|
|
141
169
|
protected imitateResizeObserver(): void {
|
|
@@ -148,16 +176,12 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
148
176
|
protected checkAutoBounds(parentSize: ISizeData): void {
|
|
149
177
|
const view = this.view
|
|
150
178
|
const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
|
|
151
|
-
|
|
179
|
+
const size = { width, height, pixelRatio: Platform.devicePixelRatio } as IScreenSizeData
|
|
180
|
+
if (!this.isSameSize(size)) {
|
|
152
181
|
const { style } = view
|
|
153
|
-
const { pixelRatio } = this
|
|
154
182
|
style.marginLeft = x + 'px'
|
|
155
183
|
style.marginTop = y + 'px'
|
|
156
|
-
|
|
157
|
-
const oldSize = {} as IScreenSizeData
|
|
158
|
-
DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
|
|
159
|
-
this.resize(size)
|
|
160
|
-
if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
|
|
184
|
+
this.emitResize(size)
|
|
161
185
|
}
|
|
162
186
|
}
|
|
163
187
|
|
|
@@ -170,6 +194,14 @@ export class LeaferCanvas extends LeaferCanvasBase {
|
|
|
170
194
|
}
|
|
171
195
|
}
|
|
172
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
|
+
|
|
173
205
|
public unrealCanvas(): void { // App needs to use
|
|
174
206
|
if (!this.unreal && this.parentView) {
|
|
175
207
|
const view = this.view
|
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { IAutoBounds, IResizeEventListener,
|
|
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 {
|
|
5
5
|
view: HTMLCanvasElement;
|
|
6
6
|
parentView: HTMLElement;
|
|
7
|
+
set zIndex(zIndex: number);
|
|
8
|
+
set childIndex(index: number);
|
|
7
9
|
protected resizeObserver: ResizeObserver;
|
|
8
10
|
protected autoBounds: IAutoBounds;
|
|
9
11
|
protected resizeListener: IResizeEventListener;
|
|
@@ -13,15 +15,15 @@ declare class LeaferCanvas extends LeaferCanvasBase {
|
|
|
13
15
|
set hittable(hittable: boolean);
|
|
14
16
|
get hittable(): boolean;
|
|
15
17
|
protected __createView(): void;
|
|
16
|
-
setCursor(cursor: ICursorType | ICursorType[]): void;
|
|
17
|
-
protected eachCursor(cursor: ICursorType | ICursorType[], list: ICursorType[], level?: number): void;
|
|
18
18
|
protected __createViewFrom(inputView: string | object): void;
|
|
19
|
+
protected setAbsolute(view: HTMLCanvasElement): void;
|
|
19
20
|
updateViewSize(): void;
|
|
20
21
|
updateClientBounds(): void;
|
|
21
22
|
startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
|
|
22
23
|
protected imitateResizeObserver(): void;
|
|
23
24
|
protected checkAutoBounds(parentSize: ISizeData): void;
|
|
24
25
|
stopAutoLayout(): void;
|
|
26
|
+
protected emitResize(size: IScreenSizeData): void;
|
|
25
27
|
unrealCanvas(): void;
|
|
26
28
|
destroy(): void;
|
|
27
29
|
}
|