@leafer/web 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.
Files changed (2) hide show
  1. package/package.json +7 -7
  2. package/src/index.ts +44 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/web",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.5",
4
4
  "description": "@leafer/web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,13 +19,13 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/core": "1.0.0-beta.2",
23
- "@leafer/partner": "1.0.0-beta.2",
24
- "@leafer/canvas-web": "1.0.0-beta.2",
25
- "@leafer/interaction-web": "1.0.0-beta.2",
26
- "@leafer/image-web": "1.0.0-beta.2"
22
+ "@leafer/core": "1.0.0-beta.5",
23
+ "@leafer/partner": "1.0.0-beta.5",
24
+ "@leafer/canvas-web": "1.0.0-beta.5",
25
+ "@leafer/interaction-web": "1.0.0-beta.5",
26
+ "@leafer/image-web": "1.0.0-beta.5"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-beta.2"
29
+ "@leafer/interface": "1.0.0-beta.5"
30
30
  }
31
31
  }
package/src/index.ts CHANGED
@@ -5,12 +5,16 @@ export * from '@leafer/canvas-web'
5
5
  export * from '@leafer/image-web'
6
6
  export * from '@leafer/interaction-web'
7
7
 
8
- import { ICreator, IFunction } from '@leafer/interface'
8
+ import { ICreator, IFunction, IExportImageType, IExportFileType, IObject, ICanvasType } from '@leafer/interface'
9
9
  import { Platform, Creator } from '@leafer/core'
10
10
 
11
11
  import { LeaferCanvas } from '@leafer/canvas-web'
12
12
  import { LeaferImage } from '@leafer/image-web'
13
13
  import { Interaction } from '@leafer/interaction-web'
14
+ import { FileHelper } from '@leafer/file'
15
+
16
+
17
+ const { mineType, fileType } = FileHelper
14
18
 
15
19
 
16
20
  Object.assign(Creator, {
@@ -21,10 +25,47 @@ Object.assign(Creator, {
21
25
  interaction: (target, canvas, selector, options?) => new Interaction(target, canvas, selector, options),
22
26
  } as ICreator)
23
27
 
28
+
29
+ export function useCanvas(_canvasType: ICanvasType, _power?: IObject): void {
30
+ Platform.origin = {
31
+ createCanvas(width: number, height: number): HTMLCanvasElement {
32
+ const canvas = document.createElement('canvas')
33
+ canvas.width = width
34
+ canvas.height = height
35
+ return canvas
36
+ },
37
+ canvasToDataURL: (canvas: HTMLCanvasElement, type?: IExportImageType, quality?: number) => canvas.toDataURL(mineType(type), quality),
38
+ canvasToBolb: (canvas: HTMLCanvasElement, type?: IExportFileType, quality?: number) => new Promise((resolve) => canvas.toBlob(resolve, mineType(type), quality)),
39
+ canvasSaveAs: (canvas: HTMLCanvasElement, filename: string, quality?: any) => {
40
+ return new Promise((resolve) => {
41
+ let el = document.createElement('a')
42
+ el.href = canvas.toDataURL(mineType(fileType(filename)), quality)
43
+ el.download = filename
44
+ document.body.appendChild(el)
45
+ el.click()
46
+ document.body.removeChild(el)
47
+ resolve()
48
+ })
49
+ },
50
+ loadImage(src: any): Promise<HTMLImageElement> {
51
+ return new Promise((resolve, reject) => {
52
+ const img = new Image()
53
+ img.setAttribute('crossOrigin', 'anonymous')
54
+ img.crossOrigin = 'anonymous'
55
+ img.onload = () => { resolve(img) }
56
+ img.onerror = (e) => { reject(e) }
57
+ if (!src.startsWith('data:')) src.includes("?") ? src + "&xhr" : src + "?xhr" // 需要带上xhr区分image标签的缓存,否则导致浏览器跨域问题
58
+ img.src = src
59
+ })
60
+ }
61
+ }
62
+
63
+ Platform.canvas = Creator.canvas()
64
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
65
+ }
66
+
24
67
  Platform.requestRender = function (render: IFunction): void { window.requestAnimationFrame(render) }
25
- Platform.canvas = Creator.canvas()
26
68
  Platform.devicePixelRatio = devicePixelRatio
27
- Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient
28
69
 
29
70
  const { userAgent } = navigator
30
71