smoothBezier.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var _vector = require("../../core/vector");
  2. var v2Min = _vector.min;
  3. var v2Max = _vector.max;
  4. var v2Scale = _vector.scale;
  5. var v2Distance = _vector.distance;
  6. var v2Add = _vector.add;
  7. var v2Clone = _vector.clone;
  8. var v2Sub = _vector.sub;
  9. /**
  10. * 贝塞尔平滑曲线
  11. * @module zrender/shape/util/smoothBezier
  12. * @author pissang (https://www.github.com/pissang)
  13. * Kener (@Kener-林峰, kener.linfeng@gmail.com)
  14. * errorrik (errorrik@gmail.com)
  15. */
  16. /**
  17. * 贝塞尔平滑曲线
  18. * @alias module:zrender/shape/util/smoothBezier
  19. * @param {Array} points 线段顶点数组
  20. * @param {number} smooth 平滑等级, 0-1
  21. * @param {boolean} isLoop
  22. * @param {Array} constraint 将计算出来的控制点约束在一个包围盒内
  23. * 比如 [[0, 0], [100, 100]], 这个包围盒会与
  24. * 整个折线的包围盒做一个并集用来约束控制点。
  25. * @param {Array} 计算出来的控制点数组
  26. */
  27. function _default(points, smooth, isLoop, constraint) {
  28. var cps = [];
  29. var v = [];
  30. var v1 = [];
  31. var v2 = [];
  32. var prevPoint;
  33. var nextPoint;
  34. var min;
  35. var max;
  36. if (constraint) {
  37. min = [Infinity, Infinity];
  38. max = [-Infinity, -Infinity];
  39. for (var i = 0, len = points.length; i < len; i++) {
  40. v2Min(min, min, points[i]);
  41. v2Max(max, max, points[i]);
  42. } // 与指定的包围盒做并集
  43. v2Min(min, min, constraint[0]);
  44. v2Max(max, max, constraint[1]);
  45. }
  46. for (var i = 0, len = points.length; i < len; i++) {
  47. var point = points[i];
  48. if (isLoop) {
  49. prevPoint = points[i ? i - 1 : len - 1];
  50. nextPoint = points[(i + 1) % len];
  51. } else {
  52. if (i === 0 || i === len - 1) {
  53. cps.push(v2Clone(points[i]));
  54. continue;
  55. } else {
  56. prevPoint = points[i - 1];
  57. nextPoint = points[i + 1];
  58. }
  59. }
  60. v2Sub(v, nextPoint, prevPoint); // use degree to scale the handle length
  61. v2Scale(v, v, smooth);
  62. var d0 = v2Distance(point, prevPoint);
  63. var d1 = v2Distance(point, nextPoint);
  64. var sum = d0 + d1;
  65. if (sum !== 0) {
  66. d0 /= sum;
  67. d1 /= sum;
  68. }
  69. v2Scale(v1, v, -d0);
  70. v2Scale(v2, v, d1);
  71. var cp0 = v2Add([], point, v1);
  72. var cp1 = v2Add([], point, v2);
  73. if (constraint) {
  74. v2Max(cp0, cp0, min);
  75. v2Min(cp0, cp0, max);
  76. v2Max(cp1, cp1, min);
  77. v2Min(cp1, cp1, max);
  78. }
  79. cps.push(cp0);
  80. cps.push(cp1);
  81. }
  82. if (isLoop) {
  83. cps.push(cps.shift());
  84. }
  85. return cps;
  86. }
  87. module.exports = _default;