@leafer/platform 1.12.3 → 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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/Platform.ts +27 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/platform",
3
- "version": "1.12.3",
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": "1.12.3",
26
- "@leafer/debug": "1.12.3"
25
+ "@leafer/data": "2.0.0",
26
+ "@leafer/debug": "2.0.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.12.3"
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,23 +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 } = Platform.image
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(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),)
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
- 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)
42
+
43
+ if (image.canUse(view)) {
44
+ if (clip) {
45
+ const scaleX = width / clip.width, scaleY = height / clip.height
46
+ ctx.setTransform(scaleX, 0, 0, scaleY, -clip.x * scaleX, -clip.y * scaleY)
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
+ }
54
+ }
55
+
41
56
  return canvas
42
57
  },
58
+ canUse(view: any): boolean {
59
+ return view && view.width && !view.__closed // __closed 为内部标记已销毁
60
+ },
43
61
  setPatternTransform(pattern: ICanvasPattern, transform?: IMatrixData, paint?: ILeaferImagePatternPaint): void {
44
62
  try {
45
63
  if (transform && pattern.setTransform) {