Skip to content

Instantly share code, notes, and snippets.

@LeoOnTheEarth
Last active March 16, 2020 06:38
Show Gist options
  • Save LeoOnTheEarth/9c11d1e80b58118f2a1bf1a2946f1c0d to your computer and use it in GitHub Desktop.
Save LeoOnTheEarth/9c11d1e80b58118f2a1bf1a2946f1c0d to your computer and use it in GitHub Desktop.
Wheel Of Fortune!!
;$(function() {
/**
* @param {Element} wheel
*/
(function initWheelOfFortune() {
$('[data-toggle="wheel-of-fortune"]').each(function () {
var $wheel = $(this);
var angle = parseInt($wheel.data('current-angle')) || 0;
var stopAngle = angle;
var runDegree = 7;
var stopDegree = runDegree;
var finalStopDegree1 = 2;
var finalStopDegree2 = 1;
// Speed down for 1 degree / 1 second
var speedDownPeriod = 100;
// Includes: "stopped", "running", "speed-down", "stopping"
var state = 'stopped';
var speeDownWait = 2;
function speedDown()
{
if (stopDegree > finalStopDegree1) {
stopDegree -= 0.1;
setTimeout(function () {
speedDown();
}, speedDownPeriod);
} else {
if (speeDownWait-- <= 0) {
state = 'stopping';
} else {
stopDegree = stopDegree-- < finalStopDegree2 ? finalStopDegree2 : stopDegree;
setTimeout(function () {
speedDown();
}, speedDownPeriod * 5);
}
}
}
(function run() {
if (state !== 'stopped') {
if (state === 'running') {
angle += runDegree;
} else if (state === 'speed-down' || state === 'stopping') {
angle += stopDegree;
}
angle = angle > 360 ? angle - 360 : angle;
$wheel.css('transform', "rotate(" + angle.toString() + "deg)");
if (state === 'stopping' && Math.abs(stopAngle - angle) < 2) {
state = 'stopped';
$wheel.data('current-angle', angle);
}
}
requestAnimationFrame(run);
})();
$wheel.on('startWheelOfFortune', function(event) {
angle = parseInt($wheel.data('current-angle')) || 0;
state = 'running';
stopDegree = runDegree;
speeDownWait = 2;
});
$wheel.on('stopWheelOfFortune', function(event, finalStopAngle) {
speedDown();
state = 'speed-down';
stopAngle = parseInt(finalStopAngle / 2) * 2;
});
});
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment