@leafer/canvas 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",
3
- "version": "1.0.0-beta",
3
+ "version": "1.0.0-beta.10",
4
4
  "description": "@leafer/canvas",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,12 +19,15 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/list": "1.0.0-beta",
23
- "@leafer/math": "1.0.0-beta",
24
- "@leafer/data": "1.0.0-beta",
25
- "@leafer/platform": "1.0.0-beta"
22
+ "@leafer/file": "1.0.0-beta.10",
23
+ "@leafer/list": "1.0.0-beta.10",
24
+ "@leafer/math": "1.0.0-beta.10",
25
+ "@leafer/data": "1.0.0-beta.10",
26
+ "@leafer/path": "1.0.0-beta.10",
27
+ "@leafer/debug": "1.0.0-beta.10",
28
+ "@leafer/platform": "1.0.0-beta.10"
26
29
  },
27
30
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-beta"
31
+ "@leafer/interface": "1.0.0-beta.10"
29
32
  }
30
33
  }
package/src/Canvas.ts CHANGED
@@ -136,6 +136,7 @@ export class Canvas implements ICanvasAttr {
136
136
  method = (this.context as IObject)[name]
137
137
  if (method) (this as IObject)[name] = method.bind(this.context)
138
138
  })
139
+ this.textBaseline = "alphabetic"
139
140
  }
140
141
 
141
142
  // canvas method
@@ -155,8 +156,13 @@ export class Canvas implements ICanvasAttr {
155
156
  @contextMethod()
156
157
  public restore(): void { }
157
158
 
158
- @contextMethod()
159
- public transform(_a: number, _b: number, _c: number, _d: number, _e: number, _f: number): void { }
159
+ public transform(a: number | IMatrixData, b?: number, c?: number, d?: number, e?: number, f?: number): void {
160
+ if (typeof a === 'object') {
161
+ this.context.transform(a.a, a.b, a.c, a.d, a.e, a.f)
162
+ } else {
163
+ this.context.transform(a, b, c, d, e, f)
164
+ }
165
+ }
160
166
 
161
167
  @contextMethod()
162
168
  public translate(_x: number, _y: number): void { }
@@ -49,7 +49,7 @@ export class CanvasManager implements ICanvasManager {
49
49
  this.list.length = 0
50
50
  }
51
51
 
52
- public destory(): void {
52
+ public destroy(): void {
53
53
  this.clear()
54
54
  }
55
55
 
@@ -1,13 +1,16 @@
1
- import { IBounds, ILeaferCanvas, ICanvasStrokeOptions, ILeaferCanvasConfig, IMatrixData, IBoundsData, IAutoBounds, IScreenSizeData, IResizeEventListener, IMatrixWithBoundsData, IPointData, InnerId, ICanvasManager, IWindingRule, IBlendMode } from '@leafer/interface'
1
+ import { IBounds, ILeaferCanvas, ICanvasStrokeOptions, ILeaferCanvasConfig, IMatrixData, IBoundsData, IAutoBounds, IScreenSizeData, IResizeEventListener, IMatrixWithBoundsData, IPointData, InnerId, ICanvasManager, IWindingRule, IBlendMode, IExportImageType, IExportFileType, IBlob } from '@leafer/interface'
2
2
  import { Bounds, BoundsHelper, IncrementId } from '@leafer/math'
3
3
  import { Creator, Platform } from '@leafer/platform'
4
4
  import { DataHelper } from '@leafer/data'
5
+ import { FileHelper } from '@leafer/file'
6
+ import { Debug } from '@leafer/debug'
5
7
 
6
8
  import { Canvas } from './Canvas'
7
9
 
8
10
 
9
11
  const temp = new Bounds()
10
12
  const minSize: IScreenSizeData = { width: 1, height: 1, pixelRatio: 1 }
13
+ const debug = Debug.get('LeaferCanvasBase')
11
14
 
12
15
  export const canvasSizeAttrs = ['width', 'height', 'pixelRatio']
13
16
 
@@ -22,16 +25,18 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
22
25
  public pixelRatio: number
23
26
  public get pixelWidth(): number { return this.width * this.pixelRatio }
24
27
  public get pixelHeight(): number { return this.height * this.pixelRatio }
28
+
25
29
  public get allowBackgroundColor(): boolean { return this.view && this.parentView && !this.offscreen }
26
30
 
27
31
  public bounds: IBounds
32
+ public clientBounds: IBoundsData
28
33
 
29
34
  public config: ILeaferCanvasConfig
30
35
 
31
36
  public autoLayout: boolean
32
37
 
33
- public view: unknown
34
- public parentView: unknown
38
+ public view: any
39
+ public parentView: any
35
40
 
36
41
  public unreal?: boolean
37
42
 
@@ -59,19 +64,61 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
59
64
  this.config = config
60
65
 
61
66
  this.init()
62
-
63
- this.textBaseline = "alphabetic"
64
67
  }
65
68
 
66
69
  public init(): void { }
67
- public setBackgroundColor(_color: string): void { }
68
- public setHittable(_hittable: boolean): void { }
70
+
71
+ protected __createContext(): void {
72
+ this.context = this.view.getContext('2d')
73
+ this.__bindContext()
74
+ }
75
+
76
+ public toBlob(type?: IExportFileType, quality?: number): Promise<IBlob> {
77
+ return new Promise((resolve) => {
78
+ const canvas = this.getSaveCanvas(type)
79
+ Platform.origin.canvasToBolb(canvas.view, type, quality).then((blob) => {
80
+ canvas.recycle()
81
+ resolve(blob)
82
+ }).catch((e) => {
83
+ debug.error(e)
84
+ resolve(null)
85
+ })
86
+ })
87
+ }
88
+
89
+ public toDataURL(type?: IExportImageType, quality?: number): string {
90
+ const canvas = this.getSaveCanvas(type)
91
+ const data = Platform.origin.canvasToDataURL(canvas.view, type, quality)
92
+ canvas.recycle()
93
+ return data
94
+ }
95
+
96
+ public saveAs(filename: string, quality?: number): Promise<boolean> {
97
+ return new Promise((resolve) => {
98
+ const canvas = this.getSaveCanvas(FileHelper.fileType(filename))
99
+ Platform.origin.canvasSaveAs(canvas.view, filename, quality).then(() => {
100
+ canvas.recycle()
101
+ resolve(true)
102
+ }).catch((e) => {
103
+ debug.error(e)
104
+ resolve(false)
105
+ })
106
+ })
107
+ }
108
+
109
+ public getSaveCanvas(type: string): ILeaferCanvas {
110
+ const { backgroundColor, bounds } = this as ILeaferCanvas
111
+ const canvas = this.getSameCanvas()
112
+ if (['jpg', 'jpeg'].includes(type)) canvas.fillWorld(bounds, '#FFFFFF')
113
+ if (backgroundColor) canvas.fillWorld(bounds, backgroundColor)
114
+ canvas.copyWorld(this)
115
+ return canvas
116
+ }
117
+
69
118
 
70
119
  public resize(size: IScreenSizeData): void {
71
120
  if (this.isSameSize(size)) return
72
121
 
73
- const { width, height } = size
74
-
75
122
  let takeCanvas: ILeaferCanvas
76
123
  if (this.context && !this.unreal && this.width) {
77
124
  takeCanvas = this.getSameCanvas()
@@ -79,13 +126,16 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
79
126
  }
80
127
 
81
128
  DataHelper.copyAttrs(this, size, canvasSizeAttrs)
82
- this.bounds = new Bounds(0, 0, width, height)
129
+ this.bounds = new Bounds(0, 0, this.width, this.height)
130
+ this.pixelRatio || (this.pixelRatio = 1)
83
131
 
84
132
  if (!this.unreal) {
85
- this.setViewSize(size)
133
+ this.updateViewSize()
86
134
  this.smooth = this.config.smooth
87
135
  }
88
136
 
137
+ this.updateClientBounds()
138
+
89
139
  if (this.context && !this.unreal && takeCanvas) {
90
140
  this.clearWorld(takeCanvas.bounds)
91
141
  this.copyWorld(takeCanvas)
@@ -93,7 +143,9 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
93
143
  }
94
144
  }
95
145
 
96
- public setViewSize(_size: IScreenSizeData): void { }
146
+ public updateViewSize(): void { }
147
+ public updateClientBounds(): void { }
148
+
97
149
  public startAutoLayout(_autoBounds: IAutoBounds, _listener: IResizeEventListener): void { }
98
150
  public stopAutoLayout(): void { }
99
151
 
@@ -160,7 +212,7 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
160
212
  }
161
213
 
162
214
  public hitFill(point: IPointData, fillRule?: IWindingRule): boolean {
163
- return this.context.isPointInPath(point.x, point.y, fillRule)
215
+ return fillRule ? this.context.isPointInPath(point.x, point.y, fillRule) : this.context.isPointInPath(point.x, point.y)
164
216
  }
165
217
 
166
218
  public hitStroke(point: IPointData, strokeWidth?: number): boolean {
@@ -213,6 +265,10 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
213
265
  this.copyWorld(maskCanvas, fromBounds, toBounds, 'destination-in')
214
266
  }
215
267
 
268
+ public useEraser(eraserCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void {
269
+ this.copyWorld(eraserCanvas, fromBounds, toBounds, 'destination-out')
270
+ }
271
+
216
272
  public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
217
273
  if (blendMode) this.blendMode = blendMode
218
274
  this.fillStyle = color
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { CanvasManager } from './CanvasManager'
2
2
  export { HitCanvasManager } from './HitCanvasManager'
3
3
  export { LeaferCanvasBase, canvasSizeAttrs } from './LeaferCanvasBase'
4
-
4
+ export { canvasPatch } from './patch'
@@ -0,0 +1,7 @@
1
+ import { IPathDrawer } from '@leafer/interface'
2
+
3
+ import { roundRect } from './roundRect'
4
+
5
+ export function canvasPatch(drawer: IPathDrawer): void {
6
+ roundRect(drawer)
7
+ }
@@ -0,0 +1,18 @@
1
+ import { IPathDrawer } from '@leafer/interface'
2
+ import { RectHelper } from '@leafer/path'
3
+
4
+
5
+ const { drawRoundRect } = RectHelper
6
+
7
+ export function roundRect(drawer: IPathDrawer): void {
8
+
9
+ if (drawer && !drawer.roundRect) {
10
+
11
+ drawer.roundRect = function (x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void {
12
+
13
+ drawRoundRect(this as IPathDrawer, x, y, width, height, cornerRadius)
14
+ }
15
+ }
16
+
17
+ }
18
+