index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. (0, 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, type = _a.type, size = _a.size;
  73. if (type === '' || !(0, version_1.canIUseCanvas2d)()) {
  74. var ctx = wx.createCanvasContext('van-circle', this);
  75. return Promise.resolve(ctx);
  76. }
  77. var dpr = (0, utils_1.getSystemInfoSync)().pixelRatio;
  78. return new Promise(function (resolve) {
  79. wx.createSelectorQuery()
  80. .in(_this)
  81. .select('#van-circle')
  82. .node()
  83. .exec(function (res) {
  84. var canvas = res[0].node;
  85. var ctx = canvas.getContext(type);
  86. if (!_this.inited) {
  87. _this.inited = true;
  88. canvas.width = size * dpr;
  89. canvas.height = size * dpr;
  90. ctx.scale(dpr, dpr);
  91. }
  92. resolve((0, canvas_1.adaptor)(ctx));
  93. });
  94. });
  95. },
  96. setHoverColor: function () {
  97. var _this = this;
  98. var _a = this.data, color = _a.color, size = _a.size;
  99. if ((0, validator_1.isObj)(color)) {
  100. return this.getContext().then(function (context) {
  101. var LinearColor = context.createLinearGradient(size, 0, 0, 0);
  102. Object.keys(color)
  103. .sort(function (a, b) { return parseFloat(a) - parseFloat(b); })
  104. .map(function (key) {
  105. return LinearColor.addColorStop(parseFloat(key) / 100, color[key]);
  106. });
  107. _this.hoverColor = LinearColor;
  108. });
  109. }
  110. this.hoverColor = color;
  111. return Promise.resolve();
  112. },
  113. presetCanvas: function (context, strokeStyle, beginAngle, endAngle, fill) {
  114. var _a = this.data, strokeWidth = _a.strokeWidth, lineCap = _a.lineCap, clockwise = _a.clockwise, size = _a.size;
  115. var position = size / 2;
  116. var radius = position - strokeWidth / 2;
  117. context.setStrokeStyle(strokeStyle);
  118. context.setLineWidth(strokeWidth);
  119. context.setLineCap(lineCap);
  120. context.beginPath();
  121. context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
  122. context.stroke();
  123. if (fill) {
  124. context.setFillStyle(fill);
  125. context.fill();
  126. }
  127. },
  128. renderLayerCircle: function (context) {
  129. var _a = this.data, layerColor = _a.layerColor, fill = _a.fill;
  130. this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
  131. },
  132. renderHoverCircle: function (context, formatValue) {
  133. var clockwise = this.data.clockwise;
  134. // 结束角度
  135. var progress = PERIMETER * (formatValue / 100);
  136. var endAngle = clockwise
  137. ? BEGIN_ANGLE + progress
  138. : 3 * Math.PI - (BEGIN_ANGLE + progress);
  139. this.presetCanvas(context, this.hoverColor, BEGIN_ANGLE, endAngle);
  140. },
  141. drawCircle: function (currentValue) {
  142. var _this = this;
  143. var size = this.data.size;
  144. this.getContext().then(function (context) {
  145. context.clearRect(0, 0, size, size);
  146. _this.renderLayerCircle(context);
  147. var formatValue = format(currentValue);
  148. if (formatValue !== 0) {
  149. _this.renderHoverCircle(context, formatValue);
  150. }
  151. context.draw();
  152. });
  153. },
  154. reRender: function () {
  155. var _this = this;
  156. // tofector 动画暂时没有想到好的解决方案
  157. var _a = this.data, value = _a.value, speed = _a.speed;
  158. if (speed <= 0 || speed > 1000) {
  159. this.drawCircle(value);
  160. return;
  161. }
  162. this.clearMockInterval();
  163. this.currentValue = this.currentValue || 0;
  164. var run = function () {
  165. _this.interval = setTimeout(function () {
  166. if (_this.currentValue !== value) {
  167. if (Math.abs(_this.currentValue - value) < STEP) {
  168. _this.currentValue = value;
  169. }
  170. else if (_this.currentValue < value) {
  171. _this.currentValue += STEP;
  172. }
  173. else {
  174. _this.currentValue -= STEP;
  175. }
  176. _this.drawCircle(_this.currentValue);
  177. run();
  178. }
  179. else {
  180. _this.clearMockInterval();
  181. }
  182. }, 1000 / speed);
  183. };
  184. run();
  185. },
  186. clearMockInterval: function () {
  187. if (this.interval) {
  188. clearTimeout(this.interval);
  189. this.interval = null;
  190. }
  191. },
  192. },
  193. mounted: function () {
  194. var _this = this;
  195. this.currentValue = this.data.value;
  196. this.setHoverColor().then(function () {
  197. _this.drawCircle(_this.currentValue);
  198. });
  199. },
  200. destroyed: function () {
  201. this.clearMockInterval();
  202. },
  203. });