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

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.10",
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.10"
28
26
  },
29
27
  "devDependencies": {
30
- "@leafer/interface": "1.0.0-beta.9"
28
+ "@leafer/interface": "1.0.0-rc.10"
31
29
  }
32
30
  }
@@ -1,17 +1,13 @@
1
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'
2
+ import { LeaferCanvasBase, canvasSizeAttrs, ResizeEvent, DataHelper, Platform, Debug } 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,15 @@ 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
+ if (this.parentView) this.parentView.style.userSelect = 'none'
24
+
25
+ if (Platform.syncDomFont && !this.parentView) { // fix: firefox default font
26
+ this.view.style.display = 'none'
27
+ document.body.appendChild(this.view)
30
28
  }
31
29
 
32
30
  this.__createContext()
@@ -34,21 +32,13 @@ export class LeaferCanvas extends LeaferCanvasBase {
34
32
  if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
35
33
  }
36
34
 
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 }
35
+ public set backgroundColor(color: string) { this.view.style.backgroundColor = color }
36
+ public get backgroundColor(): string { return this.view.style.backgroundColor }
39
37
 
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' }
38
+ public set hittable(hittable: boolean) { this.view.style.pointerEvents = hittable ? 'auto' : 'none' }
39
+ public get hittable() { return this.view.style.pointerEvents !== 'none' }
42
40
 
43
41
  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
42
  this.view = document.createElement('canvas')
53
43
  }
54
44
 
@@ -72,7 +62,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
72
62
  }
73
63
 
74
64
  this.__createView()
75
- const view = this.view as HTMLCanvasElement
65
+ const view = this.view
76
66
 
77
67
  if (parent.hasChildNodes()) {
78
68
  const { style } = view
@@ -92,43 +82,38 @@ export class LeaferCanvas extends LeaferCanvasBase {
92
82
  public updateViewSize(): void {
93
83
  const { width, height, pixelRatio } = this
94
84
 
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
85
+ const { style } = this.view
86
+ style.width = width + 'px'
87
+ style.height = height + 'px'
103
88
 
89
+ this.view.width = Math.ceil(width * pixelRatio)
90
+ this.view.height = Math.ceil(height * pixelRatio)
104
91
  }
105
92
 
106
93
  public updateClientBounds(): void {
107
- if (!this.offscreen) this.clientBounds = (this.view as HTMLCanvasElement).getBoundingClientRect()
94
+ this.clientBounds = this.view.getBoundingClientRect()
108
95
  }
109
96
 
110
97
  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
- }
98
+ this.autoBounds = autoBounds
99
+ this.resizeListener = listener
100
+ try {
101
+
102
+ this.resizeObserver = new ResizeObserver((entries) => {
103
+ this.updateClientBounds()
104
+ for (const entry of entries) this.checkAutoBounds(entry.contentRect)
105
+ })
106
+
107
+ const parent = this.parentView
108
+ if (parent) {
109
+ this.resizeObserver.observe(parent)
110
+ this.checkAutoBounds(parent.getBoundingClientRect())
111
+ }
126
112
 
127
- } catch (e) {
113
+ } catch {
128
114
 
129
- this.imitateResizeObserver()
115
+ this.imitateResizeObserver()
130
116
 
131
- }
132
117
  }
133
118
  }
134
119
 
@@ -140,7 +125,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
140
125
  }
141
126
 
142
127
  protected checkAutoBounds(parentSize: ISizeData): void {
143
- const view = this.view as HTMLCanvasElement
128
+ const view = this.view
144
129
  const { x, y, width, height } = this.autoBounds.getBoundsFrom(parentSize)
145
130
  if (width !== this.width || height !== this.height) {
146
131
  const { style } = view
@@ -166,7 +151,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
166
151
 
167
152
  public unrealCanvas(): void { // App needs to use
168
153
  if (!this.unreal && this.parentView) {
169
- const view = this.view as HTMLCanvasElement
154
+ const view = this.view
170
155
  if (view) view.remove()
171
156
 
172
157
  this.view = this.parentView as HTMLCanvasElement
@@ -177,8 +162,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
177
162
  public destroy(): void {
178
163
  if (this.view) {
179
164
  this.stopAutoLayout()
180
- if (!this.unreal && !this.offscreen) {
181
- const view = this.view as HTMLCanvasElement
165
+ if (!this.unreal) {
166
+ const view = this.view
182
167
  if (view.parentElement) view.remove()
183
168
  }
184
169
  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,27 @@
1
+ import { IAutoBounds, IResizeEventListener, 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
+ protected __createViewFrom(inputView: string | object): void;
17
+ updateViewSize(): void;
18
+ updateClientBounds(): void;
19
+ startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void;
20
+ protected imitateResizeObserver(): void;
21
+ protected checkAutoBounds(parentSize: ISizeData): void;
22
+ stopAutoLayout(): void;
23
+ unrealCanvas(): void;
24
+ destroy(): void;
25
+ }
26
+
27
+ export { LeaferCanvas };