{"id":13102,"date":"2024-02-22T17:55:56","date_gmt":"2024-02-22T09:55:56","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=13102"},"modified":"2025-07-04T09:15:24","modified_gmt":"2025-07-04T01:15:24","slug":"%e6%b4%be%e5%b0%8d%e9%81%8a%e6%88%b2%e5%b9%b3%e5%8f%b0-%e4%bd%bf%e7%94%a8-winwheel-js-%e8%a3%bd%e9%80%a0%e8%bd%89%e7%9b%a4","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=13102","title":{"rendered":"\u6d3e\u5c0d\u904a\u6232\u5e73\u53f0 - \u4f7f\u7528 Winwheel.js \u88fd\u9020\u8f49\u76e4"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/33850201\/how-to-draw-a-wheel-of-fortune\" target=\"_blank\" rel=\"noopener\">how to draw a wheel of fortune?<\/a><\/li>\n<li><a href=\"https:\/\/dougtesting.net\/\" target=\"_blank\" rel=\"noopener\">Create HTML5 Canvas Spinning Prize Wheels with the Winwheel.js JavaScript Library.<\/a><\/li>\n<li><a href=\"http:\/\/dougtesting.net\/winwheel\/refs\/class_winwheel\">dougTesting.net : Winwheel.js class reference: Winwheel<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u5be6\u4f5c<\/h2>\n<p>\u6839\u64da <a href=\"http:\/\/dougtesting.net\/winwheel\/docs\" target=\"_blank\" rel=\"noopener\">Tutorials &amp; Docs<\/a>\u00a0\u4e00\u6b65\u4e00\u6b65\u9032\u884c\u5373\u53ef\uff0c\u6587\u4ef6\u53ea\u6709\u5c11\u8a31\u932f\u8aa4\uff0c\u642d\u914d\u7db2\u9801\u539f\u59cb\u78bc\u5373\u53ef\u7406\u89e3<\/p>\n<p>\u5982\u679c\u5716\u6c92\u6709\u51fa\u73fe\uff0c\u8acb\u5c07\u7db2\u5740\u7684 https \u6539\u6210 http \u5373\u53ef<\/p>\n<p>--<\/p>\n<h2>\u6210\u679c<\/h2>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-13107\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/Winwheel.png\" alt=\"\" width=\"307\" height=\"322\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/Winwheel.png 307w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/Winwheel-286x300.png 286w\" sizes=\"(max-width: 307px) 100vw, 307px\" \/><\/p>\n<p>\u7a0b\u5f0f\u78bc<\/p>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"zh-TW\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;Winwheel&lt;\/title&gt;\r\n\r\n    &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.7.0.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- bootstrap --&gt;\r\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/fastbootstrap@2.2.0\/dist\/css\/fastbootstrap.min.css\" rel=\"stylesheet\"&gt;\r\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.2\/dist\/js\/bootstrap.bundle.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;script src='Winwheel.min.js'&gt;&lt;\/script&gt;\r\n    &lt;script src='TweenMax.min.js'&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;canvas id='canvas' width='300' height='320' onClick=\"startSpin();\"&gt;&lt;\/canvas&gt;\r\n\r\n&lt;script&gt;\r\n    let theWheel = new Winwheel({\r\n        numSegments     : 6,\r\n        textFontSize    : 32,\r\n        lineWidth       : 0,\r\n        innerRadius     : 20,\r\n        textOrientation : 'horizontal',\r\n        centerY         : 170,\r\n        rotationAngle   : -30,\r\n        segments :\r\n            [\r\n                {'fillStyle' : '#eae56f', 'text' : '1'},\r\n                {'fillStyle' : '#89f26e', 'text' : '2'},\r\n                {'fillStyle' : '#7de6ef', 'text' : '3'},\r\n                {'fillStyle' : '#adc8ff', 'text' : '4'},\r\n                {'fillStyle' : '#d7b5fe', 'text' : '5'},\r\n                {'fillStyle' : '#e7706f', 'text' : '6'},\r\n            ],\r\n        animation :\r\n            {\r\n                type             : 'spinToStop',\r\n                duration         : 0.5,\r\n                spins            : 2,\r\n                callbackFinished : 'alertPrize()',\r\n                callbackAfter    : 'drawTriangle()',\r\n            }\r\n    });\r\n\r\n    drawTriangle();\r\n\r\n    function startSpin(){\r\n        theWheel.rotationAngle = theWheel.rotationAngle % 360;\r\n        theWheel.startAnimation();\r\n    }\r\n\r\n    function alertPrize(){\r\n        let winningSegment = theWheel.getIndicatedSegment();\r\n        console.log(winningSegment.text);\r\n    }\r\n\r\n    function drawTriangle(){\r\n        c = theWheel.ctx;\r\n        c.save();\r\n        c.lineWidth = 2;\r\n        c.strokeStyle = 'black';\r\n        c.fillStyle = 'black';\r\n        c.beginPath();\r\n        c.moveTo(130, 10);\r\n        c.lineTo(170, 10);\r\n        c.lineTo(150, 42);\r\n        c.lineTo(130, 10);\r\n        c.stroke();\r\n        c.fill();\r\n        c.restore();\r\n    }\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>\u8f03\u5c0f\u7684\u4e09\u89d2\u5f62<\/strong><\/p>\n<p>\u4fee\u6539 drawTriangle() \u7684c \u8def\u5f91<\/p>\n<pre class=\"lang:js decode:true \">c.moveTo(140, 10);\r\nc.lineTo(160, 10);\r\nc.lineTo(150, 25);\r\nc.lineTo(140, 10);<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-13139\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/2024-03-12-09-11-14.png\" alt=\"\" width=\"315\" height=\"325\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/2024-03-12-09-11-14.png 315w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2024\/02\/2024-03-12-09-11-14-291x300.png 291w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/><\/p>\n<p>--<\/p>\n<h2>\u79fb\u9664\u9078\u4e2d<\/h2>\n<p>\u7a0b\u5f0f\u78bc<\/p>\n<pre class=\"lang:js decode:true\">let segmentNumber = theWheel.getIndicatedSegmentNumber();\r\ntheWheel.deleteSegment(segmentNumber);\r\ntheWheel.draw();<\/pre>\n<ul>\n<li>.getIndicatedSegmentNumber() \u53d6\u5f97\u4f4d\u7f6e<\/li>\n<li>.deleteSegment() \u522a\u9664<\/li>\n<li>.draw() \u91cd\u65b0\u7e6a\u88fd<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u5728 Modal \u986f\u793a<\/h2>\n<p>\u6307\u5b9a id \u4f7f\u7528 canvasId<\/p>\n<pre class=\"lang:js decode:true\">theWheel = new Winwheel({\r\n    canvasId: 'wheelCanvas',\r\n    outerRadius: 190,\r\n    innerRadius: 50,\r\n    textFontSize: 16,\r\n    numSegments: segments.length,\r\n    segments: segments,\r\n    animation: {\r\n        type: 'spinToStop',\r\n        duration: 5,\r\n        spins: 8,\r\n        callbackFinished: function (segment) {\r\n            console.log('\ud83c\udf89 \u606d\u559c\u7372\u5f97: ' + segment.text);\r\n            $('#spinButton').prop('disabled', false);\r\n        }\r\n    }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13102\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,969&nbsp;total views, &nbsp;4&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 how to ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13102\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,969&nbsp;total views, &nbsp;4&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13102"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13102"}],"version-history":[{"count":14,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13102\/revisions"}],"predecessor-version":[{"id":13137,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13102\/revisions\/13137"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}