@leafer/path 1.0.0-beta.8 → 1.0.0-rc.1

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,12 +1,15 @@
1
1
  {
2
2
  "name": "@leafer/path",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.0-rc.1",
4
4
  "description": "@leafer/path",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
+ "types": "types/index.d.ts",
8
9
  "files": [
9
- "src"
10
+ "src",
11
+ "types",
12
+ "dist"
10
13
  ],
11
14
  "repository": {
12
15
  "type": "git",
@@ -19,10 +22,10 @@
19
22
  "leaferjs"
20
23
  ],
21
24
  "dependencies": {
22
- "@leafer/math": "1.0.0-beta.8",
23
- "@leafer/debug": "1.0.0-beta.8"
25
+ "@leafer/math": "1.0.0-rc.1",
26
+ "@leafer/debug": "1.0.0-rc.1"
24
27
  },
25
28
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-beta.8"
29
+ "@leafer/interface": "1.0.0-rc.1"
27
30
  }
28
31
  }
@@ -5,13 +5,74 @@ import { PathCommandMap } from './PathCommandMap'
5
5
  import { RectHelper } from './RectHelper'
6
6
  import { PathHelper } from './PathHelper'
7
7
 
8
- const { sin, cos, atan2, ceil, abs, PI } = Math
8
+ const { sin, cos, atan2, ceil, abs, PI, sqrt, pow } = Math
9
9
  const { setPoint, addPoint } = TwoPointBoundsHelper
10
10
  const { set } = PointHelper
11
+ const { M, L, C, Q, Z } = PathCommandMap
11
12
  const tempPoint = {} as IPointData
12
13
 
13
14
  export const BezierHelper = {
14
15
 
16
+ points(data: IPathCommandData, points: number[], curve?: boolean | number, close?: boolean): void {
17
+ data.push(M, points[0], points[1])
18
+
19
+ if (curve && points.length > 5) {
20
+
21
+ let aX: number, aY: number, bX: number, bY: number, cX: number, cY: number, c1X: number, c1Y: number, c2X: number, c2Y: number
22
+ let ba: number, cb: number, d: number, len = points.length
23
+ const t = curve === true ? 0.5 : curve as number
24
+
25
+ if (close) {
26
+ points = [points[len - 2], points[len - 1], ...points, points[0], points[1], points[2], points[3]]
27
+ len = points.length
28
+ }
29
+
30
+ for (let i = 2; i < len - 2; i += 2) {
31
+ aX = points[i - 2]
32
+ aY = points[i - 1]
33
+
34
+ bX = points[i]
35
+ bY = points[i + 1]
36
+
37
+ cX = points[i + 2]
38
+ cY = points[i + 3]
39
+
40
+ ba = sqrt(pow(bX - aX, 2) + pow(bY - aY, 2))
41
+ cb = sqrt(pow(cX - bX, 2) + pow(cY - bY, 2))
42
+
43
+ d = ba + cb
44
+ ba = (t * ba) / d
45
+ cb = (t * cb) / d
46
+
47
+ cX -= aX
48
+ cY -= aY
49
+
50
+ c1X = bX - ba * cX
51
+ c1Y = bY - ba * cY
52
+
53
+ if (i === 2) {
54
+ if (!close) data.push(Q, c1X, c1Y, bX, bY)
55
+ } else {
56
+ data.push(C, c2X, c2Y, c1X, c1Y, bX, bY)
57
+ }
58
+
59
+ c2X = bX + cb * cX
60
+ c2Y = bY + cb * cY
61
+ }
62
+
63
+ if (!close) data.push(Q, c2X, c2Y, points[len - 2], points[len - 1])
64
+
65
+ } else {
66
+
67
+ for (let i = 2, len = points.length; i < len; i += 2) {
68
+ data.push(L, points[i], points[i + 1])
69
+ }
70
+
71
+ }
72
+
73
+ if (close) data.push(Z)
74
+ },
75
+
15
76
  rect(data: IPathCommandData, x: number, y: number, width: number, height: number) {
16
77
  PathHelper.creator.path = data
17
78
  PathHelper.creator.moveTo(x, y).lineTo(x + width, y).lineTo(x + width, y + height).lineTo(x, y + height).lineTo(x, y)
@@ -35,7 +96,7 @@ export const BezierHelper = {
35
96
  if (totalRadian < 0) totalRadian += PI2
36
97
 
37
98
  if (totalRadian === PI || (abs(BAx + BAy) < 1.e-12) || (abs(CBx + CBy) < 1.e-12)) { // invalid
38
- if (data) data.push(PathCommandMap.L, x1, y1)
99
+ if (data) data.push(L, x1, y1)
39
100
  if (setPointBounds) {
40
101
  setPoint(setPointBounds, fromX, fromY)
41
102
  addPoint(setPointBounds, x1, y1)
@@ -97,7 +158,7 @@ export const BezierHelper = {
97
158
 
98
159
  let fromX = cx + x, fromY = cy + y
99
160
 
100
- if (data) data.push(PathCommandMap.L, fromX, fromY)
161
+ if (data) data.push(L, fromX, fromY)
101
162
  if (setPointBounds) setPoint(setPointBounds, fromX, fromY)
102
163
  if (setStartPoint) set(setStartPoint, fromX, fromY)
103
164
 
@@ -113,7 +174,7 @@ export const BezierHelper = {
113
174
  x2 = cx + x + control * (rotationCos * radiusX * endSin + rotationSin * radiusY * endCos)
114
175
  y2 = cy + y + control * (rotationSin * radiusX * endSin - rotationCos * radiusY * endCos)
115
176
 
116
- if (data) data.push(PathCommandMap.C, x1, y1, x2, y2, cx + x, cy + y)
177
+ if (data) data.push(C, x1, y1, x2, y2, cx + x, cy + y)
117
178
  if (setPointBounds) toTwoPointBounds(cx + startX, cy + startY, x1, y1, x2, y2, cx + x, cy + y, setPointBounds, true)
118
179
 
119
180
  startX = x
@@ -129,7 +190,7 @@ export const BezierHelper = {
129
190
  },
130
191
 
131
192
  quadraticCurveTo(data: IPathCommandData, fromX: number, fromY: number, x1: number, y1: number, toX: number, toY: number): void {
132
- data.push(PathCommandMap.C, (fromX + 2 * x1) / 3, (fromY + 2 * y1) / 3, (toX + 2 * x1) / 3, (toY + 2 * y1) / 3, toX, toY)
193
+ data.push(C, (fromX + 2 * x1) / 3, (fromY + 2 * y1) / 3, (toX + 2 * x1) / 3, (toY + 2 * y1) / 3, toX, toY)
133
194
  },
134
195
 
135
196
  toTwoPointBoundsByQuadraticCurve(fromX: number, fromY: number, x1: number, y1: number, toX: number, toY: number, pointBounds: ITwoPointBoundsData, addMode?: boolean): void {
@@ -73,7 +73,13 @@ export const PathCommandDataHelper = {
73
73
  }
74
74
  },
75
75
 
76
- moveToEllipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
76
+ arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number): void {
77
+ data.push(U, x1, y1, x2, y2, radius)
78
+ },
79
+
80
+ // new
81
+
82
+ drawEllipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
77
83
  if (rotation === undefined) rotation = 0
78
84
  if (startAngle === undefined) startAngle = 0
79
85
  if (endAngle === undefined) endAngle = 360
@@ -82,7 +88,7 @@ export const PathCommandDataHelper = {
82
88
  ellipse(data, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
83
89
  },
84
90
 
85
- moveToArc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
91
+ drawArc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
86
92
  if (startAngle === undefined) startAngle = 0
87
93
  if (endAngle === undefined) endAngle = 360
88
94
  BezierHelper.arc(null, x, y, radius, startAngle, endAngle, anticlockwise, null, null, startPoint)
@@ -90,9 +96,10 @@ export const PathCommandDataHelper = {
90
96
  arc(data, x, y, radius, startAngle, endAngle, anticlockwise)
91
97
  },
92
98
 
93
- arcTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, radius: number): void {
94
- data.push(U, x1, y1, x2, y2, radius)
99
+ drawPoints(data: IPathCommandData, points: number[], curve?: boolean | number, close?: boolean): void {
100
+ BezierHelper.points(data, points, curve, close)
95
101
  }
102
+
96
103
  }
97
104
 
98
105
  const { ellipse, arc } = PathCommandDataHelper
@@ -3,7 +3,7 @@ import { PathCommandDataHelper } from './PathCommandDataHelper'
3
3
  import { PathHelper } from './PathHelper'
4
4
 
5
5
 
6
- const { moveTo, lineTo, quadraticCurveTo, bezierCurveTo, closePath, beginPath, rect, roundRect, ellipse, arc, arcTo, moveToEllipse, moveToArc } = PathCommandDataHelper
6
+ const { moveTo, lineTo, quadraticCurveTo, bezierCurveTo, closePath, beginPath, rect, roundRect, ellipse, arc, arcTo, drawEllipse, drawArc, drawPoints } = PathCommandDataHelper
7
7
 
8
8
  export class PathCreator implements IPathDrawer {
9
9
 
@@ -78,13 +78,18 @@ export class PathCreator implements IPathDrawer {
78
78
 
79
79
  // moveTo, then draw
80
80
 
81
- public moveToEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
82
- moveToEllipse(this.path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
81
+ public drawEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
82
+ drawEllipse(this.path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
83
83
  return this
84
84
  }
85
85
 
86
- public moveToArc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
87
- moveToArc(this.path, x, y, radius, startAngle, endAngle, anticlockwise)
86
+ public drawArc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
87
+ drawArc(this.path, x, y, radius, startAngle, endAngle, anticlockwise)
88
+ return this
89
+ }
90
+
91
+ public drawPoints(points: number[], curve?: boolean | number, close?: boolean): PathCreator {
92
+ drawPoints(this.path, points, curve, close)
88
93
  return this
89
94
  }
90
95
 
@@ -0,0 +1,100 @@
1
+ import { IPathCreator, IPathCommandData, IPathDrawer, IPathString, IBoundsData, ITwoPointBoundsData, IPointData, INumberMap, IStringMap } from '@leafer/interface';
2
+
3
+ declare const PathHelper: {
4
+ creator: IPathCreator;
5
+ parse(_pathString: string, _curveMode?: boolean): IPathCommandData;
6
+ convertToCanvasData(_old: IPathCommandData, _curveMode?: boolean): IPathCommandData;
7
+ };
8
+
9
+ interface ICurrentCommand {
10
+ name?: number;
11
+ length?: number;
12
+ index?: number;
13
+ dot?: number;
14
+ }
15
+ declare const PathConvert: {
16
+ current: ICurrentCommand;
17
+ stringify(data: IPathCommandData): string;
18
+ parse(pathString: string, curveMode?: boolean): IPathCommandData;
19
+ toCanvasData(old: IPathCommandData, curveMode?: boolean): IPathCommandData;
20
+ copyData(data: IPathCommandData, old: IPathCommandData, index: number, count: number): void;
21
+ pushData(data: IPathCommandData, strNum: string | number): void;
22
+ };
23
+
24
+ declare class PathCreator implements IPathDrawer {
25
+ path: IPathCommandData;
26
+ constructor(path?: IPathCommandData | IPathString);
27
+ beginPath(): PathCreator;
28
+ moveTo(x: number, y: number): PathCreator;
29
+ lineTo(x: number, y: number): PathCreator;
30
+ bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): PathCreator;
31
+ quadraticCurveTo(x1: number, y1: number, x: number, y: number): PathCreator;
32
+ closePath(): PathCreator;
33
+ rect(x: number, y: number, width: number, height: number): PathCreator;
34
+ roundRect(x: number, y: number, width: number, height: number, cornerRadius: number | number[]): PathCreator;
35
+ ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
36
+ arc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
37
+ arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): PathCreator;
38
+ drawEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
39
+ drawArc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
40
+ drawPoints(points: number[], curve?: boolean | number, close?: boolean): PathCreator;
41
+ }
42
+
43
+ declare const PathCommandDataHelper: {
44
+ beginPath(data: IPathCommandData): void;
45
+ moveTo(data: IPathCommandData, x: number, y: number): void;
46
+ lineTo(data: IPathCommandData, x: number, y: number): void;
47
+ bezierCurveTo(data: IPathCommandData, x1: number, y1: number, x2: number, y2: number, x: number, y: number): void;
48
+ quadraticCurveTo(data: IPathCommandData, x1: number, y1: number, x: number, y: number): void;
49
+ closePath(data: IPathCommandData): void;
50
+ rect(data: IPathCommandData, x: number, y: number, width: number, height: number): void;
51
+ roundRect(data: IPathCommandData, x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void;
52
+ ellipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
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;
55
+ drawEllipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
56
+ drawArc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void;
57
+ drawPoints(data: IPathCommandData, points: number[], curve?: boolean | number, close?: boolean): void;
58
+ };
59
+
60
+ declare const PathDrawer: {
61
+ drawPathByData(drawer: IPathDrawer, data: IPathCommandData): void;
62
+ };
63
+
64
+ declare const PathBounds: {
65
+ toBounds(data: IPathCommandData, setBounds: IBoundsData): void;
66
+ toTwoPointBounds(data: IPathCommandData, setPointBounds: ITwoPointBoundsData): void;
67
+ };
68
+
69
+ declare const PathCorner: {
70
+ smooth(data: IPathCommandData, _cornerRadius: number, _cornerSmoothing?: number): IPathCommandData;
71
+ };
72
+
73
+ declare const BezierHelper: {
74
+ points(data: IPathCommandData, points: number[], curve?: boolean | number, close?: boolean): void;
75
+ rect(data: IPathCommandData, x: number, y: number, width: number, height: number): void;
76
+ roundRect(data: IPathCommandData, x: number, y: number, width: number, height: number, radius: number | number[]): void;
77
+ arcTo(data: IPathCommandData | null | void, fromX: number, fromY: number, x1: number, y1: number, toX: number, toY: number, radius: number, setPointBounds?: ITwoPointBoundsData, setEndPoint?: IPointData, setStartPoint?: IPointData): void;
78
+ arc(data: IPathCommandData | null | void, x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean, setPointBounds?: ITwoPointBoundsData, setEndPoint?: IPointData, setStartPoint?: IPointData): void;
79
+ ellipse(data: IPathCommandData | null | void, cx: number, cy: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: boolean, setPointBounds?: ITwoPointBoundsData, setEndPoint?: IPointData, setStartPoint?: IPointData): void;
80
+ quadraticCurveTo(data: IPathCommandData, fromX: number, fromY: number, x1: number, y1: number, toX: number, toY: number): void;
81
+ toTwoPointBoundsByQuadraticCurve(fromX: number, fromY: number, x1: number, y1: number, toX: number, toY: number, pointBounds: ITwoPointBoundsData, addMode?: boolean): void;
82
+ toTwoPointBounds(fromX: number, fromY: number, x1: number, y1: number, x2: number, y2: number, toX: number, toY: number, pointBounds: ITwoPointBoundsData, addMode?: boolean): void;
83
+ getPointAndSet(t: number, fromX: number, fromY: number, x1: number, y1: number, x2: number, y2: number, toX: number, toY: number, setPoint: IPointData): void;
84
+ getPoint(t: number, fromX: number, fromY: number, x1: number, y1: number, x2: number, y2: number, toX: number, toY: number): IPointData;
85
+ };
86
+
87
+ declare const EllipseHelper: {
88
+ ellipticalArc(data: IPathCommandData, fromX: number, fromY: number, radiusX: number, radiusY: number, rotation: number, largeFlag: number, sweepFlag: number, toX: number, toY: number, curveMode?: boolean): void;
89
+ };
90
+
91
+ declare const RectHelper: {
92
+ drawRoundRect(drawer: IPathDrawer, x: number, y: number, width: number, height: number, cornerRadius: number | number[]): void;
93
+ };
94
+
95
+ declare const PathCommandMap: INumberMap;
96
+ declare const NeedConvertToCanvasCommandMap: INumberMap;
97
+ declare const PathNumberCommandMap: IStringMap;
98
+ declare const PathNumberCommandLengthMap: INumberMap;
99
+
100
+ export { BezierHelper, EllipseHelper, NeedConvertToCanvasCommandMap, PathBounds, PathCommandDataHelper, PathCommandMap, PathConvert, PathCorner, PathCreator, PathDrawer, PathHelper, PathNumberCommandLengthMap, PathNumberCommandMap, RectHelper };