Spin the Roulette Wheel using your mouse

I came across this interesting example (jsfiddle) that is almost perfect for my needs. However, I require the user to be able to interact with the roulette by "grabbing" it with the mouse and spinning it just like you would in real life with your hand.

Essentially, the idea is to click and hold on the wheel, have it "stick" to the mouse cursor, then move the mouse left or right before releasing the button, causing the wheel to spin until it comes to a stop.

Additionally, I'm curious if there's a way to predetermine the order in which the wheel stops even when the user is interacting with it in this manner.

Here is the jsFiddle code snippet for reference:

$(function(){

            var overWheel = false;
            var mouseDown = false;
            var lastMousePos = 0;

            $('.wheel').on('mouseover', function(){
                overWheel = true;
            }).on('mouseout', function(){
                overWheel = false;
            });


            $(document).on('mousedown', function(e){
                if(overWheel){
                    lastMousePos = e.offsetY;
                    mouseDown = true;
                }
            }).on('mouseup', function(){
                mouseDown = false;
            });


            $(document).on('mousemove', function(e){
                if(overWheel && mouseDown){
                    handleWheel(e);
                }
            });


            function handleWheel(e) {

                var yPos = e.offsetY;
                var direction = 0;

                var deg = getRotationDegrees($('.wheel'));


                if(yPos < lastMousePos){ // mouse is going up, move against the clock
                    console.log(yPos);
                    direction = -2;

                } else { //mouse is going down, move with the clock

                    direction = 2;

                }

                $('.wheel').css({'-webkit-transform': 'rotate(' + (deg + (direction)) + 'deg)'});
            }

            function getRotationDegrees(obj){
                var matrix = obj.css("-webkit-transform");
                if(matrix !== 'none') {
                    var values = matrix.split('(')[1].split(')')[0].split(',');
                    var a = values[0];
                    var b = values[1];
                    var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
                } else { var angle = 0; }
                return angle;
            }

        });​

Answer №1

I successfully completed the task using the amazing jQuery Rotate library.

Shoutout to the developer for creating this awesome tool!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Add a personalized class to a mat-tab within a mat-tab-group

I am looking to add unique styles to specific mat-tabs within a mat-tab-group based on a condition. The main mat-tab-group already has a default custom style that is functioning correctly. The styling is applied as follows: .mat-mdc-tab-group.round-tabs, ...

How to disable the click handler of a div using only classes

Although I'm not an expert in HTML, I am eager to create a basic bootstrap button in HTML that can be enabled and disabled. My question relates to the following scenario: <div class="button" onClick=doSomething >My Button</div ...

Connect to the Kendo dropdown list undefined

I am looking to automatically bind a model to a Kendo dropdown list. The model is retrieved from the server and can sometimes be undefined or a valid object. My problem arises when the value is undefined. In this case, Kendo selects the first item in the ...

Unable to Delete Record Using Jquery and Express in 'DELETE' Request

While working on my api using node/express and jquery, I encountered an issue where my DELETE requests are not functioning as expected. JQUERY: $('.formula-body').on('click', function (e) { if (e.target.className == 'fa-trash- ...

Enhancing functionality by updating a function to accept an object as input instead of individual key:value pairs in TypeScript

I'm currently tackling a challenge with a friend's icon library component, specifically with their set() function. The issue arises when I want to invoke two functions, namely setRandomColor() and setColor(), both intended to update two values w ...

The button colors in Material Ui do not update properly after switching themes

React Material UI is being utilized in this project. Although the theme is being overridden, the colors of buttons and inputs remain unchanged. Here is how the Material UI theme is created in theme.js // @flow import { createMuiTheme } from '@materi ...

Saving a promise object as a $scope variable in AngularJS: A quick guide

When working with my application, I use Constants.getContants as a promise to retrieve all the necessary constants. I want to store this information in a $scope variable so that it can be easily accessed throughout the controller or application. However, I ...

Access the data from a table row when a checkbox is selected

Edit: I want to rephrase my inquiry I am attempting to retrieve the text inside the anchor tags within a <td> when a checkbox is checked. The issue is that the anchor tags do not have an id or class assigned to them. Below is a simplified version ...

What are the steps to effectively utilize the static folder in Django for organizing CSS and JavaScript files?

I'm completely new to the Django framework. I managed to create a simple welcome page, but now I'm struggling to include a CSS file in my project. Whenever I try to apply the CSS file, I encounter an error saying "NetworkError: 404 NOT FOUND - / ...

The combination of NextJS and Firebase Auth using the GoogleAuthProvider is powerful

I am encountering challenges while trying to integrate firebase authentication with Google Provider in NextJS. I have set up the necessary environment variables and successfully established a connection with firebase. However, I keep running into an error ...

Parent function variable cannot be updated within a $.ajax call

I'm facing an issue with pushing a value from inside an ajax call to an array located outside of the call but still within the parent function. It appears that I am unable to access or update any variable from inside the ajax success statement. Any as ...

Searching through dynamic tables using jQuery quicksearch

After making an ajax call, I am in the process of creating a table with a unique ID. The data rows are being retrieved from the database and then used to create a dynamic table with that generated ID for display purposes. However, I also need to implemen ...

Hide the header content when the menu is opened

I am trying to create a function where clicking on a button will toggle the visibility of an element - hiding it when clicked once and showing it again when clicked again. I attempted to implement this using jQuery, but as I am new to Javascript, I can&apo ...

Warning: Promise rejection not handled - unable to save data to the database

When data is coming from the req, it is stored in the Comment collection but not in the Product collection, causing an error message to appear. app.post("/products/:id/comments", function(req, res) { //lookup product using ID Product.findById(req ...

Adjust size of container div once ajax call is complete

I am currently facing an issue with a webpage where I am using ajax to load a Twitter feed. Below is my HTML and jQuery setup: <body> <div class="document-wrapper"> <div class="document"> <div class="content"> ...

Attempting to incorporate a border underneath the H1 title labeled MY TODO LIST

<html lang="fr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" conte ...

Troubleshooting Problems with the Width of the Datepicker in Angular's

When using the Angular UI Bootstrap datepicker popup on a modal dialog, I've noticed that the width of the outside border does not always match up with the inside contents. Sometimes the outside box is larger than the inside contents, and other times ...

What is the best way to display data received from an AJAX request in a hidden div container?

I recently came across a module while working on a project. Page 1 Users need to use the search bar to navigate to Page 2. Page 2 On Page 2, all fetched results are displayed in divs with checkboxes associated with each result. https://i.sstatic.ne ...

The children routes in my Vue application are failing to render their content once resolved, instead opting to render the parent view

I've gone through the entire process again, cleaning up the code a bit (I'm still learning Vue so my code might be a bit messy) but I can't seem to get the component for the child path to render. It always defaults to the parent path. This ...

"Mastering the art of passing variables within the index parameter for JavaScript push method

I need help passing a variable inside index in JavaScript push while working with Angular. Here is my current code: angular.forEach(Val, function (Value,Key) { angular.forEach(Value, function (Value1,Key1) { saveDetailArr.push({ 'option_i ...