nav-canvas.dev.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.NavCanvas = void 0;
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  8. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  9. var NavCanvas =
  10. /*#__PURE__*/
  11. function () {
  12. function NavCanvas(el, tabSelector) {
  13. var selectedTabIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  14. _classCallCheck(this, NavCanvas);
  15. this.j = 0.85;
  16. this.k = 10;
  17. this.l = 4;
  18. this.pattern = null;
  19. this.tabWidthList = [];
  20. this.tabHeight = 0;
  21. this.opt = {
  22. currentIndex: 0,
  23. nextIndex: 1,
  24. speed: 1,
  25. timer: 0,
  26. width: 200,
  27. height: 100,
  28. animating: false,
  29. curDisX: 0,
  30. distance: 0
  31. };
  32. this.canvas = document.getElementById(el);
  33. this.tabs = document.querySelectorAll(tabSelector);
  34. var opt = this.opt;
  35. this.calcTabs();
  36. this.initCanvas(this.canvas, opt.width, opt.height);
  37. this.createPattern(this.canvas);
  38. this.startDraw(0);
  39. this.toggle(selectedTabIndex);
  40. }
  41. _createClass(NavCanvas, [{
  42. key: "initCanvas",
  43. value: function initCanvas(canvas, width, height) {
  44. var ctx = canvas.getContext('2d');
  45. var _window = window,
  46. devicePixelRatio = _window.devicePixelRatio;
  47. canvas.width = width * devicePixelRatio;
  48. canvas.height = height * devicePixelRatio;
  49. canvas.style.width = "".concat(width, "px");
  50. canvas.style.height = "".concat(height, "px");
  51. ctx.scale(devicePixelRatio, devicePixelRatio);
  52. }
  53. }, {
  54. key: "calcTabs",
  55. value: function calcTabs() {
  56. var opt = this.opt,
  57. tabs = this.tabs;
  58. var ws = [];
  59. var totalWidth = 0;
  60. tabs.forEach(function (node) {
  61. ws.push(totalWidth);
  62. totalWidth += node.offsetWidth;
  63. });
  64. ws[0] = -20;
  65. ws.push(totalWidth);
  66. this.tabWidthList = ws;
  67. this.tabHeight = tabs[0].offsetHeight + 0;
  68. opt.height = this.tabHeight + 20;
  69. opt.width = window.innerWidth;
  70. }
  71. }, {
  72. key: "createPattern",
  73. value: function createPattern(canvas) {
  74. var w = 140;
  75. var h = 63;
  76. var r = 1;
  77. var imgCanvas = document.createElement('canvas');
  78. imgCanvas.width = w;
  79. imgCanvas.height = h;
  80. imgCanvas.style.width = "".concat(w / r, "px");
  81. imgCanvas.style.height = "".concat(h / r, "px");
  82. var ctx = imgCanvas.getContext('2d');
  83. ctx.scale(r, r);
  84. ctx.lineWidth = 0.4;
  85. for (var g = 3, _h = 0.8, j = 1; j < 30; j++) {
  86. ctx.strokeStyle = "RGBA(22, 120, 160, ".concat(_h, ")");
  87. ctx.beginPath();
  88. ctx.moveTo(0, j * g);
  89. ctx.lineTo(w, j * g);
  90. ctx.stroke();
  91. ctx.closePath();
  92. if (j > 10) {
  93. _h -= 0.1;
  94. }
  95. }
  96. this.pattern = canvas.getContext('2d').createPattern(imgCanvas, 'repeat-x');
  97. }
  98. }, {
  99. key: "calcAvgSpeed",
  100. value: function calcAvgSpeed(a) {
  101. var l = this.l,
  102. j = this.j,
  103. k = this.k;
  104. var b = (l * j * a + k * (1 - j) * a) / (k * l * 20);
  105. return Math.max(Math.abs(b), 2.5) * Math.sign(b);
  106. }
  107. }, {
  108. key: "getCurSpeed",
  109. value: function getCurSpeed(a, b) {
  110. var l = this.l,
  111. j = this.j,
  112. k = this.k,
  113. opt = this.opt;
  114. return Math.abs(a) > Math.abs(j * b) ? l * opt.speed : k * opt.speed;
  115. }
  116. }, {
  117. key: "calCurve",
  118. value: function calCurve(ctx, a, b, c, d, e) {
  119. ctx.bezierCurveTo(a + e, b, c - e, d, c, d);
  120. }
  121. }, {
  122. key: "drawHightlight",
  123. value: function drawHightlight(index) {
  124. var opt = this.opt;
  125. var ctx = this.canvas.getContext('2d');
  126. var d = 0.3;
  127. ctx.clearRect(0, 0, 2 * opt.width, 2 * opt.height);
  128. ctx.shadowColor = 'rgba(36, 131, 255, 1)';
  129. ctx.shadowBlur = 5;
  130. ctx.strokeStyle = '#004CB3';
  131. ctx.lineWidth = 0.8;
  132. ctx.fillStyle = 'none';
  133. this.draw(ctx, false);
  134. var gradient = ctx.createLinearGradient(0, 0, opt.width, opt.height);
  135. var f = index - d;
  136. gradient.addColorStop(Math.min(1, Math.max(0, 0 + f)), 'rgba(0,0,0,0)');
  137. gradient.addColorStop(Math.min(1, Math.max(0, 0 + f + 0.1)), '#8ed6ff');
  138. gradient.addColorStop(Math.min(1, 0 + f + d), '#8ed6ff');
  139. gradient.addColorStop(Math.min(1, 0 + f + d + 0.1), 'rgba(0,0,0,0)');
  140. gradient.addColorStop(1, 'rgba(0,0,0,0)');
  141. ctx.lineWidth = 1.5;
  142. ctx.strokeStyle = gradient;
  143. ctx.fillStyle = this.pattern;
  144. this.draw(ctx, true);
  145. }
  146. }, {
  147. key: "startDraw",
  148. value: function startDraw(index) {
  149. var _this = this;
  150. this.drawHightlight(index);
  151. this.opt.timer = requestAnimationFrame(function () {
  152. _this.startDraw((index + 0.005) % 1.6);
  153. });
  154. }
  155. }, {
  156. key: "draw",
  157. value: function draw(ctx, trueorfalse) {
  158. var opt = this.opt,
  159. tabWidthList = this.tabWidthList,
  160. tabHeight = this.tabHeight;
  161. var navindex = opt.currentIndex;
  162. var tableHeight = tabHeight;
  163. var f = 0;
  164. var g = 40;
  165. var i = 20;
  166. var j = 0.5;
  167. var k = 2.5;
  168. var l = 0;
  169. ctx.beginPath();
  170. ctx.moveTo(-50, opt.height + 10);
  171. ctx.lineTo(-50, tableHeight + j);
  172. if (opt.animating) {
  173. var m = this.getCurSpeed(opt.curDisX, opt.distance);
  174. l = Math.min(Math.abs(opt.distance), Math.abs(opt.curDisX + m)) * Math.sign(m);
  175. }
  176. ctx.lineTo(f + tabWidthList[navindex] + l - g / 2, tableHeight + j);
  177. this.calCurve(ctx, f + tabWidthList[navindex] + l - g / 2, tableHeight + j, f + tabWidthList[navindex] + l + g / 2, k + j, i);
  178. if (opt.animating) {
  179. var o = tabWidthList[opt.nextIndex + 1] - tabWidthList[opt.nextIndex];
  180. ctx.lineTo(f + tabWidthList[navindex] + o + l - g / 2, k + j);
  181. this.calCurve(ctx, f + tabWidthList[navindex] + o + l - g / 2, k + j, f + tabWidthList[navindex] + o + l + g / 2, tableHeight + j, i);
  182. } else {
  183. ctx.lineTo(f + tabWidthList[navindex + 1] + l - g / 2, k + j);
  184. this.calCurve(ctx, f + tabWidthList[navindex + 1] + l - g / 2, k + j, f + tabWidthList[navindex + 1] + l + g / 2, tableHeight + j, i);
  185. }
  186. ctx.lineTo(opt.width + 10, tableHeight + j);
  187. ctx.lineTo(opt.width + 10, opt.height + 10);
  188. ctx.closePath();
  189. ctx.stroke();
  190. if (trueorfalse) {
  191. ctx.fill();
  192. }
  193. if (trueorfalse && opt.animating) {
  194. opt.curDisX = l;
  195. if (Math.abs(l) >= Math.abs(opt.distance)) {
  196. opt.animating = false;
  197. opt.currentIndex = opt.nextIndex;
  198. }
  199. }
  200. }
  201. }, {
  202. key: "toggle",
  203. value: function toggle(navindex) {
  204. var opt = this.opt,
  205. tabWidthList = this.tabWidthList;
  206. if (navindex !== opt.currentIndex && tabWidthList && tabWidthList.length && (!opt.animating || navindex !== opt.nextIndex)) {
  207. opt.animating = true;
  208. opt.distance = tabWidthList[navindex] - tabWidthList[opt.currentIndex];
  209. opt.speed = this.calcAvgSpeed(opt.distance);
  210. opt.curDisX = 0;
  211. opt.nextIndex = navindex;
  212. }
  213. }
  214. }, {
  215. key: "resize",
  216. value: function resize() {
  217. var opt = this.opt;
  218. if (opt.timer) {
  219. cancelAnimationFrame(opt.timer);
  220. }
  221. this.calcTabs();
  222. this.initCanvas(this.canvas, opt.width, opt.height);
  223. this.startDraw(0);
  224. }
  225. }]);
  226. return NavCanvas;
  227. }();
  228. exports.NavCanvas = NavCanvas;