温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何利用canvas绘制幸运大转盘功能

发布时间:2021-05-19 11:49:37 来源:亿速云 阅读:281 作者:小新 栏目:web开发

这篇文章给大家分享的是有关微信小程序如何利用canvas绘制幸运大转盘功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

代码如下

Page({
/**
 * 页面的初始数据
*/
data: {
awardsConfig: {},
restaraunts: [], //大转盘奖品信息
},
/**
 * 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var self = this;
wx.getSystemInfo({
//获取系统信息成功,将系统窗口的宽高赋给页面的宽高
success: function (res) {
self.width = res.windowWidth
self.height = res.windowHeight
}
})
self.data.awardsConfig = {
chance: true,
awards: self.data.restaraunts//存放奖项信息
}
self.tab();
})
},
tab: function() {
var self = this;
// 绘制转盘
var awardsConfig = self.data.awardsConfig.awards,
len = awardsConfig.length,
rotateDeg = 360 / len / 2 + 90,
html = [],
turnNum = 1 / len // 文字旋转 turn 值
self.setData({
btnDisabled: self.data.awardsConfig.chance ? '' : 'disabled'
})
var ctx = wx.createContext();
for (var i = 0; i < len; i++) {
var w = self.width; //页面宽
var r = w * 0.38; //圆半径 0.35w
// 保存当前状态
ctx.save();
// 开始一条新路径
ctx.beginPath();
// 位移到圆心,下面需要围绕圆心旋转
// ctx.translate(150, 150);
ctx.translate(w / 2 - 14, w / 2 - 18);
// 从(0, 0)坐标开始定义一条新的子路径
ctx.moveTo(0, 0);
// 旋转弧度,需将角度转换为弧度,使用 degrees * Math.PI/180 公式进行计算。
ctx.rotate((360 / len * i - rotateDeg) * Math.PI / 180);
// 绘制圆弧
ctx.arc(0, 0, r, 0, 2 * Math.PI / len, false);
// 颜色间隔
if (i % 2 == 0) {
ctx.setFillStyle('#ffffff');
} else {
ctx.setFillStyle('#ffeab0');
}
// 填充扇形
ctx.fill();
// 绘制边框
ctx.setLineWidth(0.5);
ctx.setStrokeStyle('#e4370e');
ctx.stroke();
// 恢复前一个状态
ctx.restore();
// 奖项列表
html.push({
turn: (i + 1) * turnNum + 'turn',
award: awardsConfig[i]
});
}
self.setData({
awardsList: html
});
wx.drawCanvas({
canvasId: 'canvas',
actions: ctx.getActions()
})
},
inner: function(e) {
const self = this;
if (self.data.awardsConfig.chance) {
self.data.awardsConfig.chance = false;//转动时禁止再次触发点击事件
var json = res.data;//后端自动分配奖项,并传给前端奖项信息
var item = parseInt(json.grade); //获取从1到奖品数量之间的随机数
self.getLottery(item + 1, self.data.restaraunts[item]); //奖项位置 (+1 是为了转动的时候计算角度),对应奖项
}
},
getLottery: function(item, txt) {
var self = this
var awardsConfig = self.data.awardsConfig.awards,
len = awardsConfig.length;
var awardIndex = item;
// 获取奖品配置
var awardsConfig = self.data.awardsConfig
if (awardIndex < 2) awardsConfig.chance = false
// 初始化 rotate
var animationInit = wx.createAnimation({
duration: 1
})
this.animationInit = animationInit;
animationInit.rotate(0).step()
this.setData({
animationData: animationInit.export(),
btnDisabled: 'disabled'
})
// 旋转抽奖 执行动画效果
setTimeout(function () {
var animationRun = wx.createAnimation({
duration: 4000,
timingFunction: 'ease'
})
self.animationRun = animationRun
animationRun.rotate(0 - (360 * 8 - awardIndex * (360 / len))).step()
self.setData({
animationData: animationRun.export()
})
}, 100)
// 记录奖品
var winAwards = wx.getStorageSync('winAwards') || {
data: []
}
var textInfo = txt === "谢谢参与" ? txt : txt + '1个';
winAwards.data.push(textInfo)
wx.setStorageSync('winAwards', winAwards)
var jh = parseInt(self.data.jh) - 1;
// 中奖提示
setTimeout(function () {
if (txt === "谢谢参与") {
wx.showModal({
title: '很遗憾',
content: '祝您好运',
showCancel: false
})
} else {
wx.showModal({
title: '恭喜',
content: '获得' + txt,
showCancel: false
})
}
self.data.awardsConfig.chance = true;
if (awardsConfig.chance) {
self.setData({
btnDisabled: ''
})
}
}, 4100);
},
function(err) {
console.log(err)
console.log("err")
//error
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
// this.drawTurntable(this, new Date());
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})

感谢各位的阅读!关于“微信小程序如何利用canvas绘制幸运大转盘功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI