@leafer/canvas-web 1.0.0-beta → 1.0.0-beta.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/canvas-web",
3
- "version": "1.0.0-beta",
3
+ "version": "1.0.0-beta.10",
4
4
  "description": "@leafer/canvas-web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,13 +19,14 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/canvas": "1.0.0-beta",
23
- "@leafer/math": "1.0.0-beta",
24
- "@leafer/data": "1.0.0-beta",
25
- "@leafer/event": "1.0.0-beta",
26
- "@leafer/debug": "1.0.0-beta"
22
+ "@leafer/canvas": "1.0.0-beta.10",
23
+ "@leafer/math": "1.0.0-beta.10",
24
+ "@leafer/data": "1.0.0-beta.10",
25
+ "@leafer/event": "1.0.0-beta.10",
26
+ "@leafer/platform": "1.0.0-beta.10",
27
+ "@leafer/debug": "1.0.0-beta.10"
27
28
  },
28
29
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-beta"
30
+ "@leafer/interface": "1.0.0-beta.10"
30
31
  }
31
32
  }
@@ -1,7 +1,8 @@
1
- import { ICanvasContext2D, IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
1
+ import { IAutoBounds, ISizeData, IScreenSizeData, IResizeEventListener } from '@leafer/interface'
2
2
  import { LeaferCanvasBase, canvasSizeAttrs } from '@leafer/canvas'
3
3
  import { ResizeEvent } from '@leafer/event'
4
4
  import { DataHelper } from '@leafer/data'
5
+ import { Platform } from '@leafer/platform'
5
6
  import { Debug } from '@leafer/debug'
6
7
 
7
8
 
@@ -24,7 +25,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
24
25
  } else {
25
26
  view ? this.__createViewFrom(view) : this.__createView()
26
27
  const { style } = this.view as HTMLCanvasElement
27
- if (this.autoLayout) style.display || (style.display = 'block')
28
+ style.display || (style.display = 'block')
28
29
  this.parentView = (this.view as HTMLCanvasElement).parentElement
29
30
  }
30
31
 
@@ -33,20 +34,11 @@ export class LeaferCanvas extends LeaferCanvasBase {
33
34
  if (!this.autoLayout) this.resize(this.config as IScreenSizeData)
34
35
  }
35
36
 
36
- public setBackgroundColor(color: string): void {
37
- const view = this.view as HTMLElement
38
- view.style.backgroundColor = color
39
- }
40
-
41
- public setHittable(hittable: boolean): void {
42
- const view = this.view as HTMLElement
43
- view.style.pointerEvents = hittable ? 'auto' : 'none'
44
- }
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 }
45
39
 
46
- protected __createContext(): void {
47
- this.context = this.view.getContext('2d') as ICanvasContext2D
48
- this.__bindContext()
49
- }
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' }
50
42
 
51
43
  protected __createView(): void {
52
44
  if (this.offscreen) {
@@ -97,8 +89,8 @@ export class LeaferCanvas extends LeaferCanvasBase {
97
89
  }
98
90
  }
99
91
 
100
- public setViewSize(size: IScreenSizeData): void {
101
- const { width, height, pixelRatio } = size
92
+ public updateViewSize(): void {
93
+ const { width, height, pixelRatio } = this
102
94
 
103
95
  if (!this.offscreen) {
104
96
  const { style } = this.view as HTMLCanvasElement
@@ -108,6 +100,11 @@ export class LeaferCanvas extends LeaferCanvasBase {
108
100
 
109
101
  this.view.width = width * pixelRatio
110
102
  this.view.height = height * pixelRatio
103
+
104
+ }
105
+
106
+ public updateClientBounds(): void {
107
+ if (!this.offscreen) this.clientBounds = (this.view as HTMLCanvasElement).getBoundingClientRect()
111
108
  }
112
109
 
113
110
  public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {
@@ -117,6 +114,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
117
114
  try {
118
115
 
119
116
  this.resizeObserver = new ResizeObserver((entries) => {
117
+ this.updateClientBounds()
120
118
  for (const entry of entries) this.checkAutoBounds(entry.contentRect)
121
119
  })
122
120
 
@@ -137,7 +135,7 @@ export class LeaferCanvas extends LeaferCanvasBase {
137
135
  protected imitateResizeObserver(): void {
138
136
  if (this.autoLayout) {
139
137
  if (this.parentView) this.checkAutoBounds(this.parentView.getBoundingClientRect())
140
- window.requestAnimationFrame(this.imitateResizeObserver.bind(this))
138
+ Platform.requestRender(this.imitateResizeObserver.bind(this))
141
139
  }
142
140
  }
143
141
 
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { canvasPatch } from '@leafer/canvas'
2
+
1
3
  export { LeaferCanvas } from './LeaferCanvas'
2
4
 
3
- import { patch } from './patch'
4
- patch()
5
+ canvasPatch(CanvasRenderingContext2D.prototype)
6
+ canvasPatch(Path2D.prototype)
@@ -1,5 +0,0 @@
1
- import { roundRect } from './roundRect'
2
-
3
- export function patch(): void {
4
- roundRect()
5
- }
@@ -1,20 +0,0 @@
1
- import { IObject, IPathDrawer } from '@leafer/interface'
2
- import { RectHelper } from '@leafer/path'
3
-
4
-
5
- const { drawRoundRect } = RectHelper
6
-
7
- export function roundRect(): void {
8
-
9
- const canvas = CanvasRenderingContext2D.prototype as IPathDrawer
10
-
11
- if (!canvas.roundRect) {
12
-
13
- canvas.roundRect = (Path2D.prototype as IObject).roundRect = function (x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void {
14
-
15
- drawRoundRect(this as IPathDrawer, x, y, width, height, cornerRadius)
16
- }
17
- }
18
-
19
- }
20
-