@leafer/path 1.0.0-rc.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/path",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0-rc.10",
4
4
  "description": "@leafer/path",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,10 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/math": "1.0.0-rc.1",
26
- "@leafer/debug": "1.0.0-rc.1"
25
+ "@leafer/math": "1.0.0-rc.10",
26
+ "@leafer/debug": "1.0.0-rc.10"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.1"
29
+ "@leafer/interface": "1.0.0-rc.10"
30
30
  }
31
31
  }
@@ -1,9 +1,11 @@
1
1
  import { IPathCommandData } from '@leafer/interface'
2
2
  import { OneRadian, PI2 } from '@leafer/math'
3
+ import { Platform } from '@leafer/platform'
3
4
 
4
5
  import { PathCommandMap } from './PathCommandMap'
5
6
  import { BezierHelper } from './BezierHelper'
6
7
 
8
+
7
9
  const { sin, cos, sqrt, atan2 } = Math
8
10
  const { ellipse } = BezierHelper
9
11
 
@@ -56,7 +58,7 @@ export const EllipseHelper = {
56
58
 
57
59
  const anticlockwise = totalRadian < 0 ? 1 : 0
58
60
 
59
- if (curveMode) {
61
+ if (curveMode || Platform.ellipseToCurve) {
60
62
  ellipse(data, centerX, centerY, radiusX, radiusY, rotation, startRadian / OneRadian, endRadian / OneRadian, anticlockwise as unknown as boolean)
61
63
  } else {
62
64
  if (radiusX === radiusY && !rotation) {
package/src/PathBounds.ts CHANGED
@@ -8,7 +8,7 @@ import { PathCommandMap as Command } from './PathCommandMap'
8
8
 
9
9
  const { M, L, C, Q, Z, N, D, X, G, F, O, P, U } = Command
10
10
  const { toTwoPointBounds, toTwoPointBoundsByQuadraticCurve, arcTo, arc, ellipse } = BezierHelper
11
- const { add, copy, addPoint, setPoint, addBounds, toBounds } = TwoPointBoundsHelper
11
+ const { addPointBounds, copy, addPoint, setPoint, addBounds, toBounds } = TwoPointBoundsHelper
12
12
  const debug = Debug.get('PathBounds')
13
13
 
14
14
  let radius: number, radiusX: number, radiusY: number
@@ -54,7 +54,7 @@ export const PathBounds = {
54
54
  toX = data[i + 5]
55
55
  toY = data[i + 6]
56
56
  toTwoPointBounds(x, y, data[i + 1], data[i + 2], data[i + 3], data[i + 4], toX, toY, tempPointBounds)
57
- add(setPointBounds, tempPointBounds)
57
+ addPointBounds(setPointBounds, tempPointBounds)
58
58
  x = toX
59
59
  y = toY
60
60
  i += 7
@@ -65,7 +65,7 @@ export const PathBounds = {
65
65
  toX = data[i + 3]
66
66
  toY = data[i + 4]
67
67
  toTwoPointBoundsByQuadraticCurve(x, y, x1, y1, toX, toY, tempPointBounds)
68
- add(setPointBounds, tempPointBounds)
68
+ addPointBounds(setPointBounds, tempPointBounds)
69
69
  x = toX
70
70
  y = toY
71
71
  i += 5
@@ -91,7 +91,7 @@ export const PathBounds = {
91
91
  break
92
92
  case G: // ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
93
93
  ellipse(null, data[i + 1], data[i + 2], data[i + 3], data[i + 4], data[i + 5], data[i + 6], data[i + 7], data[i + 8] as unknown as boolean, tempPointBounds, setEndPoint)
94
- i === 0 ? copy(setPointBounds, tempPointBounds) : add(setPointBounds, tempPointBounds)
94
+ i === 0 ? copy(setPointBounds, tempPointBounds) : addPointBounds(setPointBounds, tempPointBounds)
95
95
  x = setEndPoint.x
96
96
  y = setEndPoint.y
97
97
  i += 9
@@ -107,7 +107,7 @@ export const PathBounds = {
107
107
  break
108
108
  case O: // arc(x, y, radius, startAngle, endAngle, anticlockwise)
109
109
  arc(null, data[i + 1], data[i + 2], data[i + 3], data[i + 4], data[i + 5], data[i + 6] as unknown as boolean, tempPointBounds, setEndPoint)
110
- i === 0 ? copy(setPointBounds, tempPointBounds) : add(setPointBounds, tempPointBounds)
110
+ i === 0 ? copy(setPointBounds, tempPointBounds) : addPointBounds(setPointBounds, tempPointBounds)
111
111
  x = setEndPoint.x
112
112
  y = setEndPoint.y
113
113
  i += 7
@@ -122,7 +122,7 @@ export const PathBounds = {
122
122
  break
123
123
  case U: // arcTo(x1, y1, x2, y2, radius)
124
124
  arcTo(null, x, y, data[i + 1], data[i + 2], data[i + 3], data[i + 4], data[i + 5], tempPointBounds, setEndPoint)
125
- i === 0 ? copy(setPointBounds, tempPointBounds) : add(setPointBounds, tempPointBounds)
125
+ i === 0 ? copy(setPointBounds, tempPointBounds) : addPointBounds(setPointBounds, tempPointBounds)
126
126
  x = setEndPoint.x
127
127
  y = setEndPoint.y
128
128
  i += 6
@@ -1,10 +1,13 @@
1
1
  import { IPathCommandData, IPointData } from '@leafer/interface'
2
+ import { MathHelper, PointHelper } from '@leafer/math'
3
+
2
4
  import { PathCommandMap } from './PathCommandMap'
3
5
  import { BezierHelper } from './BezierHelper'
4
- import { MathHelper } from '@leafer/math'
5
6
 
6
7
 
7
8
  const { M, L, C, Q, Z, N, D, X, G, F, O, P, U } = PathCommandMap
9
+ const { getMinDistanceFrom, getRadianFrom } = PointHelper
10
+ const { tan, min, abs } = Math
8
11
  const startPoint = {} as IPointData
9
12
 
10
13
  export const PathCommandDataHelper = {
@@ -73,8 +76,13 @@ export const PathCommandDataHelper = {
73
76
  }
74
77
  },
75
78
 
76
- arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number): void {
77
- data.push(U, x1, y1, x2, y2, radius)
79
+ arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number, lastX?: number, lastY?: number): void {
80
+ if (lastX !== undefined) {
81
+ const maxRadius = tan(getRadianFrom(lastX, lastY, x1, y1, x2, y2) / 2) * (getMinDistanceFrom(lastX, lastY, x1, y1, x2, y2) / 2)
82
+ data.push(U, x1, y1, x2, y2, min(radius, abs(maxRadius)))
83
+ } else {
84
+ data.push(U, x1, y1, x2, y2, radius)
85
+ }
78
86
  },
79
87
 
80
88
  // new
@@ -1,5 +1,5 @@
1
1
  import { IPathCommandData, IPointData } from '@leafer/interface'
2
- import { StringNumberMap } from '@leafer/math'
2
+ import { MathHelper, StringNumberMap } from '@leafer/math'
3
3
  import { Debug } from '@leafer/debug'
4
4
 
5
5
  import { PathCommandMap as Command, NeedConvertToCanvasCommandMap, NeedConvertToCurveCommandMap, PathCommandLengthMap, PathNumberCommandMap, PathNumberCommandLengthMap } from './PathCommandMap'
@@ -26,7 +26,7 @@ export const PathConvert = {
26
26
 
27
27
  current: { dot: 0 } as ICurrentCommand,
28
28
 
29
- stringify(data: IPathCommandData): string {
29
+ stringify(data: IPathCommandData, floatLength?: number): string {
30
30
  let i = 0, len = data.length, count: number, str: string = '', command: number, lastCommand: number
31
31
  while (i < len) {
32
32
  command = data[i]
@@ -38,7 +38,7 @@ export const PathConvert = {
38
38
  }
39
39
 
40
40
  for (let j = 1; j < count; j++) {
41
- str += data[i + j];
41
+ str += MathHelper.float(data[i + j], floatLength);
42
42
  (j === count - 1) || (str += ' ')
43
43
  }
44
44
 
package/src/PathCorner.ts CHANGED
@@ -1,10 +1,74 @@
1
1
  import { IPathCommandData } from '@leafer/interface'
2
+ import { PointHelper } from '@leafer/math'
2
3
 
4
+ import { PathCommandMap as Command } from './PathCommandMap'
5
+ import { PathCommandDataHelper } from './PathCommandDataHelper'
6
+
7
+
8
+ const { M, L, C, Z } = Command
9
+ const { getCenterX, getCenterY } = PointHelper
10
+ const { arcTo } = PathCommandDataHelper
3
11
 
4
12
  export const PathCorner = {
5
13
 
6
- smooth(data: IPathCommandData, _cornerRadius: number, _cornerSmoothing?: number): IPathCommandData {
7
- return data
14
+ smooth(data: IPathCommandData, cornerRadius: number, _cornerSmoothing?: number): IPathCommandData {
15
+ let command: number
16
+ let i = 0, x = 0, y = 0, startX = 0, startY = 0, secondX = 0, secondY = 0, lastX = 0, lastY = 0
17
+
18
+ const len = data.length
19
+ const smooth: IPathCommandData = []
20
+
21
+ while (i < len) {
22
+ command = data[i]
23
+ switch (command) {
24
+ case M: //moveto(x, y)
25
+ startX = lastX = data[i + 1]
26
+ startY = lastY = data[i + 2]
27
+ i += 3
28
+ if (data[i] === L) { // next lineTo
29
+ secondX = data[i + 1]
30
+ secondY = data[i + 2]
31
+ smooth.push(M, getCenterX(startX, secondX), getCenterY(startY, secondY))
32
+ } else {
33
+ smooth.push(M, startX, startY)
34
+ }
35
+ break
36
+ case L: //lineto(x, y)
37
+ x = data[i + 1]
38
+ y = data[i + 2]
39
+ i += 3
40
+ switch (data[i]) { // next command
41
+ case L: // lineTo()
42
+ arcTo(smooth, x, y, data[i + 1], data[i + 2], cornerRadius, lastX, lastY) // use arcTo(x1, y1, x2, y2, radius)
43
+ break
44
+ case Z: // closePath()
45
+ arcTo(smooth, x, y, startX, startY, cornerRadius, lastX, lastY) // use arcTo(x1, y1, x2, y2, radius)
46
+ break
47
+ default:
48
+ smooth.push(L, x, y)
49
+ }
50
+ lastX = x
51
+ lastY = y
52
+ break
53
+ case C: //bezierCurveTo(x1, y1, x2, y2, x, y)
54
+ smooth.push(C, data[i + 1], data[i + 2], data[i + 3], data[i + 4], data[i + 5], data[i + 6])
55
+ i += 7
56
+ break
57
+ case Z: //closepath()
58
+ arcTo(smooth, startX, startY, secondX, secondY, cornerRadius, lastX, lastY) // use arcTo(x1, y1, x2, y2, radius)
59
+ smooth.push(Z)
60
+ i += 1
61
+ break
62
+
63
+ }
64
+ }
65
+
66
+ if (command !== Z) {
67
+ smooth[1] = startX
68
+ smooth[2] = startY
69
+ }
70
+
71
+ return smooth
8
72
  }
9
73
 
10
74
  }
package/src/RectHelper.ts CHANGED
@@ -4,19 +4,16 @@ import { MathHelper } from '@leafer/math'
4
4
  export const RectHelper = {
5
5
 
6
6
  drawRoundRect(drawer: IPathDrawer, x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void {
7
- let [topLeft, topRight, bottomRight, bottomLeft] = MathHelper.fourNumber(cornerRadius)
7
+ const data = MathHelper.fourNumber(cornerRadius, Math.min(width / 2, height / 2))
8
8
 
9
- const max = Math.min(width / 2, height / 2)
10
- if (topLeft > max) topLeft = max
11
- if (topRight > max) topRight = max
12
- if (bottomRight > max) bottomRight = max
13
- if (bottomLeft > max) bottomLeft = max
9
+ const right = x + width
10
+ const bottom = y + height
14
11
 
15
- topLeft ? drawer.moveTo(x + topLeft, y) : drawer.moveTo(x, y)
16
- topRight ? drawer.arcTo(x + width, y, x + width, y + height, topRight) : drawer.lineTo(x + width, y)
17
- bottomRight ? drawer.arcTo(x + width, y + height, x, y + height, bottomRight) : drawer.lineTo(x + width, y + height)
18
- bottomLeft ? drawer.arcTo(x, y + height, x, y, bottomLeft) : drawer.lineTo(x, y + height)
19
- topLeft ? drawer.arcTo(x, y, x + width, y, topLeft) : drawer.lineTo(x, y)
12
+ data[0] ? drawer.moveTo(x + data[0], y) : drawer.moveTo(x, y)
13
+ data[1] ? drawer.arcTo(right, y, right, bottom, data[1]) : drawer.lineTo(right, y)
14
+ data[2] ? drawer.arcTo(right, bottom, x, bottom, data[2]) : drawer.lineTo(right, bottom)
15
+ data[3] ? drawer.arcTo(x, bottom, x, y, data[3]) : drawer.lineTo(x, bottom)
16
+ data[0] ? drawer.arcTo(x, y, right, y, data[0]) : drawer.lineTo(x, y)
20
17
  }
21
18
 
22
19
  }
package/types/index.d.ts CHANGED
@@ -14,7 +14,7 @@ interface ICurrentCommand {
14
14
  }
15
15
  declare const PathConvert: {
16
16
  current: ICurrentCommand;
17
- stringify(data: IPathCommandData): string;
17
+ stringify(data: IPathCommandData, floatLength?: number): string;
18
18
  parse(pathString: string, curveMode?: boolean): IPathCommandData;
19
19
  toCanvasData(old: IPathCommandData, curveMode?: boolean): IPathCommandData;
20
20
  copyData(data: IPathCommandData, old: IPathCommandData, index: number, count: number): void;
@@ -51,7 +51,7 @@ declare const PathCommandDataHelper: {
51
51
  roundRect(data: IPathCommandData, x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void;
52
52
  ellipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
53
53
  arc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
54
- arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number): void;
54
+ arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number, lastX?: number, lastY?: number): void;
55
55
  drawEllipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
56
56
  drawArc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
57
57
  drawPoints(data: IPathCommandData, points: number[], curve?: boolean | number, close?: boolean): void;
@@ -67,7 +67,7 @@ declare const PathBounds: {
67
67
  };
68
68
 
69
69
  declare const PathCorner: {
70
- smooth(data: IPathCommandData, _cornerRadius: number, _cornerSmoothing?: number): IPathCommandData;
70
+ smooth(data: IPathCommandData, cornerRadius: number, _cornerSmoothing?: number): IPathCommandData;
71
71
  };
72
72
 
73
73
  declare const BezierHelper: {