@leafer/platform 1.12.4 → 2.0.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 +23 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/platform",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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": "
|
|
26
|
-
"@leafer/debug": "
|
|
25
|
+
"@leafer/data": "2.0.0",
|
|
26
|
+
"@leafer/debug": "2.0.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@leafer/interface": "
|
|
29
|
+
"@leafer/interface": "2.0.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/Platform.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { IPlatform, IObject, IBoundsData, ICanvasPattern, IMatrixData, ILeaferImagePatternPaint, ISizeData, ICanvasContext2D } from '@leafer/interface'
|
|
1
|
+
import { IPlatform, IObject, IBoundsData, ICanvasPattern, IMatrixData, ILeaferImagePatternPaint, ISizeData, ICanvasContext2D, IInterlace } from '@leafer/interface'
|
|
2
2
|
import { DataHelper } from '@leafer/data'
|
|
3
|
+
import { UnitConvertHelper } from '@leafer/math'
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
const { floor, max } = Math
|
|
@@ -23,25 +24,40 @@ export const Platform: IPlatform = {
|
|
|
23
24
|
return image.isLarge(size, scaleX, scaleY, image.maxPatternSize)
|
|
24
25
|
},
|
|
25
26
|
getRealURL(url: string): string {
|
|
26
|
-
const { prefix, suffix } =
|
|
27
|
+
const { prefix, suffix } = image
|
|
27
28
|
if (suffix && !url.startsWith('data:') && !url.startsWith('blob:')) url += (url.includes("?") ? "&" : "?") + suffix
|
|
28
29
|
if (prefix && url[0] === '/') url = prefix + url
|
|
29
30
|
return url
|
|
30
31
|
},
|
|
31
|
-
resize(
|
|
32
|
-
const
|
|
32
|
+
resize(view: any, width: number, height: number, xGap?: number, yGap?: number, clip?: IBoundsData, smooth?: boolean, opacity?: number, _filters?: IObject, interlace?: IInterlace): any {
|
|
33
|
+
const realWidth = max(floor(width + (xGap || 0)), 1), realHeight = max(floor(height + (yGap || 0)), 1)
|
|
34
|
+
|
|
35
|
+
let interlaceX: boolean, interlaceY: boolean, interlaceOffset: number
|
|
36
|
+
if (interlace && (interlaceOffset = UnitConvertHelper.number(interlace.offset, (interlaceX = interlace.type === 'x') ? width : height))) interlaceX || (interlaceY = true)
|
|
37
|
+
|
|
38
|
+
const canvas = Platform.origin.createCanvas(interlaceY ? realWidth * 2 : realWidth, interlaceX ? realHeight * 2 : realHeight)
|
|
33
39
|
const ctx: ICanvasContext2D = canvas.getContext('2d')
|
|
34
40
|
if (opacity) ctx.globalAlpha = opacity
|
|
35
41
|
ctx.imageSmoothingEnabled = smooth === false ? false : true // 平滑绘制
|
|
36
|
-
|
|
42
|
+
|
|
43
|
+
if (image.canUse(view)) {
|
|
37
44
|
if (clip) {
|
|
38
45
|
const scaleX = width / clip.width, scaleY = height / clip.height
|
|
39
46
|
ctx.setTransform(scaleX, 0, 0, scaleY, -clip.x * scaleX, -clip.y * scaleY)
|
|
40
|
-
ctx.drawImage(
|
|
41
|
-
} else ctx.drawImage(
|
|
47
|
+
ctx.drawImage(view, 0, 0, view.width, view.height)
|
|
48
|
+
} else ctx.drawImage(view, 0, 0, width, height)
|
|
49
|
+
|
|
50
|
+
if (interlaceOffset) {
|
|
51
|
+
ctx.drawImage(canvas, 0, 0, realWidth, realHeight, interlaceX ? interlaceOffset - realWidth : realWidth, interlaceX ? realHeight : interlaceOffset - realHeight, realWidth, realHeight)
|
|
52
|
+
ctx.drawImage(canvas, 0, 0, realWidth, realHeight, interlaceX ? interlaceOffset : realWidth, interlaceX ? realHeight : interlaceOffset, realWidth, realHeight)
|
|
53
|
+
}
|
|
42
54
|
}
|
|
55
|
+
|
|
43
56
|
return canvas
|
|
44
57
|
},
|
|
58
|
+
canUse(view: any): boolean {
|
|
59
|
+
return view && view.width && !view.__closed // __closed 为内部标记已销毁
|
|
60
|
+
},
|
|
45
61
|
setPatternTransform(pattern: ICanvasPattern, transform?: IMatrixData, paint?: ILeaferImagePatternPaint): void {
|
|
46
62
|
try {
|
|
47
63
|
if (transform && pattern.setTransform) {
|