@leafer-ui/image 1.0.0-rc.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-ui/image
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@leafer-ui/image",
3
+ "version": "1.0.0-rc.10",
4
+ "description": "@leafer-ui/image",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "types": "types/index.d.ts",
9
+ "files": [
10
+ "src",
11
+ "types",
12
+ "dist"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/leaferjs/ui.git"
17
+ },
18
+ "homepage": "https://github.com/leaferjs/ui/tree/main/packages/partner/image",
19
+ "bugs": "https://github.com/leaferjs/ui/issues",
20
+ "keywords": [
21
+ "leafer-ui",
22
+ "leaferjs"
23
+ ],
24
+ "dependencies": {
25
+ "@leafer/core": "1.0.0-rc.10",
26
+ "@leafer/draw": "1.0.0-rc.10"
27
+ },
28
+ "devDependencies": {
29
+ "@leafer/interface": "1.0.0-rc.10",
30
+ "@leafer-ui/interface": "1.0.0-rc.10"
31
+ }
32
+ }
package/src/check.ts ADDED
@@ -0,0 +1,60 @@
1
+
2
+ import { ILeaferCanvas } from '@leafer/interface'
3
+ import { ImageManager, Platform } from '@leafer/core'
4
+
5
+ import { IUI, ILeafPaint } from '@leafer-ui/interface'
6
+ import { Export } from '@leafer-ui/draw'
7
+
8
+ import { createPattern } from './pattern'
9
+
10
+ const { abs } = Math
11
+
12
+ export function checkImage(ui: IUI, canvas: ILeaferCanvas, paint: ILeafPaint, allowPaint?: boolean): boolean {
13
+ const { scaleX, scaleY } = ui.__world
14
+
15
+ if (!paint.data || paint.patternId === scaleX + '-' + scaleY) {
16
+ return false
17
+ } else {
18
+
19
+ const { data } = paint
20
+
21
+ if (allowPaint) {
22
+ if (!data.repeat) {
23
+ let { width, height } = data
24
+ width *= abs(scaleX) * canvas.pixelRatio
25
+ height *= abs(scaleY) * canvas.pixelRatio
26
+ if (data.scaleX) {
27
+ width *= data.scaleX
28
+ height *= data.scaleY
29
+ }
30
+ allowPaint = width * height > Platform.image.maxCacheSize
31
+ } else {
32
+ allowPaint = false
33
+ }
34
+ }
35
+
36
+ if (allowPaint) {
37
+ canvas.save()
38
+ canvas.clip()
39
+ if (paint.blendMode) canvas.blendMode = paint.blendMode
40
+ if (data.opacity) canvas.opacity *= data.opacity
41
+ if (data.transform) canvas.transform(data.transform)
42
+ canvas.drawImage(paint.image.view as any, 0, 0, data.width, data.height)
43
+ canvas.restore()
44
+ return true
45
+ } else {
46
+ if (!paint.style || Export.running) {
47
+ createPattern(ui, paint, canvas.pixelRatio)
48
+ } else {
49
+ if (!paint.patternTask) {
50
+ paint.patternTask = ImageManager.patternTasker.add(async () => {
51
+ paint.patternTask = null
52
+ if (canvas.bounds.hit(ui.__world)) createPattern(ui, paint, canvas.pixelRatio)
53
+ ui.forceUpdate('surface')
54
+ }, 300)
55
+ }
56
+ }
57
+ return false
58
+ }
59
+ }
60
+ }
package/src/data.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { IBoundsData, ILeaferImage } from '@leafer/interface'
2
+ import { MatrixHelper } from '@leafer/core'
3
+
4
+ import { IImagePaint, ILeafPaint, ILeafPaintPatternData } from '@leafer-ui/interface'
5
+
6
+ import { clipMode, fillOrFitMode, repeatMode } from './mode'
7
+
8
+
9
+ const { get, translate } = MatrixHelper
10
+
11
+ export function createData(leafPaint: ILeafPaint, image: ILeaferImage, paint: IImagePaint, box: IBoundsData): void {
12
+ let { width, height } = image
13
+
14
+ const { opacity, mode, offset, scale, size, rotation, blendMode, repeat } = paint
15
+ const sameBox = box.width === width && box.height === height
16
+ if (blendMode) leafPaint.blendMode = blendMode
17
+
18
+ const data: ILeafPaintPatternData = leafPaint.data = { mode }
19
+
20
+ let x: number, y: number, scaleX: number, scaleY: number
21
+ if (offset) x = offset.x, y = offset.y
22
+ if (size) {
23
+ scaleX = (typeof size === 'number' ? size : size.width) / width
24
+ scaleY = (typeof size === 'number' ? size : size.height) / height
25
+ } else if (scale) {
26
+ scaleX = typeof scale === 'number' ? scale : scale.x
27
+ scaleY = typeof scale === 'number' ? scale : scale.y
28
+ }
29
+
30
+ switch (mode) {
31
+ case 'strench':
32
+ if (!sameBox) width = box.width, height = box.height
33
+ if (box.x || box.y) {
34
+ data.transform = get()
35
+ translate(data.transform, box.x, box.y)
36
+ }
37
+ break
38
+ case 'clip':
39
+ if (offset || scaleX || rotation) clipMode(data, box, x, y, scaleX, scaleY, rotation)
40
+ break
41
+ case 'repeat':
42
+ if (!sameBox || scaleX || rotation) repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation)
43
+ if (!repeat) data.repeat = 'repeat'
44
+ break
45
+ case 'fit':
46
+ case 'cover':
47
+ default:
48
+ if (!sameBox || rotation) fillOrFitMode(data, mode, box, width, height, rotation)
49
+ }
50
+
51
+ data.width = width
52
+ data.height = height
53
+ if (opacity) data.opacity = opacity
54
+ if (repeat) data.repeat = typeof repeat === 'string' ? (repeat === 'x' ? 'repeat-x' : 'repeat-y') : 'repeat'
55
+ }
package/src/image.ts ADDED
@@ -0,0 +1,110 @@
1
+ import { IBoundsData, IImageEvent, ILeaferImage, IObject } from '@leafer/interface'
2
+ import { Bounds, BoundsHelper, ImageEvent, ImageManager } from '@leafer/core'
3
+
4
+ import { IUI, IImagePaint, ILeafPaint } from '@leafer-ui/interface'
5
+
6
+ import { createData } from './data'
7
+
8
+
9
+ interface IImagePaintCache {
10
+ leafPaint: ILeafPaint
11
+ paint: IImagePaint
12
+ boxBounds: IBoundsData
13
+ }
14
+
15
+ let cache: IImagePaintCache, box = new Bounds()
16
+ const { isSame } = BoundsHelper
17
+
18
+ export function image(ui: IUI, attrName: string, paint: IImagePaint, boxBounds: IBoundsData, firstUse: boolean): ILeafPaint {
19
+ let leafPaint: ILeafPaint, event: IImageEvent
20
+ const image = ImageManager.get(paint)
21
+
22
+ if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
23
+ leafPaint = cache.leafPaint
24
+ } else {
25
+ leafPaint = { type: paint.type }
26
+ leafPaint.image = image
27
+
28
+ cache = image.use > 1 ? { leafPaint, paint, boxBounds: box.set(boxBounds) } : null
29
+ }
30
+
31
+ if (firstUse || image.loading) event = { image, attrName, attrValue: paint }
32
+
33
+ if (image.ready) {
34
+
35
+ checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds)
36
+
37
+ if (firstUse) {
38
+ onLoad(ui, event)
39
+ onLoadSuccess(ui, event)
40
+ }
41
+
42
+ } else if (image.error) {
43
+
44
+ if (firstUse) onLoadError(ui, event, image.error)
45
+
46
+ } else { // need load
47
+
48
+ if (firstUse) onLoad(ui, event)
49
+
50
+ leafPaint.loadId = image.load(
51
+ () => {
52
+ if (!ui.destroyed) {
53
+ if (checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds)) ui.forceUpdate('surface')
54
+ onLoadSuccess(ui, event)
55
+ }
56
+ leafPaint.loadId = null
57
+ },
58
+ (error) => {
59
+ onLoadError(ui, event, error)
60
+ leafPaint.loadId = null
61
+ }
62
+ )
63
+
64
+ }
65
+
66
+ return leafPaint
67
+ }
68
+
69
+
70
+ function checkSizeAndCreateData(ui: IUI, attrName: string, paint: IImagePaint, image: ILeaferImage, leafPaint: ILeafPaint, boxBounds: IBoundsData): boolean {
71
+ if (attrName === 'fill' && !ui.__.__naturalWidth) {
72
+ const data = ui.__
73
+ data.__naturalWidth = image.width
74
+ data.__naturalHeight = image.height
75
+ if (data.__autoWidth || data.__autoHeight) {
76
+ ui.forceUpdate('width')
77
+ if (ui.__proxyData) {
78
+ ui.setProxyAttr('width', data.width)
79
+ ui.setProxyAttr('height', data.height)
80
+ }
81
+ return false
82
+ }
83
+ }
84
+
85
+ if (!leafPaint.data) createData(leafPaint, image, paint, boxBounds)
86
+ return true
87
+ }
88
+
89
+
90
+
91
+ function onLoad(ui: IUI, event: IImageEvent): void {
92
+ emit(ui, ImageEvent.LOAD, event)
93
+ }
94
+
95
+
96
+ function onLoadSuccess(ui: IUI, event: IImageEvent): void {
97
+ emit(ui, ImageEvent.LOADED, event)
98
+ }
99
+
100
+
101
+ function onLoadError(ui: IUI, event: IImageEvent, error: string | IObject,): void {
102
+ event.error = error
103
+ ui.forceUpdate('surface')
104
+ emit(ui, ImageEvent.ERROR, event)
105
+ }
106
+
107
+
108
+ function emit(ui: IUI, type: string, data: IImageEvent): void {
109
+ if (ui.hasEvent(type)) ui.emitEvent(new ImageEvent(type, data))
110
+ }
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { IPaintImageModule } from '@leafer-ui/interface'
2
+
3
+ import { image } from './image'
4
+ import { createData } from './data'
5
+ import { fillOrFitMode, clipMode, repeatMode } from './mode'
6
+ import { createPattern } from './pattern'
7
+ import { checkImage } from './check'
8
+ import { recycleImage } from './recycle'
9
+
10
+
11
+ export const PaintImageModule: IPaintImageModule = {
12
+ image,
13
+ createData,
14
+ fillOrFitMode,
15
+ clipMode,
16
+ repeatMode,
17
+ createPattern,
18
+ checkImage,
19
+ recycleImage
20
+ }
package/src/mode.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { IBoundsData, IPointData, IMatrixData, } from '@leafer/interface'
2
+ import { MatrixHelper } from '@leafer/core'
3
+
4
+ import { IImagePaintMode, ILeafPaintPatternData } from '@leafer-ui/interface'
5
+
6
+
7
+ let origin = {} as IPointData
8
+ const { get, rotateOfOuter, translate, scaleOfOuter, scale: scaleHelper, rotate } = MatrixHelper
9
+
10
+ export function fillOrFitMode(data: ILeafPaintPatternData, mode: IImagePaintMode, box: IBoundsData, width: number, height: number, rotation: number): void {
11
+ const transform: IMatrixData = get()
12
+ const swap = rotation && rotation !== 180
13
+ const sw = box.width / (swap ? height : width)
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)
20
+ if (rotation) rotateOfOuter(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation)
21
+ data.scaleX = data.scaleY = scale
22
+ data.transform = transform
23
+ }
24
+
25
+
26
+ export function clipMode(data: ILeafPaintPatternData, box: IBoundsData, x: number, y: number, scaleX: number, scaleY: number, rotation: number): void {
27
+ const transform: IMatrixData = get()
28
+ translate(transform, box.x, box.y)
29
+ if (x || y) translate(transform, x, y)
30
+ if (scaleX) {
31
+ scaleHelper(transform, scaleX, scaleY)
32
+ data.scaleX = transform.a
33
+ data.scaleY = transform.d
34
+ }
35
+ if (rotation) rotate(transform, rotation)
36
+ data.transform = transform
37
+ }
38
+
39
+
40
+ export function repeatMode(data: ILeafPaintPatternData, box: IBoundsData, width: number, height: number, x: number, y: number, scaleX: number, scaleY: number, rotation: number): void {
41
+ const transform = get()
42
+ if (rotation) {
43
+ rotate(transform, rotation)
44
+ switch (rotation) {
45
+ case 90:
46
+ translate(transform, height, 0)
47
+ break
48
+ case 180:
49
+ translate(transform, width, height)
50
+ break
51
+ case 270:
52
+ translate(transform, 0, width)
53
+ break
54
+ }
55
+ }
56
+ origin.x = box.x
57
+ origin.y = box.y
58
+ if (x || y) origin.x += x, origin.y += y
59
+ translate(transform, origin.x, origin.y)
60
+ if (scaleX) {
61
+ scaleOfOuter(transform, origin, scaleX, scaleY)
62
+ data.scaleX = scaleX
63
+ data.scaleY = scaleY
64
+ }
65
+ data.transform = transform
66
+ }
package/src/pattern.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { Platform, MatrixHelper } from '@leafer/core'
2
+
3
+ import { IUI, ILeafPaint, IMatrixData } from '@leafer-ui/interface'
4
+
5
+
6
+ const { get, scale, copy } = MatrixHelper
7
+ const { round, abs } = Math
8
+
9
+ export function createPattern(ui: IUI, paint: ILeafPaint, pixelRatio: number): boolean {
10
+
11
+ let { scaleX, scaleY } = ui.__world
12
+
13
+ const id = scaleX + '-' + scaleY
14
+
15
+ if (paint.patternId !== id && !ui.destroyed) {
16
+
17
+ scaleX = abs(scaleX) // maybe -1
18
+ scaleY = abs(scaleY)
19
+
20
+ const { image, data } = paint
21
+ let imageScale: number, imageMatrix: IMatrixData, { width, height, scaleX: sx, scaleY: sy, opacity, transform, repeat } = data
22
+
23
+ if (sx) {
24
+ imageMatrix = get()
25
+ copy(imageMatrix, transform)
26
+ scale(imageMatrix, 1 / sx, 1 / sy)
27
+ scaleX *= sx
28
+ scaleY *= sy
29
+ }
30
+
31
+ scaleX *= pixelRatio
32
+ scaleY *= pixelRatio
33
+ width *= scaleX
34
+ height *= scaleY
35
+
36
+ const size = width * height
37
+
38
+ if (!repeat) {
39
+ if (size > Platform.image.maxCacheSize) return false // same as check()
40
+ }
41
+
42
+ let maxSize = Platform.image.maxPatternSize
43
+
44
+ if (!image.isSVG) {
45
+ const imageSize = image.width * image.height
46
+ if (maxSize > imageSize) maxSize = imageSize
47
+ }
48
+
49
+ if (size > maxSize) imageScale = Math.sqrt(size / maxSize)
50
+
51
+ if (imageScale) {
52
+ scaleX /= imageScale
53
+ scaleY /= imageScale
54
+ width /= imageScale
55
+ height /= imageScale
56
+ }
57
+
58
+ if (sx) {
59
+ scaleX /= sx
60
+ scaleY /= sy
61
+ }
62
+
63
+ if (transform || scaleX !== 1 || scaleY !== 1) {
64
+ if (!imageMatrix) {
65
+ imageMatrix = get()
66
+ if (transform) copy(imageMatrix, transform)
67
+ }
68
+ scale(imageMatrix, 1 / scaleX, 1 / scaleY)
69
+ }
70
+
71
+ const canvas = image.getCanvas(width < 1 ? 1 : round(width), height < 1 ? 1 : round(height), opacity)
72
+ const pattern = image.getPattern(canvas, repeat || (Platform.origin.noRepeat || 'no-repeat'), imageMatrix, paint)
73
+
74
+ paint.style = pattern
75
+ paint.patternId = id
76
+
77
+ return true
78
+
79
+ } else {
80
+
81
+ return false
82
+
83
+ }
84
+
85
+ }
package/src/recycle.ts ADDED
@@ -0,0 +1,41 @@
1
+ import { IBooleanMap, ILeaferImage, IObject } from '@leafer/interface'
2
+ import { ImageManager } from '@leafer/core'
3
+
4
+ import { IImagePaint, ILeafPaint, IPaintAttr, IUIData } from '@leafer-ui/interface'
5
+
6
+
7
+ export function recycleImage(attrName: IPaintAttr, data: IUIData): IBooleanMap {
8
+ const paints = (data as IObject)['_' + attrName] as ILeafPaint[]
9
+
10
+ if (paints instanceof Array) {
11
+
12
+ let image: ILeaferImage, recycleMap: IBooleanMap, input: IImagePaint[], url: string
13
+
14
+ for (let i = 0, len = paints.length; i < len; i++) {
15
+
16
+ image = paints[i].image
17
+ url = image && image.url
18
+
19
+ if (url) {
20
+ if (!recycleMap) recycleMap = {}
21
+ recycleMap[url] = true
22
+ ImageManager.recycle(image)
23
+
24
+ // stop load
25
+ if (image.loading) {
26
+ if (!input) {
27
+ input = (data.__input && data.__input[attrName]) || []
28
+ if (!(input instanceof Array)) input = [input]
29
+ }
30
+ image.unload(paints[i].loadId, !input.some((item: IImagePaint) => item.url === url))
31
+ }
32
+ }
33
+
34
+ }
35
+
36
+ return recycleMap
37
+
38
+ }
39
+
40
+ return null
41
+ }
@@ -0,0 +1,5 @@
1
+ import { IPaintImageModule } from '@leafer-ui/interface';
2
+
3
+ declare const PaintImageModule: IPaintImageModule;
4
+
5
+ export { PaintImageModule };