@leafer/canvas 1.0.0-beta.2 → 1.0.0-beta.5

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.2",
3
+ "version": "1.0.0-beta.5",
4
4
  "description": "@leafer/canvas",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,12 +19,12 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/list": "1.0.0-beta.2",
23
- "@leafer/math": "1.0.0-beta.2",
24
- "@leafer/data": "1.0.0-beta.2",
25
- "@leafer/platform": "1.0.0-beta.2"
22
+ "@leafer/list": "1.0.0-beta.5",
23
+ "@leafer/math": "1.0.0-beta.5",
24
+ "@leafer/data": "1.0.0-beta.5",
25
+ "@leafer/platform": "1.0.0-beta.5"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-beta.2"
28
+ "@leafer/interface": "1.0.0-beta.5"
29
29
  }
30
30
  }
@@ -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
5
 
6
6
  import { Canvas } from './Canvas'
7
+ import { FileHelper } from '@leafer/file'
8
+ import { Debug } from '@leafer/debug'
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,6 +25,7 @@ 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
@@ -64,14 +68,54 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
64
68
  }
65
69
 
66
70
  public init(): void { }
67
- public setBackgroundColor(_color: string): void { }
68
- public setHittable(_hittable: boolean): void { }
71
+
72
+
73
+ public toBlob(type?: IExportFileType, quality?: number): Promise<IBlob> {
74
+ return new Promise((resolve) => {
75
+ const canvas = this.getSaveCanvas(type)
76
+ Platform.origin.canvasToBolb(canvas.view, type, quality).then((blob) => {
77
+ canvas.recycle()
78
+ resolve(blob)
79
+ }).catch((e) => {
80
+ debug.error(e)
81
+ resolve(null)
82
+ })
83
+ })
84
+ }
85
+
86
+ public toDataURL(type?: IExportImageType, quality?: number): string {
87
+ const canvas = this.getSaveCanvas(type)
88
+ const data = Platform.origin.canvasToDataURL(canvas.view, type, quality)
89
+ canvas.recycle()
90
+ return data
91
+ }
92
+
93
+ public saveAs(filename: string, quality?: number): Promise<boolean> {
94
+ return new Promise((resolve) => {
95
+ const canvas = this.getSaveCanvas(FileHelper.fileType(filename))
96
+ Platform.origin.canvasSaveAs(canvas.view, filename, quality).then(() => {
97
+ canvas.recycle()
98
+ resolve(true)
99
+ }).catch((e) => {
100
+ debug.error(e)
101
+ resolve(false)
102
+ })
103
+ })
104
+ }
105
+
106
+ public getSaveCanvas(type: string): ILeaferCanvas {
107
+ const { backgroundColor, bounds } = this as ILeaferCanvas
108
+ const canvas = this.getSameCanvas()
109
+ if (['jpg', 'jpeg'].includes(type)) canvas.fillWorld(bounds, '#FFFFFF')
110
+ if (backgroundColor) canvas.fillWorld(bounds, backgroundColor)
111
+ canvas.copyWorld(this)
112
+ return canvas
113
+ }
114
+
69
115
 
70
116
  public resize(size: IScreenSizeData): void {
71
117
  if (this.isSameSize(size)) return
72
118
 
73
- const { width, height } = size
74
-
75
119
  let takeCanvas: ILeaferCanvas
76
120
  if (this.context && !this.unreal && this.width) {
77
121
  takeCanvas = this.getSameCanvas()
@@ -79,10 +123,11 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
79
123
  }
80
124
 
81
125
  DataHelper.copyAttrs(this, size, canvasSizeAttrs)
82
- this.bounds = new Bounds(0, 0, width, height)
126
+ this.bounds = new Bounds(0, 0, this.width, this.height)
127
+ this.pixelRatio || (this.pixelRatio = 1)
83
128
 
84
129
  if (!this.unreal) {
85
- this.setViewSize(size)
130
+ this.updateViewSize()
86
131
  this.smooth = this.config.smooth
87
132
  }
88
133
 
@@ -93,7 +138,7 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
93
138
  }
94
139
  }
95
140
 
96
- public setViewSize(_size: IScreenSizeData): void { }
141
+ public updateViewSize(): void { }
97
142
  public startAutoLayout(_autoBounds: IAutoBounds, _listener: IResizeEventListener): void { }
98
143
  public stopAutoLayout(): void { }
99
144
 
@@ -213,6 +258,10 @@ export class LeaferCanvasBase extends Canvas implements ILeaferCanvas {
213
258
  this.copyWorld(maskCanvas, fromBounds, toBounds, 'destination-in')
214
259
  }
215
260
 
261
+ public useEraser(eraserCanvas: ILeaferCanvas, fromBounds?: IBoundsData, toBounds?: IBoundsData): void {
262
+ this.copyWorld(eraserCanvas, fromBounds, toBounds, 'destination-out')
263
+ }
264
+
216
265
  public fillWorld(bounds: IBoundsData, color: string | object, blendMode?: IBlendMode): void {
217
266
  if (blendMode) this.blendMode = blendMode
218
267
  this.fillStyle = color