@leafer-ui/image 1.0.0-rc.21 → 1.0.0-rc.23
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 +5 -5
- package/src/data.ts +33 -9
- package/src/mode.ts +26 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-ui/image",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.23",
|
|
4
4
|
"description": "@leafer-ui/image",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leafer/core": "1.0.0-rc.
|
|
26
|
-
"@leafer-ui/draw": "1.0.0-rc.
|
|
25
|
+
"@leafer/core": "1.0.0-rc.23",
|
|
26
|
+
"@leafer-ui/draw": "1.0.0-rc.23"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@leafer/interface": "1.0.0-rc.
|
|
30
|
-
"@leafer-ui/interface": "1.0.0-rc.
|
|
29
|
+
"@leafer/interface": "1.0.0-rc.23",
|
|
30
|
+
"@leafer-ui/interface": "1.0.0-rc.23"
|
|
31
31
|
}
|
|
32
32
|
}
|
package/src/data.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IBoundsData, ILeaferImage } from '@leafer/interface'
|
|
2
|
-
import { MatrixHelper, Bounds } from '@leafer/core'
|
|
1
|
+
import { IBoundsData, ILeaferImage, IPointData } from '@leafer/interface'
|
|
2
|
+
import { MatrixHelper, Bounds, AlignHelper } from '@leafer/core'
|
|
3
3
|
|
|
4
4
|
import { IImagePaint, ILeafPaint, ILeafPaintPatternData } from '@leafer-ui/interface'
|
|
5
5
|
|
|
@@ -8,20 +8,29 @@ import { clipMode, fillOrFitMode, repeatMode } from './mode'
|
|
|
8
8
|
|
|
9
9
|
const { get, translate } = MatrixHelper
|
|
10
10
|
const tempBox = new Bounds()
|
|
11
|
+
const tempPoint = {} as IPointData
|
|
11
12
|
|
|
12
13
|
export function createData(leafPaint: ILeafPaint, image: ILeaferImage, paint: IImagePaint, box: IBoundsData): void {
|
|
13
14
|
let { width, height } = image
|
|
14
15
|
if (paint.padding) box = tempBox.set(box).shrink(paint.padding)
|
|
15
16
|
|
|
16
|
-
const { opacity, mode, offset, scale, size, rotation, blendMode, repeat } = paint
|
|
17
|
+
const { opacity, mode, align, offset, scale, size, rotation, blendMode, repeat } = paint
|
|
17
18
|
const sameBox = box.width === width && box.height === height
|
|
18
19
|
if (blendMode) leafPaint.blendMode = blendMode
|
|
19
20
|
|
|
20
21
|
const data: ILeafPaintPatternData = leafPaint.data = { mode }
|
|
22
|
+
const swapSize = align !== 'center' && (rotation || 0) % 180 === 90
|
|
23
|
+
const swapWidth = swapSize ? height : width, swapHeight = swapSize ? width : height
|
|
21
24
|
|
|
22
|
-
let x
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
+
let x = 0, y = 0, scaleX: number, scaleY: number
|
|
26
|
+
|
|
27
|
+
if (!mode || mode === 'cover' || mode === 'fit') {
|
|
28
|
+
if (!sameBox || rotation) {
|
|
29
|
+
const sw = box.width / swapWidth, sh = box.height / swapHeight
|
|
30
|
+
scaleX = scaleY = mode === 'fit' ? Math.min(sw, sh) : Math.max(sw, sh)
|
|
31
|
+
x += (box.width - width * scaleX) / 2, y += (box.height - height * scaleY) / 2
|
|
32
|
+
}
|
|
33
|
+
} else if (size) {
|
|
25
34
|
scaleX = (typeof size === 'number' ? size : size.width) / width
|
|
26
35
|
scaleY = (typeof size === 'number' ? size : size.height) / height
|
|
27
36
|
} else if (scale) {
|
|
@@ -29,21 +38,31 @@ export function createData(leafPaint: ILeafPaint, image: ILeaferImage, paint: II
|
|
|
29
38
|
scaleY = typeof scale === 'number' ? scale : scale.y
|
|
30
39
|
}
|
|
31
40
|
|
|
41
|
+
if (align) {
|
|
42
|
+
const imageBounds = { x, y, width: swapWidth, height: swapHeight }
|
|
43
|
+
if (scaleX) imageBounds.width *= scaleX, imageBounds.height *= scaleY
|
|
44
|
+
AlignHelper.toPoint(align, imageBounds, box, tempPoint, true)
|
|
45
|
+
x += tempPoint.x, y += tempPoint.y
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (offset) x += offset.x, y += offset.y
|
|
49
|
+
|
|
32
50
|
switch (mode) {
|
|
33
51
|
case 'strench':
|
|
34
52
|
if (!sameBox) width = box.width, height = box.height
|
|
35
53
|
break
|
|
54
|
+
case 'normal':
|
|
36
55
|
case 'clip':
|
|
37
|
-
if (
|
|
56
|
+
if (x || y || scaleX || rotation) clipMode(data, box, x, y, scaleX, scaleY, rotation)
|
|
38
57
|
break
|
|
39
58
|
case 'repeat':
|
|
40
|
-
if (!sameBox || scaleX || rotation) repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation)
|
|
59
|
+
if (!sameBox || scaleX || rotation) repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align)
|
|
41
60
|
if (!repeat) data.repeat = 'repeat'
|
|
42
61
|
break
|
|
43
62
|
case 'fit':
|
|
44
63
|
case 'cover':
|
|
45
64
|
default:
|
|
46
|
-
if (
|
|
65
|
+
if (scaleX) fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation)
|
|
47
66
|
}
|
|
48
67
|
|
|
49
68
|
if (!data.transform) {
|
|
@@ -53,6 +72,11 @@ export function createData(leafPaint: ILeafPaint, image: ILeaferImage, paint: II
|
|
|
53
72
|
}
|
|
54
73
|
}
|
|
55
74
|
|
|
75
|
+
if (scaleX && mode !== 'strench') {
|
|
76
|
+
data.scaleX = scaleX
|
|
77
|
+
data.scaleY = scaleY
|
|
78
|
+
}
|
|
79
|
+
|
|
56
80
|
data.width = width
|
|
57
81
|
data.height = height
|
|
58
82
|
if (opacity) data.opacity = opacity
|
package/src/mode.ts
CHANGED
|
@@ -1,66 +1,53 @@
|
|
|
1
|
-
import { IBoundsData, IPointData, IMatrixData, } from '@leafer/interface'
|
|
1
|
+
import { IBoundsData, IPointData, IMatrixData, IAlign, } from '@leafer/interface'
|
|
2
2
|
import { MatrixHelper } from '@leafer/core'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { ILeafPaintPatternData } from '@leafer-ui/interface'
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
let origin = {} as IPointData
|
|
8
8
|
const { get, rotateOfOuter, translate, scaleOfOuter, scale: scaleHelper, rotate } = MatrixHelper
|
|
9
9
|
|
|
10
|
-
export function fillOrFitMode(data: ILeafPaintPatternData,
|
|
10
|
+
export function fillOrFitMode(data: ILeafPaintPatternData, box: IBoundsData, x: number, y: number, scaleX: number, scaleY: number, rotation: number): void {
|
|
11
11
|
const transform: IMatrixData = get()
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const sh = box.height / (swap ? width : height)
|
|
15
|
-
const scale = mode === 'fit' ? Math.min(sw, sh) : Math.max(sw, sh)
|
|
16
|
-
const x = box.x + (box.width - width * scale) / 2
|
|
17
|
-
const y = box.y + (box.height - height * scale) / 2
|
|
18
|
-
translate(transform, x, y)
|
|
19
|
-
scaleHelper(transform, scale)
|
|
12
|
+
translate(transform, box.x + x, box.y + y)
|
|
13
|
+
scaleHelper(transform, scaleX, scaleY)
|
|
20
14
|
if (rotation) rotateOfOuter(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation)
|
|
21
|
-
data.scaleX = data.scaleY = scale
|
|
22
15
|
data.transform = transform
|
|
23
16
|
}
|
|
24
17
|
|
|
25
18
|
|
|
26
19
|
export function clipMode(data: ILeafPaintPatternData, box: IBoundsData, x: number, y: number, scaleX: number, scaleY: number, rotation: number): void {
|
|
27
20
|
const transform: IMatrixData = get()
|
|
28
|
-
translate(transform, box.x, box.y)
|
|
29
|
-
if (
|
|
30
|
-
if (scaleX) {
|
|
31
|
-
scaleHelper(transform, scaleX, scaleY)
|
|
32
|
-
data.scaleX = transform.a
|
|
33
|
-
data.scaleY = transform.d
|
|
34
|
-
}
|
|
21
|
+
translate(transform, box.x + x, box.y + y)
|
|
22
|
+
if (scaleX) scaleHelper(transform, scaleX, scaleY)
|
|
35
23
|
if (rotation) rotate(transform, rotation)
|
|
36
24
|
data.transform = transform
|
|
37
25
|
}
|
|
38
26
|
|
|
39
27
|
|
|
40
|
-
export function repeatMode(data: ILeafPaintPatternData, box: IBoundsData, width: number, height: number, x: number, y: number, scaleX: number, scaleY: number, rotation: number): void {
|
|
28
|
+
export function repeatMode(data: ILeafPaintPatternData, box: IBoundsData, width: number, height: number, x: number, y: number, scaleX: number, scaleY: number, rotation: number, align: IAlign): void {
|
|
41
29
|
const transform = get()
|
|
42
30
|
if (rotation) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
31
|
+
if (align === 'center') {
|
|
32
|
+
rotateOfOuter(transform, { x: width / 2, y: height / 2 }, rotation)
|
|
33
|
+
} else {
|
|
34
|
+
rotate(transform, rotation)
|
|
35
|
+
switch (rotation) {
|
|
36
|
+
case 90:
|
|
37
|
+
translate(transform, height, 0)
|
|
38
|
+
break
|
|
39
|
+
case 180:
|
|
40
|
+
translate(transform, width, height)
|
|
41
|
+
break
|
|
42
|
+
case 270:
|
|
43
|
+
translate(transform, 0, width)
|
|
44
|
+
break
|
|
45
|
+
}
|
|
54
46
|
}
|
|
55
47
|
}
|
|
56
|
-
origin.x = box.x
|
|
57
|
-
origin.y = box.y
|
|
58
|
-
if (x || y) origin.x += x, origin.y += y
|
|
48
|
+
origin.x = box.x + x
|
|
49
|
+
origin.y = box.y + y
|
|
59
50
|
translate(transform, origin.x, origin.y)
|
|
60
|
-
if (scaleX)
|
|
61
|
-
scaleOfOuter(transform, origin, scaleX, scaleY)
|
|
62
|
-
data.scaleX = scaleX
|
|
63
|
-
data.scaleY = scaleY
|
|
64
|
-
}
|
|
51
|
+
if (scaleX) scaleOfOuter(transform, origin, scaleX, scaleY)
|
|
65
52
|
data.transform = transform
|
|
66
53
|
}
|