-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcpu_monitor_ui_canvas.js
158 lines (139 loc) · 4.2 KB
/
cpu_monitor_ui_canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
define(['./cpu_monitor_ui'],function(require,exports){
var CPUJS = {};
CPUJS.Monitor = CPUJS.Monitor || {};
/**
* CPUJS Monitor的UI逻辑,如果不需要画图,就不需要引入这一份逻辑
* @author rizenguo aka GRZ/郭小帅
* @type
*/
CPUJS.Monitor.UI = require('./cpu_monitor_ui');
/**
* CPUJS Monitor的UI逻辑,这个Monitor.UI.Canvas是给支持HTML canvas的浏览器使用的
* @author rizenguo aka GRZ/郭小帅
* @type
*/
CPUJS.Monitor.UI.Drawer = {
initCanvas:function(){
var p =CPUJS.Monitor.UI;
p.canvasDom = document.createElement("CANVAS");
if(p.canvasDom&&typeof(p.canvasDom.getContext)=="function"){
p.canvasDom.id = "cpujs_monitor_ui_canvas";
p.canvasDom.width = p.PANEL_WIDTH;
p.canvasDom.height = p.PANEL_HEIGHT;
p.container.appendChild(p.canvasDom);
p.canvas = p.canvasDom.getContext("2d");
var c = p.canvas;
c.fillStyle = "rgb(0,0,0)";
c.lineWidth = p.ZOOM;
c.lineCap = "round";
c.lineJoin = "round";
c.fillRect(0, 0, p.PANEL_WIDTH, p.PANEL_HEIGHT);
c.beginPath();
c.strokeStyle = "rgb(0,255,0)";
c.stroke();
p.Drawer.PercentNum.init();
return true;
}
return false;
},
/**
* 从上一个点把线连到一个新的点(x,y)是这个点的坐标
*/
drawPoint:function(x,y){
var p = CPUJS.Monitor.UI,c = p.canvas;
var g = c.createLinearGradient(0,0,0,p.PANEL_HEIGHT);
g.addColorStop(0,'rgb(255,0,0)');
g.addColorStop(0.6, "#FFFF00");
g.addColorStop(1,'rgb(0,255,0)');
c.strokeStyle = g;
c.beginPath();
c.moveTo(p.LAST_X,p.LAST_Y);
c.lineTo(x,y);
p.CURRENT_X =x;
p.LAST_X = x;
p.LAST_Y = y;
c.stroke();
},
/**
* 一次性将所有的线给绘制出来,方便实现曲线的平移
*/
drawLine:function(){
var p = CPUJS.Monitor.UI,po;
p.canvas.fillRect(0, 0, p.PANEL_WIDTH, p.PANEL_HEIGHT);
for(var i=0,len=p.point_list.length;i<len;i++){
po = p.point_list[i];
CPUJS.Monitor.UI.Drawer.drawPoint(po[0],po[1]);
po[0]-=p.STEP_WIDTH;
}
CPUJS.Monitor.UI.Mark.move();
},
/**
* cpu曲线的百分比数字
*/
PercentNum:{
PANEL_WIDTH:50,
PANEL_HEIGHT:35,
canvasDom:null,
canvas:null,
init:function(){
var p = CPUJS.Monitor.UI.Drawer.PercentNum;
p.PANEL_WIDTH*=CPUJS.Monitor.UI.ZOOM;
p.PANEL_HEIGHT*=CPUJS.Monitor.UI.ZOOM;
p.canvasDom = document.createElement("CANVAS");
if(p.canvasDom&&typeof(p.canvasDom.getContext)=="function"){
p.canvasDom.id = "cpujs_monitor_ui_canvas_per_num";
p.canvasDom.style.cssText = "left:"+CPUJS.Monitor.UI.PANEL_WIDTH+"px;top:0px;";
p.canvasDom.width = p.PANEL_WIDTH;
p.canvasDom.height = p.PANEL_HEIGHT;
CPUJS.Monitor.UI.container.appendChild(p.canvasDom);
p.canvas = p.canvasDom.getContext("2d");
var c = p.canvas;
c.fillStyle = "rgb(0,0,0)";
c.lineWidth = p.ZOOM;
c.lineCap = "round";
c.lineJoin = "round";
c.fillRect(0, 0, p.PANEL_WIDTH, p.PANEL_HEIGHT);
c.beginPath();
c.stroke();
var fontSize = "40",
fontWeight = "normal",
fontStyle = "normal",
fontFace="arial";
c.font = fontWeight + " " + fontStyle + " " + fontSize + "px " + fontFace;
c.fillStyle = "#ff0000";
c.textBaseLine = "middle";
c.textAlign = "center";
//c.fillText("abc",0,0);
return true;
}
return false;
},
clear:function(){
var p = CPUJS.Monitor.UI.Drawer.PercentNum;
p.canvas.fillStyle = "rgb(0,0,0)";
p.canvas.fillRect(0, 0, p.PANEL_WIDTH, p.PANEL_HEIGHT);
},
show:function(num){
var p = CPUJS.Monitor.UI.Drawer.PercentNum;
p.clear();
p.canvas.fillStyle = "#00ff00";
if(num>25){
p.canvas.fillStyle = "#FFFF00";
}
if(num>50){
p.canvas.fillStyle = "#FF6600";
}
if(num>80){
p.canvas.fillStyle = "#FF0000";
}
p.canvas.fillText(num+"%",p.PANEL_WIDTH/2,p.PANEL_HEIGHT/2+20);
}
}
};
CPUJS.Monitor.UI.bootstrap = function(){
CPUJS.Monitor.UI.init();
CPUJS.Monitor.UI.Drawer.initCanvas();
}
return CPUJS.Monitor.UI;
;
});