css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console