vislite 0.6.0-alpha.2 → 0.6.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 (104) hide show
  1. package/.eslintrc.js +3 -1
  2. package/CHANGELOG +1 -1
  3. package/README.md +11 -3
  4. package/lib/Buffer/index.umd.js +2 -2
  5. package/lib/Buffer/index.umd.min.js +2 -2
  6. package/lib/Canvas/index.umd.js +2 -2
  7. package/lib/Canvas/index.umd.min.js +2 -2
  8. package/lib/Cardinal/index.umd.js +2 -2
  9. package/lib/Cardinal/index.umd.min.js +2 -2
  10. package/lib/Eoap/index.umd.js +2 -2
  11. package/lib/Eoap/index.umd.min.js +2 -2
  12. package/lib/Geometry/index.umd.js +2 -2
  13. package/lib/Geometry/index.umd.min.js +2 -2
  14. package/lib/Hermite/index.umd.js +2 -2
  15. package/lib/Hermite/index.umd.min.js +2 -2
  16. package/lib/Matrix4/index.umd.js +2 -2
  17. package/lib/Matrix4/index.umd.min.js +2 -2
  18. package/lib/Mercator/index.umd.js +2 -2
  19. package/lib/Mercator/index.umd.min.js +2 -2
  20. package/lib/OralCanvas/index.es.js +2 -2
  21. package/lib/OralCanvas/index.es.min.js +2 -2
  22. package/lib/OralWebGL/index.es.js +2 -2
  23. package/lib/OralWebGL/index.es.min.js +2 -2
  24. package/lib/SVG/index.umd.js +2 -2
  25. package/lib/SVG/index.umd.min.js +2 -2
  26. package/lib/Shader/index.umd.js +2 -2
  27. package/lib/Shader/index.umd.min.js +2 -2
  28. package/lib/Texture/index.umd.js +2 -2
  29. package/lib/Texture/index.umd.min.js +2 -2
  30. package/lib/TreeLayout/index.umd.js +2 -2
  31. package/lib/TreeLayout/index.umd.min.js +2 -2
  32. package/lib/WebGL/index.umd.js +2 -2
  33. package/lib/WebGL/index.umd.min.js +2 -2
  34. package/lib/animation/index.umd.js +2 -2
  35. package/lib/animation/index.umd.min.js +2 -2
  36. package/lib/formatColor/index.umd.js +2 -2
  37. package/lib/formatColor/index.umd.min.js +2 -2
  38. package/lib/getLoopColors/index.umd.js +2 -2
  39. package/lib/getLoopColors/index.umd.min.js +2 -2
  40. package/lib/getWebGLContext/index.umd.js +2 -2
  41. package/lib/getWebGLContext/index.umd.min.js +2 -2
  42. package/lib/index.umd.js +2 -2
  43. package/lib/index.umd.min.js +2 -2
  44. package/lib/move/index.umd.js +2 -2
  45. package/lib/move/index.umd.min.js +2 -2
  46. package/lib/resizeObserver/index.umd.js +2 -2
  47. package/lib/resizeObserver/index.umd.min.js +2 -2
  48. package/lib/rotate/index.umd.js +2 -2
  49. package/lib/rotate/index.umd.min.js +2 -2
  50. package/lib/ruler/index.umd.js +2 -2
  51. package/lib/ruler/index.umd.min.js +2 -2
  52. package/lib/scale/index.umd.js +2 -2
  53. package/lib/scale/index.umd.min.js +2 -2
  54. package/lib/throttle/index.umd.js +2 -2
  55. package/lib/throttle/index.umd.min.js +2 -2
  56. package/lib/viewHandler/index.umd.js +2 -2
  57. package/lib/viewHandler/index.umd.min.js +2 -2
  58. package/package.json +1 -1
  59. package/src/Canvas.ts +70 -70
  60. package/src/Eoap.ts +66 -66
  61. package/src/Geometry.ts +82 -82
  62. package/src/Matrix4/move.ts +12 -12
  63. package/src/Matrix4/rotate.ts +15 -15
  64. package/src/Matrix4/transform.ts +64 -64
  65. package/src/Mercator.ts +27 -27
  66. package/src/SVG.ts +24 -24
  67. package/src/WebGL.ts +66 -66
  68. package/src/animation.ts +98 -98
  69. package/src/common/assemble.ts +22 -22
  70. package/src/common/canvas/arc.ts +51 -51
  71. package/src/common/canvas/config.ts +158 -158
  72. package/src/common/canvas/gradient.ts +29 -29
  73. package/src/common/canvas/painter.ts +417 -417
  74. package/src/common/geometry/prism-horizontal.ts +35 -35
  75. package/src/common/geometry/prism-vertical.ts +40 -40
  76. package/src/common/geometry/sphere-fragment.ts +40 -40
  77. package/src/common/geometry/tool/circle.ts +11 -11
  78. package/src/common/mergeOption.ts +6 -6
  79. package/src/common/setStyle.ts +5 -5
  80. package/src/common/svg/config.ts +186 -186
  81. package/src/common/svg/gradient.ts +63 -63
  82. package/src/common/svg/index.ts +356 -356
  83. package/src/common/svg/tool.ts +44 -44
  84. package/src/common/tree/toInnerTree.ts +57 -57
  85. package/src/common/tree/toPlainTree.ts +132 -132
  86. package/src/common/webgl/buffer.ts +79 -79
  87. package/src/common/webgl/doDraw.ts +108 -108
  88. package/src/common/webgl/getColorFactory.ts +26 -26
  89. package/src/common/webgl/getShader.ts +48 -48
  90. package/src/common/webgl/getTexture.ts +11 -11
  91. package/src/common/webgl/getWebGLContext.ts +28 -28
  92. package/src/common/webgl/index.ts +224 -224
  93. package/src/common/webgl/shader.ts +75 -75
  94. package/src/common/webgl/texture.ts +97 -97
  95. package/src/formatColor.ts +44 -44
  96. package/src/getLoopColors.ts +42 -42
  97. package/src/interpolation/Cardinal.ts +110 -110
  98. package/src/interpolation/Hermite.ts +65 -65
  99. package/src/move.ts +7 -7
  100. package/src/resizeObserver.ts +37 -37
  101. package/src/rotate.ts +7 -7
  102. package/src/throttle.ts +52 -52
  103. package/src/viewHandler.ts +158 -158
  104. package/types/CanvasOption.d.ts +8 -8
@@ -1,98 +1,98 @@
1
- // 初始化一个纹理对象
2
- // type = gl.TEXTURE_2D 二维纹理
3
- // type = gl.TEXTURE_CUBE_MAP 立方体纹理
4
- // unit表示开启的纹理单元编号
5
- const initTexture = (painter: WebGLRenderingContext, type: number, unit: number) => {
6
-
7
- // 创建纹理对象
8
- const texture = painter.createTexture()
9
-
10
- // 开启纹理单元,unit表示开启的编号
11
- painter.activeTexture(painter['TEXTURE' + unit])
12
-
13
- // 绑定纹理对象到目标上
14
- painter.bindTexture(type, texture)
15
-
16
- return texture
17
- }
18
-
19
- // 链接资源图片
20
- // level默认传入0即可,和金字塔纹理有关
21
- // format表示图像的内部格式:
22
- // gl.RGB(红绿蓝)
23
- // gl.RGBA(红绿蓝透明度)
24
- // gl.ALPHA(0.0,0.0,0.0,透明度)
25
- // gl.LUMINANCE(L、L、L、1L:流明)
26
- // gl.LUMINANCE_ALPHA(L、L、L,透明度)
27
- // textureType表示纹理数据的格式:
28
- // gl.UNSIGNED_BYTE: 表示无符号整形,每一个颜色分量占据1字节
29
- // gl.UNSIGNED_SHORT_5_6_5: 表示RGB,每一个分量分别占据占据5, 6, 5比特
30
- // gl.UNSIGNED_SHORT_4_4_4_4: 表示RGBA,每一个分量分别占据占据4, 4, 4, 4比特
31
- // gl.UNSIGNED_SHORT_5_5_5_1: 表示RGBA,每一个分量分别占据占据5比特,A分量占据1比特
32
- const linkImage = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, image: TexImageSource) => {
33
- painter.texImage2D(type, level, format, format, textureType, image)
34
- }
35
-
36
- const linkCube = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, images: TexImageSource[], width: number, height: number, texture: WebGLTexture) => {
37
-
38
- const types = [
39
- painter.TEXTURE_CUBE_MAP_POSITIVE_X, // 右
40
- painter.TEXTURE_CUBE_MAP_NEGATIVE_X, // 左
41
- painter.TEXTURE_CUBE_MAP_POSITIVE_Y, // 上
42
- painter.TEXTURE_CUBE_MAP_NEGATIVE_Y, // 下
43
- painter.TEXTURE_CUBE_MAP_POSITIVE_Z, // 后
44
- painter.TEXTURE_CUBE_MAP_NEGATIVE_Z // 前
45
- ]
46
- let target: number
47
-
48
- for (let i = 0; i < types.length; i++) {
49
- if (images[i]) {
50
- target = types[i]
51
- painter.texImage2D(target, level, format, width, height, 0, format, textureType, null)
52
- painter.bindTexture(type, texture)
53
- painter.texImage2D(target, level, format, format, textureType, images[i])
54
- }
55
- }
56
-
57
- painter.generateMipmap(type)
58
- }
59
-
60
- class TextureObject {
61
-
62
- private __painter: WebGLRenderingContext
63
- private __type: number
64
-
65
- private __texture: WebGLTexture
66
-
67
- constructor(painter: WebGLRenderingContext, type: string, unit = 0) {
68
- this.__painter = painter
69
- this.__type = {
70
- "2d": painter.TEXTURE_2D,
71
- "cube": painter.TEXTURE_CUBE_MAP
72
- }[type]
73
-
74
- // 创建纹理
75
- this.__texture = initTexture(painter, this.__type, unit)
76
-
77
- // 配置纹理
78
- painter.texParameteri(this.__type, painter.TEXTURE_MIN_FILTER, painter.NEAREST)
79
- painter.texParameteri(this.__type, painter.TEXTURE_MAG_FILTER, painter.NEAREST)
80
- painter.texParameteri(this.__type, painter.TEXTURE_WRAP_S, painter.CLAMP_TO_EDGE)
81
- painter.texParameteri(this.__type, painter.TEXTURE_WRAP_T, painter.CLAMP_TO_EDGE)
82
- }
83
-
84
- // 链接图片资源(单一)
85
- useImage(image: TexImageSource) {
86
- linkImage(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, image)
87
- return this
88
- }
89
-
90
- // 链接图片资源(多张)
91
- useCube(images: TexImageSource[], width: number, height: number) {
92
- linkCube(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, images, width, height, this.__texture)
93
- return this
94
- }
95
-
96
- }
97
-
1
+ // 初始化一个纹理对象
2
+ // type = gl.TEXTURE_2D 二维纹理
3
+ // type = gl.TEXTURE_CUBE_MAP 立方体纹理
4
+ // unit表示开启的纹理单元编号
5
+ const initTexture = (painter: WebGLRenderingContext, type: number, unit: number) => {
6
+
7
+ // 创建纹理对象
8
+ const texture = painter.createTexture()
9
+
10
+ // 开启纹理单元,unit表示开启的编号
11
+ painter.activeTexture(painter['TEXTURE' + unit])
12
+
13
+ // 绑定纹理对象到目标上
14
+ painter.bindTexture(type, texture)
15
+
16
+ return texture
17
+ }
18
+
19
+ // 链接资源图片
20
+ // level默认传入0即可,和金字塔纹理有关
21
+ // format表示图像的内部格式:
22
+ // gl.RGB(红绿蓝)
23
+ // gl.RGBA(红绿蓝透明度)
24
+ // gl.ALPHA(0.0,0.0,0.0,透明度)
25
+ // gl.LUMINANCE(L、L、L、1L:流明)
26
+ // gl.LUMINANCE_ALPHA(L、L、L,透明度)
27
+ // textureType表示纹理数据的格式:
28
+ // gl.UNSIGNED_BYTE: 表示无符号整形,每一个颜色分量占据1字节
29
+ // gl.UNSIGNED_SHORT_5_6_5: 表示RGB,每一个分量分别占据占据5, 6, 5比特
30
+ // gl.UNSIGNED_SHORT_4_4_4_4: 表示RGBA,每一个分量分别占据占据4, 4, 4, 4比特
31
+ // gl.UNSIGNED_SHORT_5_5_5_1: 表示RGBA,每一个分量分别占据占据5比特,A分量占据1比特
32
+ const linkImage = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, image: TexImageSource) => {
33
+ painter.texImage2D(type, level, format, format, textureType, image)
34
+ }
35
+
36
+ const linkCube = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, images: TexImageSource[], width: number, height: number, texture: WebGLTexture) => {
37
+
38
+ const types = [
39
+ painter.TEXTURE_CUBE_MAP_POSITIVE_X, // 右
40
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_X, // 左
41
+ painter.TEXTURE_CUBE_MAP_POSITIVE_Y, // 上
42
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_Y, // 下
43
+ painter.TEXTURE_CUBE_MAP_POSITIVE_Z, // 后
44
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_Z // 前
45
+ ]
46
+ let target: number
47
+
48
+ for (let i = 0; i < types.length; i++) {
49
+ if (images[i]) {
50
+ target = types[i]
51
+ painter.texImage2D(target, level, format, width, height, 0, format, textureType, null)
52
+ painter.bindTexture(type, texture)
53
+ painter.texImage2D(target, level, format, format, textureType, images[i])
54
+ }
55
+ }
56
+
57
+ painter.generateMipmap(type)
58
+ }
59
+
60
+ class TextureObject {
61
+
62
+ private __painter: WebGLRenderingContext
63
+ private __type: number
64
+
65
+ private __texture: WebGLTexture
66
+
67
+ constructor(painter: WebGLRenderingContext, type: string, unit = 0) {
68
+ this.__painter = painter
69
+ this.__type = {
70
+ "2d": painter.TEXTURE_2D,
71
+ "cube": painter.TEXTURE_CUBE_MAP
72
+ }[type]
73
+
74
+ // 创建纹理
75
+ this.__texture = initTexture(painter, this.__type, unit)
76
+
77
+ // 配置纹理
78
+ painter.texParameteri(this.__type, painter.TEXTURE_MIN_FILTER, painter.NEAREST)
79
+ painter.texParameteri(this.__type, painter.TEXTURE_MAG_FILTER, painter.NEAREST)
80
+ painter.texParameteri(this.__type, painter.TEXTURE_WRAP_S, painter.CLAMP_TO_EDGE)
81
+ painter.texParameteri(this.__type, painter.TEXTURE_WRAP_T, painter.CLAMP_TO_EDGE)
82
+ }
83
+
84
+ // 链接图片资源(单一)
85
+ useImage(image: TexImageSource) {
86
+ linkImage(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, image)
87
+ return this
88
+ }
89
+
90
+ // 链接图片资源(多张)
91
+ useCube(images: TexImageSource[], width: number, height: number) {
92
+ linkCube(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, images, width, height, this.__texture)
93
+ return this
94
+ }
95
+
96
+ }
97
+
98
98
  export default TextureObject
@@ -1,45 +1,45 @@
1
- const toHex = (value: number) => {
2
- const hex = value.toString(16)
3
- return hex.length == 1 ? ("0" + hex) : hex
4
- }
5
-
6
- export default function (color: string, format: string) {
7
-
8
- const colorNode = document.getElementsByTagName('head')[0]
9
- colorNode.style['color'] = color
10
-
11
- // 获取结点的全部样式
12
- const allStyle = document.defaultView && document.defaultView.getComputedStyle ?
13
- document.defaultView.getComputedStyle(colorNode, null) :
14
- (colorNode as any).currentStyle
15
-
16
- const oralFormat = allStyle.getPropertyValue('color').replace(/rgba?\(/, '').replace(")", "").split(',')
17
- for (let index = 0; index < oralFormat.length; index++) {
18
- oralFormat[index] = +oralFormat[index]
19
- }
20
- oralFormat.push(1)
21
-
22
- switch (format) {
23
- case "3d": {
24
- return [
25
- oralFormat[0] / 255,
26
- oralFormat[1] / 255,
27
- oralFormat[2] / 255,
28
- oralFormat[3]
29
- ]
30
- }
31
- case "rgba": {
32
- return `rgba(${oralFormat[0]}, ${oralFormat[1]}, ${oralFormat[2]}, ${oralFormat[3]})`
33
- }
34
- case "rgb": {
35
- return `rgb(${oralFormat[0]}, ${oralFormat[1]}, ${oralFormat[2]})`
36
- }
37
- case "hexAlpha": {
38
- return `#${toHex(oralFormat[0])}${toHex(oralFormat[1])}${toHex(oralFormat[2])}${toHex(+(oralFormat[3] * 255).toFixed(0))}`
39
- }
40
- case "hex": {
41
- return `#${toHex(oralFormat[0])}${toHex(oralFormat[1])}${toHex(oralFormat[2])}`
42
- }
43
- }
44
- throw new Error("Unsupported color format: " + format)
1
+ const toHex = (value: number) => {
2
+ const hex = value.toString(16)
3
+ return hex.length == 1 ? ("0" + hex) : hex
4
+ }
5
+
6
+ export default function (color: string, format: string) {
7
+
8
+ const colorNode = document.getElementsByTagName('head')[0]
9
+ colorNode.style['color'] = color
10
+
11
+ // 获取结点的全部样式
12
+ const allStyle = document.defaultView && document.defaultView.getComputedStyle ?
13
+ document.defaultView.getComputedStyle(colorNode, null) :
14
+ (colorNode as any).currentStyle
15
+
16
+ const oralFormat = allStyle.getPropertyValue('color').replace(/rgba?\(/, '').replace(")", "").split(',')
17
+ for (let index = 0; index < oralFormat.length; index++) {
18
+ oralFormat[index] = +oralFormat[index]
19
+ }
20
+ oralFormat.push(1)
21
+
22
+ switch (format) {
23
+ case "3d": {
24
+ return [
25
+ oralFormat[0] / 255,
26
+ oralFormat[1] / 255,
27
+ oralFormat[2] / 255,
28
+ oralFormat[3]
29
+ ]
30
+ }
31
+ case "rgba": {
32
+ return `rgba(${oralFormat[0]}, ${oralFormat[1]}, ${oralFormat[2]}, ${oralFormat[3]})`
33
+ }
34
+ case "rgb": {
35
+ return `rgb(${oralFormat[0]}, ${oralFormat[1]}, ${oralFormat[2]})`
36
+ }
37
+ case "hexAlpha": {
38
+ return `#${toHex(oralFormat[0])}${toHex(oralFormat[1])}${toHex(oralFormat[2])}${toHex(+(oralFormat[3] * 255).toFixed(0))}`
39
+ }
40
+ case "hex": {
41
+ return `#${toHex(oralFormat[0])}${toHex(oralFormat[1])}${toHex(oralFormat[2])}`
42
+ }
43
+ }
44
+ throw new Error("Unsupported color format: " + format)
45
45
  }
@@ -1,42 +1,42 @@
1
- // 获取一组循环色彩
2
- export default function (num: number, alpha: number = 1) {
3
- // 颜色集合
4
- const 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
+ const 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,111 +1,111 @@
1
- /**
2
- * Cardinal三次插值
3
- * ----------------------------
4
- * Hermite拟合的计算是,确定两个点和两个点的斜率
5
- * 用一个y=ax(3)+bx(2)+cx+d的三次多项式来求解
6
- * 而Cardinal是建立在此基础上
7
- * 给定需要拟合的两个点和第一个点的前一个点+最后一个点的后一个点
8
- * 第一个点的斜率由第一个点的前一个点和第二个点的斜率确定
9
- * 第二个点的斜率由第一个点和第二个点的后一个点的斜率确定
10
- */
11
-
12
- import type CardinalType from '../../types/Cardinal'
13
-
14
- import Hermite from './Hermite'
15
-
16
- class Cardinal implements CardinalType {
17
- readonly name: string = 'Cardinal'
18
-
19
- // 该参数用于调整曲线走势,默认数值t=0,分水岭t=-1,|t-(-1)|的值越大,曲线走势调整的越严重
20
- private __t: number
21
-
22
- private __HS: {
23
- x: number[],
24
- h: Hermite[]
25
- }
26
- private __i: number
27
-
28
- constructor(t: number = 0) {
29
- this.__t = t
30
- }
31
-
32
- // 设置点的位置
33
- // 参数格式:[[x1, y1], [x2, y2], ... ]
34
- // 至少两个点
35
- setP(points: number[][]) {
36
-
37
- this.__HS = {
38
- "x": [],
39
- "h": []
40
- };
41
- let flag: number,
42
- slope = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]),
43
- temp: number
44
- this.__HS.x[0] = points[0][0]
45
- for (flag = 1; flag < points.length; flag++) {
46
- if (points[flag][0] <= points[flag - 1][0]) throw new Error('The point position should be increamented!')
47
- this.__HS.x[flag] = points[flag][0]
48
-
49
- // 求点斜率
50
-
51
- // temp = flag < points.length - 1 ?
52
- // (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0]) :
53
- // (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
54
-
55
- // 修复超过界限的值问题
56
- // 2023年6月25日 于南京
57
- if (flag < points.length - 1) {
58
- if (
59
- (points[flag + 1][1] > points[flag][1] && points[flag - 1][1] > points[flag][1]) ||
60
- (points[flag + 1][1] < points[flag][1] && points[flag - 1][1] < points[flag][1]) ||
61
- points[flag + 1][1] == points[flag][1] ||
62
- points[flag - 1][1] == points[flag][1]
63
- ) {
64
- temp = 0
65
- } else {
66
- temp = (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0])
67
- }
68
- } else {
69
- temp = (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
70
- }
71
-
72
- // 求解两个点直接的插值方程
73
- // 第一个点的前一个点直接取第一个点
74
- // 最后一个点的后一个点直接取最后一个点
75
- 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)
76
- slope = temp
77
- }
78
- return this
79
-
80
- }
81
-
82
- // 根据x值返回y值
83
- use(x: number) {
84
- if (this.__HS) {
85
- this.__i = -1
86
-
87
- // 寻找记录x所在位置的区间
88
- // 这里就是寻找对应的拟合函数
89
- 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]))) {
90
- this.__i += 1
91
- }
92
-
93
- // 由于js浮点运算不准确,我们对于越界的情况进行兼容
94
-
95
- if (this.__i < 0) {
96
- return this.__HS.h[0].use(this.__HS.x[0])
97
- }
98
-
99
- else if (this.__i >= this.__HS.h.length) {
100
- return this.__HS.h[this.__HS.h.length - 1].use(this.__HS.x[this.__HS.x.length - 1])
101
- }
102
-
103
- return this.__HS.h[this.__i].use(x)
104
- } else {
105
- throw new Error('You shoud first set the position!')
106
- }
107
- }
108
-
109
- }
110
-
1
+ /**
2
+ * Cardinal三次插值
3
+ * ----------------------------
4
+ * Hermite拟合的计算是,确定两个点和两个点的斜率
5
+ * 用一个y=ax(3)+bx(2)+cx+d的三次多项式来求解
6
+ * 而Cardinal是建立在此基础上
7
+ * 给定需要拟合的两个点和第一个点的前一个点+最后一个点的后一个点
8
+ * 第一个点的斜率由第一个点的前一个点和第二个点的斜率确定
9
+ * 第二个点的斜率由第一个点和第二个点的后一个点的斜率确定
10
+ */
11
+
12
+ import type CardinalType from '../../types/Cardinal'
13
+
14
+ import Hermite from './Hermite'
15
+
16
+ class Cardinal implements CardinalType {
17
+ readonly name: string = 'Cardinal'
18
+
19
+ // 该参数用于调整曲线走势,默认数值t=0,分水岭t=-1,|t-(-1)|的值越大,曲线走势调整的越严重
20
+ private __t: number
21
+
22
+ private __HS: {
23
+ x: number[],
24
+ h: Hermite[]
25
+ }
26
+ private __i: number
27
+
28
+ constructor(t: number = 0) {
29
+ this.__t = t
30
+ }
31
+
32
+ // 设置点的位置
33
+ // 参数格式:[[x1, y1], [x2, y2], ... ]
34
+ // 至少两个点
35
+ setP(points: number[][]) {
36
+
37
+ this.__HS = {
38
+ "x": [],
39
+ "h": []
40
+ };
41
+ let flag: number,
42
+ slope = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]),
43
+ temp: number
44
+ this.__HS.x[0] = points[0][0]
45
+ for (flag = 1; flag < points.length; flag++) {
46
+ if (points[flag][0] <= points[flag - 1][0]) throw new Error('The point position should be increamented!')
47
+ this.__HS.x[flag] = points[flag][0]
48
+
49
+ // 求点斜率
50
+
51
+ // temp = flag < points.length - 1 ?
52
+ // (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0]) :
53
+ // (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
54
+
55
+ // 修复超过界限的值问题
56
+ // 2023年6月25日 于南京
57
+ if (flag < points.length - 1) {
58
+ if (
59
+ (points[flag + 1][1] > points[flag][1] && points[flag - 1][1] > points[flag][1]) ||
60
+ (points[flag + 1][1] < points[flag][1] && points[flag - 1][1] < points[flag][1]) ||
61
+ points[flag + 1][1] == points[flag][1] ||
62
+ points[flag - 1][1] == points[flag][1]
63
+ ) {
64
+ temp = 0
65
+ } else {
66
+ temp = (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0])
67
+ }
68
+ } else {
69
+ temp = (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0])
70
+ }
71
+
72
+ // 求解两个点直接的插值方程
73
+ // 第一个点的前一个点直接取第一个点
74
+ // 最后一个点的后一个点直接取最后一个点
75
+ 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)
76
+ slope = temp
77
+ }
78
+ return this
79
+
80
+ }
81
+
82
+ // 根据x值返回y值
83
+ use(x: number) {
84
+ if (this.__HS) {
85
+ this.__i = -1
86
+
87
+ // 寻找记录x所在位置的区间
88
+ // 这里就是寻找对应的拟合函数
89
+ 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]))) {
90
+ this.__i += 1
91
+ }
92
+
93
+ // 由于js浮点运算不准确,我们对于越界的情况进行兼容
94
+
95
+ if (this.__i < 0) {
96
+ return this.__HS.h[0].use(this.__HS.x[0])
97
+ }
98
+
99
+ else if (this.__i >= this.__HS.h.length) {
100
+ return this.__HS.h[this.__HS.h.length - 1].use(this.__HS.x[this.__HS.x.length - 1])
101
+ }
102
+
103
+ return this.__HS.h[this.__i].use(x)
104
+ } else {
105
+ throw new Error('You shoud first set the position!')
106
+ }
107
+ }
108
+
109
+ }
110
+
111
111
  export default Cardinal