I am looking to create a wheel of fortune using JavaScript. My goal is to set both the output and starting position of the wheel. When the user spins the wheel at a minimum speed, it should rotate multiple times based on the speed before landing on the desired field.
Currently, I have only managed to implement the basic functionality. I am seeking assistance in completing the process as I am currently stuck.
How can I make the wheel rotate multiple times before stopping at the target angle?
let element = document.getElementById("wheel");
let region = new ZingTouch.Region(element);
let currentAngle = 0,
targetAngle = 0;
let minimumAngle = 30,
rotatedAngle = 0,
spinning = false
setInterval(() => {
rotatedAngle = 0;
}, 200)
region.bind(element, 'rotate', function(e) {
if (rotatedAngle > minimumAngle && rotatedAngle > 180) {
spinning = true;
document.getElementById("spinning").innerHTML = "spinning = true"
// How can I make the wheel rotate multiple times ending with the target angle?
} else if (!spinning) {
currentAngle += e.detail.distanceFromLast;
rotatedAngle += e.detail.distanceFromLast;
element.style.transform = 'rotate(' + currentAngle + 'deg)';
}
})
#wheel {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 20%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zingtouch/1.0.6/zingtouch.min.js"></script>
<img id="wheel" src="https://reel-mkt.com/templates/fortune_reel_lp/img/wheel.png">
<p id="spinning">spinning = false</p>