HTML5 画布轮盘

1 html javascript canvas

我有这个http://jsfiddle.net/e7fwt4wb/!轮盘在 html5 画布中旋转 - 正常运行,当我调用方法 spin 时,轮盘旋转并停止在我的数字数组的随机数中!如何调用传递参数的函数以停在我的数字数组的位置?

<script type="text/javascript">
var colors = ["#336600", "#660000", "#000000", "#660000",
    "#000000", "#660000", "#000000", "#660000",
    "#000000", "#660000", "#000000", "#660000", "#000000", "#660000", "#000000"];
var numbers = ["0", "1", "8", "2",
    "9", "3", "10", "4",
    "11", "5", "12", "6", "13", "7", "14"];

var startAngle = 0;
var arc = Math.PI / 6;
var spinTimeout = null;

var spinArcStart = 10;
var spinTime = 0;
var spinTimeTotal = 0;

var ctx;

function drawRouletteWheel() {
    var canvas = document.getElementById("canvas");
    if (canvas.getContext) {
        var outsideRadius = 200;
        var textRadius = 160;
        var insideRadius = 125;

        ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, 500, 500);


        ctx.strokeStyle = "black";
        ctx.lineWidth = 2;
        ctx.font = 'bold 18px Helvetica, Arial';

        for (var i = 0; i < 12; i++) {
            var angle = startAngle + i * arc;
            ctx.fillStyle = colors[i];

            ctx.beginPath();
            ctx.arc(250, 250, outsideRadius, angle, angle + arc, false);
            ctx.arc(250, 250, insideRadius, angle + arc, angle, true);
            ctx.stroke();
            ctx.fill();

            ctx.save();
            ctx.shadowOffsetX = -1;
            ctx.shadowOffsetY = -1;
            ctx.shadowBlur = 0;
            ctx.shadowColor = "rgb(220,220,220)";
            ctx.fillStyle = "white";
            ctx.translate(250 + Math.cos(angle + arc / 2) * textRadius,
                    250 + Math.sin(angle + arc / 2) * textRadius);
            ctx.rotate(angle + arc / 2 + Math.PI / 2);
            var text = numbers[i];
            ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
            ctx.restore();
        }

        //Arrow
        ctx.fillStyle = "yellow";
        ctx.beginPath();
        ctx.moveTo(250 - 4, 250 - (outsideRadius + 5));
        ctx.lineTo(250 + 4, 250 - (outsideRadius + 5));
        ctx.lineTo(250 + 4, 250 - (outsideRadius - 5));
        ctx.lineTo(250 + 9, 250 - (outsideRadius - 5));
        ctx.lineTo(250 + 0, 250 - (outsideRadius - 13));
        ctx.lineTo(250 - 9, 250 - (outsideRadius - 5));
        ctx.lineTo(250 - 4, 250 - (outsideRadius - 5));
        ctx.lineTo(250 - 4, 250 - (outsideRadius + 5));
        ctx.fill();
    }
}

function spin() {
    spinAngleStart = Math.random() * 10 + 10;
    spinTime = 0;
    spinTimeTotal = Math.random() * 3 + 4 * 1500;
    rotateWheel();
}

function rotateWheel() {
    spinTime += 30;
    if (spinTime >= spinTimeTotal) {
        stopRotateWheel();
        return;
    }
    var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
    startAngle += (spinAngle * Math.PI / 180);
    drawRouletteWheel();
    spinTimeout = setTimeout('rotateWheel()', 30);
}

function stopRotateWheel() {
    clearTimeout(spinTimeout);
    var degrees = startAngle * 180 / Math.PI + 90;
    var arcd = arc * 180 / Math.PI;
    var index = Math.floor((360 - degrees % 360) / arcd);
    ctx.save();
    ctx.font = 'bold 30px Helvetica, Arial';
    ctx.textAlign = "center";
    var text = numbers[index]
    ctx.fillStyle = colors[index];
    ctx.fillText("Rolled: " + text, 250 - ctx.measureText(text).width / 2, 250 + 10);
    ctx.restore();
}

function easeOut(t, b, c, d) {
    var ts = (t /= d) * t;
    var tc = ts * t;
    return b + c * (tc + -3 * ts + 3 * t);
}

drawRouletteWheel();
</script>
Run Code Online (Sandbox Code Playgroud)

mar*_*rkE 5

以下是如何旋转您的轮子并在轮子上的所需数字处停止。

要使用 Penner 的缓动方程旋转您的轮子,您需要定义以下 4 个属性:

  • 当前动画经过的时间
  • 车轮起始角
  • 将车轮旋转到所需数量所需的总角度变化
  • 动画总时长

鉴于这 4 个属性,您可以在动画期间的任何时间应用缓动方程之一来计算车轮角度:

// t: current time inside duration, 
// b: beginning value,
// c: total change from beginning value,
// d: total duration
function easeOutQuart(t, b, c, d){
    // return the current eased value based on the current time
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
Run Code Online (Sandbox Code Playgroud)

例如,假设您想在 2 秒内旋转到 9 号口袋。然后将应用这些属性值:

  • 当前经过时间:(800ms例如),
  • 车轮起始角:0 radians,
  • 旋转到 9 号所需的总角度变化: 3.6652 radians
  • 总时长:2000ms,

您可以像这样计算 800 毫秒时车轮旋转的缓和角度:

easeOutQuart(800,0,3.6652,2000);
Run Code Online (Sandbox Code Playgroud)

三个必需的 Penner 属性是“给定的”,但旋转到数字 9 所需的总变化是这样计算的:

// "9" is element#4 in numbers[]
var indexOfNumber9 = 4;  

// calc what angle each number occupies in the circle
var angleSweepPerNumber = (Math.PI*2) / totalCountOfNumbersOnWheel;

// calc the change in angle required to rotate the wheel to number-9
var endingAngleAt9 = (Math.PI*2) - angleSweepPerNumber * (indexOfNumber9+1);

// the arrow is at the top of the wheel so rotate another -PI/2 (== -90 degrees)
endingAngleAt9 -= Math.PI/2;

// endingAngle is now at the leading edge of the wedge
// so rotate a bit further so the array is clearly inside wedge#9
endingAngleAt9 += Math.random()*angleSweepPerNumber;
Run Code Online (Sandbox Code Playgroud)

这是示例代码和演示:

// t: current time inside duration, 
// b: beginning value,
// c: total change from beginning value,
// d: total duration
function easeOutQuart(t, b, c, d){
    // return the current eased value based on the current time
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
Run Code Online (Sandbox Code Playgroud)
easeOutQuart(800,0,3.6652,2000);
Run Code Online (Sandbox Code Playgroud)
// "9" is element#4 in numbers[]
var indexOfNumber9 = 4;  

// calc what angle each number occupies in the circle
var angleSweepPerNumber = (Math.PI*2) / totalCountOfNumbersOnWheel;

// calc the change in angle required to rotate the wheel to number-9
var endingAngleAt9 = (Math.PI*2) - angleSweepPerNumber * (indexOfNumber9+1);

// the arrow is at the top of the wheel so rotate another -PI/2 (== -90 degrees)
endingAngleAt9 -= Math.PI/2;

// endingAngle is now at the leading edge of the wedge
// so rotate a bit further so the array is clearly inside wedge#9
endingAngleAt9 += Math.random()*angleSweepPerNumber;
Run Code Online (Sandbox Code Playgroud)