vislite 0.2.0 → 0.3.0-alpha.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.
Files changed (88) hide show
  1. package/AUTHORS.txt +6 -6
  2. package/CHANGELOG +27 -1
  3. package/LICENSE +20 -20
  4. package/README.md +53 -53
  5. package/lib/Canvas/index.umd.js +87 -14
  6. package/lib/Canvas/index.umd.min.js +3 -3
  7. package/lib/Cardinal/index.umd.js +16 -5
  8. package/lib/Cardinal/index.umd.min.js +3 -3
  9. package/lib/Eoap/index.umd.js +2 -2
  10. package/lib/Eoap/index.umd.min.js +2 -2
  11. package/lib/Hermite/index.umd.js +2 -2
  12. package/lib/Hermite/index.umd.min.js +2 -2
  13. package/lib/Matrix4/index.umd.js +2 -2
  14. package/lib/Matrix4/index.umd.min.js +2 -2
  15. package/lib/Mercator/index.umd.js +2 -2
  16. package/lib/Mercator/index.umd.min.js +2 -2
  17. package/lib/OralCanvas/index.es.js +87 -14
  18. package/lib/OralCanvas/index.es.min.js +3 -3
  19. package/lib/OralWebGL/index.es.js +17 -6
  20. package/lib/OralWebGL/index.es.min.js +3 -3
  21. package/lib/SVG/index.umd.js +78 -62
  22. package/lib/SVG/index.umd.min.js +3 -3
  23. package/lib/WebGL/index.umd.js +15 -6
  24. package/lib/WebGL/index.umd.min.js +3 -3
  25. package/lib/animation/index.umd.js +2 -2
  26. package/lib/animation/index.umd.min.js +2 -2
  27. package/lib/getLoopColors/index.umd.js +2 -2
  28. package/lib/getLoopColors/index.umd.min.js +2 -2
  29. package/lib/index.umd.js +190 -81
  30. package/lib/index.umd.min.js +3 -3
  31. package/lib/rotate/index.umd.js +2 -2
  32. package/lib/rotate/index.umd.min.js +2 -2
  33. package/lib/ruler/index.umd.js +2 -2
  34. package/lib/ruler/index.umd.min.js +2 -2
  35. package/lib/viewHandler/index.umd.js +2 -2
  36. package/lib/viewHandler/index.umd.min.js +2 -2
  37. package/miniprogram/ui-canvas/index.json +4 -4
  38. package/miniprogram/ui-canvas/index.wxml +4 -4
  39. package/miniprogram/ui-canvas/index.wxss +5 -5
  40. package/package/Canvas/index.ts +1 -1
  41. package/package/Cardinal/index.ts +2 -2
  42. package/package/Eoap/index.ts +2 -2
  43. package/package/Hermite/index.ts +2 -2
  44. package/package/Matrix4/index.ts +2 -2
  45. package/package/OralCanvas/index.ts +1 -1
  46. package/package/OralWebGL/index.ts +1 -1
  47. package/package/SVG/index.ts +2 -2
  48. package/package/animation/index.ts +2 -2
  49. package/package/getLoopColors/index.ts +2 -2
  50. package/package/rotate/index.ts +2 -2
  51. package/package/ruler/index.ts +2 -2
  52. package/package.json +4 -4
  53. package/src/Matrix4/index.ts +76 -76
  54. package/src/Matrix4/move.ts +12 -12
  55. package/src/Matrix4/rotate.ts +15 -15
  56. package/src/Matrix4/scale.ts +11 -11
  57. package/src/Matrix4/transform.ts +64 -64
  58. package/src/animation.ts +99 -99
  59. package/src/common/canvas/arc.ts +50 -50
  60. package/src/common/canvas/config.ts +142 -61
  61. package/src/common/canvas/gradient.ts +29 -0
  62. package/src/common/canvas/index.ts +122 -82
  63. package/src/common/canvas/painter.ts +12 -0
  64. package/src/common/setStyle.ts +5 -5
  65. package/src/common/svg/config.ts +173 -128
  66. package/src/common/svg/index.ts +315 -266
  67. package/src/common/svg/tool.ts +43 -43
  68. package/src/common/webgl/doDraw.ts +13 -3
  69. package/src/common/webgl/getShader.ts +6 -0
  70. package/src/common/webgl/index.ts +4 -0
  71. package/src/getLoopColors.ts +42 -42
  72. package/src/interpolation/Cardinal.ts +109 -91
  73. package/src/interpolation/Hermite.ts +65 -65
  74. package/src/rotate.ts +7 -7
  75. package/src/ruler.ts +99 -99
  76. package/types/Canvas.d.ts +282 -221
  77. package/types/CanvasConfig.d.ts +3 -3
  78. package/types/Cardinal.d.ts +13 -13
  79. package/types/Gradient.d.ts +15 -0
  80. package/types/Hermite.d.ts +18 -18
  81. package/types/Matrix4.d.ts +42 -42
  82. package/types/Object3D.d.ts +7 -0
  83. package/types/SVG.d.ts +210 -210
  84. package/types/animation.d.ts +6 -6
  85. package/types/getLoopColors.d.ts +2 -2
  86. package/types/rotate.d.ts +2 -2
  87. package/types/ruler.d.ts +2 -2
  88. package/uni-app/ui-canvas.vue +22 -46
@@ -1,44 +1,44 @@
1
- import { XLINK_ATTRIBUTE } from "./dictionary"
2
- import SVGConfigType from '../../../types/SVGConfig'
3
-
4
- // 新建节点
5
- export function toNode(tagname: string) {
6
- return document.createElementNS('http://www.w3.org/2000/svg', tagname)
7
- }
8
-
9
- // 设置属性
10
- let _setAttribute = function (el: SVGElement, key: string, value: string | number) {
11
-
12
- // 需要使用xlink命名空间的xml属性
13
- if (XLINK_ATTRIBUTE.indexOf(key) > -1) {
14
- el.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:' + key, value + "")
15
- }
16
-
17
- // 否则
18
- else {
19
- el.setAttribute(key, value + "")
20
- }
21
- }
22
- export let setAttribute = _setAttribute
23
-
24
- // 获取属性
25
- export function getAttribute(el: SVGElement, key: string) {
26
- if (XLINK_ATTRIBUTE.indexOf(key) > -1) key = 'xlink:' + key
27
- return el.getAttribute(key)
28
- }
29
-
30
- export function full(el: SVGElement, config: SVGConfigType) {
31
- _setAttribute(el, "stroke", config.strokeStyle)
32
- _setAttribute(el, "fill", config.fillStyle)
33
- _setAttribute(el, "stroke-dasharray", config.lineDash.join(','))
34
- }
35
-
36
- export function fill(el: SVGElement, config: SVGConfigType) {
37
- _setAttribute(el, "fill", config.fillStyle)
38
- }
39
-
40
- export function stroke(el: SVGElement, config: SVGConfigType) {
41
- _setAttribute(el, "stroke", config.strokeStyle)
42
- _setAttribute(el, "fill", "none")
43
- _setAttribute(el, "stroke-dasharray", config.lineDash.join(','))
1
+ import { XLINK_ATTRIBUTE } from "./dictionary"
2
+ import SVGConfigType from '../../../types/SVGConfig'
3
+
4
+ // 新建节点
5
+ export function toNode(tagname: string) {
6
+ return document.createElementNS('http://www.w3.org/2000/svg', tagname)
7
+ }
8
+
9
+ // 设置属性
10
+ let _setAttribute = function (el: SVGElement, key: string, value: string | number) {
11
+
12
+ // 需要使用xlink命名空间的xml属性
13
+ if (XLINK_ATTRIBUTE.indexOf(key) > -1) {
14
+ el.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:' + key, value + "")
15
+ }
16
+
17
+ // 否则
18
+ else {
19
+ el.setAttribute(key, value + "")
20
+ }
21
+ }
22
+ export let setAttribute = _setAttribute
23
+
24
+ // 获取属性
25
+ export function getAttribute(el: SVGElement, key: string) {
26
+ if (XLINK_ATTRIBUTE.indexOf(key) > -1) key = 'xlink:' + key
27
+ return el.getAttribute(key)
28
+ }
29
+
30
+ export function full(el: SVGElement, config: SVGConfigType) {
31
+ _setAttribute(el, "stroke", config.strokeStyle)
32
+ _setAttribute(el, "fill", config.fillStyle)
33
+ _setAttribute(el, "stroke-dasharray", config.lineDash.join(','))
34
+ }
35
+
36
+ export function fill(el: SVGElement, config: SVGConfigType) {
37
+ _setAttribute(el, "fill", config.fillStyle)
38
+ }
39
+
40
+ export function stroke(el: SVGElement, config: SVGConfigType) {
41
+ _setAttribute(el, "stroke", config.strokeStyle)
42
+ _setAttribute(el, "fill", "none")
43
+ _setAttribute(el, "stroke-dasharray", config.lineDash.join(','))
44
44
  }
@@ -20,7 +20,7 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
20
20
  // 点数据写入缓冲区
21
21
  new BufferObject(painter).use()
22
22
  .write(mesh.geometry.attributes.position.array as Float32Array)
23
- .divide(painter.getAttribLocation(shader.program, "a_position"), mesh.geometry.attributes.position.itemSize, 3, 0)
23
+ .divide(painter.getAttribLocation(shader.program, "a_position"), mesh.geometry.attributes.position.itemSize, mesh.geometry.attributes.position.itemSize, 0)
24
24
 
25
25
  // 写入矩阵
26
26
  painter.uniformMatrix4fv(painter.getUniformLocation(shader.program, "u_matrix_world"), false, globalWorld.matrix)
@@ -31,7 +31,7 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
31
31
  if (mesh.geometry.attributes.normal) {
32
32
  new BufferObject(painter).use()
33
33
  .write(mesh.geometry.attributes.normal.array as Float32Array)
34
- .divide(painter.getAttribLocation(shader.program, "a_normal"), mesh.geometry.attributes.normal.itemSize, 3, 0)
34
+ .divide(painter.getAttribLocation(shader.program, "a_normal"), mesh.geometry.attributes.normal.itemSize, mesh.geometry.attributes.normal.itemSize, 0)
35
35
  }
36
36
 
37
37
  // 颜色
@@ -45,10 +45,20 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
45
45
  , mesh.material.color.alpha)
46
46
  }
47
47
 
48
+ // 多颜色
49
+ if ("colors" in mesh.material) {
50
+
51
+ // 写入颜色序列
52
+ new BufferObject(painter).use()
53
+ .write(mesh.material.colors.array as Float32Array)
54
+ .divide(painter.getAttribLocation(shader.program, "a_color"), mesh.material.colors.itemSize, mesh.material.colors.itemSize, 0)
55
+
56
+ }
57
+
48
58
  // 立方纹理
49
59
  else if ("cube" in mesh.material) {
50
60
 
51
- let size = mesh.material.cube.right.image.value.width
61
+ let size = (mesh.material.cube.right.image.value as any).width
52
62
 
53
63
  getTexture(type, painter, 'cube').useCube([
54
64
  mesh.material.cube.right.image.value,
@@ -9,6 +9,7 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
9
9
  (mesh.geometry.attributes.normal ? "1" : "0") +
10
10
  (mesh.material.image ? "1" : "0") +
11
11
  (mesh.material.color ? "1" : "0") +
12
+ (mesh.material.colors ? "1" : "0") +
12
13
  (mesh.material.cube ? "1" : "0")
13
14
  if (shaders[uniqueName]) return shaders[uniqueName].use()
14
15
 
@@ -24,6 +25,8 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
24
25
 
25
26
  ${mesh.material.image ? 'attribute vec2 a_textcoord;varying vec2 v_textcoord;' : ''}
26
27
 
28
+ ${mesh.material.colors ? 'attribute vec4 a_color;varying vec4 v_color;' : ''}
29
+
27
30
  void main(){
28
31
  vec4 temp = a_position;
29
32
 
@@ -43,6 +46,7 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
43
46
 
44
47
  ${mesh.geometry.attributes.normal ? 'v_normal = normalize(vec3(a_normal));' : ''}
45
48
  ${mesh.material.image ? 'v_textcoord = a_textcoord;' : ''}
49
+ ${mesh.material.colors ? 'v_color=a_color;' : ''}
46
50
  }
47
51
  `
48
52
 
@@ -54,11 +58,13 @@ export default function (type: string, painter: WebGLRenderingContext, mesh: mes
54
58
  ${mesh.material.color ? 'uniform vec4 u_color;' : ''}
55
59
  ${mesh.material.cube ? 'uniform samplerCube u_texture;' : ''}
56
60
  ${mesh.material.image ? 'uniform sampler2D u_sampler;varying vec2 v_textcoord;' : ''}
61
+ ${mesh.material.colors ? 'varying vec4 v_color;' : ''}
57
62
 
58
63
  void main(){
59
64
  ${mesh.material.color ? 'gl_FragColor = u_color;' : ''}
60
65
  ${mesh.material.cube ? 'gl_FragColor = textureCube(u_texture,normalize(v_normal));' : ''}
61
66
  ${mesh.material.image ? 'gl_FragColor = texture2D(u_sampler,v_textcoord);' : ''}
67
+ ${mesh.material.colors ? 'gl_FragColor=v_color;' : ''}
62
68
  }
63
69
  `
64
70
 
@@ -165,6 +165,10 @@ class WebGL {
165
165
  mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
166
166
  }
167
167
 
168
+ if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
169
+ mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
170
+ }
171
+
168
172
  }
169
173
 
170
174
  this.__scene.children.push(object3D)
@@ -1,42 +1,42 @@
1
- // 获取一组循环色彩
2
- export default function (num: number, alpha: number = 1) {
3
- // 颜色集合
4
- let colorList = [
5
- 'rgba(84,112,198,' + alpha + ")", 'rgba(145,204,117,' + alpha + ")",
6
- 'rgba(250,200,88,' + alpha + ")", 'rgba(238,102,102,' + alpha + ")",
7
- 'rgba(115,192,222,' + alpha + ")", 'rgba(59,162,114,' + alpha + ")",
8
- 'rgba(252,132,82,' + alpha + ")", 'rgba(154,96,180,' + alpha + ")",
9
- 'rgba(234,124,204,' + alpha + ")"
10
- ]
11
-
12
- let colors: string[] = []
13
-
14
- // 根据情况返回颜色数组
15
- if (num <= colorList.length) {
16
- // 这种情况就不需要任何处理
17
- return colorList
18
- } else {
19
- // 如果正好是集合长度的倍数
20
- if (num % colorList.length == 0) {
21
- // 将颜色数组循环加入后再返回
22
- for (let i = 0; i < (num / colorList.length); i++) {
23
- colors = colors.concat(colorList)
24
- }
25
- } else {
26
- for (let j = 1; j < (num / colorList.length); j++) {
27
- colors = colors.concat(colorList)
28
- }
29
- // 防止最后一个颜色和第一个颜色重复
30
- if (num % colorList.length == 1) {
31
- colors = colors.concat(colorList[4])
32
- } else {
33
- for (let k = 0; k < num % colorList.length; k++) {
34
- colors = colors.concat(colorList[k])
35
- }
36
- }
37
- }
38
- }
39
-
40
- // 返回结果
41
- return colors
42
- }
1
+ // 获取一组循环色彩
2
+ export default function (num: number, alpha: number = 1) {
3
+ // 颜色集合
4
+ let colorList = [
5
+ 'rgba(84,112,198,' + alpha + ")", 'rgba(145,204,117,' + alpha + ")",
6
+ 'rgba(250,200,88,' + alpha + ")", 'rgba(238,102,102,' + alpha + ")",
7
+ 'rgba(115,192,222,' + alpha + ")", 'rgba(59,162,114,' + alpha + ")",
8
+ 'rgba(252,132,82,' + alpha + ")", 'rgba(154,96,180,' + alpha + ")",
9
+ 'rgba(234,124,204,' + alpha + ")"
10
+ ]
11
+
12
+ let colors: string[] = []
13
+
14
+ // 根据情况返回颜色数组
15
+ if (num <= colorList.length) {
16
+ // 这种情况就不需要任何处理
17
+ return colorList
18
+ } else {
19
+ // 如果正好是集合长度的倍数
20
+ if (num % colorList.length == 0) {
21
+ // 将颜色数组循环加入后再返回
22
+ for (let i = 0; i < (num / colorList.length); i++) {
23
+ colors = colors.concat(colorList)
24
+ }
25
+ } else {
26
+ for (let j = 1; j < (num / colorList.length); j++) {
27
+ colors = colors.concat(colorList)
28
+ }
29
+ // 防止最后一个颜色和第一个颜色重复
30
+ if (num % colorList.length == 1) {
31
+ colors = colors.concat(colorList[4])
32
+ } else {
33
+ for (let k = 0; k < num % colorList.length; k++) {
34
+ colors = colors.concat(colorList[k])
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ // 返回结果
41
+ return colors
42
+ }
@@ -1,92 +1,110 @@
1
- /**
2
- * Cardinal三次插值
3
- * ----------------------------
4
- * Hermite拟合的计算是,确定两个点和两个点的斜率
5
- * 用一个y=ax(3)+bx(2)+cx+d的三次多项式来求解
6
- * 而Cardinal是建立在此基础上
7
- * 给定需要拟合的两个点和第一个点的前一个点+最后一个点的后一个点
8
- * 第一个点的斜率由第一个点的前一个点和第二个点的斜率确定
9
- * 第二个点的斜率由第一个点和第二个点的后一个点的斜率确定
10
- */
11
-
12
- import CardinalType from '../../types/Cardinal'
13
- import Hermite from './Hermite'
14
-
15
- class Cardinal implements CardinalType {
16
- readonly name: string = 'Cardinal'
17
-
18
- // 该参数用于调整曲线走势,默认数值t=0,分水岭t=-1,|t-(-1)|的值越大,曲线走势调整的越严重
19
- private __t: number
20
-
21
- private __HS: {
22
- x: number[],
23
- h: Hermite[]
24
- }
25
- private __i: number
26
-
27
- constructor(t: number = 0) {
28
- this.__t = t
29
- }
30
-
31
- // 设置点的位置
32
- // 参数格式:[[x1, y1], [x2, y2], ... ]
33
- // 至少两个点
34
- setP(points: number[][]) {
35
-
36
- this.__HS = {
37
- "x": [],
38
- "h": []
39
- };
40
- var flag: number,
41
- slope = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]),
42
- temp: number
43
- this.__HS.x[0] = points[0][0]
44
- for (flag = 1; flag < points.length; flag++) {
45
- if (points[flag][0] <= points[flag - 1][0]) throw new Error('The point position should be increamented!')
46
- this.__HS.x[flag] = points[flag][0]
47
-
48
- // 求点斜率
49
- temp = flag < points.length - 1 ?
50
- (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0]) :
51
- (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
52
-
53
- // 求解两个点直接的拟合方程
54
- // 第一个点的前一个点直接取第一个点
55
- // 最后一个点的后一个点直接取最后一个点
56
- this.__HS.h[flag - 1] = new Hermite((1 - this.__t) * 0.5).setP(points[flag - 1][0], points[flag - 1][1], points[flag][0], points[flag][1], slope, temp)
57
- slope = temp
58
- }
59
- return this
60
-
61
- }
62
-
63
- // 根据x值返回y值
64
- use(x: number) {
65
- if (this.__HS) {
66
- this.__i = -1
67
-
68
- // 寻找记录x所在位置的区间
69
- // 这里就是寻找对应的拟合函数
70
- while (this.__i + 1 < this.__HS.x.length && (x > this.__HS.x[this.__i + 1] || (this.__i == -1 && x >= this.__HS.x[this.__i + 1]))) {
71
- this.__i += 1
72
- }
73
-
74
- // 由于js浮点运算不准确,我们对于越界的情况进行兼容
75
-
76
- if (this.__i < 0) {
77
- return this.__HS.h[0].use(this.__HS.x[0])
78
- }
79
-
80
- else if (this.__i >= this.__HS.h.length) {
81
- return this.__HS.h[this.__HS.h.length - 1].use(this.__HS.x[this.__HS.x.length - 1])
82
- }
83
-
84
- return this.__HS.h[this.__i].use(x)
85
- } else {
86
- throw new Error('You shoud first set the position!')
87
- }
88
- }
89
-
90
- }
91
-
1
+ /**
2
+ * Cardinal三次插值
3
+ * ----------------------------
4
+ * Hermite拟合的计算是,确定两个点和两个点的斜率
5
+ * 用一个y=ax(3)+bx(2)+cx+d的三次多项式来求解
6
+ * 而Cardinal是建立在此基础上
7
+ * 给定需要拟合的两个点和第一个点的前一个点+最后一个点的后一个点
8
+ * 第一个点的斜率由第一个点的前一个点和第二个点的斜率确定
9
+ * 第二个点的斜率由第一个点和第二个点的后一个点的斜率确定
10
+ */
11
+
12
+ import CardinalType from '../../types/Cardinal'
13
+ import Hermite from './Hermite'
14
+
15
+ class Cardinal implements CardinalType {
16
+ readonly name: string = 'Cardinal'
17
+
18
+ // 该参数用于调整曲线走势,默认数值t=0,分水岭t=-1,|t-(-1)|的值越大,曲线走势调整的越严重
19
+ private __t: number
20
+
21
+ private __HS: {
22
+ x: number[],
23
+ h: Hermite[]
24
+ }
25
+ private __i: number
26
+
27
+ constructor(t: number = 0) {
28
+ this.__t = t
29
+ }
30
+
31
+ // 设置点的位置
32
+ // 参数格式:[[x1, y1], [x2, y2], ... ]
33
+ // 至少两个点
34
+ setP(points: number[][]) {
35
+
36
+ this.__HS = {
37
+ "x": [],
38
+ "h": []
39
+ };
40
+ var flag: number,
41
+ slope = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]),
42
+ temp: number
43
+ this.__HS.x[0] = points[0][0]
44
+ for (flag = 1; flag < points.length; flag++) {
45
+ if (points[flag][0] <= points[flag - 1][0]) throw new Error('The point position should be increamented!')
46
+ this.__HS.x[flag] = points[flag][0]
47
+
48
+ // 求点斜率
49
+
50
+ // temp = flag < points.length - 1 ?
51
+ // (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0]) :
52
+ // (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
53
+
54
+ // 修复超过界限的值问题
55
+ // 2023年6月25日 于南京
56
+ if (flag < points.length - 1) {
57
+ if (
58
+ (points[flag + 1][1] > points[flag][1] && points[flag - 1][1] > points[flag][1]) ||
59
+ (points[flag + 1][1] < points[flag][1] && points[flag - 1][1] < points[flag][1]) ||
60
+ points[flag + 1][1] == points[flag][1] ||
61
+ points[flag - 1][1] == points[flag][1]
62
+ ) {
63
+ temp = 0
64
+ } else {
65
+ temp = (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0])
66
+ }
67
+ } else {
68
+ temp = (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
69
+ }
70
+
71
+ // 求解两个点直接的插值方程
72
+ // 第一个点的前一个点直接取第一个点
73
+ // 最后一个点的后一个点直接取最后一个点
74
+ this.__HS.h[flag - 1] = new Hermite((1 - this.__t) * 0.5).setP(points[flag - 1][0], points[flag - 1][1], points[flag][0], points[flag][1], slope, temp)
75
+ slope = temp
76
+ }
77
+ return this
78
+
79
+ }
80
+
81
+ // 根据x值返回y值
82
+ use(x: number) {
83
+ if (this.__HS) {
84
+ this.__i = -1
85
+
86
+ // 寻找记录x所在位置的区间
87
+ // 这里就是寻找对应的拟合函数
88
+ while (this.__i + 1 < this.__HS.x.length && (x > this.__HS.x[this.__i + 1] || (this.__i == -1 && x >= this.__HS.x[this.__i + 1]))) {
89
+ this.__i += 1
90
+ }
91
+
92
+ // 由于js浮点运算不准确,我们对于越界的情况进行兼容
93
+
94
+ if (this.__i < 0) {
95
+ return this.__HS.h[0].use(this.__HS.x[0])
96
+ }
97
+
98
+ else if (this.__i >= this.__HS.h.length) {
99
+ return this.__HS.h[this.__HS.h.length - 1].use(this.__HS.x[this.__HS.x.length - 1])
100
+ }
101
+
102
+ return this.__HS.h[this.__i].use(x)
103
+ } else {
104
+ throw new Error('You shoud first set the position!')
105
+ }
106
+ }
107
+
108
+ }
109
+
92
110
  export default Cardinal
@@ -1,66 +1,66 @@
1
- import HermiteType from '../../types/Hermite'
2
-
3
- class Hermite implements HermiteType {
4
- readonly name: string = 'Hermite'
5
-
6
- private __u: number // 张弛系数
7
-
8
- private __MR: number[]
9
- private __a: number
10
- private __b: number
11
-
12
- constructor(u: number = 0.5) {
13
- this.__u = u
14
- }
15
-
16
- /**
17
- * 设置点的位置
18
- * @param {Number} x1 左边点的位置
19
- * @param {Number} y1
20
- * @param {Number} x2 右边点的位置
21
- * @param {Number} y2
22
- * @param {Number} s1 两个点的斜率
23
- * @param {Number} s2
24
- */
25
- setP(x1: number, y1: number, x2: number, y2: number, s1: number, s2: number) {
26
- if (x1 < x2) {
27
- // 记录原始尺寸
28
- this.__a = x1
29
- this.__b = x2
30
- let p3 = this.__u * s1,
31
- p4 = this.__u * s2
32
- // 缩放到[0,1]定义域
33
- y1 /= (x2 - x1)
34
- y2 /= (x2 - x1)
35
- // MR是提前计算好的多项式通解矩阵
36
- // 为了加速计算
37
- // 如上面说的
38
- // 统一在[0,1]上计算后再通过缩放和移动恢复
39
- // 避免了动态求解矩阵的麻烦
40
- this.__MR = [
41
- 2 * y1 - 2 * y2 + p3 + p4,
42
- 3 * y2 - 3 * y1 - 2 * p3 - p4,
43
- p3,
44
- y1
45
- ]
46
- } else throw new Error('The point x-position should be increamented!')
47
- return this
48
- }
49
-
50
-
51
- /**
52
- * 根据x值返回y值
53
- * @param {Number} x
54
- */
55
- use(x: number) {
56
- if (this.__MR) {
57
- let sx = (x - this.__a) / (this.__b - this.__a),
58
- sx2 = sx * sx,
59
- sx3 = sx * sx2
60
- let sResult = sx3 * this.__MR[0] + sx2 * this.__MR[1] + sx * this.__MR[2] + this.__MR[3]
61
- return sResult * (this.__b - this.__a)
62
- } else throw new Error('You shoud first set the position!')
63
- }
64
- }
65
-
1
+ import HermiteType from '../../types/Hermite'
2
+
3
+ class Hermite implements HermiteType {
4
+ readonly name: string = 'Hermite'
5
+
6
+ private __u: number // 张弛系数
7
+
8
+ private __MR: number[]
9
+ private __a: number
10
+ private __b: number
11
+
12
+ constructor(u: number = 0.5) {
13
+ this.__u = u
14
+ }
15
+
16
+ /**
17
+ * 设置点的位置
18
+ * @param {Number} x1 左边点的位置
19
+ * @param {Number} y1
20
+ * @param {Number} x2 右边点的位置
21
+ * @param {Number} y2
22
+ * @param {Number} s1 两个点的斜率
23
+ * @param {Number} s2
24
+ */
25
+ setP(x1: number, y1: number, x2: number, y2: number, s1: number, s2: number) {
26
+ if (x1 < x2) {
27
+ // 记录原始尺寸
28
+ this.__a = x1
29
+ this.__b = x2
30
+ let p3 = this.__u * s1,
31
+ p4 = this.__u * s2
32
+ // 缩放到[0,1]定义域
33
+ y1 /= (x2 - x1)
34
+ y2 /= (x2 - x1)
35
+ // MR是提前计算好的多项式通解矩阵
36
+ // 为了加速计算
37
+ // 如上面说的
38
+ // 统一在[0,1]上计算后再通过缩放和移动恢复
39
+ // 避免了动态求解矩阵的麻烦
40
+ this.__MR = [
41
+ 2 * y1 - 2 * y2 + p3 + p4,
42
+ 3 * y2 - 3 * y1 - 2 * p3 - p4,
43
+ p3,
44
+ y1
45
+ ]
46
+ } else throw new Error('The point x-position should be increamented!')
47
+ return this
48
+ }
49
+
50
+
51
+ /**
52
+ * 根据x值返回y值
53
+ * @param {Number} x
54
+ */
55
+ use(x: number) {
56
+ if (this.__MR) {
57
+ let sx = (x - this.__a) / (this.__b - this.__a),
58
+ sx2 = sx * sx,
59
+ sx3 = sx * sx2
60
+ let sResult = sx3 * this.__MR[0] + sx2 * this.__MR[1] + sx * this.__MR[2] + this.__MR[3]
61
+ return sResult * (this.__b - this.__a)
62
+ } else throw new Error('You shoud first set the position!')
63
+ }
64
+ }
65
+
66
66
  export default Hermite
package/src/rotate.ts CHANGED
@@ -1,8 +1,8 @@
1
- // 点(x,y)围绕中心(cx,cy)旋转deg度
2
- export default function (cx: number, cy: number, deg: number, x: number, y: number) {
3
- let cos = Math.cos(deg), sin = Math.sin(deg)
4
- return [
5
- (x - cx) * cos - (y - cy) * sin + cx,
6
- (x - cx) * sin + (y - cy) * cos + cy
7
- ]
1
+ // 点(x,y)围绕中心(cx,cy)旋转deg度
2
+ export default function (cx: number, cy: number, deg: number, x: number, y: number) {
3
+ let cos = Math.cos(deg), sin = Math.sin(deg)
4
+ return [
5
+ (x - cx) * cos - (y - cy) * sin + cx,
6
+ (x - cx) * sin + (y - cy) * cos + cy
7
+ ]
8
8
  }