@leafer/web 1.0.0-bate → 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.
Files changed (2) hide show
  1. package/package.json +7 -6
  2. package/src/index.ts +55 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/web",
3
- "version": "1.0.0-bate",
3
+ "version": "1.0.0-beta.10",
4
4
  "description": "@leafer/web",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,12 +19,13 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/core": "1.0.0-bate",
23
- "@leafer/canvas-web": "1.0.0-bate",
24
- "@leafer/interaction-web": "1.0.0-bate",
25
- "@leafer/image-web": "1.0.0-bate"
22
+ "@leafer/core": "1.0.0-beta.10",
23
+ "@leafer/partner": "1.0.0-beta.10",
24
+ "@leafer/canvas-web": "1.0.0-beta.10",
25
+ "@leafer/interaction-web": "1.0.0-beta.10",
26
+ "@leafer/image-web": "1.0.0-beta.10"
26
27
  },
27
28
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-bate"
29
+ "@leafer/interface": "1.0.0-beta.10"
29
30
  }
30
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,10 +24,49 @@ 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
+
66
+ Platform.name = 'web'
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
69
+ Platform.realtimeLayout = true
28
70
 
29
71
  const { userAgent } = navigator
30
72
 
@@ -34,3 +76,12 @@ if (userAgent.indexOf("Firefox") > -1) {
34
76
  } else if (userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1) {
35
77
  Platform.fullImageShadow = true
36
78
  }
79
+
80
+ if (userAgent.indexOf('Windows') > -1) {
81
+ Platform.os = 'Windows'
82
+ Platform.intWheelDeltaY = true
83
+ } else if (userAgent.indexOf('Mac') > -1) {
84
+ Platform.os = 'Mac'
85
+ } else if (userAgent.indexOf('Linux') > -1) {
86
+ Platform.os = 'Linux'
87
+ }