cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
    <header class="mdl-layout__header">
      <div class="mdl-layout__header-row">
        <span class="mdl-layout__title">Khetmani's Pomodoro Clock</span>
      </div>
    </header>
    <div class="mdl-layout__drawer drawer-content">
      <span class="mdl-layout__title">Info</span>
      <nav class="mdl-navigation">
        For more info about the Pomodoro Technique check out
        <a class="mdl-navigation__link" href="https://en.wikipedia.org/wiki/Pomodoro_Technique">Wikipedia</a>
        Designed and Developed by John Behan from
        <a class="mdl-navigation__link" href="http://boxitoff.com">Box It Off</a>
      </nav>
    </div>

    <main class="mdl-layout__content main-content">
      <div class="mdl-grid">
        <div class="mdl-cell mdl-cell--12-col">
          <canvas id="time"></canvas>
        </div>
      </div>
      <div class="mdl-grid">
        <div class="mdl-cell mdl-cell--4-col mdl-cell--2-col-tablet mdl-cell--hide-phone">
        </div>
        <div class="mdl-cell">
          <button id="start-timer" class="mdl-button mdl-button--raised mdl-button--accent mdl-js-button mdl-js-ripple-effect">
            Start
          </button>
        </div>
      </div>
      <div class="mdl-grid">
        <div class="mdl-cell mdl-cell--2-col mdl-cell--hide-tablet mdl-cell--hide-phone">
        </div>
        <div class="mdl-cell mdl-cell--2-col-phone">
          <button id="add-minutes" class="mdl-button mdl-button--fab mdl-button--primary mdl-js-button mdl-js-ripple-effect">
            <i class="material-icons">add</i>
          </button>
        </div>

        <div class="mdl-cell mdl-cell--2-col-phone">
          <button id="remove-minutes" class="mdl-button mdl-button--fab mdl-button--primary mdl-js-button mdl-js-ripple-effect">
            <i class="material-icons">remove</i>
          </button>
        </div>
      </div>
    </main>
  </div>
            
          
!
            
              body {
      background-color: #fafafa;
      color: #ffffff;
    }

    .drawer-content {
      padding: 10px;
    }

    .main-content {
      text-align: center;
    }
            
          
!
            
              var started = false; // is the timer started
var timeCanvas = document.getElementById('time');
var viewportWidth = document.documentElement.clientWidth;
var viewportHeight = document.documentElement.clientHeight;
var canvasLength, radius, center;
var startTime, durationMiliseconds;
var clockTime = 25;
var animID;
var timeUp = false;
var alarm = new Audio('https://s3-eu-west-1.amazonaws.com/jjmax/audio/alarm.mp3');

// canvas styles
var arcWidth = 40;
var innerArcColor = "#7C4DFF";
var outerArcColor = "#03A9F4";
var fontStyle = "48px Roboto";
var fontColor = "#000000";

var canvasDimensions = function() {
  canvasLength = Math.min(viewportWidth, viewportHeight) / 1.5;
  timeCanvas.width = canvasLength;
  timeCanvas.height = canvasLength;

  radius = canvasLength / 2.5;
  center = canvasLength / 2;
};

var degToRad = function(deg) {
  return deg * (Math.PI / 180);
};

var milisecondsConvert = function(milis) {
  var minutes = Math.floor(milis / 60000);
  var seconds = milis % 60000; //in miliseconds to allow smoother animation on seconds arc
  return [minutes, seconds];
};

var drawClock = function() {

  var diff = durationMiliseconds - (Date.now() - startTime | 0);

  if (diff < 0) {
    started = false;
    diff = 0;
    timeUp = true;
    alarm.play();
  }

  var diffTime = milisecondsConvert(diff); //returns array [mins, secs]
  var diffSecondsDegrees = diffTime[1] * 6 / 1000 || 360; // 1 second = six degrees
  var diffMinutesDegrees = diff / durationMiliseconds * 360 || 360; // convert total remaining time to degrees of full circle

  if (timeCanvas.getContext) {
    // the function to draw/redraw the clock
    var ctx = timeCanvas.getContext('2d');
    ctx.save();

    // setup the context
    ctx.clearRect(0, 0, canvasLength, canvasLength);
    ctx.lineWidth = arcWidth;
    ctx.translate(center, center);
    ctx.save();
    ctx.rotate(-1.57079633); // rotate canvas -90 degrees to set 0 degress at 12 o'clock

    // draw the outer ring
    ctx.strokeStyle = outerArcColor;
    ctx.beginPath();
    ctx.arc(0, 0, radius, 0, degToRad(diffSecondsDegrees));
    ctx.stroke();

    // draw the inner ring
    ctx.strokeStyle = innerArcColor;
    ctx.beginPath();
    ctx.arc(0, 0, radius - arcWidth, 0, degToRad(diffMinutesDegrees));
    ctx.stroke();

    // draw the numbers
    ctx.restore();
    ctx.textBaseline = "middle";
    ctx.textAlign = "center";
    ctx.font = fontStyle;
    ctx.fillStyle = fontColor;
    ctx.fillText(diffTime[0] + " : " + ("0" + Math.floor(diffTime[1] / 1000)).slice(-2), 0, 0);

    ctx.restore();
  }

  if (started === true) {
    animID = window.requestAnimationFrame(drawClock);
  }

};

var clock = function(duration) {
  // function to setup initial clock state
  // starting point - http://irae.pro.br/lab/canvas_pie_countdown/
  // timer using Date code - http://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer
  startTime = Date.now();
  durationMiliseconds = duration * 60 * 1000;

  drawClock(); //initial clock
};

// events
document.getElementById('start-timer').addEventListener('click', function(e) {
  if (started === true) {
    started = false;
    this.innerHTML = "Start";
    window.cancelAnimationFrame(animID);
    clock(clockTime);
  } else {
    this.innerHTML = "Reset";
    started = true;
    clock(clockTime);
  }
});

document.getElementById('add-minutes').addEventListener('click', function(e) {
  clockTime += 5;
  clock(clockTime);
});

document.getElementById('remove-minutes').addEventListener('click', function(e) {
  clockTime -= 5;
  clock(clockTime);
});

// Start of MDN Resize
// from MDN - https://developer.mozilla.org/en-US/docs/Web/Events/resize
// efficient way to calculate resizes of window
;(function() {
  var throttle = function(type, name, objRec) {
    var obj = objRec || window;
    var running = false;
    var func = function() {
      if (running) {
        return;
      }
      running = true;
      requestAnimationFrame(function() {
        obj.dispatchEvent(new CustomEvent(name));
        running = false;
      });
    };
    obj.addEventListener(type, func);
  };

  /* init - you can init any event */
  throttle("resize", "optimizedResize");
})();

// handle event
window.addEventListener("optimizedResize", function() {
  viewportWidth = document.documentElement.clientWidth;
  viewportHeight = document.documentElement.clientHeight;
  canvasDimensions();
  if (!started) {
    clock(clockTime);
  }
});
// END of MDN Resize

var init = function() {
  canvasDimensions();
  clock(clockTime);
};

init();

            
          
!
999px
Loading ..................

Console