微信小程序

您所在的位置:网站首页 抽奖小程序编程教程视频 微信小程序

微信小程序

2024-07-16 11:09:39| 来源: 网络整理| 查看: 265

本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章。

问题描述

在抽奖的应用或小程序中,大多会采用一种常见的大转盘抽奖方式,这种方式能直观展现出这个抽奖活动的形式和内容,且能直接吸引人参与。那么这个功能是如何实现的呢?

效果图:

解决方案

(1)首先要实现这个大转盘的样式设计,通过canvas画布和animation动画来实现。(这两个api的用法小编在前面的实战文章有过讲解,感兴趣可以去看一看。)

wxml代码示例:

代码语言:javascript复制                                                                                                                                                                                                                                                                                                                                                                {{item.award}}                                                                                                                                             抽奖                                        

(2)wxss中需要实现的则是几种不同颜色效果以及这个圆盘样式的效果。

这其中需要实现一个圆的效果,和圆上的平分六条分割线的样式。中间的抽奖样式,实际上是由下面一个圆上面一个三角形进行重合来展现的,当然,这里也可以直接换成一张类似抽奖样式的图片更方便。

部分代码示例:

代码语言:javascript复制//转盘 .canvas-container{ margin: 0 auto; position: relative; width: 300px; height: 300px; border-radius: 50%; } .canvas-content{ position: absolute; left: 0; top: 0; z-index: 1; display: block; width: 300px; height: 300px; border-radius: inherit; background-clip: padding-box; background-color: #ffcb3f; } .canvas-element{ position: relative; z-index: 1; width: inherit; height: inherit; border-radius: 50%; } .canvas-list{ position: absolute; left: 0; top: 0; width: inherit; height: inherit; z-index: 9999; } .canvas-item{ position: absolute; left: 0; top: 0; width: 100%; height: 100%; color: #e4370e; font-weight: bold; text-shadow: 0 1px 1px rgba(255,255,255,.6); }

(3)js中需要实现转盘转动的六个分区,需要用Math的相关属性,其用法类似于时钟(小编前面的《动态时钟》的文章中也有相关介绍,可以去了解一下);然后转盘旋转需要调用wx.createAnimation,设置旋转参数,包括转速和频率等;中奖提示调用wx.showModal,在里面设置中奖内容即可。

代码语言:javascript复制// 旋转抽奖 app.runDegs = app.runDegs || 0 console.log('deg', app.runDegs) app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (360 * runNum - awardIndex * (360 / 6)) console.log('deg', app.runDegs) var animationRun = wx.createAnimation({ duration: 4000, timingFunction: 'ease' }) that.animationRun = animationRun animationRun.rotate(app.runDegs).step() that.setData({ animationData: animationRun.export(), btnDisabled: 'disabled' }) // 中奖提示 setTimeout(function() { wx.showModal({ title: '恭喜', content: '获得' + (awardsConfig.awards[awardIndex].name), showCancel: false }) if (awardsConfig.chance) { that.setData({ btnDisabled: '' }) } }, 4000); }, onReady: function (e) { var that = this; // getAwardsConfig app.awardsConfig = { chance: true, awards:[ {'index': 0, 'name': '1元红包'}, {'index': 1, 'name': '5元话费'}, {'index': 2, 'name': '6元红包'}, {'index': 3, 'name': '8元红包'}, {'index': 4, 'name': '10元话费'}, {'index': 5, 'name': '10元红包'} ] } // 绘制转盘 var awardsConfig = app.awardsConfig.awards, len = awardsConfig.length, rotateDeg = 360 / len / 2 + 90, html = [], turnNum = 1 / len // 文字旋转 turn 值 that.setData({ btnDisabled: app.awardsConfig.chance ? '' : 'disabled' }) var ctx = wx.createContext() for (var i = 0; i < len; i++) { // 保存当前状态 ctx.save(); // 开始一条新路径 ctx.beginPath(); // 位移到圆心,下面需要围绕圆心旋转 ctx.translate(150, 150); // 从(0, 0)坐标开始定义一条新的子路径 ctx.moveTo(0, 0); // 旋转弧度,需将角度转换为弧度,使用 degrees * Math.PI/180 公式进行计算。 ctx.rotate((360 / len * i - rotateDeg) * Math.PI/180); // 绘制圆弧 ctx.arc(0, 0, 150, 0, 2 * Math.PI / len, false); // 颜色间隔 if (i % 2 == 0) { ctx.setFillStyle('rgba(255,184,32,.1)'); }else{ ctx.setFillStyle('rgba(255,203,63,.1)'); }


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭