Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/easy-pie-chart/src/renderer/canvas.js
1293 views
1
/**
2
* Renderer to render the chart on a canvas object
3
* @param {DOMElement} el DOM element to host the canvas (root of the plugin)
4
* @param {object} options options object of the plugin
5
*/
6
var CanvasRenderer = function(el, options) {
7
var cachedBackground;
8
var canvas = document.createElement('canvas');
9
10
el.appendChild(canvas);
11
12
if (typeof(G_vmlCanvasManager) === 'object') {
13
G_vmlCanvasManager.initElement(canvas);
14
}
15
16
var ctx = canvas.getContext('2d');
17
18
canvas.width = canvas.height = options.size;
19
20
// canvas on retina devices
21
var scaleBy = 1;
22
if (window.devicePixelRatio > 1) {
23
scaleBy = window.devicePixelRatio;
24
canvas.style.width = canvas.style.height = [options.size, 'px'].join('');
25
canvas.width = canvas.height = options.size * scaleBy;
26
ctx.scale(scaleBy, scaleBy);
27
}
28
29
// move 0,0 coordinates to the center
30
ctx.translate(options.size / 2, options.size / 2);
31
32
// rotate canvas -90deg
33
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);
34
35
var radius = (options.size - options.lineWidth) / 2;
36
if (options.scaleColor && options.scaleLength) {
37
radius -= options.scaleLength + 2; // 2 is the distance between scale and bar
38
}
39
40
// IE polyfill for Date
41
Date.now = Date.now || function() {
42
return +(new Date());
43
};
44
45
/**
46
* Draw a circle around the center of the canvas
47
* @param {strong} color Valid CSS color string
48
* @param {number} lineWidth Width of the line in px
49
* @param {number} percent Percentage to draw (float between -1 and 1)
50
*/
51
var drawCircle = function(color, lineWidth, percent) {
52
percent = Math.min(Math.max(-1, percent || 0), 1);
53
var isNegative = percent <= 0 ? true : false;
54
55
ctx.beginPath();
56
ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);
57
58
ctx.strokeStyle = color;
59
ctx.lineWidth = lineWidth;
60
61
ctx.stroke();
62
};
63
64
/**
65
* Draw the scale of the chart
66
*/
67
var drawScale = function() {
68
var offset;
69
var length;
70
71
ctx.lineWidth = 1;
72
ctx.fillStyle = options.scaleColor;
73
74
ctx.save();
75
for (var i = 24; i > 0; --i) {
76
if (i % 6 === 0) {
77
length = options.scaleLength;
78
offset = 0;
79
} else {
80
length = options.scaleLength * 0.6;
81
offset = options.scaleLength - length;
82
}
83
ctx.fillRect(-options.size/2 + offset, 0, length, 1);
84
ctx.rotate(Math.PI / 12);
85
}
86
ctx.restore();
87
};
88
89
/**
90
* Request animation frame wrapper with polyfill
91
* @return {function} Request animation frame method or timeout fallback
92
*/
93
var reqAnimationFrame = (function() {
94
return window.requestAnimationFrame ||
95
window.webkitRequestAnimationFrame ||
96
window.mozRequestAnimationFrame ||
97
function(callback) {
98
window.setTimeout(callback, 1000 / 60);
99
};
100
}());
101
102
/**
103
* Draw the background of the plugin including the scale and the track
104
*/
105
var drawBackground = function() {
106
if(options.scaleColor) drawScale();
107
if(options.trackColor) drawCircle(options.trackColor, options.trackWidth || options.lineWidth, 1);
108
};
109
110
/**
111
* Canvas accessor
112
*/
113
this.getCanvas = function() {
114
return canvas;
115
};
116
117
/**
118
* Canvas 2D context 'ctx' accessor
119
*/
120
this.getCtx = function() {
121
return ctx;
122
};
123
124
/**
125
* Clear the complete canvas
126
*/
127
this.clear = function() {
128
ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);
129
};
130
131
/**
132
* Draw the complete chart
133
* @param {number} percent Percent shown by the chart between -100 and 100
134
*/
135
this.draw = function(percent) {
136
// do we need to render a background
137
if (!!options.scaleColor || !!options.trackColor) {
138
// getImageData and putImageData are supported
139
if (ctx.getImageData && ctx.putImageData) {
140
if (!cachedBackground) {
141
drawBackground();
142
cachedBackground = ctx.getImageData(0, 0, options.size * scaleBy, options.size * scaleBy);
143
} else {
144
ctx.putImageData(cachedBackground, 0, 0);
145
}
146
} else {
147
this.clear();
148
drawBackground();
149
}
150
} else {
151
this.clear();
152
}
153
154
ctx.lineCap = options.lineCap;
155
156
// if barcolor is a function execute it and pass the percent as a value
157
var color;
158
if (typeof(options.barColor) === 'function') {
159
color = options.barColor(percent);
160
} else {
161
color = options.barColor;
162
}
163
164
// draw bar
165
drawCircle(color, options.lineWidth, percent / 100);
166
}.bind(this);
167
168
/**
169
* Animate from some percent to some other percentage
170
* @param {number} from Starting percentage
171
* @param {number} to Final percentage
172
*/
173
this.animate = function(from, to) {
174
var startTime = Date.now();
175
options.onStart(from, to);
176
var animation = function() {
177
var process = Math.min(Date.now() - startTime, options.animate.duration);
178
var currentValue = options.easing(this, process, from, to - from, options.animate.duration);
179
this.draw(currentValue);
180
options.onStep(from, to, currentValue);
181
if (process >= options.animate.duration) {
182
options.onStop(from, to);
183
} else {
184
reqAnimationFrame(animation);
185
}
186
}.bind(this);
187
188
reqAnimationFrame(animation);
189
}.bind(this);
190
};
191
192