首页 > 其他分享 >uniapp vue 实现大转盘

uniapp vue 实现大转盘

时间:2024-07-25 11:53:49浏览次数:9  
标签:uniapp vue text top fonts stop 大转盘 15% background

vue uniapp 小程序抽奖转盘老虎机和九宫格
1.npm 下载插件

# npm 安装
npm install @lucky-canvas/vue@latest

# 或者 yarn 安装
yarn add @lucky-canvas/vue@latest
2. 然后找到 main.js 引入插件并 use
// vue2完整加载
import VueLuckyCanvas from '@lucky-canvas/vue'
Vue.use(VueLuckyCanvas)

//vue3 完整加载
import VueLuckyCanvas from '@lucky-canvas/vue'
createApp(App).use(VueLuckyCanvas).mount('#app')
3.在页面中使用

<template>
	<view style="border: 1px solid blue;">
		<LuckyGrid
			ref="myLucky"
			width="600rpx" //九宫格宽度
			height="600rpx" //九宫格高度
			:blocks="blocks" //九宫格外圈样式背景
			:prizes="prizes" // 九宫格奖品列表
			:buttons="buttons"  //九宫格按钮
			@start="startCallBack" //开始抽奖事件
			@end="endCallBack" //结束抽奖事件
		/>
	</view>
</template>

<script>
import LuckyGrid from '@/components/@lucky-canvas/uni/lucky-grid'; // 引入九宫格插件
export default {
	components: { LuckyGrid },
	data() {
		return {
			blocks: [{ padding: '10px', background: '#869cfa' }, { padding: '10px', 
            background: '#e9e8fe' }],
			prizes: [
				{ x: 0, y: 0, range: 0, fonts: [{ text: '0', top: '35%' }], imgs: [{ src: 
                require('../../static/image/rose.png'), width: '50%', top: '22%' 
                }],background: '#E77697' },
				{ x: 1, y: 0, range: 0, fonts: [{ text: '现金200元', top: 
                '70%',fontColor: '#fff', fontSize: '24rpx' },], imgs: [{ src: 
                require('../../static/image/money.png'), width: '70%', top: '22%' 
                }],background: '#E77697' },
				{ x: 2, y: 0, range: 0, fonts: [{ text: '2', top: '35%' }],background: 
                '#E77697' },
				{ x: 2, y: 1, range: 0, fonts: [{ text: '3', top: '35%' }],background: 
                '#E77697' },
				{ x: 2, y: 2, range: 0, fonts: [{ text: '4', top: '35%' }],background: 
                '#E77697'  },
				{ x: 1, y: 2, range: 10, fonts: [{ text: '5', top: '35%' }],background: 
                '#E77697'  },
				{ x: 0, y: 2, range: 0, fonts: [{ text: '6', top: '35%' }],background: 
                '#E77697'  },
				{ x: 0, y: 1, range: 0, fonts: [{ text: '7', top: '35%' }] ,background: 
                '#E77697' }
			     ],
			   buttons: [
				{
					x: 1,
					y: 1,
					background: '#7f95d1',
					fonts: [{ text: '开始', top: '35%' }],
					imgs: [{ src: require('../../static/image/rose.png'), width: '50%', 
                   top: '22%' }]
				}
			],
			
		};
	},
	methods: {
		// 点击抽奖按钮触发回调
		startCallBack(e, button) {
			// 先开始旋转
			// 先开始旋转
			console.log('1-----------', this.$refs);
			this.$refs.myLucky.play();
			// 使用定时器来模拟请求接口
			setTimeout(() => {
				// 假设后端返回的中奖索引是0
				const index = 0;
				// 调用stop停止旋转并传递中奖索引
				// this.$refs.myLucky.stop(index)
				this.$refs.myLucky.stop();
			}, 3000);
		},
		// 抽奖结束触发回调
		endCallBack(prize) {
			// 奖品详情
			alert('恭喜中奖: ' + prize.fonts[0].text);
		}
	}
};
</script>
<style lang="less"></style>

4.老虎机demo

<template>
	<view>
		<view style="border: 1px solid blue;">
		  <SlotMachine  
			  ref="myLucky"
		    width="240px"
		    height="180px"
		    :blocks="blocks"
		    :slots="slots"
			  :prizes="prizes"
			  :defaultStyle="defaultStyle"
		    :defaultConfig="defaultConfig"
			  @end="endCallBack"
		  />
		</view>
		<view>
			  <button @click="startCallBack()">开始</button>
		</view>
		
		
		<view @click="endClick()" style="margin-top: 30px;">
			<button>结束</button>
		</view>
	</view>
</template>

<script>
  import SlotMachine from '@/components/@lucky-canvas/uni/slot-machine' // 老虎机
  export default {
    components: { SlotMachine },
    data () {
      return {
        blocks: [
              { padding: '10px', background: '#869cfa' },
              { padding: '10px', background: '#e9e8fe' },
            ],
		slots: [
		  { order: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], direction: 1 },
		  { order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], direction: -1 },
		  { order: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1], direction: 1 },
		],
		prizes: [
		  { fonts: [{ text: '0', top: '15%' }]},
			{ fonts: [{ text: '1', top: '15%' }] },
			{ fonts: [{ text: '2', top: '15%' }] },
			{ fonts: [{ text: '3', top: '15%' }] },
			{ fonts: [{ text: '4', top: '15%' }] },
			{ fonts: [{ text: '5', top: '15%' }] },
			{ fonts: [{ text: '6', top: '15%' }] },
			{ fonts: [{ text: '7', top: '15%' }] },
			{ fonts: [{ text: '8', top: '15%' }] },
			{ fonts: [{ text: '9', top: '15%' }] },
		],
		defaultStyle: {
		  borderRadius: Infinity,
		  background: '#bac5ee',
		  fontSize: '32px',
		  fontColor: '#333'
		},
		defaultConfig: {
		  rowSpacing: '25px',
		  colSpacing: '10px'
		}
      }
    },
	onLoad() {
		
	},
    methods: {
      // 点击抽奖按钮触发回调
      startCallBack () {
			// 先开始旋转
			this.$refs.myLucky.play()
			// setTimeout(() => {
			//   // 假设后端返回的中奖索引是0
			//   const index = [5,7,1]
			//   // 调用stop停止旋转并传递中奖索引
			//   this.$refs.myLucky.stop(index)
			// }, 3000)
      },
	  endClick(){
		  const index = [2,3,8]
		  // 调用stop停止旋转并传递中奖索引
		  this.$refs.myLucky.stop(index)
	  },
      // 抽奖结束触发回调
      endCallBack (prize) {
        // 奖品详情
		console.log('---------中奖的:',prize)
        // alert('恭喜中奖: '+ prize.fonts[0].text)
      }
    }
  }
</script>
5.大转盘demo

<template>
  <LuckyWheel
    ref="myLucky"
    width="300px"
    height="300px"
    :prizes="prizes"
    :blocks="blocks"
    :buttons="buttons"
    @start="startCallback"
    @end="endCallback"
  />
</template>

<script>
export default {
  data () {
    return {
      blocks: [{ padding: '13px', background: '#617df2' }],
      prizes: [
        { fonts: [{ text: '0', top: '10%' }], background: '#e9e8fe' },
        { fonts: [{ text: '1', top: '10%' }], background: '#b8c5f2' },
        { fonts: [{ text: '2', top: '10%' }], background: '#e9e8fe' },
        { fonts: [{ text: '3', top: '10%' }], background: '#b8c5f2' },
        { fonts: [{ text: '4', top: '10%' }], background: '#e9e8fe' },
        { fonts: [{ text: '5', top: '10%' }], background: '#b8c5f2' },
      ],
      buttons: [
        { radius: '50px', background: '#617df2' },
        { radius: '45px', background: '#afc8ff' },
        {
          radius: '40px', background: '#869cfa',
          pointer: true,
          fonts: [{ text: '开始\n抽奖', top: '-20px' }]
        },
      ],
    }
  },
  methods: {
    // 点击抽奖按钮会触发star回调
    startCallback () {
      // 调用抽奖组件的play方法开始游戏
      this.$refs.myLucky.play()
      // 模拟调用接口异步抽奖
      setTimeout(() => {
        // 假设后端返回的中奖索引是0
        const index = 0
        // 调用stop停止旋转并传递中奖索引
        this.$refs.myLucky.stop(index)
      }, 3000)
    },
    // 抽奖结束会触发end回调
    endCallback (prize) {
      console.log(prize)
    },
  }
}
</script>

标签:uniapp,vue,text,top,fonts,stop,大转盘,15%,background
From: https://blog.csdn.net/zhang804633234/article/details/140612387

相关文章

  • vue的组件注册/组件传递数据_props/组件传递多种数据类型/组件传递Props校验
    1.组件注册全局注册:注册完后所有vue都能使用局部注册:仅当前页面能够使用全局注册虽然方便但有以下几个问题一.在打包时会将它一同打包,有时候没有用到它也打包,无任何意义二.课维护性低,在大型项目中使项目的依赖关系不明确2.组件传递数据_props父级传递子级数据(注意传递数......
  • VUE 排查400 (Bad Request)和解决这个问题
    排查400(BadRequest)和解决这个问题,可以按照以下步骤进行:检查URL和端点:确保URLhttp://127.0.0.1:8008/basicApp/BS037HModel/是正确的,并且该端点在服务器上存在。检查请求参数:确认发送请求时的任何参数都是正确的,包括查询参数、请求体和头部信息。检查后端错误日志:查......
  • 免费分享一套微信小程序图书馆座位预约管理系统(SpringBoot后端+Vue管理端)【论文+源
    大家好,我是java1234_小锋老师,看到一个不错的微信小程序图书馆座位预约管理系统(SpringBoot后端+Vue管理端),分享下哈。项目介绍随着移动互联网技术的飞速发展和智能设备的普及,图书馆服务模式正在经历深刻的变革。本论文旨在探讨如何利用微信小程序这一便捷高效的平台,开发一款......
  • 条件组合组件--vue3版
    参考手把手教你写一个条件组合组件此随笔是借鉴以上写的vue版,记录一下组件前期准备1.vue3的全套工具2.element-plus3.lodash数据结构主要是嵌套结构关键点在RelationGroup组件内引用本身,注意key值,不能用i,不然删除操作,会从最后删起组件结构主要是这3个文件引用......
  • vue 2 实现自定义组件一到多个v-model双向数据绑定的方法(转)
    原文链接:https://blog.csdn.net/Dobility/article/details/110147985前言有时候我们需要对一个组件绑定自定义v-model,以更方便地实现双向数据,例如自定义表单输入控件。甚至有时候,我们想要实现绑定多个“v-model”,也就是多个“双向绑定”,例如带表单输入的模块框,想同时控制模态......
  • 涨见识了!脱离vue项目竟然也可以使用响应式API
    前言vue3的响应式API大家应该都特别熟悉,比如ref、watch、watchEffect等。平时大家都是在vue-cli或者vite创建的vue项目里面使用的这些响应式API,今天欧阳给大家带来一些不一样的。脱离vue项目,在node.js项目中使用vue的响应式API。关注公众号:【前端欧阳】,给自己一个进阶vue的机会......
  • vue大小写总结
    1.组件组件的定义有两种命名方式:PascalCase  和   kebab-casePascalCase 定义的组件的引用:PascalCase  和   kebab-case  均可//PascalCase定义方式Vue.component('MyComponentName',{/*...*/})//引用方式一<my-component-name/>//引用方......
  • Vue Router【实用教程】(2024最新版)vue3 路由管理
    VueRouter是Vue官方的客户端路由解决方案,在单页应用(SPA)中,用户在应用中浏览不同页面时,URL会随之更新,但页面不需要从服务器重新加载。核心思想:通过配置路由来告诉VueRouter为每个URL路径显示哪些组件。官网https://router.vuejs.org/zh/guide/安装通常......
  • 计算机专业论文 (SpringBoot/SpringCloud+Vue+MySql)
    (可辅导论文)需要源码dd毕业设计(论文)  论文题目:基于Vue和SpringCloud的旅游网站设计与实现 摘 要 本论文主要介绍了基于Vue和SpringCloud的旅游网站的设计与实现。如今,旅游业已成为社会发展中的重要组成部分,旅游平台聚集多种多样的旅行方案以及攻略,越来越多的人......
  • Vue全家桶 - pinia 的理解和学习1(Pinia 核心概念的 Store、State、Getter、Action)
    Pinia(Vue的专属状态管理库)Pinia和Vuex的区别设计理念和架构Vuex采用集中式架构,所有状态存储在一个全局状态树中,通过mutations和actions来修改和处理状态。Pinia采用去中心化的架构,每个模块有自己的状态,这使得Pinia在代码分割和模块化方面更加灵活。TypeSc......