我正在使用 html5 开发基于赌场的游戏。动画工作正常,但这不是很平滑,即一旦车轮停止旋转,我移动球作为最终重新定位以平滑过渡,但这并不符合预期。完整代码在这里
BallReposition 函数 - 在我的轮子停止运动后运行,以对球进行最终重新定位,从而为动画提供一些真实感。
function ballReposition(){
curX = findNearestOnCircle(curX);
if(curX > deadXRight){
sign = "-";
}else if(curX < deadXLeft){
sign = "+";
}
if(sign == "+"){
curX = parseInt(curX) + ballRepositionIncVal;
curY = Math.floor(Math.abs(getYOnCircle(curX, 130, 1)) + 0.5);
}else{
curX = parseInt(curX) - ballRepositionIncVal;
curY = Math.floor(Math.abs(getYOnCircle(curX, 130, 0)) + 0.5);
}
var xy = normalizeXY(curX, curY);
curX = parseInt(xy.split("-")[0]);
curY = parseInt(xy.split("-")[1]);
surface = document.getElementById("myCanvas");
var surfaceContext = surface.getContext("2d");
//removing older ball image.
surfaceContext.save();
// Translate to the center point of our image
surfaceContext.translate(happy.width * 0.5, happy.height * 0.5);
// Perform the rotation
surfaceContext.rotate(DegToRad(angle));
// Translate back to the top left of our image
surfaceContext.translate(-happy.width * 0.5, -happy.height * 0.5);
surface.getContext("2d").drawImage(happy, 0, 0);
surface.getContext("2d").drawImage(ball, curX, curY);
console.log(curX + curY);
surfaceContext.restore();
ballRepositionIncVal-=5;
if(ballRepositionIncVal <= 0){
clearInterval(myIntervalVar);
}
}
其他功能详情——
- drawCanvas - 加载图像,一旦加载图像,它将开始调用循环函数,该函数将旋转轮子并以逆时针方向移动球。
- normalizeXY - 用于将球放在一些离散的位置,即低于轮数的适当位置。
编辑 -在这里更新小提琴配置