Skip to content

Instantly share code, notes, and snippets.

@fxck
Last active December 30, 2015 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fxck/7804798 to your computer and use it in GitHub Desktop.
Save fxck/7804798 to your computer and use it in GitHub Desktop.
costlocker's donuts
/*
atrs:
size, lineWidth, bg, bc, val
*/
app.directive('clDonut', function() {
return {
restrict: 'E',
template: '<canvas></canvas>',
replace: true,
link: function(scope, el, attrs) {
var w = attrs.size;
el.attr('width', w);
el.attr('height', w);
var defaults = {
bg: '#CCCCCC',
color: '#000000'
};
var c = el.get(0).getContext("2d");
var v = attrs.val || 0;
var lineWidth = attrs.lineWidth ? attrs.lineWidth : w/5.714285714285714;
var w2 = w / 2;
var radius = w2 - lineWidth / 2;
var angleArc = 360 * Math.PI / 180;
var startAngle = 1.5 * Math.PI;
var endAngle = 1.5 * Math.PI + angleArc;
var angle = v*angleArc/100;
var ea = startAngle+angle;
c.lineWidth = lineWidth;
var render = function() {
c.beginPath();
c.strokeStyle = attrs.bg ? attrs.bg : defaults.bg;
c.arc(w2, w2, radius, endAngle, startAngle, true);
c.stroke();
c.beginPath();
c.strokeStyle = attrs.bc ? attrs.bc : defaults.color;
c.arc(w2, w2, radius, startAngle, ea, false);
c.stroke();
}
attrs.$observe('val', function() {
render();
});
render();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment