index.js 6.0 KB

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