@leafer/canvas-web 1.0.0-beta.12 → 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.12",
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.12",
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"
24
+ "@leafer/core": "1.0.0-beta.15"
28
25
  },
29
26
  "devDependencies": {
30
- "@leafer/interface": "1.0.0-beta.12"
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,172 +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
- view ? this.__createViewFrom(view) : this.__createView()
24
- const { style } = this.view as HTMLCanvasElement
25
- style.display || (style.display = 'block')
26
- this.parentView = (this.view as HTMLCanvasElement).parentElement
27
-
28
- this.__createContext()
29
-
30
- if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
31
- }
32
-
33
- public set backgroundColor(color: string) { (this.view as HTMLElement).style.backgroundColor = color }
34
- public get backgroundColor(): string { return (this.view as HTMLElement).style.backgroundColor }
35
-
36
- public set hittable(hittable: boolean) { (this.view as HTMLElement).style.pointerEvents = hittable ? 'auto' : 'none' }
37
- public get hittable() { return (this.view as HTMLElement).style.pointerEvents !== 'none' }
38
-
39
- protected __createView(): void {
40
- this.view = document.createElement('canvas')
41
- }
42
-
43
- protected __createViewFrom(inputView: string | object): void {
44
- let find: unknown = (typeof inputView === 'string') ? document.getElementById(inputView) : inputView as HTMLElement
45
- if (find) {
46
- if (find instanceof HTMLCanvasElement) {
47
-
48
- this.view = find
49
-
50
- } else {
51
-
52
- let parent = find as HTMLDivElement
53
- if (find === window || find === document) {
54
- const div = document.createElement('div')
55
- const { style } = div
56
- style.position = 'absolute'
57
- style.top = style.bottom = style.left = style.right = '0px'
58
- document.body.appendChild(div)
59
- parent = div
60
- }
61
-
62
- this.__createView()
63
- const view = this.view as HTMLCanvasElement
64
-
65
- if (parent.hasChildNodes()) {
66
- const { style } = view
67
- style.position = 'absolute'
68
- style.top = style.left = '0px'
69
- parent.style.position || (parent.style.position = 'relative')
70
- }
71
-
72
- parent.appendChild(view)
73
- }
74
- } else {
75
- debug.error(`no id: ${inputView}`)
76
- this.__createView()
77
- }
78
- }
79
-
80
- public updateViewSize(): void {
81
- const { width, height, pixelRatio } = this
82
-
83
- const { style } = this.view as HTMLCanvasElement
84
- style.width = width + 'px'
85
- style.height = height + 'px'
86
-
87
- this.view.width = width * pixelRatio
88
- this.view.height = height * pixelRatio
89
-
90
- }
91
-
92
- public updateClientBounds(): void {
93
- this.clientBounds = (this.view as HTMLCanvasElement).getBoundingClientRect()
94
- }
95
-
96
- public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
97
- this.autoBounds = autoBounds
98
- this.resizeListener = listener
99
- try {
100
-
101
- this.resizeObserver = new ResizeObserver((entries) => {
102
- this.updateClientBounds()
103
- for (const entry of entries) this.checkAutoBounds(entry.contentRect)
104
- })
105
-
106
- const parent = this.parentView
107
- if (parent) {
108
- this.resizeObserver.observe(parent)
109
- this.checkAutoBounds(parent.getBoundingClientRect())
110
- }
111
-
112
- } catch (e) {
113
-
114
- this.imitateResizeObserver()
115
-
116
- }
117
- }
118
-
119
- protected imitateResizeObserver(): void {
120
- if (this.autoLayout) {
121
- if (this.parentView) this.checkAutoBounds(this.parentView.getBoundingClientRect())
122
- Platform.requestRender(this.imitateResizeObserver.bind(this))
123
- }
124
- }
125
-
126
- protected checkAutoBounds(parentSize: ISizeData): void {
127
- const view = this.view as HTMLCanvasElement
128
- const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
129
- if (width !== this.width || height !== this.height) {
130
- const { style } = view
131
- const { pixelRatio } = this
132
- style.marginLeft = x + 'px'
133
- style.marginTop = y + 'px'
134
- const size = { width, height, pixelRatio }
135
- const oldSize = {} as IScreenSizeData
136
- DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)
137
- this.resize(size)
138
- if (this.width !== undefined) this.resizeListener(new ResizeEvent(size, oldSize))
139
- }
140
- }
141
-
142
- public stopAutoLayout(): void {
143
- this.autoLayout = false
144
- this.resizeListener = null
145
- if (this.resizeObserver) {
146
- this.resizeObserver.disconnect()
147
- this.resizeObserver = null
148
- }
149
- }
150
-
151
- public unrealCanvas(): void { // App needs to use
152
- if (!this.unreal && this.parentView) {
153
- const view = this.view as HTMLCanvasElement
154
- if (view) view.remove()
155
-
156
- this.view = this.parentView as HTMLCanvasElement
157
- this.unreal = true
158
- }
159
- }
160
-
161
- public destroy(): void {
162
- if (this.view) {
163
- this.stopAutoLayout()
164
- if (!this.unreal) {
165
- const view = this.view as HTMLCanvasElement
166
- if (view.parentElement) view.remove()
167
- }
168
- super.destroy()
169
- }
170
- }
171
-
172
- }