circle.wxml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <wxs module="animateModule">
  2. var Timing = {
  3. easeIn: function easeIn(pos) {
  4. return Math.pow(pos, 3);
  5. },
  6. easeOut: function easeOut(pos) {
  7. return Math.pow(pos - 1, 3) + 1;
  8. },
  9. easeInOut: function easeInOut(pos) {
  10. if ((pos /= 0.5) < 1) {
  11. return 0.5 * Math.pow(pos, 3);
  12. } else {
  13. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  14. }
  15. },
  16. linear: function linear(pos) {
  17. return pos;
  18. }
  19. };
  20. function setTimeout(t, cb, d) {
  21. if (d > 0) {
  22. var s = getDate().getTime();
  23. var fn = function() {
  24. if (getDate().getTime() - s > d) {
  25. cb && cb();
  26. } else
  27. t.requestAnimationFrame(fn);
  28. }
  29. fn();
  30. } else
  31. cb && cb();
  32. }
  33. function Animation(opts) {
  34. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  35. opts.timing = opts.timing || 'linear';
  36. var delay = 17;
  37. function createAnimationFrame() {
  38. if (typeof setTimeout !== 'undefined') {
  39. return function(step, delay) {
  40. setTimeout(opts.instance, function() {
  41. var timeStamp = getDate()
  42. step(timeStamp);
  43. }, delay)
  44. };
  45. } else if (typeof requestAnimationFrame !== 'undefined') {
  46. return requestAnimationFrame;
  47. } else {
  48. return function(step) {
  49. step(null);
  50. };
  51. }
  52. };
  53. var animationFrame = createAnimationFrame();
  54. var startTimeStamp = null;
  55. var _step = function step(timestamp) {
  56. if (timestamp === null) {
  57. opts.onProcess && opts.onProcess(1);
  58. opts.onAnimationFinish && opts.onAnimationFinish();
  59. return;
  60. }
  61. if (startTimeStamp === null) {
  62. startTimeStamp = timestamp;
  63. }
  64. if (timestamp - startTimeStamp < opts.duration) {
  65. var process = (timestamp - startTimeStamp) / opts.duration;
  66. var timingFunction = Timing[opts.timing];
  67. process = timingFunction(process);
  68. opts.onProcess && opts.onProcess(process);
  69. animationFrame(_step, delay);
  70. } else {
  71. opts.onProcess && opts.onProcess(1);
  72. opts.onAnimationFinish && opts.onAnimationFinish();
  73. }
  74. };
  75. animationFrame(_step, delay);
  76. }
  77. function getPath(deg) {
  78. var path = '50% 50%'
  79. //各个锚点
  80. var ps = ['0% 0%', '100% 0%', '100% 100%', '0% 100%']
  81. var ps1 = path + ',' + ps[0]
  82. var ps2 = ps1 + ',' + ps[1]
  83. var ps3 = ps2 + ',' + ps[2]
  84. var ps4 = ps3 + ',' + ps[3]
  85. var ops = [
  86. function(per) {
  87. return ps1 + ',' + (per + '% 0%')
  88. },
  89. function(per) {
  90. return ps2 + ',' + ('100% ' + per + '%')
  91. },
  92. function(per) {
  93. return ps3 + ',' + (100 - per) + '% 100%'
  94. },
  95. function(per) {
  96. return ps4 + ',' + '0% ' + (100 - per) + '%'
  97. },
  98. ]
  99. if (deg == 0) {
  100. return 'polygon(50% 50%, 50% 0%)'
  101. } else if (deg % 360 == 0) {
  102. return ''
  103. }
  104. var idx = parseInt(deg / 90) % 4
  105. var pdeg = deg % 90
  106. var per = pdeg / 90 * 100
  107. if (ops[idx]) {
  108. return 'polygon(' + ops[idx](per) + ')'
  109. } else {
  110. return ''
  111. }
  112. }
  113. function setDeg(newValue, oldValue, ownerInstance, instance) {
  114. var odeg = oldValue * 360
  115. var deg = newValue * 360
  116. var offset = deg - odeg
  117. var ds = instance.getDataset()
  118. if (!ds.animate) {
  119. var path = getPath(deg)
  120. instance.setStyle({
  121. 'clip-path': path,
  122. })
  123. return
  124. }
  125. Animation({
  126. instance: ownerInstance,
  127. timing: 'easeInOut',
  128. duration: 300,
  129. onProcess: function onProcess(process) {
  130. var pdeg = odeg + process * offset
  131. var path = getPath(pdeg)
  132. var com = ownerInstance.selectComponent('.circle');
  133. com.setStyle({
  134. 'clip-path': path,
  135. })
  136. },
  137. onAnimationFinish: function onAnimationFinish() {}
  138. });
  139. }
  140. module.exports = {
  141. pro: setDeg,
  142. }
  143. </wxs>
  144. <view class="circle-progress-bar data-v-2577f78a" style="{{'width:' + j + ';' + ('height:' + k)}}"><view class="circle data-v-2577f78a" change:prop="{{animateModule.pro}}" prop="{{a}}" data-animate="{{b}}" style="{{'transform:' + c + ';' + ('border:' + d)}}"></view><view wx:if="{{e}}" class="bg data-v-2577f78a" style="{{'background:' + f}}"></view><view wx:if="{{g}}" class="border-back data-v-2577f78a" style="{{'border:' + h}}"></view><view class="center data-v-2577f78a"><text class="data-v-2577f78a">{{i}}</text></view></view>