Created
December 7, 2017 18:57
-
-
Save eliottha/0fff4f683aa2e437e7cd9d48fb2c996b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var el = document.getElementById('graph'); // get canvas | |
var options = { | |
percent: el.getAttribute('data-percent') || 25, | |
size: el.getAttribute('data-size') || 220, | |
lineWidth: el.getAttribute('data-line') || 15, | |
rotate: el.getAttribute('data-rotate') || 0 | |
} | |
var canvas = document.createElement('canvas'); | |
var span = document.createElement('span'); | |
span.textContent = options.percent + '%'; | |
if (typeof(G_vmlCanvasManager) !== 'undefined') { | |
G_vmlCanvasManager.initElement(canvas); | |
} | |
var ctx = canvas.getContext('2d'); | |
canvas.width = canvas.height = options.size; | |
el.appendChild(span); | |
el.appendChild(canvas); | |
ctx.translate(options.size / 2, options.size / 2); // change center | |
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI); // rotate -90 deg | |
//imd = ctx.getImageData(0, 0, 240, 240); | |
var radius = (options.size - options.lineWidth) / 2; | |
var drawCircle = function(color, lineWidth, percent) { | |
percent = Math.min(Math.max(0, percent || 1), 1); | |
ctx.beginPath(); | |
ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, false); | |
ctx.strokeStyle = color; | |
ctx.lineCap = 'round'; // butt, round or square | |
ctx.lineWidth = lineWidth | |
ctx.stroke(); | |
}; | |
drawCircle('#efefef', options.lineWidth, 100 / 100); | |
drawCircle('#555555', options.lineWidth, options.percent / 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment