Vue simple implementation of turntable lottery

Vue simple implementation of turntable lottery

This article shares the specific code of Vue to simply implement the wheel lottery for your reference. The specific content is as follows

1.0 Thoughts

The wheel lottery is very common. I have never written about it before. Now I have time to write about it. The analysis is as follows:

1.1 Turntable rotation ----- can be solved by using transform's rotate
1.2 Rotation animation-----Transition is used to handle the transition
1.3 What are the target locations and winning tips? ------ Control the stop position by controlling the rotation angle, winning prompt, consider adding callback

1.1 Getting Started

From the above thinking, we know the steps to be implemented. First, let's take a picture.

This disc has 10 parts, each part is 360/10 = 36 degrees. Assuming that you want to stay at the second ----> 20 gold coins, clockwise (including the initial position and counted as 1), you need to rotate a total of (2 - 1)* 36 = 36. In this way, we can conclude that the angle required to rotate to the stop position = (target - 1)*36.

1.2 Winning callback

From the above steps, we know how to control to the target position, so the next step is event notification. At first, I thought of fixing the rotation time and then starting the timer. Obviously, this is not reliable. Is there any way to notify after the animation ends? transitionend, I found this event, which can monitor the end event of element animation, but it is not easy to handle with some compatibility

/**
Animation end event compatible**/
whichTransitionEvent(){
 let el = document.createElement('span'),
 transitions = {
 'transition':'transitionend',
 'OTransition':'oTransitionEnd',
 'MozTransition':'transitionend',
 'WebkitTransition':'webkitTransitionEnd'
 };
 for(let t in transitions){
 if( el.style[t] !== undefined ){
  return transitions[t];
 }
 }
 el = null;
}

2.0 Complete Example

Now that we have found a way to control the rotation position and event notification, let’s get started!

chestnut:

Complete code

<template>
 <div>
 <h3>Rotary Draw</h3>
 <div class="round_box" >
  <img class="img_rotate" ref="rotImg" src="../assets/zhuan.png" alt="">
  <div class="center">
  <div class="pointer" ></div>
  </div>
 </div>
 <button @click="toDraw" >Click to draw</button>
 </div>
</template>

<script>
export default {
 name:'rotaryDraw',
 data() {
 return {
  rotate: 0,
  resetRotate: 0,
  hitId: 1, // 1-10
  drawStatus: false
 }
 },
 async mounted() {
 await this.$nextTick();
 let evenTransition = this.whichTransitionEvent();
 let img = this.$refs.rotImg;
 let that = this;
 const hitAre = ['30M traffic package','20 gold coins','20M traffic package','10M traffic package','5 gold coins',
    'Thank you for participating', '10 gold coins', '50M traffic package', '2 gold coins', '100M traffic package'
   ];
 //Monitor animation end img.addEventListener(evenTransition,tranHand,false);

 function tranHand() {
  // Reset that.resetRotate = that.rotate > 360 ? that.rotate % 360 : 0;
  img.style.transition = "none 0s ease 0s";
  img.style.transform = `rotate(${that.resetRotate}deg)`; 
  alert(`Lottery result [ ${hitAre[that.hitId - 1]} ]`);
  that.drawStatus = false
 }
 },
 methods: {
 start() {
  this.$refs.rotImg.style.transition = "all 3s ease 0s";
  this.$refs.rotImg.style.transform = `rotate(${this.rotate}deg)`;
 },
 toDraw() {
  if(this.drawStatus){
  console.log('Lottery in progress');
  return
  }
  // Mark status this.drawStatus = true
  /**
  * There are 10 discs in total, each with 36 degrees, stop position (id) degree (id - 1) * 36 
  * Base 3 circles 360*4
  * this.rotate current angle* **/ 
  let reset = 360 * 4;
  let idx = this.getRandomInt(1,11);
  // Set hit this.hitId = idx;
  // Need to rotate more angles let addRotate = this.resetRotate > 0 ? 360 - this.resetRotate : 0;
  // Total angle let allRotate = this.rotate + (idx - 1) * 36 + reset + addRotate;
  // Angle limit this.rotate = this.setRotate(allRotate);

  this.start()
 },
 // The recursive calculation angle does not exceed 360*6
 setRotate(deg) {
  let rest = deg - 360;
  return rest > 360*6 ? this.setRotate(rest) : deg;
 },
 getRandomInt(min, max) {
  // Collect upwards min = Math.ceil(min);
  // Collect downward max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //Not including the maximum value, including the minimum value},
 // Animation compatible whichTransitionEvent(){
  let el = document.createElement('span'),
  transitions = {
  'transition':'transitionend',
  'OTransition':'oTransitionEnd',
  'MozTransition':'transitionend',
  'WebkitTransition':'webkitTransitionEnd'
  };
  for(let t in transitions){
  if( el.style[t] !== undefined ){
   return transitions[t];
  }
  }
  el = null;
 }
 }

}
</script>

<style lang="scss" >
.img_rotate{
 transform: rotate(0deg);
}
.round_box{
 width: 100%;
 max-width: 375px;
 position: relative;
 overflow: hidden;
 img{
 width: 100%;
 }
 .center{
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%,-50%);
 .pointer{
  width: 5px;
  height: 90px;
  background-color: #f40;
  position: absolute;
  top: -90px;
 }
 .pointer::before{
  content:'';
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid #f40;
  border-left: 15px solid transparent;
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
 }
 }
}

</style>

3.0 tips

In general, there are a few points to note

1. Lock before the animation starts

2. Notify after the animation ends and reset the status

/**
for example:
Base 3 circles reset 360*3
The second stop position is (2 - 1) * 36 = 36
Total angle 360*3 + 36
After the animation stops, it is impossible to keep increasing the angle because it will continue to rotate, so it is necessary to reset 360*3 + 36. In fact, you can consider rotating 36 degrees and then increase the required angle**/

3. Continue to rotate. Because our calculation is based on the initial value of 30M flow, so the rotation still needs to start from 30M. At this time, assuming that the current stop position is 300 degrees, that is to say, if you turn 60 degrees, you will return to the initial position. I also reset according to this idea.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue3 realizes lottery template setting
  • Vue implements the big turntable lottery function
  • Vue component to achieve digital scrolling lottery effect
  • Vue realizes the palace grid rotation lottery
  • VUE implements big turntable lottery
  • Vue component to realize mobile terminal nine-square grid turntable lottery
  • Summary and implementation ideas of Vue.js big turntable lottery
  • Jiugongge lottery function based on VUE
  • Vue implements the up and down scrolling animation example of mobile phone number lottery
  • Vue realizes nine-square lottery

<<:  How to handle forgotten passwords in Windows Server 2008 R2

>>:  Detailed installation and configuration of MySql on Mac

Recommend

Detailed explanation of CSS BEM writing standards

BEM is a component-based approach to web developm...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...

Detailed explanation of where the image pulled by docker is stored

20200804Addendum: The article may be incorrect. Y...

Analysis of MySQL Aborted connection warning log

Preface: Sometimes, the session connected to MySQ...

Nexus uses API to operate

Nexus provides RestApi, but some APIs still need ...

Vue echarts realizes dynamic display of bar chart

This article shares the specific code of vue echa...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

50 Beautiful FLASH Website Design Examples

Flash enabled designers and developers to deliver...

Problems with using wangeditor rich text editing in Vue

wangEditor is a web rich text editor developed ba...