@leafer/canvas-web 1.0.0-beta.11 → 1.0.0-beta.15

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 CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@leafer/canvas-web",
3
- "version": "1.0.0-beta.11",
3
+ "version": "1.0.0-beta.15",
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
+ "types",
11
+ "dist"
10
12
  ],
11
13
  "repository": {
12
14
  "type": "git",
@@ -19,14 +21,9 @@
19
21
  "leaferjs"
20
22
  ],
21
23
  "dependencies": {
22
- "@leafer/canvas": "1.0.0-beta.11",
23
- "@leafer/math": "1.0.0-beta.11",
24
- "@leafer/data": "1.0.0-beta.11",
25
- "@leafer/event": "1.0.0-beta.11",
26
- "@leafer/platform": "1.0.0-beta.11",
27
- "@leafer/debug": "1.0.0-beta.11"
24
+ "@leafer/core": "1.0.0-beta.15"
28
25
  },
29
26
  "devDependencies": {
30
- "@leafer/interface": "1.0.0-beta.11"
27
+ "@leafer/interface": "1.0.0-beta.15"
31
28
  }
32
29
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { canvasPatch } from '@leafer/canvas'
1
+ import { canvasPatch } from '@leafer/core'
2
2
 
3
3
  export { LeaferCanvas } from './LeaferCanvas'
4
4
 
@@ -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 };
@@ -1,188 +0,0 @@
1
- import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
2
- import { LeaferCanvasBase, canvasSizeAttrs } from '@leafer/canvas'
3
- import { ResizeEvent } from '@leafer/event'
4
- import { DataHelper } from '@leafer/data'
5
- import { Platform } from '@leafer/platform'
6
- import { Debug } from '@leafer/debug'
7
-
8
-
9
- const debug = Debug.get('LeaferCanvas')
10
-
11
- export class LeaferCanvas extends LeaferCanvasBase {
12
-
13
- public view: HTMLCanvasElement | OffscreenCanvas
14
- public parentView: HTMLElement
15
-
16
- protected resizeObserver: ResizeObserver
17
- protected autoBounds: IAutoBounds
18
- protected resizeListener: IResizeEventListener
19
-
20
- public init(): void {
21
- const { view } = this.config
22
-
23
- if (this.offscreen) {
24
- view ? this.view = view as OffscreenCanvas : this.__createView()
25
- } else {
26
- view ? this.__createViewFrom(view) : this.__createView()
27
- const { style } = this.view as HTMLCanvasElement
28
- style.display || (style.display = 'block')
29
- this.parentView = (this.view as HTMLCanvasElement).parentElement
30
- }
31
-
32
- this.__createContext()
33
-
34
- if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
35
- }
36
-
37
- public set backgroundColor(color: string) { (this.view as HTMLElement).style.backgroundColor = color }
38
- public get backgroundColor(): string { return (this.view as HTMLElement).style.backgroundColor }
39
-
40
- public set hittable(hittable: boolean) { (this.view as HTMLElement).style.pointerEvents = hittable ? 'auto' : 'none' }
41
- public get hittable() { return (this.view as HTMLElement).style.pointerEvents !== 'none' }
42
-
43
- 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
- this.view = document.createElement('canvas')
53
- }
54
-
55
- protected __createViewFrom(inputView: string | object): void {
56
- let find: unknown = (typeof inputView === 'string') ? document.getElementById(inputView) : inputView as HTMLElement
57
- if (find) {
58
- if (find instanceof HTMLCanvasElement) {
59
-
60
- this.view = find
61
-
62
- } else {
63
-
64
- let parent = find as HTMLDivElement
65
- if (find === window || find === document) {
66
- const div = document.createElement('div')
67
- const { style } = div
68
- style.position = 'absolute'
69
- style.top = style.bottom = style.left = style.right = '0px'
70
- document.body.appendChild(div)
71
- parent = div
72
- }
73
-
74
- this.__createView()
75
- const view = this.view as HTMLCanvasElement
76
-
77
- if (parent.hasChildNodes()) {
78
- const { style } = view
79
- style.position = 'absolute'
80
- style.top = style.left = '0px'
81
- parent.style.position || (parent.style.position = 'relative')
82
- }
83
-
84
- parent.appendChild(view)
85
- }
86
- } else {
87
- debug.error(`no id: ${inputView}`)
88
- this.__createView()
89
- }
90
- }
91
-
92
- public updateViewSize(): void {
93
- const { width, height, pixelRatio } = this
94
-
95
- if (!this.offscreen) {
96
- const { style } = this.view as HTMLCanvasElement
97
- style.width = width + 'px'
98
- style.height = height + 'px'
99
- }
100
-
101
- this.view.width = width * pixelRatio
102
- this.view.height = height * pixelRatio
103
-
104
- }
105
-
106
- public updateClientBounds(): void {
107
- if (!this.offscreen) this.clientBounds = (this.view as HTMLCanvasElement).getBoundingClientRect()
108
- }
109
-
110
- public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
111
- if (!this.offscreen) {
112
- this.autoBounds = autoBounds
113
- this.resizeListener = listener
114
- try {
115
-
116
- this.resizeObserver = new ResizeObserver((entries) => {
117
- this.updateClientBounds()
118
- for (const entry of entries) this.checkAutoBounds(entry.contentRect)
119
- })
120
-
121
- const parent = this.parentView
122
- if (parent) {
123
- this.resizeObserver.observe(parent)
124
- this.checkAutoBounds(parent.getBoundingClientRect())
125
- }
126
-
127
- } catch (e) {
128
-
129
- this.imitateResizeObserver()
130
-
131
- }
132
- }
133
- }
134
-
135
- protected imitateResizeObserver(): void {
136
- if (this.autoLayout) {
137
- if (this.parentView) this.checkAutoBounds(this.parentView.getBoundingClientRect())
138
- Platform.requestRender(this.imitateResizeObserver.bind(this))
139
- }
140
- }
141
-
142
- protected checkAutoBounds(parentSize: ISizeData): void {
143
- const view = this.view as HTMLCanvasElement
144
- const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
145
- if (width !== this.width || height !== this.height) {
146
- const { style } = view
147
- const { pixelRatio } = this
148
- style.marginLeft = x + 'px'
149
- style.marginTop = y + 'px'
150
- const size = { width, height, pixelRatio }
151
- const oldSize = {} as IScreenSizeData
152
- DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
153
- this.resize(size)
154
- if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
155
- }
156
- }
157
-
158
- public stopAutoLayout(): void {
159
- this.autoLayout = false
160
- this.resizeListener = null
161
- if (this.resizeObserver) {
162
- this.resizeObserver.disconnect()
163
- this.resizeObserver = null
164
- }
165
- }
166
-
167
- public unrealCanvas(): void { // App needs to use
168
- if (!this.unreal && this.parentView) {
169
- const view = this.view as HTMLCanvasElement
170
- if (view) view.remove()
171
-
172
- this.view = this.parentView as HTMLCanvasElement
173
- this.unreal = true
174
- }
175
- }
176
-
177
- public destroy(): void {
178
- if (this.view) {
179
- this.stopAutoLayout()
180
- if (!this.unreal && !this.offscreen) {
181
- const view = this.view as HTMLCanvasElement
182
- if (view.parentElement) view.remove()
183
- }
184
- super.destroy()
185
- }
186
- }
187
-
188
- }