Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

116 wiersze
4.1 KiB

  1. import clamp from 'lodash-es/clamp';
  2. import { setStyle } from '../../utils/set-style';
  3. var GLOW_MAX_OPACITY = 0.75;
  4. var GLOW_MAX_OFFSET = 0.25;
  5. var Glow = /** @class */ (function () {
  6. function Glow(_scrollbar) {
  7. this._scrollbar = _scrollbar;
  8. this._canvas = document.createElement('canvas');
  9. this._ctx = this._canvas.getContext('2d');
  10. setStyle(this._canvas, {
  11. position: 'absolute',
  12. top: 0,
  13. left: 0,
  14. width: '100%',
  15. height: '100%',
  16. display: 'none',
  17. });
  18. }
  19. Glow.prototype.mount = function () {
  20. this._scrollbar.containerEl.appendChild(this._canvas);
  21. };
  22. Glow.prototype.unmount = function () {
  23. if (this._canvas.parentNode) {
  24. this._canvas.parentNode.removeChild(this._canvas);
  25. }
  26. };
  27. Glow.prototype.adjust = function () {
  28. var size = this._scrollbar.size;
  29. var DPR = window.devicePixelRatio || 1;
  30. var nextWidth = size.container.width * DPR;
  31. var nextHeight = size.container.height * DPR;
  32. if (nextWidth === this._canvas.width && nextHeight === this._canvas.height) {
  33. return;
  34. }
  35. this._canvas.width = nextWidth;
  36. this._canvas.height = nextHeight;
  37. this._ctx.scale(DPR, DPR);
  38. };
  39. Glow.prototype.recordTouch = function (event) {
  40. var touch = event.touches[event.touches.length - 1];
  41. this._touchX = touch.clientX;
  42. this._touchY = touch.clientY;
  43. };
  44. Glow.prototype.render = function (_a, color) {
  45. var _b = _a.x, x = _b === void 0 ? 0 : _b, _c = _a.y, y = _c === void 0 ? 0 : _c;
  46. if (!x && !y) {
  47. setStyle(this._canvas, {
  48. display: 'none',
  49. });
  50. return;
  51. }
  52. setStyle(this._canvas, {
  53. display: 'block',
  54. });
  55. var size = this._scrollbar.size;
  56. this._ctx.clearRect(0, 0, size.container.width, size.container.height);
  57. this._ctx.fillStyle = color;
  58. this._renderX(x);
  59. this._renderY(y);
  60. };
  61. Glow.prototype._getMaxOverscroll = function () {
  62. var options = this._scrollbar.options.plugins.overscroll;
  63. return options && options.maxOverscroll ? options.maxOverscroll : 150;
  64. };
  65. Glow.prototype._renderX = function (strength) {
  66. var size = this._scrollbar.size;
  67. var maxOverscroll = this._getMaxOverscroll();
  68. var _a = size.container, width = _a.width, height = _a.height;
  69. var ctx = this._ctx;
  70. ctx.save();
  71. if (strength > 0) {
  72. // glow on right side
  73. // horizontally flip
  74. ctx.transform(-1, 0, 0, 1, width, 0);
  75. }
  76. var opacity = clamp(Math.abs(strength) / maxOverscroll, 0, GLOW_MAX_OPACITY);
  77. var startOffset = clamp(opacity, 0, GLOW_MAX_OFFSET) * width;
  78. // controll point
  79. var x = Math.abs(strength);
  80. var y = this._touchY || (height / 2);
  81. ctx.globalAlpha = opacity;
  82. ctx.beginPath();
  83. ctx.moveTo(0, -startOffset);
  84. ctx.quadraticCurveTo(x, y, 0, height + startOffset);
  85. ctx.fill();
  86. ctx.closePath();
  87. ctx.restore();
  88. };
  89. Glow.prototype._renderY = function (strength) {
  90. var size = this._scrollbar.size;
  91. var maxOverscroll = this._getMaxOverscroll();
  92. var _a = size.container, width = _a.width, height = _a.height;
  93. var ctx = this._ctx;
  94. ctx.save();
  95. if (strength > 0) {
  96. // glow on bottom side
  97. // vertically flip
  98. ctx.transform(1, 0, 0, -1, 0, height);
  99. }
  100. var opacity = clamp(Math.abs(strength) / maxOverscroll, 0, GLOW_MAX_OPACITY);
  101. var startOffset = clamp(opacity, 0, GLOW_MAX_OFFSET) * width;
  102. // controll point
  103. var x = this._touchX || (width / 2);
  104. var y = Math.abs(strength);
  105. ctx.globalAlpha = opacity;
  106. ctx.beginPath();
  107. ctx.moveTo(-startOffset, 0);
  108. ctx.quadraticCurveTo(x, y, width + startOffset, 0);
  109. ctx.fill();
  110. ctx.closePath();
  111. ctx.restore();
  112. };
  113. return Glow;
  114. }());
  115. export { Glow };
  116. //# sourceMappingURL=glow.js.map