@leafer/web 1.0.0-beta.4 → 1.0.0-beta.6

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