index.js 5.7 KB

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