Path: blob/master/web-gui/buildyourownbotnet/assets/js/easy-pie-chart/src/renderer/canvas.js
1293 views
/**1* Renderer to render the chart on a canvas object2* @param {DOMElement} el DOM element to host the canvas (root of the plugin)3* @param {object} options options object of the plugin4*/5var CanvasRenderer = function(el, options) {6var cachedBackground;7var canvas = document.createElement('canvas');89el.appendChild(canvas);1011if (typeof(G_vmlCanvasManager) === 'object') {12G_vmlCanvasManager.initElement(canvas);13}1415var ctx = canvas.getContext('2d');1617canvas.width = canvas.height = options.size;1819// canvas on retina devices20var scaleBy = 1;21if (window.devicePixelRatio > 1) {22scaleBy = window.devicePixelRatio;23canvas.style.width = canvas.style.height = [options.size, 'px'].join('');24canvas.width = canvas.height = options.size * scaleBy;25ctx.scale(scaleBy, scaleBy);26}2728// move 0,0 coordinates to the center29ctx.translate(options.size / 2, options.size / 2);3031// rotate canvas -90deg32ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);3334var radius = (options.size - options.lineWidth) / 2;35if (options.scaleColor && options.scaleLength) {36radius -= options.scaleLength + 2; // 2 is the distance between scale and bar37}3839// IE polyfill for Date40Date.now = Date.now || function() {41return +(new Date());42};4344/**45* Draw a circle around the center of the canvas46* @param {strong} color Valid CSS color string47* @param {number} lineWidth Width of the line in px48* @param {number} percent Percentage to draw (float between -1 and 1)49*/50var drawCircle = function(color, lineWidth, percent) {51percent = Math.min(Math.max(-1, percent || 0), 1);52var isNegative = percent <= 0 ? true : false;5354ctx.beginPath();55ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);5657ctx.strokeStyle = color;58ctx.lineWidth = lineWidth;5960ctx.stroke();61};6263/**64* Draw the scale of the chart65*/66var drawScale = function() {67var offset;68var length;6970ctx.lineWidth = 1;71ctx.fillStyle = options.scaleColor;7273ctx.save();74for (var i = 24; i > 0; --i) {75if (i % 6 === 0) {76length = options.scaleLength;77offset = 0;78} else {79length = options.scaleLength * 0.6;80offset = options.scaleLength - length;81}82ctx.fillRect(-options.size/2 + offset, 0, length, 1);83ctx.rotate(Math.PI / 12);84}85ctx.restore();86};8788/**89* Request animation frame wrapper with polyfill90* @return {function} Request animation frame method or timeout fallback91*/92var reqAnimationFrame = (function() {93return window.requestAnimationFrame ||94window.webkitRequestAnimationFrame ||95window.mozRequestAnimationFrame ||96function(callback) {97window.setTimeout(callback, 1000 / 60);98};99}());100101/**102* Draw the background of the plugin including the scale and the track103*/104var drawBackground = function() {105if(options.scaleColor) drawScale();106if(options.trackColor) drawCircle(options.trackColor, options.trackWidth || options.lineWidth, 1);107};108109/**110* Canvas accessor111*/112this.getCanvas = function() {113return canvas;114};115116/**117* Canvas 2D context 'ctx' accessor118*/119this.getCtx = function() {120return ctx;121};122123/**124* Clear the complete canvas125*/126this.clear = function() {127ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);128};129130/**131* Draw the complete chart132* @param {number} percent Percent shown by the chart between -100 and 100133*/134this.draw = function(percent) {135// do we need to render a background136if (!!options.scaleColor || !!options.trackColor) {137// getImageData and putImageData are supported138if (ctx.getImageData && ctx.putImageData) {139if (!cachedBackground) {140drawBackground();141cachedBackground = ctx.getImageData(0, 0, options.size * scaleBy, options.size * scaleBy);142} else {143ctx.putImageData(cachedBackground, 0, 0);144}145} else {146this.clear();147drawBackground();148}149} else {150this.clear();151}152153ctx.lineCap = options.lineCap;154155// if barcolor is a function execute it and pass the percent as a value156var color;157if (typeof(options.barColor) === 'function') {158color = options.barColor(percent);159} else {160color = options.barColor;161}162163// draw bar164drawCircle(color, options.lineWidth, percent / 100);165}.bind(this);166167/**168* Animate from some percent to some other percentage169* @param {number} from Starting percentage170* @param {number} to Final percentage171*/172this.animate = function(from, to) {173var startTime = Date.now();174options.onStart(from, to);175var animation = function() {176var process = Math.min(Date.now() - startTime, options.animate.duration);177var currentValue = options.easing(this, process, from, to - from, options.animate.duration);178this.draw(currentValue);179options.onStep(from, to, currentValue);180if (process >= options.animate.duration) {181options.onStop(from, to);182} else {183reqAnimationFrame(animation);184}185}.bind(this);186187reqAnimationFrame(animation);188}.bind(this);189};190191192