otsdaq_prepmodernization  v2_05_02_indev
NimStream_app.js
1 //NimStream_app.js
2 //Created by Ben Hawks (bhawks at fnal.gov)
3 
4 /*
5  * TODO:
6  * * Add Open Source License popup for Color picker/Jquery mask etc
7  * * Implement function to take data from consumer and plug it into plotting function
8  * * Add more user control/configuration for the graph style (Plot color, persistance, etc)
9  * * Implement displays for all 4 channels being burst back to application
10  */
11 
12 
13 
14 
15 //functions:
16  //init()
17 
18 //top-level scope (global) variables:
19 var block1El = document.getElementById('block1');//red
20 var block2El = document.getElementById('block2');//yellow
21 var block3El = document.getElementById('block3');//blue
22 var block4El = document.getElementById('block4');//green
23 
24 
25 var canvas1 = document.getElementById("canvas1"),
26  ctx1 = canvas1.getContext("2d"),
27  canvas2 = document.getElementById("canvas2"),
28  ctx2 = canvas2.getContext("2d"),
29  canvas3 = document.getElementById("canvas3"),
30  ctx3 = canvas3.getContext("2d")
31  canvas4 = document.getElementById("canvas4"),
32  ctx4 = canvas4.getContext("2d")
33  canvas1Grid = document.getElementById("canvas1Grid"),
34  ctx1Grid = canvas1Grid.getContext("2d"),
35  canvas2Grid = document.getElementById("canvas2Grid"),
36  ctx2Grid = canvas2Grid.getContext("2d"),
37  canvas3Grid = document.getElementById("canvas3Grid"),
38  ctx3Grid = canvas3Grid.getContext("2d")
39  canvas4Grid = document.getElementById("canvas4Grid"),
40  ctx4Grid = canvas4Grid.getContext("2d");
41 
42 var color1 = document.getElementById('color1');
43 var color2 = document.getElementById('color2');
44 var color3 = document.getElementById('color3');
45 var color4 = document.getElementById('color4');
46 
47 var gblAlpha1 = document.getElementById('gblAlpha1');
48 var gblAlpha2 = document.getElementById('gblAlpha2');
49 var gblAlpha3 = document.getElementById('gblAlpha3');
50 var gblAlpha4 = document.getElementById('gblAlpha4');
51 
52 
53 // X,Y Coords in Pixels, 0,0 is top left corner of canvas
54 var defaultSigHighHeight = 150;
55 var defaultSigLowHeight = 300;
56 var defaultSigWidth = 20;
57 var defaultVertDivs = 12;
58 var defaultGblAlpha = 0.1;
59 var defaultTimeoutval = 150;
60 
61 var sigHighHeight1 = 150,
62  sigLowHeight1 = 300,
63  sigWidth1 = 20;
64 
65 var sigHighHeight2 = 150,
66  sigLowHeight2 = 300,
67  sigWidth2 = 20;
68 
69 var sigHighHeight3 = 150,
70  sigLowHeight3 = 300,
71  sigWidth3 = 20;
72 
73 var sigHighHeight4 = 150,
74  sigLowHeight4 = 300,
75  sigWidth4 = 20;
76 
77 canvas1.width = canvas1.style.width = sigWidth1*32;
78 canvas1.height = canvas1.style.height = sigLowHeight1;
79 
80 canvas2.width = canvas2.style.width = sigWidth2*32;
81 canvas2.height = canvas2.style.height = sigLowHeight2;
82 
83 canvas3.width = canvas3.style.width = sigWidth3*32;
84 canvas3.height = canvas3.style.height = sigLowHeight3;
85 
86 canvas4.width = canvas4.style.width = sigWidth4*32;
87 canvas4.height = canvas4.style.height = sigLowHeight4;
88 
89 canvas1Grid.width = canvas1Grid.style.width = sigWidth1*32;
90 canvas1Grid.height = canvas1Grid.style.height = sigLowHeight1;
91 ctx1Grid.fillStyle = "white";
92 ctx1Grid.fillRect(0, 0, canvas1Grid.width, canvas1Grid.height);
93 
94 canvas2Grid.width = canvas2Grid.style.width = sigWidth2*32;
95 canvas2Grid.height = canvas2Grid.style.height = sigLowHeight2;
96 ctx2Grid.fillStyle = "white";
97 ctx2Grid.fillRect(0, 0, canvas2Grid.width, canvas2Grid.height);
98 
99 canvas3Grid.width = canvas3Grid.style.width = sigWidth3*32;
100 canvas3Grid.height = canvas3Grid.style.height = sigLowHeight3;
101 ctx3Grid.fillStyle = "white";
102 ctx3Grid.fillRect(0, 0, canvas3Grid.width, canvas3Grid.height);
103 
104 canvas4Grid.width = canvas4Grid.style.width = sigWidth4*32;
105 canvas4Grid.height = canvas4Grid.style.height = sigLowHeight4;
106 ctx4Grid.fillStyle = "white";
107 ctx4Grid.fillRect(0, 0, canvas4Grid.width, canvas4Grid.height);
108 
109 $(block1El).css("height",sigLowHeight1);
110 $(block1El).css("width",sigWidth1*32);
111 
112 $(block2El).css("height",sigLowHeight2);
113 $(block2El).css("width",sigWidth2*32);
114 
115 $(block3El).css("height",sigLowHeight3);
116 $(block3El).css("width",sigWidth3*32);
117 
118 $(block4El).css("height",sigLowHeight4);
119 $(block4El).css("width",sigWidth4*32);
120 
121 var testCanvasEl = document.getElementById("testCanvas");
122 var testGridEl = document.getElementById("testGrid");
123 
124 
125 var getDataReq = true;
128 
129 function handlerFunction(req) {
130  console.log("handlerFunction() was called. Req: " + req.responseText);
131  //Debug.log("handlerFunction() was called. Req: " + req.responseText);
132 
133  var child1data = DesktopContent.getXMLValue(req,"rawData");
134  //var child2data = DesktopContent.getXMLValue(req,"child2");
135  ("#tstBlkEl").innerHTML = child1data;
136  ("#tstBlkEl").innerHTML += "<br>";
137  console.log("--child1data:" + child1data/*," --child2data:",child2data*/);
138 
139 }
140 
141 function getData() {
142  DesktopContent.XMLHttpRequest("Request?RequestType=getNext&bufferName=DB1&consumerName=NimStreamConsumer&args={timestamp:0,count:1200000}","",handlerFunction);
143  setTimeout(getData,5000);
144 }
145 
146 /*
147  pxCheck();
148 
149  Function:
150  Checks if a specified pixel's RGBA value is pure white/opaque, returns True if it is
151 
152  args:
153  pxData - the data property of an ImageData object as returned by getImageData of a single pixel,
154  a single dimensional array of ints 0-255 representing rgba values
155 
156  */
157 function pxCheck(pxData){
158  var isWhite = false;
159  if((pxData[0] == 255) && (pxData[1] == 255) && (pxData[2] == 255) && (pxData[3] == 255)){
160  isWhite = true;
161  }
162  return isWhite;
163 }
164 
165 
166 
167 /*
168  fadeOut()
169 
170  Function:
171  "Fades out" pulses over time simalar to a phospherous oscilloscope by compositing out the previously drawn lines with a amount of transparency
172 
173  args:
174  ctxVar - the 2D context of the canvas that the fade effect should be applied to
175  canvasVar - the canvas that the 2D Context that's being faded out resides in
176  gblAlphaVar - global alpha value, controls the strength of the fade. Lower the value, higher the "persistance"
177  timeoutval - controls the frequency that the canvas is "faded out" at. Recommended to leave low to keep fade out smooth
178  and modify the gblAlpha value to change persistance
179  */
180 function fadeOut(ctxVar,canvasVar,gblAlphaVar,timeoutVal) {
181  //https://stackoverflow.com/questions/27082720/html5-apply-transparency-to-canvas-after-drawing-through-javascript
182  ctxVar.save();
183  ctxVar.globalAlpha = gblAlphaVar.value;
184  ctxVar.globalCompositeOperation='destination-out';
185  ctxVar.fillStyle= '#FFF';
186  ctxVar.fillRect(0,0,canvasVar.width, canvasVar.height);
187  ctxVar.restore();
188 
189  return setTimeout(function() {
190  fadeOut(ctxVar,canvasVar,gblAlphaVar,timeoutVal);
191  },timeoutVal);//Tune timeout value (in ms) to adjust rate of fade out, lower will update more often and fade faster
192  }
193 
194 
195 /*
196  drawGrid()
197 
198  Function:
199  draws a grid on a ctx element based on the size/pulse parameters for the drawScope function.
200  Number of horizontal divisons is based on the canvas' width/sigWidth,
201  Number of vertical divs are based on the vertDivs var.
202 
203  args:
204  strokeColor - desired color/style of the grid, accepts any valid CSS "style" string
205  ctxVar - 2D context to draw grid on
206  canvasVar - the canvas that the 2D Context to draw the grid on resides in
207  vertDivs - Vertical divisons of the grid
208 
209 */
210 function drawGrid(strokeColor,ctxVar,canvasVar,vertDivs,sigWidth,sigLowHeight){
211  //reset the canvas to a blank (white) slate
212  console.log("vDiv Spacing: " + (sigLowHeight/vertDivs));
213  ctxVar.clearRect(0, 0, canvasVar.width, canvasVar.height);
214  ctxVar.fillStyle = "white";
215  ctxVar.fillRect(0, 0, canvasVar.width, canvasVar.height);
216 
217  //draw the grid
218  ctxVar.beginPath();
219  for (var x =0; x <= canvasVar.width; x += sigWidth){ //draw vertical lines
220  ctxVar.moveTo(0.5 + x, 0);
221  ctxVar.lineTo(0.5 + x, canvasVar.height);
222  }
223 
224  for (var x=0; x <= canvasVar.height; x += (sigLowHeight/vertDivs)){ //draw horizontal lines
225  ctxVar.moveTo(0,0.5+x);
226  ctxVar.lineTo(canvasVar.width, 0.5 + x);
227  }
228 
229  ctxVar.strokeStyle = strokeColor;
230  ctxVar.stroke();
231  ctxVar.closePath();
232 
233 }
234 
235 
236 /*
237  drawScope(scopeArr,strokeColor,ctxVar)
238 
239  Function:
240  Draws a path, based on a binary string passed in as an argument, to represent a digital signal.
241 
242  args:
243  scopeArr - binary string used as an input to draw the path, should be limited to 32 bits, as the canvas' width is based upon that,
244  but if the canvas is modified to be wider, can support a longer signal
245  strokeColor - Desired color of the path, any valid "Style" string accepted
246  ctxVar - the 2D context of the canvas that the path will be drawn in
247 
248  */
249 function drawScope(scopeArr,strokeColor,ctxVar,sigWidth,sigLowHeight,sigHighHeight){
250  var lastHeight = 0;
251 
252  ctxVar.beginPath();
253 
254  if(scopeArr[0]=="1"){
255  lastHeight = sigHighHeight+5;
256  ctxVar.moveTo(0,lastHeight);
257  }
258  else{
259  lastHeight = sigLowHeight;
260  ctxVar.moveTo(0,lastHeight);
261  }
262  for(var i = 0; i <= scopeArr.length; i++){
263  if(scopeArr[i]=="1"){
264  if(lastHeight == sigHighHeight){
265  ctxVar.lineTo(((i+1)*sigWidth), sigHighHeight);
266  }
267  else{
268  ctxVar.lineTo(((i)*sigWidth), sigHighHeight);
269  ctxVar.lineTo(((i+1)*sigWidth), sigHighHeight);
270  lastHeight = sigHighHeight;
271  }
272  }
273  else{
274  if(lastHeight == sigLowHeight){
275  ctxVar.lineTo(((i+1)*sigWidth), sigLowHeight);
276  }
277  else{
278  ctxVar.lineTo(((i)*sigWidth), sigLowHeight);
279  ctxVar.lineTo(((i+1)*sigWidth), sigLowHeight);
280  lastHeight = sigLowHeight;
281  }
282  }
283  }
284  ctxVar.strokeStyle=strokeColor;
285  ctxVar.stroke();
286  ctxVar.closePath();
287 }
288 
289 
290 
291 
292 
293 //Dummy Data functions to test drawing features
294 function keepDrawing1() {
295  drawScope("1010101010101011110000", color1.value, ctx1, sigWidth1, sigLowHeight1, sigHighHeight1);
296  setTimeout(keepDrawing1,1500);
297 }
298 
299 function keepDrawing2() {
300  drawScope("0001101000101011010101", color2.value, ctx2, sigWidth2, sigLowHeight2, sigHighHeight2);
301  setTimeout(keepDrawing2,2500);
302 }
303 
304 function keepDrawing3() {
305  drawScope("1101010010101110100010", color3.value, ctx3, sigWidth3, sigLowHeight3, sigHighHeight3);
306  setTimeout(keepDrawing3,7000);
307 }
308 
309 function keepDrawing4() {
310  drawScope("1101010010101110100010", color4.value, ctx4, sigWidth4, sigLowHeight4, sigHighHeight4);
311  setTimeout(keepDrawing4,700);
312 }
313 
314 drawGrid("lightgray",ctx1Grid,canvas1Grid,defaultVertDivs,sigWidth1,sigLowHeight1);
315 drawGrid("lightgray",ctx2Grid,canvas2Grid,defaultVertDivs,sigWidth2,sigLowHeight2);
316 drawGrid("lightgray",ctx3Grid,canvas3Grid,defaultVertDivs,sigWidth3,sigLowHeight3);
317 drawGrid("lightgray",ctx4Grid,canvas4Grid,defaultVertDivs,sigWidth4,sigLowHeight4);
318 keepDrawing1();
319 keepDrawing2();
320 keepDrawing3();
321 keepDrawing4();
322 fadeOut(ctx1,canvas1,gblAlpha1,defaultTimeoutval);
323 fadeOut(ctx2,canvas2,gblAlpha2,defaultTimeoutval);
324 fadeOut(ctx3,canvas3,gblAlpha3,defaultTimeoutval);
325 fadeOut(ctx4,canvas4,gblAlpha4,defaultTimeoutval);
326 $(document).ready(getData());
327 
328 
329 
330 
331 
332 
333