index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var utils_1 = require("../common/utils");
  5. var color_1 = require("../common/color");
  6. function format(rate) {
  7. return Math.min(Math.max(rate, 0), 100);
  8. }
  9. var PERIMETER = 2 * Math.PI;
  10. var BEGIN_ANGLE = -Math.PI / 2;
  11. var STEP = 1;
  12. component_1.VantComponent({
  13. props: {
  14. text: String,
  15. lineCap: {
  16. type: String,
  17. value: 'round'
  18. },
  19. value: {
  20. type: Number,
  21. value: 0,
  22. observer: 'reRender'
  23. },
  24. speed: {
  25. type: Number,
  26. value: 50
  27. },
  28. size: {
  29. type: Number,
  30. value: 100,
  31. },
  32. fill: String,
  33. layerColor: {
  34. type: String,
  35. value: color_1.WHITE
  36. },
  37. color: {
  38. type: [String, Object],
  39. value: color_1.BLUE,
  40. observer: 'setHoverColor'
  41. },
  42. strokeWidth: {
  43. type: Number,
  44. value: 4
  45. },
  46. clockwise: {
  47. type: Boolean,
  48. value: true
  49. }
  50. },
  51. data: {
  52. hoverColor: color_1.BLUE
  53. },
  54. methods: {
  55. getContext: function () {
  56. if (!this.ctx) {
  57. this.ctx = wx.createCanvasContext('van-circle', this);
  58. }
  59. return this.ctx;
  60. },
  61. setHoverColor: function () {
  62. var context = this.getContext();
  63. var _a = this.data, color = _a.color, size = _a.size;
  64. var hoverColor = color;
  65. if (utils_1.isObj(color)) {
  66. var LinearColor_1 = context.createLinearGradient(size, 0, 0, 0);
  67. Object.keys(color)
  68. .sort(function (a, b) { return parseFloat(a) - parseFloat(b); })
  69. .map(function (key) { return LinearColor_1.addColorStop(parseFloat(key) / 100, color[key]); });
  70. hoverColor = LinearColor_1;
  71. }
  72. this.setData({ hoverColor: hoverColor });
  73. },
  74. presetCanvas: function (context, strokeStyle, beginAngle, endAngle, fill) {
  75. var _a = this.data, strokeWidth = _a.strokeWidth, lineCap = _a.lineCap, clockwise = _a.clockwise, size = _a.size;
  76. var position = size / 2;
  77. var radius = position - strokeWidth / 2;
  78. context.setStrokeStyle(strokeStyle);
  79. context.setLineWidth(strokeWidth);
  80. context.setLineCap(lineCap);
  81. context.beginPath();
  82. context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
  83. context.stroke();
  84. if (fill) {
  85. context.setFillStyle(fill);
  86. context.fill();
  87. }
  88. },
  89. renderLayerCircle: function (context) {
  90. var _a = this.data, layerColor = _a.layerColor, fill = _a.fill;
  91. this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
  92. },
  93. renderHoverCircle: function (context, formatValue) {
  94. var _a = this.data, clockwise = _a.clockwise, hoverColor = _a.hoverColor;
  95. // 结束角度
  96. var progress = PERIMETER * (formatValue / 100);
  97. var endAngle = clockwise
  98. ? BEGIN_ANGLE + progress
  99. : 3 * Math.PI - (BEGIN_ANGLE + progress);
  100. this.presetCanvas(context, hoverColor, BEGIN_ANGLE, endAngle);
  101. },
  102. drawCircle: function (currentValue) {
  103. var context = this.getContext();
  104. var size = this.data.size;
  105. context.clearRect(0, 0, size, size);
  106. this.renderLayerCircle(context);
  107. var formatValue = format(currentValue);
  108. if (formatValue !== 0) {
  109. this.renderHoverCircle(context, formatValue);
  110. }
  111. context.draw();
  112. },
  113. reRender: function () {
  114. var _this = this;
  115. // tofector 动画暂时没有想到好的解决方案
  116. var _a = this.data, value = _a.value, speed = _a.speed;
  117. if (speed <= 0 || speed > 1000) {
  118. this.drawCircle(value);
  119. return;
  120. }
  121. this.clearInterval();
  122. this.currentValue = this.currentValue || 0;
  123. this.interval = setInterval(function () {
  124. if (_this.currentValue !== value) {
  125. if (_this.currentValue < value) {
  126. _this.currentValue += STEP;
  127. }
  128. else {
  129. _this.currentValue -= STEP;
  130. }
  131. _this.drawCircle(_this.currentValue);
  132. }
  133. else {
  134. _this.clearInterval();
  135. }
  136. }, 1000 / speed);
  137. },
  138. clearInterval: function () {
  139. if (this.interval) {
  140. clearInterval(this.interval);
  141. this.interval = null;
  142. }
  143. }
  144. },
  145. created: function () {
  146. var value = this.data.value;
  147. this.currentValue = value;
  148. this.drawCircle(value);
  149. },
  150. destroyed: function () {
  151. this.ctx = null;
  152. this.clearInterval();
  153. }
  154. });