circle.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="circle-progress-bar" :style="{
  3. width: sunit(size),
  4. height: sunit(size)}">
  5. <view class="circle" :change:prop="animateModule.pro" :prop="cpro" :data-animate="animate"
  6. :style="{transform: `rotate(${start * 360 + 45}deg)`,border: `${sunit(border_width)} solid ${border_color}`}">
  7. </view>
  8. <view class="bg" v-if="background" :style="{background: background}"></view>
  9. <view class="border-back" v-if="border_back_color"
  10. :style="{border: `calc(${sunit(border_width)} - 1px) solid ${border_back_color}`}"></view>
  11. <view class="center">
  12. <text>{{content}}</text>
  13. </view>
  14. </view>
  15. </template>
  16. <script module="animateModule" lang="wxs">
  17. var Timing = {
  18. easeIn: function easeIn(pos) {
  19. return Math.pow(pos, 3);
  20. },
  21. easeOut: function easeOut(pos) {
  22. return Math.pow(pos - 1, 3) + 1;
  23. },
  24. easeInOut: function easeInOut(pos) {
  25. if ((pos /= 0.5) < 1) {
  26. return 0.5 * Math.pow(pos, 3);
  27. } else {
  28. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  29. }
  30. },
  31. linear: function linear(pos) {
  32. return pos;
  33. }
  34. };
  35. //#ifdef MP
  36. function setTimeout(t, cb, d) {
  37. if (d > 0) {
  38. var s = getDate().getTime();
  39. var fn = function() {
  40. if (getDate().getTime() - s > d) {
  41. cb && cb();
  42. } else
  43. t.requestAnimationFrame(fn);
  44. }
  45. fn();
  46. } else
  47. cb && cb();
  48. }
  49. //#endif
  50. function Animation(opts) {
  51. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  52. opts.timing = opts.timing || 'linear';
  53. var delay = 17;
  54. function createAnimationFrame() {
  55. if (typeof setTimeout !== 'undefined') {
  56. return function(step, delay) {
  57. //#ifndef MP
  58. setTimeout(function() {
  59. var timeStamp = +new Date();
  60. step(timeStamp);
  61. }, delay);
  62. //#endif
  63. //#ifdef MP
  64. setTimeout(opts.instance, function() {
  65. var timeStamp = getDate()
  66. step(timeStamp);
  67. }, delay)
  68. //#endif
  69. };
  70. } else if (typeof requestAnimationFrame !== 'undefined') {
  71. return requestAnimationFrame;
  72. } else {
  73. return function(step) {
  74. step(null);
  75. };
  76. }
  77. };
  78. var animationFrame = createAnimationFrame();
  79. var startTimeStamp = null;
  80. var _step = function step(timestamp) {
  81. if (timestamp === null) {
  82. opts.onProcess && opts.onProcess(1);
  83. opts.onAnimationFinish && opts.onAnimationFinish();
  84. return;
  85. }
  86. if (startTimeStamp === null) {
  87. startTimeStamp = timestamp;
  88. }
  89. if (timestamp - startTimeStamp < opts.duration) {
  90. var process = (timestamp - startTimeStamp) / opts.duration;
  91. var timingFunction = Timing[opts.timing];
  92. process = timingFunction(process);
  93. opts.onProcess && opts.onProcess(process);
  94. animationFrame(_step, delay);
  95. } else {
  96. opts.onProcess && opts.onProcess(1);
  97. opts.onAnimationFinish && opts.onAnimationFinish();
  98. }
  99. };
  100. animationFrame(_step, delay);
  101. }
  102. function getPath(deg) {
  103. var path = '50% 50%'
  104. //各个锚点
  105. var ps = ['0% 0%', '100% 0%', '100% 100%', '0% 100%']
  106. var ps1 = path + ',' + ps[0]
  107. var ps2 = ps1 + ',' + ps[1]
  108. var ps3 = ps2 + ',' + ps[2]
  109. var ps4 = ps3 + ',' + ps[3]
  110. var ops = [
  111. function(per) {
  112. return ps1 + ',' + (per + '% 0%')
  113. },
  114. function(per) {
  115. return ps2 + ',' + ('100% ' + per + '%')
  116. },
  117. function(per) {
  118. return ps3 + ',' + (100 - per) + '% 100%'
  119. },
  120. function(per) {
  121. return ps4 + ',' + '0% ' + (100 - per) + '%'
  122. },
  123. ]
  124. if (deg == 0) {
  125. return 'polygon(50% 50%, 50% 0%)'
  126. } else if (deg % 360 == 0) {
  127. return ''
  128. }
  129. var idx = parseInt(deg / 90) % 4
  130. var pdeg = deg % 90
  131. var per = pdeg / 90 * 100
  132. if (ops[idx]) {
  133. return 'polygon(' + ops[idx](per) + ')'
  134. } else {
  135. return ''
  136. }
  137. }
  138. function setDeg(newValue, oldValue, ownerInstance, instance) {
  139. var odeg = oldValue * 360
  140. var deg = newValue * 360
  141. var offset = deg - odeg
  142. var ds = instance.getDataset()
  143. if (!ds.animate) {
  144. var path = getPath(deg)
  145. instance.setStyle({
  146. 'clip-path': path,
  147. })
  148. return
  149. }
  150. Animation({
  151. instance: ownerInstance,
  152. timing: 'easeInOut',
  153. duration: 300,
  154. onProcess: function onProcess(process) {
  155. var pdeg = odeg + process * offset
  156. var path = getPath(pdeg)
  157. var com = ownerInstance.selectComponent('.circle');
  158. com.setStyle({
  159. 'clip-path': path,
  160. })
  161. },
  162. onAnimationFinish: function onAnimationFinish() {}
  163. });
  164. }
  165. module.exports = {
  166. pro: setDeg,
  167. }
  168. </script>
  169. <script>
  170. export default {
  171. props: {
  172. pro: {
  173. type: Number,
  174. default: 0
  175. },
  176. //起始位置 0-1
  177. start: {
  178. type: Number,
  179. default: 0,
  180. },
  181. //圆形大小
  182. size: {
  183. type: Number,
  184. default: 100
  185. },
  186. //线宽度
  187. border_width: {
  188. type: Number,
  189. default: 10
  190. },
  191. //线颜色
  192. border_color: {
  193. type: String,
  194. default: '#07C160',
  195. },
  196. //线背景色
  197. border_back_color: {
  198. type: String,
  199. },
  200. //中心内容背景色
  201. background: {
  202. type: String,
  203. },
  204. //单位
  205. unit: {
  206. type: String,
  207. default: 'rpx',
  208. },
  209. // 内容
  210. content: {
  211. type: String,
  212. default: '0',
  213. },
  214. //是否启用动画
  215. animate: {
  216. type: Boolean,
  217. default: true,
  218. }
  219. },
  220. data() {
  221. return {
  222. cpro: 0,
  223. }
  224. },
  225. watch: {
  226. pro(val) {
  227. this.cpro = val
  228. }
  229. },
  230. mounted() {
  231. this.cpro = this.pro
  232. },
  233. methods: {
  234. sunit(num) {
  235. if (typeof num === 'number') {
  236. return num + this.unit
  237. }
  238. }
  239. }
  240. }
  241. </script>
  242. <style scoped lang="scss">
  243. .circle-progress-bar {
  244. position: relative;
  245. }
  246. .circle,
  247. .bg,
  248. .border-back {
  249. height: 100%;
  250. width: 100%;
  251. border-radius: 50%;
  252. position: absolute;
  253. box-sizing: border-box;
  254. }
  255. .circle {
  256. z-index: 1;
  257. }
  258. .border-back {
  259. height: calc(100% - 1px);
  260. width: calc(100% - 1px);
  261. left: 50%;
  262. top: 50%;
  263. transform: translate(-50%, -50%);
  264. }
  265. .point {
  266. position: absolute;
  267. border-radius: 50%;
  268. z-index: 1;
  269. }
  270. .center {
  271. position: absolute;
  272. left: 50%;
  273. top: 50%;
  274. transform: translate(-50%, -50%);
  275. z-index: 2;
  276. font-size: 12px;
  277. color: #858585;
  278. }
  279. </style>