@leafer-ui/paint 1.0.0-beta.6 → 1.0.0-beta.7

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 +5 -5
  2. package/src/paint/image.ts +53 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/paint",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.7",
4
4
  "description": "@leafer-ui/paint",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,11 +19,11 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/core": "1.0.0-beta.6",
23
- "@leafer-ui/color": "1.0.0-beta.6"
22
+ "@leafer/core": "1.0.0-beta.7",
23
+ "@leafer-ui/color": "1.0.0-beta.7"
24
24
  },
25
25
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-beta.6",
27
- "@leafer-ui/interface": "1.0.0-beta.6"
26
+ "@leafer/interface": "1.0.0-beta.7",
27
+ "@leafer-ui/interface": "1.0.0-beta.7"
28
28
  }
29
29
  }
@@ -1,4 +1,4 @@
1
- import { IBoundsData } from '@leafer/interface'
1
+ import { IBoundsData, ISizeData } from '@leafer/interface'
2
2
  import { Platform, MatrixHelper, ImageEvent } from '@leafer/core'
3
3
 
4
4
  import { IUI, IImagePaint, ILeafPaint, IMatrixData, IImagePaintMode, IPointData } from '@leafer-ui/interface'
@@ -7,53 +7,54 @@ import { IUI, IImagePaint, ILeafPaint, IMatrixData, IImagePaintMode, IPointData
7
7
  const { get, rotateOfOuter, translate, scaleOfOuter, scale: scaleHelper, rotate } = MatrixHelper
8
8
 
9
9
  export function image(ui: IUI, attrName: string, paint: IImagePaint, box: IBoundsData): ILeafPaint {
10
- let { type, blendMode } = paint
11
- let leaferPaint: ILeafPaint = {
12
- type,
13
- blendMode,
14
- style: 'rgba(255,255,255,0)'
15
- }
10
+ const { type, blendMode } = paint
11
+ let leaferPaint: ILeafPaint = { type, style: 'rgba(255,255,255,0)' }
12
+ if (blendMode) leaferPaint.blendMode = blendMode
16
13
 
17
14
  const { imageManager } = ui.leafer
18
15
  const image = imageManager.get(paint)
19
16
 
20
17
  if (image.ready) {
21
18
 
22
- let transform: IMatrixData
23
- let { opacity, mode, offset, scale, rotation } = paint
24
- let { width, height } = image
25
- const sameBox = box.width === width && box.height === height
26
-
27
- switch (mode) {
28
- case 'strench':
29
- if (!sameBox) width = box.width, height = box.height
30
- if (box.x || box.y) {
31
- transform = get()
32
- translate(transform, box.x, box.y)
33
- }
34
- break
35
- case 'clip':
36
- if (offset || scale || rotation) transform = getClipTransform(box, offset, scale, rotation)
37
- break
38
- case 'repeat':
39
- if (!sameBox || scale || rotation) transform = getRepeatTransform(box, width, height, scale as number, rotation)
40
- break
41
- case 'fit':
42
- case 'cover':
43
- default:
44
- if (!sameBox || rotation) transform = getFillOrFitTransform(mode, box, width, height, rotation)
45
- }
19
+ if (!autoSize(ui, image)) {
20
+
21
+ let transform: IMatrixData
22
+ let { opacity, mode, offset, scale, rotation } = paint
23
+ let { width, height } = image
24
+ const sameBox = box.width === width && box.height === height
25
+
26
+ switch (mode) {
27
+ case 'strench':
28
+ if (!sameBox) width = box.width, height = box.height
29
+ if (box.x || box.y) {
30
+ transform = get()
31
+ translate(transform, box.x, box.y)
32
+ }
33
+ break
34
+ case 'clip':
35
+ if (offset || scale || rotation) transform = getClipTransform(box, offset, scale, rotation)
36
+ break
37
+ case 'repeat':
38
+ if (!sameBox || scale || rotation) transform = getRepeatTransform(box, width, height, scale as number, rotation)
39
+ break
40
+ case 'fit':
41
+ case 'cover':
42
+ default:
43
+ if (!sameBox || rotation) transform = getFillOrFitTransform(mode, box, width, height, rotation)
44
+ }
46
45
 
47
- leaferPaint.style = createPattern(image.getCanvas(width, height, opacity), transform, mode === 'repeat')
46
+ leaferPaint.style = createPattern(image.getCanvas(width, height, opacity), transform, mode === 'repeat')
47
+
48
+ }
48
49
 
49
50
  } else {
50
51
 
51
52
  imageManager.load(image,
52
53
  () => {
53
- if (!ui.__.__getInput('width')) ui.width = image.width
54
- if (!ui.__.__getInput('height')) ui.height = image.height
55
- ui.forceUpdate('width')
56
- ui.emitEvent(new ImageEvent(ImageEvent.LOADED, ui, image, attrName, paint))
54
+ if (ui.leafer) {
55
+ if (!autoSize(ui, image)) ui.forceUpdate('width')
56
+ ui.emitEvent(new ImageEvent(ImageEvent.LOADED, ui, image, attrName, paint))
57
+ }
57
58
  },
58
59
  (error) => {
59
60
  ui.emitEvent(new ImageEvent(ImageEvent.ERROR, ui, image, attrName, paint, error))
@@ -65,6 +66,22 @@ export function image(ui: IUI, attrName: string, paint: IImagePaint, box: IBound
65
66
  return leaferPaint
66
67
  }
67
68
 
69
+ function autoSize(ui: IUI, image: ISizeData): boolean {
70
+ let auto: boolean
71
+ const { __: data } = ui
72
+ if (data) {
73
+ if (!data.__getInput('width')) {
74
+ auto = true
75
+ ui.width = image.width
76
+ }
77
+ if (!data.__getInput('height')) {
78
+ auto = true
79
+ ui.height = image.height
80
+ }
81
+ }
82
+ return auto
83
+ }
84
+
68
85
  function getFillOrFitTransform(mode: IImagePaintMode, box: IBoundsData, width: number, height: number, rotation: number): IMatrixData {
69
86
  const transform: IMatrixData = get()
70
87
  const swap = rotation && rotation !== 180