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

CSS XTHML writing standards and common problems summary (page optimization)

Project Documentation Directory Div+CSS Naming Sta...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

How to install FastDFS in Docker

Pull the image docker pull season/fastdfs:1.2 Sta...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page Declarative navigat...

How to install and uninstall open-vswitch in Linux

1. Compile and install ovs from source code: Inst...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

How to open port 8080 on Alibaba Cloud ECS server

For security reasons, Alibaba Cloud Server ECS co...

Using react-virtualized to implement a long list of images with dynamic height

Table of contents Problems encountered during dev...

How to display JSON data in HTML

background: Sometimes we need to display json dat...

A record of a Linux server intrusion emergency response (summary)

Recently, we received a request for help from a c...

Vue.$set failure pitfall discovery and solution

I accidentally found that Vue.$set was invalid in...

Solution to the welcome to emergency mode message when booting CentOS7.4

Today I used a virtual machine to do an experimen...