Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <svg
  xmlns="http://www.w3.org/2000/svg"
  width="300" height="300"
  id="clock-svg"
  onmousemove="drag(evt)"
  onmouseup="stopDrag()"
>
  <rect width="300" height="300" fill="#eee"/>
  <path id="timer-arc"/>
  <path id="timer-arc-inner"/>
  <g id="timer-ticks"/>
  <circle cx="150" cy="150" r="4"/>
  
  <path
        id="timer-pointer"
        d="m150 45 l8 -16a8.6 8.6 0 1 0 -16 0z"
        onMouseDown="startDrag(evt)"
  />
  
  <text x="143" y="150" id="minutes">25</text>
  <text x="157" y="150" id="seconds">00</text>
</svg>

<div class="button-container">
  <span id="start" class="button">Start</span>
  <span id="restart" class="button">Restart</span>
</div>
<p>Click and drag the clock hand to set the start time.</p>
              
            
!

CSS

              
                p {
  font-family: "Open Sans", Arial;
}

#clock {
  margin: 30px;
  font-size: 32px;
}

svg circle {
  fill: rgb(52, 137, 255);
}

svg text {
  alignment-baseline: middle;
  font-size: 32px;
  fill: rgb(52, 137, 255);
  pointer-events:none;
}

svg text#minutes {
  text-anchor: end;
}

svg line {
  stroke: rgb(52, 137, 255);
  stroke-width: 6;
  stroke-linecap: round;
}

svg #timer-arc {
  fill: none;
  stroke: rgb(52, 137, 255);
  stroke-width: 8;
  stroke-linecap: round;
}

svg #timer-arc-inner {
  fill: none;
  stroke: white;
  stroke-width: 3;
  stroke-linecap: round;
}

svg #timer-pointer {
  fill: rgb(52, 137, 255);
  stroke: white;
  stroke-width: 2;
  cursor: pointer;
}

svg #timer-pointer:hover {
  fill: rgb(80, 180, 255);
}

.button-container {
  width: 300px;
  margin-top: 5px;
  text-align: center;
}

.button {
  cursor: pointer;
  background-color: #ddd;
  padding: 5px;
  border-radius: 5px;
  font-family: Arial, sans-serif;
  font-size: 1.2em;
}

.button:hover {
  transition: all 0.3s ease;
  background-color: rgb(52, 137, 255);
  color: white;
}

.stop-button:hover {
  background-color: rgb(200, 10, 10);
}
              
            
!

JS

              
                var SVG = document.getElementById('clock-svg');
var SVGarc = SVG.getElementById('timer-arc');
var SVGarc2 = SVG.getElementById('timer-arc-inner');
var SVGticks = SVG.getElementById('timer-ticks');
var SVGminutes = SVG.getElementById('minutes');
var SVGseconds = SVG.getElementById('seconds');
var SVGpointer = SVG.getElementById('timer-pointer');
var timer;
var seconds = 25 * 60;
var maxTime = 25 * 60;
var offsetX, offsetY, dragging;

// Draw ticks on the clock
var drawTicks = function() {
  var s = 150;  // Half size of SVG
  var r = 102;
  var r2 = 92;
  var i, angle, tick, ca, sa;
  
  for (i = 0; i < 60; i++) {
    angle = Math.PI * 2 * i / 60;
    sa = Math.sin(angle);
    ca = Math.cos(angle)
    
    if (i % 5) {
      tick = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
      tick.setAttribute('cx', s + r * sa);
      tick.setAttribute('cy', s - r * ca);
      tick.setAttribute('r', 3);
    } else {
      tick = document.createElementNS('http://www.w3.org/2000/svg', 'line');
      tick.setAttribute('x1', s + r * sa);
      tick.setAttribute('y1', s - r * ca);
      tick.setAttribute('x2', s + r2 * sa);
      tick.setAttribute('y2', s - r2 * ca);
    }
    SVGticks.appendChild(tick);
  };
};

var drawAngle = function(s, r, time1, time2) {
  // Percentage of 1 hour
  var p1 = time1 / 3600;
  var p2 = time2 / 3600;
  // Make sure arc is draw even for full hour
  if (p1 === 1) { p1 -=0.001; }
  if (p2 === 1) { p2 -=0.001; }
  
  var angle1 = p1 * Math.PI * 2;
  var angle2 = p2 * Math.PI * 2;
  
  var d = "M";
  d += (s + r * Math.sin(angle1)) + " ";
  d += (s - r * Math.cos(angle1));
  d += "A" + r + " " + r + " 0 ";
  d += ((angle2 - angle1) > Math.PI ? 1 : 0) + " 1";
  d += " " + (s + r * Math.sin(angle2));
  d += " " + (s - r * Math.cos(angle2));
  return d;
};

var drawMaxTimeArc = function() {  
  var d = drawAngle(150, 124, 0, maxTime);
  SVGarc.setAttribute('d', d);
};

var drawArc = function() {
   // Percentage of 1 hour
  var p = seconds / 3600;
  // Make sure arc is draw even for full hour
  if (p === 1) { p -=0.001; }
  
  var d = drawAngle(150, 124, seconds, maxTime);
  SVGarc2.setAttribute('d', d);
};

var movePointer = function() {
  var cx = 150;
  var cy = 150;
  var r = 112;
  var angle = 360 * seconds / (60 * 60);
  var transform = 'rotate(' + angle + ' ' + cx + ' ' + cy + ')';
  SVGpointer.setAttribute('transform', transform);
};

var writeTime = function() {
  drawArc();  
  var minutes = Math.floor(seconds / 60);
  SVGminutes.innerHTML = ('0' + minutes).slice(-2);
  SVGseconds.innerHTML = ('0' + (seconds % 60)).slice(-2);
};

var countItDown = function() {
  if (seconds > 0) {
    seconds--;
    writeTime();
    movePointer();
  } else {
    window.clearInterval(timer);
  }
};

var startTimer = function() {
  var button = $('#start');
  button.text('Stop');
  button.addClass('stop-button');
  button.off();
  button.click(stopTimer);
  timer = window.setInterval(countItDown, 1000);
};

var stopTimer = function() {
  var button = $('#start');
  button.text('Start');
  button.removeClass('stop-button');
  button.off();
  button.click(startTimer);
  window.clearInterval(timer);
};

function startDrag(evt) {
  dragging = $(evt.target);
  offsetX = evt.clientX;
  offsetY = evt.clientY;
}

function drag(evt) {
  if (dragging) {
    var x = evt.clientX;
    var y = evt.clientY;
    var cx = 150;
    var cy = 150;
    var r = 112;
    var angle = Math.atan2(x - cx, cy - y);
    angle *= 180 / Math.PI;
    if (angle <= 0) { angle += 360; }
    angle = Math.round(angle);
    
    // Set maximum time
    maxTime = angle * 10;
    seconds = maxTime;
    writeTime();
    drawMaxTimeArc();
    
    dragging.attr('transform', 'rotate(' + angle + ' ' + cx + ' ' + cy + ')');
  }
}

function stopDrag() {
  dragging = false;
}

// Setup
drawMaxTimeArc();
drawTicks();
movePointer();

// Button functions
$('#start').click(startTimer);

$('#restart').click(function() {
  seconds = maxTime;
  stopTimer();
  writeTime();
  movePointer();
});
              
            
!
999px

Console