index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. var _component = require("../common/component");
  3. var _utils = require("../common/utils");
  4. var _color = require("../common/color");
  5. var _canvas = require("./canvas");
  6. var _my = require("../../__antmove/api/index.js")(my);
  7. var wx = _my;
  8. function format(rate) {
  9. return Math.min(Math.max(rate, 0), 100);
  10. }
  11. var PERIMETER = 2 * Math.PI;
  12. var BEGIN_ANGLE = -Math.PI / 2;
  13. var STEP = 1;
  14. (0, _component.VantComponent)({
  15. props: {
  16. id: {
  17. type: String,
  18. value: "van-circle"
  19. },
  20. text: String,
  21. lineCap: {
  22. type: String,
  23. value: "round"
  24. },
  25. value: {
  26. type: Number,
  27. value: 0,
  28. observer: "reRender"
  29. },
  30. speed: {
  31. type: Number,
  32. value: 50
  33. },
  34. size: {
  35. type: Number,
  36. value: 100,
  37. observer: function observer() {
  38. this.drawCircle(this.currentValue);
  39. }
  40. },
  41. fill: String,
  42. layerColor: {
  43. type: String,
  44. value: _color.WHITE
  45. },
  46. color: {
  47. type: [String, Object],
  48. value: _color.BLUE,
  49. observer: function observer() {
  50. var _this = this;
  51. this.setHoverColor().then(function () {
  52. _this.drawCircle(_this.currentValue);
  53. });
  54. }
  55. },
  56. type: {
  57. type: String,
  58. value: ""
  59. },
  60. strokeWidth: {
  61. type: Number,
  62. value: 4
  63. },
  64. clockwise: {
  65. type: Boolean,
  66. value: true
  67. }
  68. },
  69. data: {
  70. hoverColor: _color.BLUE
  71. },
  72. methods: {
  73. getContext: function getContext() {
  74. var _this2 = this;
  75. var _this$data = this.data,
  76. type = _this$data.type,
  77. size = _this$data.size;
  78. if (type === "") {
  79. var ctx = null;
  80. ctx = wx.createCanvasContext(this.props.id);
  81. return Promise.resolve(ctx);
  82. }
  83. var dpr = wx.getSystemInfoSync().pixelRatio;
  84. return new Promise(function (resolve) {
  85. var ctx = wx.createCanvasContext(_this2.props.id);
  86. if (!_this2.inited) {
  87. _this2.inited = true;
  88. _this2.setData({
  89. _size: size * dpr
  90. });
  91. ctx.scale(dpr, dpr);
  92. }
  93. resolve(ctx);
  94. });
  95. },
  96. setHoverColor: function setHoverColor() {
  97. var _this$data2 = this.data,
  98. color = _this$data2.color,
  99. size = _this$data2.size;
  100. this.hoverColor = color;
  101. return Promise.resolve();
  102. },
  103. presetCanvas: function presetCanvas(context, strokeStyle, beginAngle, endAngle, fill) {
  104. var _this$data3 = this.data,
  105. strokeWidth = _this$data3.strokeWidth,
  106. lineCap = _this$data3.lineCap,
  107. clockwise = _this$data3.clockwise,
  108. size = _this$data3.size;
  109. var position = size / 2;
  110. var radius = position - strokeWidth / 2;
  111. context.setStrokeStyle(strokeStyle);
  112. context.setLineWidth(strokeWidth);
  113. context.setLineCap(lineCap);
  114. context.beginPath();
  115. context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
  116. context.stroke();
  117. if (fill) {
  118. context.setFillStyle(fill);
  119. context.fill();
  120. }
  121. },
  122. renderLayerCircle: function renderLayerCircle(context) {
  123. var _this$data4 = this.data,
  124. layerColor = _this$data4.layerColor,
  125. fill = _this$data4.fill;
  126. this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
  127. },
  128. renderHoverCircle: function renderHoverCircle(context, formatValue) {
  129. var _this$data5 = this.data,
  130. clockwise = _this$data5.clockwise,
  131. size = _this$data5.size,
  132. color = _this$data5.color; // 结束角度
  133. var progress = PERIMETER * (formatValue / 100);
  134. var endAngle = clockwise ? BEGIN_ANGLE + progress : 3 * Math.PI - (BEGIN_ANGLE + progress);
  135. if ((0, _utils.isObj)(this.hoverColor)) {
  136. var LinearColor = context.createLinearGradient(size, 0, 0, 0);
  137. Object.keys(color).sort(function (a, b) {
  138. return parseFloat(a) - parseFloat(b);
  139. }).map(function (key) {
  140. LinearColor.addColorStop(parseFloat(key) / 100, color[key]);
  141. });
  142. this.hoverColor = LinearColor;
  143. }
  144. this.presetCanvas(context, this.hoverColor, BEGIN_ANGLE, endAngle);
  145. },
  146. drawCircle: function drawCircle(currentValue) {
  147. var _this3 = this;
  148. var size = this.data.size;
  149. this.getContext().then(function (context) {
  150. context.clearRect(0, 0, size, size);
  151. _this3.renderLayerCircle(context);
  152. var formatValue = format(currentValue);
  153. if (formatValue !== 0) {
  154. _this3.renderHoverCircle(context, formatValue);
  155. }
  156. context.draw();
  157. });
  158. },
  159. reRender: function reRender() {
  160. var _this4 = this;
  161. // tofector 动画暂时没有想到好的解决方案
  162. var _this$data6 = this.data,
  163. value = _this$data6.value,
  164. speed = _this$data6.speed;
  165. if (speed <= 0 || speed > 1000) {
  166. this.drawCircle(value);
  167. return;
  168. }
  169. this.clearInterval();
  170. this.currentValue = this.currentValue || 0;
  171. this.interval = setInterval(function () {
  172. if (_this4.currentValue !== value) {
  173. if (_this4.currentValue < value) {
  174. _this4.currentValue += STEP;
  175. } else {
  176. _this4.currentValue -= STEP;
  177. }
  178. _this4.drawCircle(_this4.currentValue);
  179. } else {
  180. _this4.clearInterval();
  181. }
  182. }, 1000 / speed);
  183. },
  184. clearInterval: function (_clearInterval) {
  185. function clearInterval() {
  186. return _clearInterval.apply(this, arguments);
  187. }
  188. clearInterval.toString = function () {
  189. return _clearInterval.toString();
  190. };
  191. return clearInterval;
  192. }(function () {
  193. if (this.interval) {
  194. clearInterval(this.interval);
  195. this.interval = null;
  196. }
  197. })
  198. },
  199. mounted: function mounted() {
  200. var _this5 = this;
  201. this.currentValue = this.data.value;
  202. this.setHoverColor().then(function () {
  203. _this5.drawCircle(_this5.currentValue);
  204. });
  205. },
  206. destroyed: function destroyed() {
  207. this.clearInterval();
  208. }
  209. });