index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if (!context)
  102. return;
  103. var LinearColor = context.createLinearGradient(size, 0, 0, 0);
  104. Object.keys(color)
  105. .sort(function (a, b) { return parseFloat(a) - parseFloat(b); })
  106. .map(function (key) {
  107. return LinearColor.addColorStop(parseFloat(key) / 100, color[key]);
  108. });
  109. _this.hoverColor = LinearColor;
  110. });
  111. }
  112. this.hoverColor = color;
  113. return Promise.resolve();
  114. },
  115. presetCanvas: function (context, strokeStyle, beginAngle, endAngle, fill) {
  116. var _a = this.data, strokeWidth = _a.strokeWidth, lineCap = _a.lineCap, clockwise = _a.clockwise, size = _a.size;
  117. var position = size / 2;
  118. var radius = position - strokeWidth / 2;
  119. context.setStrokeStyle(strokeStyle);
  120. context.setLineWidth(strokeWidth);
  121. context.setLineCap(lineCap);
  122. context.beginPath();
  123. context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
  124. context.stroke();
  125. if (fill) {
  126. context.setFillStyle(fill);
  127. context.fill();
  128. }
  129. },
  130. renderLayerCircle: function (context) {
  131. var _a = this.data, layerColor = _a.layerColor, fill = _a.fill;
  132. this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
  133. },
  134. renderHoverCircle: function (context, formatValue) {
  135. var clockwise = this.data.clockwise;
  136. // 结束角度
  137. var progress = PERIMETER * (formatValue / 100);
  138. var endAngle = clockwise
  139. ? BEGIN_ANGLE + progress
  140. : 3 * Math.PI - (BEGIN_ANGLE + progress);
  141. this.presetCanvas(context, this.hoverColor, BEGIN_ANGLE, endAngle);
  142. },
  143. drawCircle: function (currentValue) {
  144. var _this = this;
  145. var size = this.data.size;
  146. this.getContext().then(function (context) {
  147. if (!context)
  148. return;
  149. context.clearRect(0, 0, size, size);
  150. _this.renderLayerCircle(context);
  151. var formatValue = format(currentValue);
  152. if (formatValue !== 0) {
  153. _this.renderHoverCircle(context, formatValue);
  154. }
  155. context.draw();
  156. });
  157. },
  158. reRender: function () {
  159. var _this = this;
  160. // tofector 动画暂时没有想到好的解决方案
  161. var _a = this.data, value = _a.value, speed = _a.speed;
  162. if (speed <= 0 || speed > 1000) {
  163. this.drawCircle(value);
  164. return;
  165. }
  166. this.clearMockInterval();
  167. this.currentValue = this.currentValue || 0;
  168. var run = function () {
  169. _this.interval = setTimeout(function () {
  170. if (_this.currentValue !== value) {
  171. if (Math.abs(_this.currentValue - value) < STEP) {
  172. _this.currentValue = value;
  173. }
  174. else if (_this.currentValue < value) {
  175. _this.currentValue += STEP;
  176. }
  177. else {
  178. _this.currentValue -= STEP;
  179. }
  180. _this.drawCircle(_this.currentValue);
  181. run();
  182. }
  183. else {
  184. _this.clearMockInterval();
  185. }
  186. }, 1000 / speed);
  187. };
  188. run();
  189. },
  190. clearMockInterval: function () {
  191. if (this.interval) {
  192. clearTimeout(this.interval);
  193. this.interval = null;
  194. }
  195. },
  196. },
  197. mounted: function () {
  198. var _this = this;
  199. this.currentValue = this.data.value;
  200. this.setHoverColor().then(function () {
  201. _this.drawCircle(_this.currentValue);
  202. });
  203. },
  204. destroyed: function () {
  205. this.clearMockInterval();
  206. },
  207. });