JS+Canvas繪制抽獎轉盤

本文實例為大傢分享瞭JS+Canvas繪制抽獎轉盤的具體代碼,供大傢參考,具體內容如下

給大傢分享一個Canvas繪制的轉盤抽獎,點擊開關開始轉動時,轉盤開始轉動,轉盤停止時指針指向的區域即為抽中的獎品,並顯示在轉盤中間,效果圖如下:

動畫實的代碼如下:

<!DOCTYPE html>
<html>
 
<head>
    <style>
        canvas {
            background: #eee;
        }
    </style>
    <title>Canvas繪制抽獎轉盤</title>
    <script>
        window.onload = function () {
            var canvas = document.getElementById("canvas");
            var cobj = canvas.getContext("2d");
            var btn = document.getElementById("btn");
            var num = Math.PI / 180;
            cobj.translate(250, 250);
            var colorArr = ["#24a274", "#2a70a6", "#6d56c3", "#b23880", "#7a9a36", "#b48548", "#397839", "#89489c"];
            var textArr = ["js", "html", "css", "php", "mysql", "node", "flutter", "java"];
            var angle = 0;
            btn.onclick = function () {
                location.reload();
            };
            var step = 10 + 10 * Math.random();
            var t = setInterval(function () {
                if (step <= 0.3) {
                    clearInterval(t);
                    var num1 = Math.ceil(angle / 45);
                    var con = textArr[textArr.length - num1];
                    cobj.font = "18px sans-serif";
                    cobj.textAlign = "center";
                    cobj.fillText(con, 0, 0);
                } else {
                    if (angle >= 360) {
                        angle = 0;
                    }
                    step *= 0.95;
                    angle += step;
                    cobj.clearRect(-200, -200, 500, 500);
                    cobj.beginPath();
                    cobj.lineWidth = 5;
                    cobj.moveTo(135, 0);
                    cobj.lineTo(150, 0);
                    cobj.stroke();
                    cobj.lineWidth = 2;
                    cobj.save();
                    cobj.rotate(angle * num);
 
                    for (var i = 1; i <= 8; i++) {
                        cobj.beginPath();
                        cobj.moveTo(0, 0);
                        cobj.fillStyle = colorArr[i - 1];
                        cobj.arc(0, 0, 130, (i - 1) * 45 * num, i * 45 * num);
                        cobj.closePath();
                        cobj.stroke();
                        cobj.fill();
                    }
 
 
                    cobj.beginPath();
                    cobj.fillStyle = "#fff";
                    cobj.arc(0, 0, 60, 0, 2 * Math.PI);
                    cobj.fill();
 
 
                    for (var i = 0; i < 8; i++) {
                        cobj.save();
                        cobj.beginPath();
                        cobj.rotate((i * 45 + 20) * num);
                        cobj.fillStyle = "#222";
                        cobj.font = "18px sans-serif";
                        cobj.fillText(textArr[i], 75, 0);
                        cobj.restore();
                    }
                    cobj.restore();
                }
            }, 60)
        }
    </script>
</head>
 
<body>
    <canvas id="canvas" width=500 height=500></canvas>
    <input type="button" value="開始" id="btn" />
</body>
 
</html>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: