旋轉(zhuǎn)完畢 图桑靠的位置胆剧, 根據(jù)后臺(tái)返回的數(shù)據(jù)字段 判斷停留位置
prizes:{
'TT_ONE_HUNDRED_PACKAGE': [(4 * Math.PI) / 3], //
'TT_FIFTY': [(10 * Math.PI) / 6, Math.PI], // 50
'TT_TWENTY_FIVE': [Math.PI / 3],
'TT_HALF_DISCOUNT': [(2 * Math.PI) / 3], //, 半價(jià)
'TT_FULL_DISCOUNT': [0],
},
//canvas渲染每一幀效果圖
onLoadWheel = (angle)=> {
let canvas = document.getElementById('wheel'); //id選擇 元素
let context = canvas.getContext('2d');
//先保存,以便后面重繪前回到現(xiàn)在的狀態(tài)
context.save();
let width = canvas.width;
let height = canvas.height;
let img = new Image();
img.src = WheelTable; //為旋轉(zhuǎn)的圖片
img.onload = function() {
context.translate(width * 0.5, height * 0.5);
// context.rotate((Math.PI / 18) * i);
context.rotate(angle);
context.drawImage(img, -width / 2, -height / 2, width, height);
//畫圖后需要回到之前save的狀態(tài)
context.restore();
};
}
//循環(huán)調(diào)用自身叹螟,渲染出動(dòng)畫效果
startDraw = (stopAngle, boool)=> {
let that = this;
let tempSpeed = (stopAngle - currentAngle) / 100;
currentAngle += tempSpeed;
setTimeout(function () {
if ((stopAngle - currentAngle) < 0.01) {
wheelStatus = false;
that.setState({
showModal:boool,
});
} else {
that.onLoadWheel(currentAngle);
that.startDraw(stopAngle, boool);
}
}, 10);
}
//點(diǎn)擊開始抽獎(jiǎng)活動(dòng),判斷是否應(yīng)該繼續(xù)抽獎(jiǎng)
clickStart = ()=> {
//判斷是否正在抽獎(jiǎng)
if (wheelStatus) {
return;
}
let that = this;
let url = `自己接口信息`;
reqm.requestGetNoAuth(url).then(res=>{
if (res.status === 200) {
currentAngle = 0; //每次旋轉(zhuǎn)初始化
that.props.changeLeftTime(res.data.lotteryNum); //后端返回的剩余抽獎(jiǎng)次數(shù)字段
that.props.getCoupon(res.data.coupons); //展示中獎(jiǎng)信息 (自己寫樣式及其組件)
let tempResult = res.data.lotteryType; //后端返回的中獎(jiǎng)信息字段
let prize = that.state.prizes[tempResult]; //根據(jù)后端返回的中獎(jiǎng)信息字段隨機(jī)給到(對(duì)應(yīng)該獎(jiǎng)勵(lì)的圓盤)一個(gè)旋轉(zhuǎn)位置
let resultCount = prize.length;
let finalResult = Math.floor(Math.random() * resultCount);
let plusAngle = prize[finalResult];
let stopAngle = 20 * Math.PI + plusAngle;
wheelStatus = true;
that.startDraw(stopAngle);
}
}).catch(err=>{
Alert.show({
content:err
});
});
}