Skip to content

Instantly share code, notes, and snippets.

@egraether
Forked from 140bytes/LICENSE.txt
Created May 24, 2011 15:28
Show Gist options
  • Save egraether/988919 to your computer and use it in GitHub Desktop.
Save egraether/988919 to your computer and use it in GitHub Desktop.
Pie Chart Timer
function(
a, // CanvasRenderingContext2D
b, // radius in pixel
c, // duration in milliseconds
d, // optional callback
e, // placeholder for drawing function
f // placeholder for frame counter
){
a.translate(b, b); // translate context to center of pie chart
(e = function(){ // define drawing function
a.fillRect(0, -b, 1, b); // draw line from center in actual rotation
a.rotate(Math.PI / c * 50); // rotate the context
f = -~f; // initialize and increment counter
f > c / 25 ? // if counter finished...
!d || d() : // call optional callback...
setTimeout(e, 25) // else repeat drawing at 40 fps
})() // start drawing
}
function(a,b,c,d,e,f){a.translate(b,b);(e=function(){a.fillRect(0,-b,1,b);a.rotate(Math.PI/c*50);f=-~f;f>c/25?!d||d():setTimeout(e,25)})()}
Copyright (c) 2011 Eberhard Gräther, http://egraether.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "pie chart timer",
"keywords": ["canvas", "pie chart", "timer"]
}
/*
* Pomodoro Timer Example
* 25 min of work and 5 min of pause
*
* see example at: http://egraether.com/piecharttimer.html
* pomodoro starts now with pause, because CanvasRenderingContext2D.rotate(<0.0002) has no effect in Chrome on Windows
*
* warning: high framerate, timer is not accurate
*/
function pomodoroTimer() {
var canvas = document.createElement("canvas");
canvas.width = canvas.height = 501;
document.body.appendChild(canvas);
var context = canvas.getContext('2d');
var pieChartTimer = function(a,b,c,d,e,f){a.translate(b,b);(e=function(){a.fillRect(0,-b,1,b);a.rotate(Math.PI/c*50);f=-~f;f>c/25?!d||d():setTimeout(e,25)})()}
var work = function() {
if (confirm("start working?")) {
context.fillStyle = "#A00";
context.setTransform(1, 0, 0, 1, 0.5, 0.5);
pieChartTimer(context, 250, 1500000, pause);
}
}
var pause = function() {
if (confirm("start pausing?")) {
context.fillStyle = "#0A0";
context.setTransform(1, 0, 0, 1, 0.5, 0.5);
pieChartTimer(context, 250, 300000, work);
}
}
pause();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment