Avatar of john a
john a
 asked on

Dragable hours and minutes clock using javascript

I  have made some changes to this example which is an analog clock   :  https://jsfiddle.net/sarmadm/0zkd68ux/

I want the clock hand ( hours and minutes ) to be draggable to set a certain time. I don't want the clock to run, .

Does anybody have an idea?
Web DevelopmentJavaScript

Avatar of undefined
Last Comment

8/22/2022 - Mon
Julian Hansen

Very interesting question - just don't have time to look at it now but would like to tackle it later. If you don't get any bites and you don't have a solution try again in a week or two.
john a

Thanks very much , I will try to do something and I will wait your solution

Julian Hansen

The general idea is to add an event listener on the canvas for a mousedown event.
In the listener check to see if the click is within the bounds of the hand you are targeting.
If it is set then set a flag
Then listen for mousemove events - in the handler if the flag is set then work out the angle from the centre of the clock to the current mouse point. Find the difference between this angle and the current angle of the hand. Redraw the clock with the hand in the new position and reapply to the canvas.

The maths for the above should be readily available on the net.

It is a nice question - will look at it next week once I have chased the wolves away ....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Greetings   john a, , , ,  I have some code below that makes a "Clock" display adjustable by clicks-touch, , I attempted some onmove event in the SVG image, but the SVG image is a VECTOR draw, and fills up the container, so getting the cursor position in x and y pixels was not working for me to use in SVG image. I did this code below with <div> display with some HTML CSS I did a clock before. BUT if you do a MOUSEDOWN event on the "hour" div then the cursor move off of the narrow div, I tried with the mousemove using the Clock div, but the trigonometry of pixel position in x and y of the <div> space to SIN COS and ARCTAN to degrees or rads to rotate the hour hand <div> was more than I could manage. SO I did an easier user time adjustment, by clicking on the NUMBER of the clock you need the hour-minute hand to go to. Although it is not a DRAG as you asked for, it may still allow the user to Change the Time on the Clock to what they need, as long as 5 minute intervals are acceptable.

<html lang="en"><head><title>Clock Click</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />


#title {
text-align: center;
padding: 12px;
background: #eda;

border: 4px solid #447;

#sw {
position: absolute;
top: -5px;
right: -4px;

#time {
position: absolute;
bottom: 12px;
right: -4px;

#hour {
position: absolute;
top: 143px;
left: 51px;
width: 188px;
height: 13px;
z-index: 2;

#hour span{
display: block;
float: right;
width: 94px;
height: 7px;
background-color: #fcc;
border: 3px solid #668;

#minute {
position: absolute;
top: 144px;
left: 20px;
width: 250px;
height: 9px;
transform: rotate(90deg);
z-index: 3;

#minute span{
display: block;
float: right;
width: 125px;
height: 5px;
background-color: #ccf;
border: 2px solid #668;

#hrShow, #mnShow {
position: absolute;
left: -5px;
padding: 3px;
border:2px solid #b00;
cursor: pointer;

#mnShow {
background-color: #ccf;
bottom: 1px;

#hrShow {
background-color: #fcc;
top: 1px;

function id2ob(elmt1){return document.getElementById(elmt1);}

function numClick() {
if(!doMn && !doHr) return;
var p = this.id.split("d");
p = parseInt(p[1]);// 30 degree, , 0 is ONE ,0 degree is num 3
var d = (p*30)-60;
hour1.style.transform = "rotate("+d+"deg)";
tm[0] = ""+(p+1);
id2ob("time").innerHTML = tm[0]+":"+tm[1];
doHr = 0;
minute1.style.transform = "rotate("+d+"deg)";
tm[1] = (1+p)*5;
if(tm[1]> 55)tm[1]="0";
tm[1] = (tm[1]<10) ? "0"+tm[1] : ""+tm[1];
id2ob("time").innerHTML = tm[0]+":"+ tm[1];
doMn = 0;
for (var i=0;i<hrDivs.length;++i){
sw.innerHTML = "";
</head><body><h3 id="title">Clock Click</h3>
<div id="clock">
<div id="sw"></div>
<div id="time">3:30</div>
<div id="hrShow">Hour</div>
<div id="mnShow">Minute</div>
<div id="hour"><span></span></div>
<div id="minute"><span></span></div>


var sw = id2ob("sw"), clock1 = id2ob("clock"), hour1 = id2ob("hour"), minute1 = id2ob("minute"), tm=["3","30"];

var hrDivs = [], doHr=0, doMn = 0;
for(var i=0;i<12;i++){// 12 width:290px;
  var sx = 138+Math.round(132*Math.cos(30*(i-2)*Math.PI/180));
  var sy = 135+Math.round(132*Math.sin(30*(i-2)*Math.PI/180));
  var nDiv = document.createElement("div"); 
  var tn = document.createTextNode(""+(i+1));
  nDiv.id = "hrd"+i;
  nDiv.style.position = "absolute";
  nDiv.style.left = sx+"px";
  nDiv.style.top = sy+"px";
  nDiv.style.fontSize = "20px";
  nDiv.style.borderRadius = "6px";
  nDiv.style.fontFamily = "serif";
  nDiv.style.padding = "0 2px";
  nDiv.onclick = numClick;
  hrDivs[i] = nDiv;

id2ob("hrShow").onclick = function() {
doHr = 1;
sw.innerHTML = "Touch<br />Number";
for (var i=0;i<hrDivs.length;++i){

id2ob("mnShow").onclick = function() {
doMn = 1;
sw.innerHTML = "Touch<br />Number";
for (var i=0;i<hrDivs.length;++i){

Open in new window

See what you think?
Robert Schutt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

@Robert Schutt , Good work, , works in my firefox desktop.
How did you find addy for the the SVG API for the shown in the -

but the Browser compatibility for SVGMatrix shows mostly question marks except for IE, that's extremely weird!

By the by, the minute hand in firefox does not do a hand Point centered rotation, you think a change in the -
   movingElem.setAttribute('transform', 'rotate(' + deg + ' 50 50)');
could correct that?
Maybe it's due to the different widths of hands?
Robert Schutt

Thanks Slick, I work in Chrome and it works fine, at least for the svg/rect elements. That code was used in some examples I found and I worked from there, MDN is the first place I look for javascript help.

The transform/rotate setting was part of the original fiddle posted in the question (function rot). There does seem to be some inconsistencies with the svg properties that seem better when looking at the actual original code mentioned in the fiddle.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
john a

Thanks very much Dear Robert Schutt and Slick812  for your help ,  it works perfectly
john a

Can I add the the clock numbers ( 1 to 12 ) as drag and drop elements above the clock ?
Robert Schutt

1. That sounds like a new question.
2a. Who do you ask this? That sounds more related to Slick's solution than mine.
2b. Also, you may have to be a bit more specific, what/how do you want to drag the number to/from?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
john a

Hi Robert

For example displaying hours as circles with numbers (1 to 12 ) above the clock , and theses circles

can be dragged and dropped to hours location inside the clock .  My purpose is to design a simple task

by asking the user to drag and drop hour numbers and set a certain time for example (10:10 )

Robert Schutt

It sounds like a project, but that can't be the intention here. I have made an updated fiddle to give you an idea how to proceed: https://jsfiddle.net/0zkd68ux/5/

@ john a , , You have now seems to have changed, and asked about -
   "displaying hours as circles with numbers (1 to 12 ) above the clock than can be dragged and dropped to hours location inside the clock"

But as far as being EASY for the user to adjust the time, it may be the least trouble for the user if you just have two input SELECT controls, one for hours and one for minutes, this is easy to use and easy to understand how to operate the changes, AND easy to do the code for. I do not see that having numbers to drag into a clock display will be easier to use, and It does not seem like it would impress anyone with the "Graphical Effect" ? ? Also if you are considering a usable web site page, it must be mobil and use the TOUCH javascript events, the touchbegin and touchmove javascript events are somewhat difficult to stuff into a MOUSE code like we are doing here.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
john a

@ Slick812 , the idea is to display a clock to the user and ask to setup a certain time  and put the

numbers in the correct positions. I'm doing web based clock drawing test which is normally done using

paper and pencil and have the person draw the face of a clock and put the numbers in the correct

positions. then have them draw the hands to indicate the time like 3:40 -  ( one hand of the

clock on 3 and the other on the 40 )

I did not really understand the concept you just explained in your comment, , I did see meaning in this -
    "display a clock to the user and ask to setup a certain time"

but the part about - "drawing test" - on a web page in not something I understand, sorry. But tou have here two "Display" of a clock face, one with an SVG image, , and one with a <div> HTML display. There are some "Drag and Drop" javascript librarys out there, , even some built from the JQUERY, so  you may can use a javascript dragger with one of the clock displays you have here?