@leafer/path 1.0.0-rc.9 → 1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/path",
3
- "version": "1.0.0-rc.9",
3
+ "version": "1.0.0",
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.9",
26
- "@leafer/debug": "1.0.0-rc.9"
25
+ "@leafer/math": "1.0.0",
26
+ "@leafer/debug": "1.0.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.9"
29
+ "@leafer/interface": "1.0.0"
30
30
  }
31
31
  }
@@ -88,18 +88,13 @@ export const PathCommandDataHelper = {
88
88
  // new
89
89
 
90
90
  drawEllipse(data: IPathCommandData, x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
91
- if (rotation === undefined) rotation = 0
92
- if (startAngle === undefined) startAngle = 0
93
- if (endAngle === undefined) endAngle = 360
94
- BezierHelper.ellipse(null, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise, null, null, startPoint)
91
+ BezierHelper.ellipse(null, x, y, radiusX, radiusY, rotation === undefined ? 0 : rotation, startAngle === undefined ? 0 : startAngle, endAngle === undefined ? 360 : endAngle, anticlockwise, null, null, startPoint)
95
92
  data.push(M, startPoint.x, startPoint.y)
96
93
  ellipse(data, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
97
94
  },
98
95
 
99
96
  drawArc(data: IPathCommandData, x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): void {
100
- if (startAngle === undefined) startAngle = 0
101
- if (endAngle === undefined) endAngle = 360
102
- BezierHelper.arc(null, x, y, radius, startAngle, endAngle, anticlockwise, null, null, startPoint)
97
+ BezierHelper.arc(null, x, y, radius, startAngle === undefined ? 0 : startAngle, endAngle === undefined ? 360 : endAngle, anticlockwise, null, null, startPoint)
103
98
  data.push(M, startPoint.x, startPoint.y)
104
99
  arc(data, x, y, radius, startAngle, endAngle, anticlockwise)
105
100
  },
@@ -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
 
@@ -61,12 +61,12 @@ export const PathConvert = {
61
61
  if (StringNumberMap[char]) {
62
62
 
63
63
  if (char === '.') {
64
+ if (current.dot) { pushData(data, num); num = '' } // .375.375
64
65
  current.dot++
65
- if (current.dot > 1) {
66
- pushData(data, num); num = '' // .375.375
67
- }
68
66
  }
69
67
 
68
+ if (num === '0' && char !== '.') { pushData(data, num); num = '' } // 00 01
69
+
70
70
  num += char
71
71
 
72
72
  } else if (Command[char]) {
@@ -1,96 +1,106 @@
1
- import { IPathCommandData, IPathDrawer, IPathString } from '@leafer/interface'
1
+ import { IPathCommandData, IPathCreator, IPathString } from '@leafer/interface'
2
2
  import { PathCommandDataHelper } from './PathCommandDataHelper'
3
3
  import { PathHelper } from './PathHelper'
4
4
 
5
5
 
6
6
  const { moveTo, lineTo, quadraticCurveTo, bezierCurveTo, closePath, beginPath, rect, roundRect, ellipse, arc, arcTo, drawEllipse, drawArc, drawPoints } = PathCommandDataHelper
7
7
 
8
- export class PathCreator implements IPathDrawer {
8
+ export class PathCreator implements IPathCreator { // tip: rewrited Pen
9
9
 
10
- public path: IPathCommandData
10
+ public set path(value: IPathCommandData) { this.__path = value }
11
+ public get path() { return this.__path }
12
+
13
+ public __path: IPathCommandData // 提供一个更安全的内部变量(比如覆盖给Pen时需要用到,避免与原有属性冲突)
11
14
 
12
15
  constructor(path?: IPathCommandData | IPathString) {
16
+ this.set(path)
17
+ }
18
+
19
+ public set(path?: IPathCommandData | IPathString): PathCreator {
13
20
  if (path) {
14
- this.path = typeof path === 'string' ? PathHelper.parse(path) : path
21
+ this.__path = typeof path === 'string' ? PathHelper.parse(path) : path
15
22
  } else {
16
- this.path = []
23
+ this.__path = []
17
24
  }
25
+ return this
18
26
  }
19
27
 
20
28
  public beginPath(): PathCreator {
21
- beginPath(this.path)
29
+ beginPath(this.__path)
22
30
  return this
23
31
  }
24
32
 
25
33
  // svg and canvas
26
34
 
27
35
  public moveTo(x: number, y: number): PathCreator {
28
- moveTo(this.path, x, y)
36
+ moveTo(this.__path, x, y)
29
37
  return this
30
38
  }
31
39
 
32
40
  public lineTo(x: number, y: number): PathCreator {
33
- lineTo(this.path, x, y)
41
+ lineTo(this.__path, x, y)
34
42
  return this
35
43
  }
36
44
 
37
45
  public bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): PathCreator {
38
- bezierCurveTo(this.path, x1, y1, x2, y2, x, y)
46
+ bezierCurveTo(this.__path, x1, y1, x2, y2, x, y)
39
47
  return this
40
48
  }
41
49
 
42
50
  public quadraticCurveTo(x1: number, y1: number, x: number, y: number): PathCreator {
43
- quadraticCurveTo(this.path, x1, y1, x, y)
51
+ quadraticCurveTo(this.__path, x1, y1, x, y)
44
52
  return this
45
53
  }
46
54
 
47
55
  public closePath(): PathCreator {
48
- closePath(this.path)
56
+ closePath(this.__path)
49
57
  return this
50
58
  }
51
59
 
52
60
  // canvas
53
61
 
54
62
  public rect(x: number, y: number, width: number, height: number): PathCreator {
55
- rect(this.path, x, y, width, height)
63
+ rect(this.__path, x, y, width, height)
56
64
  return this
57
65
  }
58
66
 
59
67
  public roundRect(x: number, y: number, width: number, height: number, cornerRadius: number | number[]): PathCreator {
60
- roundRect(this.path, x, y, width, height, cornerRadius)
68
+ roundRect(this.__path, x, y, width, height, cornerRadius)
61
69
  return this
62
70
  }
63
71
 
64
72
  public ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
65
- ellipse(this.path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
73
+ ellipse(this.__path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
66
74
  return this
67
75
  }
68
76
 
69
77
  public arc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator {
70
- arc(this.path, x, y, radius, startAngle, endAngle, anticlockwise)
78
+ arc(this.__path, x, y, radius, startAngle, endAngle, anticlockwise)
71
79
  return this
72
80
  }
73
81
 
74
82
  public arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): PathCreator {
75
- arcTo(this.path, x1, y1, x2, y2, radius)
83
+ arcTo(this.__path, x1, y1, x2, y2, radius)
76
84
  return this
77
85
  }
78
86
 
79
87
  // moveTo, then draw
80
88
 
81
89
  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)
90
+ drawEllipse(this.__path, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
83
91
  return this
84
92
  }
85
93
 
86
94
  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)
95
+ drawArc(this.__path, x, y, radius, startAngle, endAngle, anticlockwise)
88
96
  return this
89
97
  }
90
98
 
91
99
  public drawPoints(points: number[], curve?: boolean | number, close?: boolean): PathCreator {
92
- drawPoints(this.path, points, curve, close)
100
+ drawPoints(this.__path, points, curve, close)
93
101
  return this
94
102
  }
95
103
 
104
+ public clearPath = this.beginPath
105
+
96
106
  }
package/src/index.ts CHANGED
@@ -3,7 +3,6 @@ export { PathConvert } from './PathConvert'
3
3
  export { PathCreator } from './PathCreator'
4
4
  export { PathCommandDataHelper } from './PathCommandDataHelper'
5
5
  export { PathDrawer } from './PathDrawer'
6
- export { PathScaler } from './PathScaler'
7
6
  export { PathBounds } from './PathBounds'
8
7
  export { PathCorner } from './PathCorner'
9
8
  export { BezierHelper } from './BezierHelper'
@@ -20,3 +19,5 @@ import { PathHelper } from './PathHelper'
20
19
  PathHelper.creator = new PathCreator()
21
20
  PathHelper.parse = PathConvert.parse
22
21
  PathHelper.convertToCanvasData = PathConvert.toCanvasData
22
+
23
+ export const pen = new PathCreator()
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IPathCreator, IPathCommandData, IPathDrawer, IPathString, IBoundsData, ITwoPointBoundsData, IPointData, INumberMap, IStringMap } from '@leafer/interface';
1
+ import { IPathCreator, IPathCommandData, IPathString, IPathDrawer, IBoundsData, ITwoPointBoundsData, IPointData, INumberMap, IStringMap } from '@leafer/interface';
2
2
 
3
3
  declare const PathHelper: {
4
4
  creator: IPathCreator;
@@ -14,16 +14,19 @@ 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;
21
21
  pushData(data: IPathCommandData, strNum: string | number): void;
22
22
  };
23
23
 
24
- declare class PathCreator implements IPathDrawer {
25
- path: IPathCommandData;
24
+ declare class PathCreator implements IPathCreator {
25
+ set path(value: IPathCommandData);
26
+ get path(): IPathCommandData;
27
+ __path: IPathCommandData;
26
28
  constructor(path?: IPathCommandData | IPathString);
29
+ set(path?: IPathCommandData | IPathString): PathCreator;
27
30
  beginPath(): PathCreator;
28
31
  moveTo(x: number, y: number): PathCreator;
29
32
  lineTo(x: number, y: number): PathCreator;
@@ -38,6 +41,7 @@ declare class PathCreator implements IPathDrawer {
38
41
  drawEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation?: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
39
42
  drawArc(x: number, y: number, radius: number, startAngle?: number, endAngle?: number, anticlockwise?: boolean): PathCreator;
40
43
  drawPoints(points: number[], curve?: boolean | number, close?: boolean): PathCreator;
44
+ clearPath: () => PathCreator;
41
45
  }
42
46
 
43
47
  declare const PathCommandDataHelper: {
@@ -61,11 +65,6 @@ declare const PathDrawer: {
61
65
  drawPathByData(drawer: IPathDrawer, data: IPathCommandData): void;
62
66
  };
63
67
 
64
- declare const PathScaler: {
65
- scale(data: IPathCommandData, scaleX: number, scaleY: number): void;
66
- scalePoints(data: IPathCommandData, scaleX: number, scaleY: number, start?: number, pointCount?: number): void;
67
- };
68
-
69
68
  declare const PathBounds: {
70
69
  toBounds(data: IPathCommandData, setBounds: IBoundsData): void;
71
70
  toTwoPointBounds(data: IPathCommandData, setPointBounds: ITwoPointBoundsData): void;
@@ -102,4 +101,6 @@ declare const NeedConvertToCanvasCommandMap: INumberMap;
102
101
  declare const PathNumberCommandMap: IStringMap;
103
102
  declare const PathNumberCommandLengthMap: INumberMap;
104
103
 
105
- export { BezierHelper, EllipseHelper, NeedConvertToCanvasCommandMap, PathBounds, PathCommandDataHelper, PathCommandMap, PathConvert, PathCorner, PathCreator, PathDrawer, PathHelper, PathNumberCommandLengthMap, PathNumberCommandMap, PathScaler, RectHelper };
104
+ declare const pen: PathCreator;
105
+
106
+ export { BezierHelper, EllipseHelper, NeedConvertToCanvasCommandMap, PathBounds, PathCommandDataHelper, PathCommandMap, PathConvert, PathCorner, PathCreator, PathDrawer, PathHelper, PathNumberCommandLengthMap, PathNumberCommandMap, RectHelper, pen };
package/src/PathScaler.ts DELETED
@@ -1,93 +0,0 @@
1
- import { IPathCommandData } from '@leafer/interface'
2
-
3
- import { PathCommandMap as Command } from './PathCommandMap'
4
-
5
-
6
- const { M, L, C, Q, Z, N, D, X, G, F, O, P, U } = Command
7
-
8
- export const PathScaler = {
9
-
10
- scale(data: IPathCommandData, scaleX: number, scaleY: number): void {
11
- if (!data) return
12
-
13
- let command: number
14
- let i = 0, len = data.length
15
-
16
- while (i < len) {
17
- command = data[i]
18
- switch (command) {
19
- case M: //moveto(x, y)
20
- scalePoints(data, scaleX, scaleY, i, 1)
21
- i += 3
22
- break
23
- case L: //lineto(x, y)
24
- scalePoints(data, scaleX, scaleY, i, 1)
25
- i += 3
26
- break
27
- case C: //bezierCurveTo(x1, y1, x2, y2, x, y)
28
- scalePoints(data, scaleX, scaleY, i, 3)
29
- i += 7
30
- break
31
- case Q: //quadraticCurveTo(x1, y1, x, y)
32
- scalePoints(data, scaleX, scaleY, i, 2)
33
- i += 5
34
- break
35
- case Z: //closepath()
36
- i += 1
37
- break
38
-
39
- // canvas command
40
-
41
- case N: // rect(x, y, width, height)
42
- scalePoints(data, scaleX, scaleY, i, 2)
43
- i += 5
44
- break
45
- case D: // roundRect(x, y, width, height, radius1, radius2, radius3, radius4)
46
- scalePoints(data, scaleX, scaleY, i, 2)
47
- i += 9
48
- break
49
- case X: // simple roundRect(x, y, width, height, radius)
50
- scalePoints(data, scaleX, scaleY, i, 2)
51
- i += 6
52
- break
53
- case G: // ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)
54
- scalePoints(data, scaleX, scaleY, i, 2)
55
- i += 9
56
- break
57
- case F: // simple ellipse(x, y, radiusX, radiusY)
58
- scalePoints(data, scaleX, scaleY, i, 2)
59
- i += 5
60
- break
61
- case O: // arc(x, y, radius, startAngle, endAngle, anticlockwise)
62
- data[i] = G // to ellipse
63
- data.splice(i + 4, 0, data[i + 3], 0)
64
- scalePoints(data, scaleX, scaleY, i, 2)
65
- i += 7 + 2
66
- len += 2
67
- break
68
- case P: // simple arc(x, y, radius)
69
- data[i] = F // to simple ellipse
70
- data.splice(i + 4, 0, data[i + 3])
71
- scalePoints(data, scaleX, scaleY, i, 2)
72
- i += 4 + 1
73
- len += 1
74
- break
75
- case U: // arcTo(x1, y1, x2, y2, radius)
76
- scalePoints(data, scaleX, scaleY, i, 2)
77
- i += 6
78
- break
79
- }
80
- }
81
-
82
- },
83
-
84
- scalePoints(data: IPathCommandData, scaleX: number, scaleY: number, start?: number, pointCount?: number): void {
85
- for (let i = pointCount ? start + 1 : 0, end = pointCount ? i + pointCount * 2 : data.length; i < end; i += 2) {
86
- data[i] *= scaleX
87
- data[i + 1] *= scaleY
88
- }
89
- }
90
-
91
- }
92
-
93
- const { scalePoints } = PathScaler