|
| 1 | +<!-- |
| 2 | + Winhweel.js basic code wheel example by Douglas McKechie @ www.dougtesting.net |
| 3 | + See website for tutorials and other documentation. |
| 4 | +
|
| 5 | + The MIT License (MIT) |
| 6 | +
|
| 7 | + Copyright (c) 2016 Douglas McKechie |
| 8 | +
|
| 9 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + of this software and associated documentation files (the "Software"), to deal |
| 11 | + in the Software without restriction, including without limitation the rights |
| 12 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + copies of the Software, and to permit persons to whom the Software is |
| 14 | + furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | + The above copyright notice and this permission notice shall be included in all |
| 17 | + copies or substantial portions of the Software. |
| 18 | +
|
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | + SOFTWARE. |
| 26 | +--> |
| 27 | +<html> |
| 28 | + <head> |
| 29 | + <title>HTML5 Canvas Winning Wheel</title> |
| 30 | + <link rel="stylesheet" href="main.css" type="text/css" /> |
| 31 | + <script type="text/javascript" src="../../Winwheel.js"></script> |
| 32 | + <script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <div align="center"> |
| 36 | + <h1>Winwheel.js example wheel - basic image wheel</h1> |
| 37 | + <p>Here is an example of a basic image drawn wheel, where the entire face of the wheel has been created out of a graphically rich image.</p> |
| 38 | + <br /> |
| 39 | + <p>Choose a power setting then press the Spin button. You will be alerted to the prize won when the spinning stops.</p> |
| 40 | + <br /> |
| 41 | + <table cellpadding="0" cellspacing="0" border="0"> |
| 42 | + <tr> |
| 43 | + <td> |
| 44 | + <div class="power_controls"> |
| 45 | + <br /> |
| 46 | + <br /> |
| 47 | + <table class="power" cellpadding="10" cellspacing="0"> |
| 48 | + <tr> |
| 49 | + <th align="center">Power</th> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td width="78" align="center" id="pw3" onClick="powerSelected(3);">High</td> |
| 53 | + </tr> |
| 54 | + <tr> |
| 55 | + <td align="center" id="pw2" onClick="powerSelected(2);">Med</td> |
| 56 | + </tr> |
| 57 | + <tr> |
| 58 | + <td align="center" id="pw1" onClick="powerSelected(1);">Low</td> |
| 59 | + </tr> |
| 60 | + </table> |
| 61 | + <br /> |
| 62 | + <img id="spin_button" src="spin_off.png" alt="Spin" onClick="startSpin();" /> |
| 63 | + <br /><br /> |
| 64 | + <a href="#" onClick="resetWheel(); return false;">Play Again</a><br /> (reset) |
| 65 | + </div> |
| 66 | + </td> |
| 67 | + <td width="318" height="418" class="the_wheel" align="center" valign="center"> |
| 68 | + <canvas id="canvas" width="315" height="418"> |
| 69 | + <p style="{color: white}" align="center">Sorry, your browser doesn't support canvas. Please try another.</p> |
| 70 | + </canvas> |
| 71 | + </td> |
| 72 | + </tr> |
| 73 | + </table> |
| 74 | + <script> |
| 75 | + // Create new wheel object specifying the parameters at creation time. |
| 76 | + var theWheel = new Winwheel({ |
| 77 | + 'numSegments' : 4, // Specify number of segments. |
| 78 | + 'outerRadius' : 150, // Set outer radius so wheel fits inside the background. |
| 79 | + 'drawMode' : 'image', // drawMode must be set to image. |
| 80 | + 'drawText' : true, // Need to set this true if want code-drawn text on image wheels. |
| 81 | + 'textFontSize' : 12, // Set text options as desired. |
| 82 | + 'textOrientation' : 'curved', |
| 83 | + 'textDirection' : 'reversed', |
| 84 | + 'textAlignment' : 'outer', |
| 85 | + 'textMargin' : 5, |
| 86 | + 'textFontFamily' : 'monospace', |
| 87 | + 'textStrokeStyle' : 'black', |
| 88 | + 'textLineWidth' : 2, |
| 89 | + 'textFillStyle' : 'white', |
| 90 | + 'segments' : // Define segments. |
| 91 | + [ |
| 92 | + {'text' : 'T-55 Vampire'}, |
| 93 | + {'text' : 'P-40 Kittyhawk'}, |
| 94 | + {'text' : 'North American Harvard'}, |
| 95 | + {'text' : 'L-39C Albatross'} |
| 96 | + ], |
| 97 | + 'animation' : // Specify the animation to use. |
| 98 | + { |
| 99 | + 'type' : 'spinToStop', |
| 100 | + 'duration' : 5, // Duration in seconds. |
| 101 | + 'spins' : 8, // Number of complete spins. |
| 102 | + 'callbackFinished' : 'alertPrize()' |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // Create new image object in memory. |
| 107 | + var loadedImg = new Image(); |
| 108 | + |
| 109 | + // Create callback to execute once the image has finished loading. |
| 110 | + loadedImg.onload = function() |
| 111 | + { |
| 112 | + theWheel.wheelImage = loadedImg; // Make wheelImage equal the loaded image object. |
| 113 | + theWheel.draw(); // Also call draw function to render the wheel. |
| 114 | + } |
| 115 | + |
| 116 | + // Set the image source, once complete this will trigger the onLoad callback (above). |
| 117 | + loadedImg.src = "planes.png"; |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + // Vars used by the code in this page to do power controls. |
| 122 | + var wheelPower = 0; |
| 123 | + var wheelSpinning = false; |
| 124 | + |
| 125 | + // ------------------------------------------------------- |
| 126 | + // Function to handle the onClick on the power buttons. |
| 127 | + // ------------------------------------------------------- |
| 128 | + function powerSelected(powerLevel) |
| 129 | + { |
| 130 | + // Ensure that power can't be changed while wheel is spinning. |
| 131 | + if (wheelSpinning == false) |
| 132 | + { |
| 133 | + // Reset all to grey incase this is not the first time the user has selected the power. |
| 134 | + document.getElementById('pw1').className = ""; |
| 135 | + document.getElementById('pw2').className = ""; |
| 136 | + document.getElementById('pw3').className = ""; |
| 137 | + |
| 138 | + // Now light up all cells below-and-including the one selected by changing the class. |
| 139 | + if (powerLevel >= 1) |
| 140 | + { |
| 141 | + document.getElementById('pw1').className = "pw1"; |
| 142 | + } |
| 143 | + |
| 144 | + if (powerLevel >= 2) |
| 145 | + { |
| 146 | + document.getElementById('pw2').className = "pw2"; |
| 147 | + } |
| 148 | + |
| 149 | + if (powerLevel >= 3) |
| 150 | + { |
| 151 | + document.getElementById('pw3').className = "pw3"; |
| 152 | + } |
| 153 | + |
| 154 | + // Set wheelPower var used when spin button is clicked. |
| 155 | + wheelPower = powerLevel; |
| 156 | + |
| 157 | + // Light up the spin button by changing it's source image and adding a clickable class to it. |
| 158 | + document.getElementById('spin_button').src = "spin_on.png"; |
| 159 | + document.getElementById('spin_button').className = "clickable"; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // ------------------------------------------------------- |
| 164 | + // Click handler for spin button. |
| 165 | + // ------------------------------------------------------- |
| 166 | + function startSpin() |
| 167 | + { |
| 168 | + // Ensure that spinning can't be clicked again while already running. |
| 169 | + if (wheelSpinning == false) |
| 170 | + { |
| 171 | + // Based on the power level selected adjust the number of spins for the wheel, the more times is has |
| 172 | + // to rotate with the duration of the animation the quicker the wheel spins. |
| 173 | + if (wheelPower == 1) |
| 174 | + { |
| 175 | + theWheel.animation.spins = 2; |
| 176 | + } |
| 177 | + else if (wheelPower == 2) |
| 178 | + { |
| 179 | + theWheel.animation.spins = 5; |
| 180 | + } |
| 181 | + else if (wheelPower == 3) |
| 182 | + { |
| 183 | + theWheel.animation.spins = 8; |
| 184 | + } |
| 185 | + |
| 186 | + // Disable the spin button so can't click again while wheel is spinning. |
| 187 | + document.getElementById('spin_button').src = "spin_off.png"; |
| 188 | + document.getElementById('spin_button').className = ""; |
| 189 | + |
| 190 | + // Begin the spin animation by calling startAnimation on the wheel object. |
| 191 | + theWheel.startAnimation(); |
| 192 | + |
| 193 | + // Set to true so that power can't be changed and spin button re-enabled during |
| 194 | + // the current animation. The user will have to reset before spinning again. |
| 195 | + wheelSpinning = true; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // ------------------------------------------------------- |
| 200 | + // Function for reset button. |
| 201 | + // ------------------------------------------------------- |
| 202 | + function resetWheel() |
| 203 | + { |
| 204 | + theWheel.stopAnimation(false); // Stop the animation, false as param so does not call callback function. |
| 205 | + theWheel.rotationAngle = 0; // Re-set the wheel angle to 0 degrees. |
| 206 | + theWheel.draw(); // Call draw to render changes to the wheel. |
| 207 | + |
| 208 | + document.getElementById('pw1').className = ""; // Remove all colours from the power level indicators. |
| 209 | + document.getElementById('pw2').className = ""; |
| 210 | + document.getElementById('pw3').className = ""; |
| 211 | + |
| 212 | + wheelSpinning = false; // Reset to false to power buttons and spin can be clicked again. |
| 213 | + } |
| 214 | + |
| 215 | + // ------------------------------------------------------- |
| 216 | + // Called when the spin animation has finished by the callback feature of the wheel because I specified callback in the parameters. |
| 217 | + // ------------------------------------------------------- |
| 218 | + function alertPrize() |
| 219 | + { |
| 220 | + // Get the segment indicated by the pointer on the wheel background which is at 0 degrees. |
| 221 | + var winningSegment = theWheel.getIndicatedSegment(); |
| 222 | + |
| 223 | + // Do basic alert of the segment text. You would probably want to do something more interesting with this information. |
| 224 | + alert("The wheel stopped on " + winningSegment.text); |
| 225 | + } |
| 226 | + </script> |
| 227 | + </body> |
| 228 | +</html> |
0 commit comments