@leafer/canvas-web 1.0.0-beta.9 → 1.0.0-rc.2

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,15 @@
1
1
  {
2
2
  "name": "@leafer/canvas-web",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-rc.2",
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/canvas": "1.0.0-beta.9",
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.2"
28
26
  },
29
27
  "devDependencies": {
30
- "@leafer/interface": "1.0.0-beta.9"
28
+ "@leafer/interface": "1.0.0-rc.2"
31
29
  }
32
30
  }
@@ -1,17 +1,13 @@
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'
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 | OffscreenCanvas
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,14 @@ export class LeaferCanvas extends LeaferCanvasBase {
20
16
  public init(): void {
21
17
  const { view } = this.config
22
18
 
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
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
+
24
+ if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
25
+ this.view.style.display = 'none'
26
+ document.body.appendChild(this.view)
30
27
  }
31
28
 
32
29
  this.__createContext()
@@ -34,22 +31,35 @@ export class LeaferCanvas extends LeaferCanvasBase {
34
31
  if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
35
32
  }
36
33
 
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 }
34
+ public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
35
+ public get backgroundColor(): string { return this.view.style.backgroundColor }
39
36
 
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' }
37
+ public set hittable(hittable: boolean) { this.view.style.pointerEvents = hittable ? 'auto' : 'none' }
38
+ public get hittable() { return this.view.style.pointerEvents !== 'none' }
42
39
 
43
40
  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)
41
+ this.view = document.createElement('canvas')
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)
50
61
  }
51
62
  }
52
- this.view = document.createElement('canvas')
53
63
  }
54
64
 
55
65
  protected __createViewFrom(inputView: string | object): void {
@@ -72,7 +82,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
72
82
  }
73
83
 
74
84
  this.__createView()
75
- const view = this.view as HTMLCanvasElement
85
+ const view = this.view
76
86
 
77
87
  if (parent.hasChildNodes()) {
78
88
  const { style } = view
@@ -92,11 +102,9 @@ export class LeaferCanvas extends LeaferCanvasBase {
92
102
  public updateViewSize(): void {
93
103
  const { width, height, pixelRatio } = this
94
104
 
95
- if (!this.offscreen) {
96
- const { style } = this.view as HTMLCanvasElement
97
- style.width = width + 'px'
98
- style.height = height + 'px'
99
- }
105
+ const { style } = this.view
106
+ style.width = width + 'px'
107
+ style.height = height + 'px'
100
108
 
101
109
  this.view.width = width * pixelRatio
102
110
  this.view.height = height * pixelRatio
@@ -104,31 +112,29 @@ export class LeaferCanvas extends LeaferCanvasBase {
104
112
  }
105
113
 
106
114
  public updateClientBounds(): void {
107
- if (!this.offscreen) this.clientBounds = (this.view as HTMLCanvasElement).getBoundingClientRect()
115
+ this.clientBounds = this.view.getBoundingClientRect()
108
116
  }
109
117
 
110
118
  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
- }
119
+ this.autoBounds = autoBounds
120
+ this.resizeListener = listener
121
+ try {
122
+
123
+ this.resizeObserver = new ResizeObserver((entries) => {
124
+ this.updateClientBounds()
125
+ for (const entry of entries) this.checkAutoBounds(entry.contentRect)
126
+ })
127
+
128
+ const parent = this.parentView
129
+ if (parent) {
130
+ this.resizeObserver.observe(parent)
131
+ this.checkAutoBounds(parent.getBoundingClientRect())
132
+ }
126
133
 
127
- } catch (e) {
134
+ } catch {
128
135
 
129
- this.imitateResizeObserver()
136
+ this.imitateResizeObserver()
130
137
 
131
- }
132
138
  }
133
139
  }
134
140
 
@@ -140,7 +146,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
140
146
  }
141
147
 
142
148
  protected checkAutoBounds(parentSize: ISizeData): void {
143
- const view = this.view as HTMLCanvasElement
149
+ const view = this.view
144
150
  const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
145
151
  if (width !== this.width || height !== this.height) {
146
152
  const { style } = view
@@ -166,7 +172,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
166
172
 
167
173
  public unrealCanvas(): void { // App needs to use
168
174
  if (!this.unreal && this.parentView) {
169
- const view = this.view as HTMLCanvasElement
175
+ const view = this.view
170
176
  if (view) view.remove()
171
177
 
172
178
  this.view = this.parentView as HTMLCanvasElement
@@ -177,8 +183,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
177
183
  public destroy(): void {
178
184
  if (this.view) {
179
185
  this.stopAutoLayout()
180
- if (!this.unreal && !this.offscreen) {
181
- const view = this.view as HTMLCanvasElement
186
+ if (!this.unreal) {
187
+ const view = this.view
182
188
  if (view.parentElement) view.remove()
183
189
  }
184
190
  super.destroy()
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 };