lucky-wheel.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view v-if="isShow" class="lucky-box" :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }">
  3. <canvas
  4. type="2d"
  5. id="lucky-wheel"
  6. canvas-id="lucky-wheel"
  7. :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
  8. ></canvas>
  9. <image
  10. v-if="imgSrc"
  11. :src="imgSrc"
  12. @load="myLucky.clearCanvas()"
  13. :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
  14. ></image>
  15. <!-- #ifdef APP-PLUS -->
  16. <view class="lucky-wheel-btn" @click="toPlay" :style="{ width: btnWidth + 'px', height: btnHeight + 'px' }"></view>
  17. <!-- #endif -->
  18. <!-- #ifndef APP-PLUS -->
  19. <cover-view class="lucky-wheel-btn" @click="toPlay" :style="{ width: btnWidth + 'px', height: btnHeight + 'px' }"></cover-view>
  20. <!-- #endif -->
  21. <!-- #ifndef H5 -->
  22. <view v-if="myLucky">
  23. <div class="lucky-imgs">
  24. <div v-for="(block, index) in blocks" :key="index">
  25. <div v-if="block.imgs">
  26. <image v-for="(img, i) in block.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'blocks', index, i)"></image>
  27. </div>
  28. </div>
  29. </div>
  30. <div class="lucky-imgs">
  31. <div v-for="(prize, index) in prizes" :key="index">
  32. <div v-if="prize.imgs">
  33. <image v-for="(img, i) in prize.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'prizes', index, i)"></image>
  34. </div>
  35. </div>
  36. </div>
  37. <div class="lucky-imgs">
  38. <div v-for="(btn, index) in buttons" :key="index">
  39. <div v-if="btn.imgs">
  40. <image v-for="(img, i) in btn.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'buttons', index, i)"></image>
  41. </div>
  42. </div>
  43. </div>
  44. </view>
  45. <!-- #endif -->
  46. </view>
  47. </template>
  48. <script>
  49. import { changeUnits, resolveImage, getImage } from './utils.js'
  50. import { LuckyWheel } from '../../lucky-canvas'
  51. export default {
  52. name: 'lucky-wheel',
  53. data () {
  54. return {
  55. imgSrc: '',
  56. myLucky: null,
  57. canvas: null,
  58. isShow: false,
  59. boxWidth: 100,
  60. boxHeight: 100,
  61. btnWidth: 0,
  62. btnHeight: 0,
  63. dpr: 1,
  64. }
  65. },
  66. props: {
  67. width: {
  68. type: String,
  69. default: '600rpx'
  70. },
  71. height: {
  72. type: String,
  73. default: '600rpx'
  74. },
  75. blocks: {
  76. type: Array,
  77. default: () => []
  78. },
  79. prizes: {
  80. type: Array,
  81. default: () => []
  82. },
  83. buttons: {
  84. type: Array,
  85. default: () => []
  86. },
  87. defaultConfig: {
  88. type: Object,
  89. default: () => ({})
  90. },
  91. defaultStyle: {
  92. type: Object,
  93. default: () => ({})
  94. },
  95. },
  96. mounted () {
  97. // #ifdef APP-PLUS
  98. console.error('该抽奖插件的最新版暂不支持app端, 请通过npm安装旧版本【npm i uni-luck-draw@1.3.9】')
  99. // #endif
  100. // #ifndef APP-PLUS
  101. this.initLucky()
  102. // #endif
  103. },
  104. watch: {
  105. blocks (newData) {
  106. this.myLucky && (this.myLucky.blocks = newData)
  107. },
  108. prizes (newData) {
  109. this.myLucky && (this.myLucky.prizes = newData)
  110. },
  111. buttons (newData) {
  112. this.myLucky && (this.myLucky.buttons = newData)
  113. },
  114. defaultStyle (newData) {
  115. this.myLucky && (this.myLucky.defaultStyle = newData)
  116. },
  117. defaultConfig (newData) {
  118. this.myLucky && (this.myLucky.defaultConfig = newData)
  119. },
  120. },
  121. methods: {
  122. async imgBindload (res, name, index, i) {
  123. const img = this[name][index].imgs[i]
  124. resolveImage(img, this.canvas)
  125. },
  126. getImage () {
  127. return getImage.call(this, 'lucky-wheel', this.canvas)
  128. },
  129. hideCanvas () {
  130. // #ifdef MP
  131. this.getImage().then(res => {
  132. this.imgSrc = res.tempFilePath
  133. })
  134. // #endif
  135. },
  136. initLucky () {
  137. this.boxWidth = changeUnits(this.width)
  138. this.boxHeight = changeUnits(this.height)
  139. this.isShow = true
  140. // 某些情况下获取不到 canvas
  141. this.$nextTick(() => {
  142. setTimeout(() => {
  143. this.draw()
  144. })
  145. })
  146. },
  147. draw () {
  148. const _this = this
  149. uni.createSelectorQuery().in(this).select('#lucky-wheel').fields({
  150. node: true, size: true
  151. }).exec((res) => {
  152. // #ifdef H5
  153. res[0].node = document.querySelector('#lucky-wheel canvas')
  154. // #endif
  155. if (!res[0] || !res[0].node) return console.error('lucky-canvas 获取不到 canvas 标签')
  156. const { node, width, height } = res[0]
  157. const canvas = this.canvas = node
  158. const ctx = this.ctx = canvas.getContext('2d')
  159. const dpr = this.dpr = uni.getSystemInfoSync().pixelRatio
  160. // #ifndef H5
  161. canvas.width = width * dpr
  162. canvas.height = height * dpr
  163. ctx.scale(dpr, dpr)
  164. // #endif
  165. const Radius = Math.min(width, height) / 2
  166. const myLucky = this.myLucky = new LuckyWheel({
  167. // #ifdef H5
  168. flag: 'WEB',
  169. // #endif
  170. // #ifdef MP
  171. flag: 'MP-WX',
  172. // #endif
  173. ctx,
  174. dpr,
  175. setTimeout,
  176. clearTimeout,
  177. setInterval,
  178. clearInterval,
  179. // #ifdef H5
  180. rAF: requestAnimationFrame,
  181. // #endif
  182. unitFunc: (num, unit) => changeUnits(num + unit),
  183. beforeCreate: function () {
  184. ctx.translate(Radius, Radius)
  185. },
  186. beforeResize: function () {
  187. ctx.translate(-Radius, -Radius)
  188. },
  189. afterInit: function () {
  190. // 动态设置按钮
  191. _this.btnWidth = this.maxBtnRadius * 2
  192. _this.btnHeight = this.maxBtnRadius * 2
  193. _this.$forceUpdate()
  194. },
  195. afterStart: () => {
  196. this.imgSrc = ''
  197. },
  198. }, {
  199. ...this.$props,
  200. width,
  201. height,
  202. start: (...rest) => {
  203. this.$emit('start', ...rest)
  204. },
  205. end: (...rest) => {
  206. this.$emit('end', ...rest)
  207. this.hideCanvas()
  208. },
  209. })
  210. })
  211. },
  212. toPlay (e) {
  213. this.myLucky.startCallback()
  214. },
  215. init () {
  216. this.myLucky.init()
  217. },
  218. play (...rest) {
  219. this.myLucky.play(...rest)
  220. },
  221. stop (...rest) {
  222. this.myLucky.stop(...rest)
  223. },
  224. },
  225. }
  226. </script>
  227. <style scoped>
  228. .lucky-box {
  229. position: relative;
  230. overflow: hidden;
  231. margin: 0 auto;
  232. }
  233. .lucky-box canvas {
  234. position: absolute;
  235. pointer-events: none;
  236. left: 0;
  237. top: 0;
  238. }
  239. .lucky-wheel-btn {
  240. position: absolute;
  241. left: 50%;
  242. top: 50%;
  243. transform: translate(-50%, -50%);
  244. background: rgba(0, 0, 0, 0);
  245. border-radius: 50%;
  246. cursor: pointer;
  247. }
  248. .lucky-imgs {
  249. width: 0;
  250. height: 0;
  251. visibility: hidden;
  252. }
  253. </style>