@leafer/platform 1.9.12 → 1.10.0
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 +4 -4
- package/src/Platform.ts +34 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/platform",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "@leafer/platform",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leafer/data": "1.
|
|
26
|
-
"@leafer/debug": "1.
|
|
25
|
+
"@leafer/data": "1.10.0",
|
|
26
|
+
"@leafer/debug": "1.10.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@leafer/interface": "1.
|
|
29
|
+
"@leafer/interface": "1.10.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/Platform.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { IPlatform } from '@leafer/interface'
|
|
1
|
+
import { IPlatform, IObject, IBoundsData, ICanvasPattern, IMatrixData, ILeaferImagePatternPaint, ISizeData, ICanvasContext2D } from '@leafer/interface'
|
|
2
|
+
import { DataHelper } from '@leafer/data'
|
|
2
3
|
|
|
3
4
|
|
|
5
|
+
const { floor, max } = Math
|
|
6
|
+
|
|
4
7
|
export const Platform: IPlatform = {
|
|
5
8
|
toURL(text: string, fileType?: 'text' | 'svg'): string {
|
|
6
9
|
let url = encodeURIComponent(text)
|
|
@@ -13,11 +16,40 @@ export const Platform: IPlatform = {
|
|
|
13
16
|
maxCacheSize: 2560 * 1600, // 2k
|
|
14
17
|
maxPatternSize: 4096 * 2160, // 4k
|
|
15
18
|
crossOrigin: 'anonymous',
|
|
19
|
+
isLarge(size: ISizeData, scaleX?: number, scaleY?: number, largeSize?: number): boolean {
|
|
20
|
+
return size.width * size.height * (scaleX ? scaleX * scaleY : 1) > (largeSize || image.maxCacheSize)
|
|
21
|
+
},
|
|
22
|
+
isSuperLarge(size: ISizeData, scaleX?: number, scaleY?: number): boolean {
|
|
23
|
+
return image.isLarge(size, scaleX, scaleY, image.maxPatternSize)
|
|
24
|
+
},
|
|
16
25
|
getRealURL(url: string): string {
|
|
17
26
|
const { prefix, suffix } = Platform.image
|
|
18
27
|
if (suffix && !url.startsWith('data:') && !url.startsWith('blob:')) url += (url.includes("?") ? "&" : "?") + suffix
|
|
19
28
|
if (prefix && url[0] === '/') url = prefix + url
|
|
20
29
|
return url
|
|
30
|
+
},
|
|
31
|
+
resize(image: any, width: number, height: number, xGap?: number, yGap?: number, clip?: IBoundsData, smooth?: boolean, opacity?: number, _filters?: IObject): any {
|
|
32
|
+
const canvas = Platform.origin.createCanvas(max(floor(width + (xGap || 0)), 1), max(floor(height + (yGap || 0)), 1),)
|
|
33
|
+
const ctx: ICanvasContext2D = canvas.getContext('2d')
|
|
34
|
+
if (opacity) ctx.globalAlpha = opacity
|
|
35
|
+
ctx.imageSmoothingEnabled = smooth === false ? false : true // 平滑绘制
|
|
36
|
+
if (clip) {
|
|
37
|
+
const scaleX = width / clip.width, scaleY = height / clip.height
|
|
38
|
+
ctx.setTransform(scaleX, 0, 0, scaleY, -clip.x * scaleX, -clip.y * scaleY)
|
|
39
|
+
ctx.drawImage(image, 0, 0, image.width, image.height)
|
|
40
|
+
} else ctx.drawImage(image, 0, 0, width, height)
|
|
41
|
+
return canvas
|
|
42
|
+
},
|
|
43
|
+
setPatternTransform(pattern: ICanvasPattern, transform?: IMatrixData, paint?: ILeaferImagePatternPaint): void {
|
|
44
|
+
try {
|
|
45
|
+
if (transform && pattern.setTransform) {
|
|
46
|
+
pattern.setTransform(transform) // maybe error
|
|
47
|
+
transform = undefined
|
|
48
|
+
}
|
|
49
|
+
} catch { }
|
|
50
|
+
if (paint) DataHelper.stintSet(paint, 'transform', transform)
|
|
21
51
|
}
|
|
22
52
|
}
|
|
23
|
-
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const { image } = Platform
|