Subversion Repositories wimsdev

Rev

Rev 18218 | Rev 18556 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15111 schaersvoo 1
#include "canvasdraw.h"
2
/*
3
collection of javascript utilities (with no configurable parameters)
4
*/
5
void add_javascript_function(){
16643 schaersvoo 6
#ifdef KATEX_INSTALLED
16642 schaersvoo 7
char *mathjax="centered(xml_div,obj);";
8
#else
9
char *mathjax="setTimeout(function(){centered(xml_div,obj)},100);";
10
#endif
15111 schaersvoo 11
int i;
12
int type;
13
for(i = 0 ; i < MAX_JS_FUNCTIONS; i++){
14
/*
17351 bpr 15
fprintf(stdout,"checking js_function[%d] = %d<br>",i,js_function[i]);
15111 schaersvoo 16
*/
18553 bpr 17
  if( js_function[i] == 1){
15111 schaersvoo 18
    switch(i){
18553 bpr 19
      case JS_ARROWHEAD:
15690 schaersvoo 20
/* draw an arrowhead on object ; arrowhead on (x:y) from direction (x0:y0) */
18553 bpr 21
        fprintf(js_include_file,"\n/* draw an arrowhead on object ; arrowhead on (x:y) from direction (x0:y0) */\
15690 schaersvoo 22
function draw_arrowhead(x,y,x0,y0,ctx){\
23
var angle = Math.atan2(x - x0,y - y0) + Math.PI;\
24
var pie = Math.PI/6;\
25
ctx.save();\
26
ctx.fillStyle=ctx.strokeStyle;\
27
ctx.beginPath();\
28
ctx.moveTo(x,y);\
29
ctx.moveTo(x - (arrow_head * Math.sin(angle - pie)),y - (arrow_head * Math.cos(angle - pie)));\
30
ctx.lineTo(x,y);\
31
ctx.lineTo(x - (arrow_head * Math.sin(angle + pie)),y - (arrow_head * Math.cos(angle + pie)));\
32
ctx.closePath();\
33
ctx.stroke();\
34
ctx.fill();\
35
ctx.restore();\
36
};");
18553 bpr 37
        break;
38
      case JS_LOAD_IMAGE:
39
        fprintf(js_include_file,"\n/* get_image_from_url */\
15111 schaersvoo 40
var image_cnt = 0;\
41
if( typeof(image_patterns) !== 'object'){var image_patterns = [];};\
42
var get_image_from_url = function(url){\
43
 var idx = Math.ceil(1000*(Math.random()));\
44
 var canvas = create_canvas%d(idx,xsize,ysize);\
45
 canvas.style.visibility = 'hidden';\
46
 var ctx = canvas.getContext(\"2d\");\
47
 var img = new Image();\
48
 img.src = url;\
49
 img.onload = function(){\
50
  var pat = ctx.createPattern(img,\"repeat\");\
51
  image_patterns.push(pat);\
52
 };\
53
};",canvas_root_id);
18553 bpr 54
        break;
55
      case JS_SAFE_EVAL:
15111 schaersvoo 56
/* to avoid easy js-code injection...but is it a real problem ? */
18553 bpr 57
        fprintf(js_include_file," \n/* add safe eval function */function safe_eval(exp){\
15111 schaersvoo 58
 exp = exp.replace(/pi/g,'3.14159');\
59
 if(exp.indexOf('^') != -1){\
60
  exp = exp.replace(/[a-zA-Z]/g,' ');\
61
  exp = exp.replace(/\\*10\\^-/g,'e-');\
62
  exp = exp.replace(/\\*10\\^/g,'e+');\
63
  exp = exp.replace(/10\\^-/g,'1e-');exp = exp.replace(/10\\^/g,'1e+');\
64
  exp = eval(exp);\
65
  if(isNaN(exp)){alert(\"invalid input\\ntry just a real number \\ne.g. no calculations...\");return null;}\
66
  return exp;\
67
 };\
68
 var reg = /(?:[a-z$_][a-z0-9$_]*)|(?:[;={}\\[\\]\"'!&<>^\\\\?:])/ig;\
69
 var valid = true;\
70
 exp = exp.replace(reg,function($0){\
71
  if(Math.hasOwnProperty($0)){\
72
   return \"Math.\"+$0;\
16844 bpr 73
  } else {\
15111 schaersvoo 74
   valid = false;\
75
  }\
76
 }\
77
 );\
78
 if( !valid ){\
79
  alert(\"hmmm \"+exp+\" ?\"); exp = null;\
16844 bpr 80
 } else {\
15111 schaersvoo 81
  try{ exp = eval(exp); } catch(e){alert(\"Invalid arithmetic expression\"); exp = null;};\
82
 };\
83
 return exp;\
84
};");
18553 bpr 85
        break;
86
      case JS_RAWMATH:
87
        fprintf(js_include_file,"\n/* add_rawmath() */\
15111 schaersvoo 88
function rawmath(i){\
89
 i=i.toLowerCase();\
90
 i=i.replace(/\\ /g,\"\");i=i.replace(/\\*\\*/g,\"^\");\
91
 if(i.indexOf(\"e+\")!=-1){i=i.replace(\"e+\",\"*10^\");};\
92
 if(i.indexOf(\"e-\")!=-1){i=i.replace(\"e-\",\"*10^-\");};\
93
 i=i.replace(/\\*\\*/g,\"*\");\
94
 if(i.charAt(0)==\"*\"){i=i.substring(1,i.length);};\
95
 var fun=[\"asin\",\"acos\",\"atan\",\"sin\",\"cos\",\"tan\",\"log\",\"ln\",\"pi\",\"e\",\"x\",\"y\"];\
96
 var cons=[\"pi\",\"e\",\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"];\
97
 var cntl = 0;var cntr = 0;\
98
 var len = i.length;\
99
 for( var p = 0;p < len; p++){\
100
  if(i.charAt(p) == '('){cntl++;}\
101
  if(i.charAt(p) == ')'){cntr++;}\
102
 };\
103
 if(cntl != cntr){alert(\"unmatched parenthesis !!\");return null;};\
104
 for(var p = 0; p < 12 ; p++){\
105
  for(var d = 0; d < 12 ; d++){\
106
   while(i.indexOf(cons[d]+\"\"+fun[p])!=-1){\
107
    i = i.replace(cons[d]+\"\"+fun[p],cons[d]+\"*\"+fun[p]);\
108
   };\
109
   while(i.indexOf(fun[p]+\"\"+cons[d])!=-1){\
110
    i = i.replace(fun[p]+\"\"+cons[d],fun[p]+\"*\"+cons[d]);\
111
   };\
112
  };\
113
 };\
114
 if(i.indexOf(\"(\")!=-1){\
115
  for(var p = 0;p < 12; p++){\
116
   if(i.indexOf(cons[p]+\"(\")!=-1){\
117
    i = i.replace(cons[p]+\"(\",cons[p]+\"*(\");\
118
   };\
119
   if(i.indexOf(\")\"+cons[p])!=-1){\
120
    i = i.replace(\")\"+cons[p],\")*\"+cons[p]);\
121
   };\
122
  };\
123
  i = i.replace(/\\)\\(/g,\")*(\");\
124
 };\
125
 return i;\
126
};");
18553 bpr 127
        break;
128
      case JS_PLOT:
129
        fprintf(js_include_file,"\n/* jsplot() */\
15111 schaersvoo 130
var x_anim_points;var y_anim_points;var animation_steps;var animation_funs;\
131
function jsplot(canvas_type,funs,linewidth,color,opacity,use_dashed,dashtype0,dashtype1,trange,plotsteps,use_parametric,use_animate,origin){\
132
 var obj = create_canvas%d(canvas_type,xsize,ysize);\
133
 var ctx = obj.getContext(\"2d\");\
134
 ctx.clearRect(0,0,xsize,ysize);\
135
 animation_funs = funs.length;\
136
 function eval_jsmath(x,func){return parseFloat(eval(func));};\
137
 if(origin == 0 ){\
138
  if( typeof(multilinewidth) !== 'undefined' && multilinewidth != null ){ linewidth = multilinewidth;};\
139
  if( typeof(multistrokecolors) !== 'undefined' && multistrokecolors != null ){ color = multistrokecolors;};\
140
  if( typeof(multistrokeopacity) !== 'undefined' && multistrokeopacity != null ){ opacity = multistrokeopacity;};\
141
  if( typeof(multidash) !== 'undefined' && multidash != null ){use_dashed = multidash;};\
142
 };\
143
 x_anim_points = [];y_anim_points = [];var idx=0;\
144
 if( use_parametric == 1){\
145
  for(var i = 0 ; i < animation_funs; i=i+2){\
15351 bpr 146
   funs[i] = funs[i].replace(/t/g,'x');\
147
   funs[i+1] = funs[i+1].replace(/t/g,'x');\
148
   funs[i] = funs[i].replace(/xan/g,'tan');\
149
   funs[i+1] = funs[i+1].replace(/xan/g,'tan');\
15111 schaersvoo 150
   var fun_x = to_js_math(funs[i]);\
151
   var fun_y = to_js_math(funs[i+1]);\
152
   if(fun_x == null || fun_y == null){alert(\"Syntax Error...\\nAttention : try use very precise notation !\\nlike :\\n6*(0.25)^(1.23)\\n1/(sin(5*x))\\n(3*x+4)/(x^(2*pi)) \");return;};\
153
   try{ parseFloat( eval_jsmath( px2x(0),fun_x ) );}catch(e){alert(\"\\nSyntax Error...\\nAttention : try use very precise notation !\\nlike :\\n6*(0.25)^(1.23)\\n1/(sin(5*x))\\n(3*x+4)/(x^(2*pi))\");return;};\
154
   ctx.lineWidth = linewidth[i] || linewidth;\
155
   ctx.strokeStyle='rgba('+color[i] || color +','+opacity[i] || opacity +')';\
156
   if(use_dashed[i] == \"1\" || use_dashed == \"1\"){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{ctx.mozDash =[dashtype0,dashtype1];}};\
157
   var y1;var x1;var y2;var x2;\
158
   ctx.beginPath();\
159
   var tmin = trange[0];var tmax = trange[1];\
160
   var step = parseFloat((tmax - tmin)/plotsteps);\
161
   for(var p = tmin ; p < tmax; p = p+step ){\
162
    x1 = x2px(parseFloat(eval_jsmath(p,fun_x)));\
163
    y1 = y2px(parseFloat(eval_jsmath(p,fun_y)));\
164
    x2 = x2px(parseFloat(eval_jsmath(p+step,fun_x)));\
165
    y2 = y2px(parseFloat(eval_jsmath(p+step,fun_y)));\
166
    x_anim_points[idx] = x1;y_anim_points[idx] = y1;idx++;\
167
     ctx.moveTo(x1,y1);\
168
     ctx.lineTo(x2,y2);\
169
     ctx.moveTo(x1,y1);\
170
     ctx.lineTo(x2,y2);\
171
   };\
172
   ctx.closePath();\
173
   ctx.stroke();\
174
  };\
175
  animation_funs = 0.5*animation_funs;\
176
 }else{\
177
  for(var i = 0 ; i < animation_funs; i++){\
178
   var fun = to_js_math(funs[i]);\
179
   if(fun == null){alert(\"Syntax Error...\\nAttention : try use very precise notation !\\nlike :\\n6*(0.25)^(1.23)\\n1/(sin(5*x))\\n(3*x+4)/(x^(2*pi)) \");return;};\
180
   try{ parseFloat( eval_jsmath( px2x(0),fun ) );}catch(e){alert(\"\\nSyntax Error...\\nAttention : try use very precise notation !\\nlike :\\n6*(0.25)^(1.23)\\n1/(sin(5*x))\\n(3*x+4)/(x^(2*pi))\");return;};\
181
   if(use_dashed[i] == \"1\" || use_dashed == \"1\"){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{ctx.mozDash =[dashtype0,dashtype1];}};\
182
   ctx.lineWidth = linewidth[i] || linewidth;\
183
   ctx.strokeStyle='rgba('+color[i] || color +','+opacity[i] || opacity +')';\
184
   var y1;var x1;var y2;var x2;\
185
   ctx.beginPath();\
186
   for(var p = 0 ; p<xsize;p++){\
187
    x1 = px2x(p);\
188
    y1 = y2px(parseFloat(eval_jsmath(x1,fun)));\
189
    x2 = px2x(p+1);\
190
    y2 = y2px(parseFloat(eval_jsmath(x2,fun)));\
191
    x_anim_points[idx] = p;y_anim_points[idx] = y1;idx++;\
192
    if(Math.abs(y2-y1) < ysize ){\
193
     ctx.moveTo(p,y1);\
194
     ctx.lineTo(p+1,y2);\
195
    };\
196
   };\
197
   ctx.closePath();\
198
   ctx.stroke();\
199
  };\
200
 };\
201
 if( use_animate == 1 ){animation_steps = idx;animate_this();}\
202
};",canvas_root_id);
18553 bpr 203
        break;
204
      case JS_MATH:
18218 schaersvoo 205
/* 5/2024 added exp -> e^ -> (2.718281828459045)^ */
18553 bpr 206
        fprintf(js_include_file,"\n/* to_js_math() */\
15111 schaersvoo 207
function to_js_math(math_fun){\
208
 if(math_fun == null){return;};\
18218 schaersvoo 209
 var infun=[\"exp\",\"sqrt\",\"^\",\"asin\",\"acos\",\"atan\",\"log\",\"pi\",\"abs\",\"sin\",\"cos\",\"tan\",\"e\"];\
210
 var outfun=[\"(2.718281828459045)^\",\"Math.sqrt\",\"Math.pow\",\"Math.asin\",\"Math.acos\",\"Math.atan\",\"Math.log\",\"(3.14159265358979)\",\"Math.abs\",\"Math.sin\",\"Math.cos\",\"Math.tan\",\"(2.718281828459045)\"];\
15111 schaersvoo 211
 var len = infun.length;var in_fun;var In_Fun;var out_fun;var w_cnt;\
212
 for(var p=0 ; p < len ; p++){\
18218 schaersvoo 213
  in_fun = infun[p];   In_Fun = in_fun.toUpperCase();out_fun = outfun[p];w_cnt=0;\
15111 schaersvoo 214
  if(math_fun.indexOf(in_fun) != -1){\
215
   if(in_fun == \"^\"){\
216
    var tab = [];var small_trick = \"___small_trick___\";\
217
    while (math_fun.indexOf(\"(\") != -1){\
218
     math_fun = math_fun.replace(/(\\([^\\(\\)]*\\))/g, function(m, t){tab.push(t);return (small_trick + (tab.length - 1));});\
219
     w_cnt++;if(w_cnt>1000){alert(\"hmmm \"+math_fun+\" ?\\nUse command plot for more complex math functions...\");return null;};\
220
    };\
221
    tab.push(math_fun);w_cnt = 0;math_fun = small_trick + (tab.length - 1);\
222
    while (math_fun.indexOf(small_trick) != -1){\
223
     math_fun = math_fun.replace(new RegExp(small_trick + \"(\\\\d+)\", \"g\"), function(m, d){return tab[d].replace(/(\\w*)\\^(\\w*)/g, out_fun+\"($1,$2)\");});\
224
     w_cnt++;if(w_cnt>1000){alert(\"hmmm \"+math_fun+\" ?\\nUse command plot for more complex math functions...\");return null;};\
225
    };\
16844 bpr 226
   } else {\
15111 schaersvoo 227
    while( math_fun.indexOf(in_fun) != -1 ){\
228
     math_fun = math_fun.replace(in_fun,out_fun);\
229
     math_fun = math_fun.replace(in_fun,In_Fun);\
230
     w_cnt++;if(w_cnt>1000){alert(\"hmmm \"+math_fun+\" ?\\nUse command plot for more complex math functions...\");return null;};\
231
    };\
232
   };\
233
  };\
234
 };\
235
 for(var p=0 ; p < len ; p++){\
236
  in_fun = infun[p];In_Fun = in_fun.toUpperCase();w_cnt = 0;\
237
  if(math_fun.indexOf(In_Fun) != -1 ){\
238
   while(math_fun.indexOf(In_Fun) != -1){\
239
    math_fun = math_fun.replace(In_Fun,in_fun);\
240
    w_cnt++;if(w_cnt>1000){alert(\"hmmm \"+math_fun+\" ?\\nUse command plot for more complex math functions...\");return null;};\
241
   };\
242
  };\
243
 };\
244
 return math_fun;\
245
};");
18553 bpr 246
        break;
247
      case INTERACTIVE:
17351 bpr 248
/* 1/8/2022 NOTE : getMouse() code should run at maximum speed and not be bothered with all kind of fancy tests !!
16943 schaersvoo 249
  1/8/2022 : latest improvements related to 'reponsiveness' will fatally lockup browsers in the pixel-by-pixel filling...at least in userdraw clickfill...*/
18553 bpr 250
        fprintf(js_include_file,"\n/* add mouse support */\
16845 bpr 251
var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0));\
252
var getMouse = function(e, element){\
253
   var mx,my;var offsetX = 0,offsetY = 0;\
254
   while( ( element = element.offsetParent) ){\
255
     offsetX += element.offsetLeft;offsetY += element.offsetTop;\
256
   };\
257
   /*console.log('===');*/\
258
   if(isTouch){\
259
      mx = e.pageX - offsetX; my = e.pageY - offsetY;\
260
   }else{\
261
      mx = e.clientX - offsetX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft:document.body.scrollLeft);\
262
      my = e.clientY - offsetY + (document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body.scrollTop);\
263
   };\
16942 schaersvoo 264
   if( typeof(clickfill) === 'function'){ return {x: mx, y: my};}else{\
16845 bpr 265
   /* test if canvas_div is inside a scrollable */\
266
   const scroll_parent = canvas_div.closest('.wims_scrollable');\
267
   if (scroll_parent){\
268
      mx = mx + scroll_parent.scrollLeft;\
269
      my = my + scroll_parent.scrollTop;\
270
   }\
271
   /* If canvas had been resized,, we have to convert these coordinates to original grid*/\
272
   /*console.log('mx before=' + mx);\
273
   console.log('xsize='+xsize);\
274
   console.log('canvas_div.offsetHeight='+canvas_div.offsetWidth);*/\
275
   mx = (mx * xsize / canvas_div.offsetWidth);\
276
   /*console.log('mx after='+mx);*/\
277
   my = (my * ysize / canvas_div.offsetHeight);\
16942 schaersvoo 278
   return {x: mx, y: my};};\
16845 bpr 279
};");
18553 bpr 280
        break;
281
      case USERDRAW_AND_ZOOM:
282
        fprintf(js_include_file,"\n/* add zoom support for multidraw */\
15111 schaersvoo 283
function recalculate_userdraw(xmin0,ymin0,xmax0,ymax0){\
284
 function x_zoom(px){\
285
  var x = parseFloat(px*(xmax0 - xmin0)/xsize + xmin0);\
286
  return parseFloat(x*xsize/(xmax - xmin) - xsize*xmin/(xmax - xmin));\
287
 };\
288
 function y_zoom(py){\
289
  var y = parseFloat(ymax0 - py*(ymax0 - ymin0)/ysize);\
290
  return parseFloat(-1*y*ysize/(ymax - ymin) + ymax*ysize/(ymax - ymin));\
291
 };\
292
 function r_zoom(r){\
293
  return r*(xmin0/xmin);\
294
 };\
295
 var len = userdraw_x.length;\
296
 var x;var y;var r;\
297
 for(var p=0;p<len;p++){\
298
  x = userdraw_x[p];\
299
  y = userdraw_y[p];\
300
  userdraw_x[p] = x_zoom(x);\
301
  userdraw_y[p] = y_zoom(y);\
302
  if( userdraw_radius[p] ){\
303
   r = userdraw_radius[p];\
304
   userdraw_radius[p] = r_zoom(r);\
305
  };\
306
 };\
307
};");
308
break;
309
 
310
case DRAG_AND_ZOOM:
311
fprintf(js_include_file,"\n/* add zoom support for drag&drop */\
312
function recalculate_dragdrop(xmin0,ymin0,xmax0,ymax0){\
313
 function x_zoom(px){\
314
  var x = parseFloat(px*(xmax0 - xmin0)/xsize + xmin0);\
315
  return parseFloat(x*xsize/(xmax - xmin) - xsize*xmin/(xmax - xmin));\
316
 };\
317
 function y_zoom(py){\
318
  var y = parseFloat(ymax0 - py*(ymax0 - ymin0)/ysize);\
319
  return parseFloat(-1*y*ysize/(ymax - ymin) + ymax*ysize/(ymax - ymin));\
320
 };\
321
 var len = dragstuff.shapes.length;\
322
 var shape;var L;var x;var y;\
323
 for(var p=0;p<len;p++){\
324
  shape = dragstuff.shapes[p];\
325
  L = shape.x.length;\
326
  for(var i=0;i<L;i++){\
327
   x = shape.x[i];\
328
   y = shape.y[i];\
329
   dragstuff.shapes[p].x[i] = x_zoom(x);\
330
   dragstuff.shapes[p].y[i] = y_zoom(y);\
331
  };\
332
 };\
333
};");
334
break;
335
 
336
case JSPLOT_AND_ZOOM:
337
fprintf(js_include_file,"\n/* add zoom support for jscurves */\
338
function redraw_jsplot(){for(var p=0; p <= all_jsplots;p++){try{var fun = eval('js_plot'+p);fun();}catch(e){};};};");
339
break;
340
 
341
case JS_ZOOM:
342
fprintf(js_include_file,"\n/* add zoom support */\
343
var x_min_start = xmin;var x_max_start = xmax;var y_min_start = ymin;var y_max_start = ymax;\
344
var zoomcanvas = create_canvas%d(%d,xsize,ysize);\
345
function redraw_canvas%d(key){\
346
 var xmin0 = xmin;var xmax0 = xmax;var ymin0 = ymin;var ymax0 = ymax;\
347
 switch(key){\
348
  case 0: xmin++;ymin++;xmax--;ymax--;break;\
349
  case 1: xmin--;ymin--;xmax++;ymax++;break;\
350
  case 2: xmin--;xmax--;break;\
351
  case 3: xmin++;xmax++;break;\
352
  case 4: ymin--;ymax--;break;\
353
  case 5: ymax++;ymin++;break;\
354
  case 6: xmin = x_min_start;xmax=x_max_start;ymin=y_min_start;ymax=y_max_start;break;\
355
  default: break;\
356
 };\
357
 if(xmin >= xmax){xmin = x_min_start;xmax = x_max_start;};\
358
 if(ymin >= ymax){ymax = y_max_start;ymin = y_min_start;};\
359
 if( typeof(recalculate_multidraw) === 'function'){recalculate_multidraw(xmin0,xmax0,ymin0,ymax0);redraw_userdraw();}\
360
 if( typeof(recalculate_userdraw) === 'function'){recalculate_userdraw(xmin0,ymin0,xmax0,ymax0);redraw_userdraw();}\
361
 if( typeof(redraw_grid) === 'function'){redraw_grid();};\
362
 if( typeof(redraw_jsplot) === 'function'){redraw_jsplot();};\
363
 if( typeof(recalculate_dragdrop) === 'function'){recalculate_dragdrop(xmin0,ymin0,xmax0,ymax0);dragstuff.valid = false;dragstuff.draw();};\
364
 return;\
365
};\
366
canvas_div.addEventListener(\"mousedown\",check_zoom_or_pan,false);\
367
canvas_div.addEventListener(\"wheel\",wheel_zoom,false);\
368
canvas_div.addEventListener(\"touchstart\",function(e){ e.preventDefault();check_zoom_or_pan(e.changedTouches[0]);},false);\
369
function wheel_zoom(evt){\
370
 evt.preventDefault();\
371
 var key = 1;\
372
 if(evt.deltaY > 0 ){key = 0};\
373
 redraw_canvas%d(key);\
374
 return;\
375
};\
376
function check_zoom_or_pan(evt){\
377
 var mouse = getMouse(evt,zoomcanvas);\
378
 var my = mouse.y;\
379
 if(my < ysize - 30){ return; };\
380
 var mx = mouse.x;\
381
 var key = -1;\
382
 for(var p = 15 ; p < 106 ; p = p+15){\
383
  key++;\
384
  if(mx > xsize - p){\
385
   redraw_canvas%d(key);\
386
   return;\
387
  };\
388
 };\
389
 return;\
390
};",canvas_root_id,ZOOM_CANVAS,canvas_root_id,canvas_root_id,canvas_root_id);
391
break;
392
 
393
case DRAW_FILL_PATTERN:
394
fprintf(js_include_file,"\n /* add fill pattern */ \
395
function create_Pattern(x0,y0,type,color){document.body.style.cursor = 'wait';var idx = Math.ceil(1000*(Math.random()));var canvas = create_canvas%d(idx,xsize,ysize);canvas.style.visibility = 'hidden';var ctx = canvas.getContext(\"2d\");var x;var y;var dx = 6;var dy = 6;ctx.lineWidth = 1;ctx.strokeStyle = color;ctx.fillStyle = color;var pat;\
396
 switch(type){\
397
  case 2: for( x = 0 ; x < xsize ; x = x + dx ){ctx.beginPath();ctx.moveTo(x,0);ctx.lineTo(x,ysize);ctx.closePath();ctx.stroke();};for( y = 0 ; y < ysize; y = y + dy ){ctx.beginPath();ctx.moveTo(0,y);ctx.lineTo(xsize,y);ctx.closePath();ctx.stroke();}; pat = ctx.createPattern(canvas,\"no-repeat\");break;\
398
  case 3: y = ysize;ctx.beginPath();for( x = 0 ; x < xsize ; x = x + dx ){ctx.moveTo(x,0);ctx.lineTo(xsize,y);y = y - dy;};y = 0;for( x = xsize ; x > 0 ; x = x - dx){ctx.moveTo(x,ysize);ctx.lineTo(0,y); y = y + dy;};ctx.closePath();ctx.stroke(); pat = ctx.createPattern(canvas,\"no-repeat\");break;\
399
  case 4: y = ysize;ctx.beginPath();for( x = 0 ; x < xsize ; x = x + dx ){ctx.moveTo(x,0); ctx.lineTo(xsize,y); y = y - dy;};y = 0;for( x = xsize ; x > 0 ; x = x - dx){ctx.moveTo(x,ysize);ctx.lineTo(0,y);y = y + dy;};y = 0;for( x = 0 ; x < xsize ; x = x + dx ){ctx.moveTo(x,0); ctx.lineTo(0,y); y = y + dy;};y = 0;for( x = 0 ; x < xsize ; x = x + dx ){ctx.moveTo(xsize,y);ctx.lineTo(x,ysize);y = y + dy;};ctx.closePath(); ctx.stroke(); pat = ctx.createPattern(canvas,\"no-repeat\");break;\
400
  case 5: for( x = 0 ; x < xsize ; x = x + dx ){for( y = 0 ; y < ysize ; y = y + dy ){ctx.beginPath();ctx.arc(x,y,1,0,2*Math.PI,false);ctx.closePath();ctx.fill();};}; pat = ctx.createPattern(canvas,\"no-repeat\");break;\
401
  case 6: alert('wait for image '+(image_cnt+1)+'...');pat = image_patterns[image_cnt];image_cnt++;break;\
402
  default: break;\
403
 };\
404
 document.body.style.cursor = 'default';return pat;\
405
};",canvas_root_id);
406
break;
407
 
408
case JS_TRANSFORM_MOUSE:
409
/* identify mouse coordinates on transformed objects using command affine */
410
fprintf(js_include_file,"\n/* translate mouse */\
411
function transform_mouse(mx,my,obj){\
412
if( typeof(obj.affine_matrix) === 'undefined' ){console.log('translate_mouse() affine_matrix undefined');{x:mx,y:my};};\
413
var m = obj.affine_matrix;\
414
var d = 1/(m[0]*m[3]-m[1]*m[2]);\
415
var im = [ m[3]*d, -m[1]*d,-m[2]*d,m[0]*d,d*(m[2]*m[5]-m[3]*m[4]),d*(m[1]*m[4]-m[0]*m[5]) ];\
416
return { x:mx*im[0]+my*im[2]+im[4], y:mx*im[1]+my*im[3]+im[5] };};");
417
break;
418
 
419
case JS_ROTATE_MOUSE:
420
/*
421
identify mouse coordinates on rotated STRING (no 14) objects used for command rotate :
422
faster than transform matrix
423
*/
424
fprintf(js_include_file,"\n/* rotate mouse */\
425
function rotate_mouse(mx,my,obj){\
426
 if( typeof(obj.angle) === 'undefined' ){console.log('rotate_mouse() angle undefined');return {x:mx,y:my};};\
427
 var cos = Math.cos(obj.angle);\
428
 var sin = Math.sin(obj.angle);\
429
 var xc = obj.rotation_center[0];\
430
 var yc = obj.rotation_center[1];\
431
 var x_r = parseInt((cos * (mx - xc)) + (sin * (my - yc)) + xc);\
432
 var y_r = parseInt((cos * (my - yc)) - (sin * (mx - xc)) + yc);\
433
 return {x:x_r,y:y_r};};");
434
break;
435
 
436
case JS_FIND_ANGLE:
437
 fprintf(js_include_file,"\n/* function find_angle() */ function find_angle(xc,yc,x1,y1){var dx = x1 - xc;var dy = yc - y1;return Math.atan2(dx,dy);};");
438
break;
439
 
440
case DRAG_EXTERNAL_IMAGE:
441
/*
442
the external_canvas is already created: it needs to be FIRST in order to do some drawing onto it
443
reply is object{} and identical to reply from dragstuff library for drag&drop&click
444
*/
445
fprintf(js_include_file,"\n/* drag external images */\
446
var external_ctx = external_canvas.getContext(\"2d\");\
447
var external_canvas_rect = external_canvas.getBoundingClientRect();\
448
canvas_div.addEventListener(\"mousedown\",setxy,false);\
449
canvas_div.addEventListener(\"mouseup\",dragstop,false);\
450
canvas_div.addEventListener(\"mousemove\",dragxy,false);\
451
canvas_div.addEventListener(\"touchstart\",function(e){ e.preventDefault();setxy(e.changedTouches[0]);},false);\
452
canvas_div.addEventListener(\"touchend\"  ,function(e){ e.preventDefault();dragstop(e.changedTouches[0]);},false);\
453
canvas_div.addEventListener(\"touchmove\" ,function(e){ e.preventDefault();dragxy(e.changedTouches[0]);},false);\
454
var selected_image = null;\
455
if( typeof(ext_drag_images) !== 'object' ){var ext_drag_images = new Array();};\
456
if( typeof(ext_image_cnt) == 'undefined'){var ext_image_cnt = 0;};\
17518 schaersvoo 457
if( typeof(slidergroup) == 'undefined' ) { var slidergroup = [];};\
15111 schaersvoo 458
function drag_external_image(URL,sx,sy,swidth,sheight,x0,y0,width,height,onclick,object_cnt,use_offset,use_snap,resize){\
459
 var image = new Image();\
460
 image.src = URL;\
461
 slidergroup[object_cnt] = null;\
462
 image.onload = function(){\
463
  if( sx < 1 ){ sx = 0; };\
464
  if( sy < 1 ){ sy = 0; };\
465
  if( swidth < 1 ){swidth = image.width;};\
466
  if( sheight < 1 ){sheight = image.height;};\
467
  if( width < 1 ){width = image.width;};\
468
  if( height < 1 ){height = image.height;};\
469
  if( resize == 1 ){\
470
   if( swidth > image.width ){ swidth = image.width; };\
471
   if( sheight > image.height){ sheight = image.height;};\
472
   if( width > image.width ){ width = image.width; };\
473
   if( height > image.height){ height = image.height;};\
474
  };\
475
  var img = new Array(18);\
476
  img[0] = onclick;img[1] = image;img[2] = sx;img[3] = sy;img[4] = swidth;img[5] = sheight;\
477
  img[8] = width;img[9] = height;img[10] = object_cnt;img[11] = use_snap;\
478
  img[6] = x0;img[7] = y0;\
479
  img[12] = ext_image_cnt;\
480
  img[13] = img[6];img[14] = img[7];img[15] = 0;img[16] = 0;img[17] = 0;\
481
  if(use_offset == 4 ){\
482
   img[16] = 0.5*img[8];\
483
   img[17] = 0.5*img[9];\
484
   img[6]= parseInt(img[6] - img[16]);\
485
   img[7]= parseInt(img[7] - img[17]);\
486
  };\
487
  external_ctx.drawImage(img[1],img[2],img[3],img[4],img[5],img[6],img[7],img[8],img[9]);\
16847 schaersvoo 488
  dragdrop_reply[img[10]] = {type:img[0],object_cnt:img[10],x:x0,y:y0,r:0,angle:0,clicked:img[15]};\
15111 schaersvoo 489
  ext_drag_images[ext_image_cnt] = img;\
490
  ext_image_cnt++;\
491
 };\
492
};\
493
function dragstop(evt){\
494
 if(selected_image != null){\
495
  var s_img = ext_drag_images[selected_image];\
16847 schaersvoo 496
  dragdrop_reply[s_img[10]] = {type:s_img[0], object_cnt:s_img[10],x:s_img[6]+s_img[16],y:s_img[7]+s_img[17],r:s_img[8],angle:s_img[15],clicked:s_img[15]};\
15111 schaersvoo 497
 };\
498
 selected_image = null;return;\
499
};\
500
function dragxy(evt){\
501
 if( selected_image != null ){\
502
  var s_img = ext_drag_images[selected_image];\
503
  var mouse = getMouse(evt,external_canvas);\
504
  var xy = multisnap_check(mouse.x,mouse.y,s_img[11]);\
505
  xy[0] = parseInt(xy[0] - 0.5*s_img[8]);\
506
  xy[1] = parseInt(xy[1] - 0.5*s_img[9]);\
507
  s_img[6] = xy[0];s_img[7] = xy[1];\
508
  ext_drag_images[selected_image] = s_img;\
509
  external_ctx.clearRect(0,0,xsize,ysize);\
510
  for(var i = 0; i < ext_image_cnt ; i++){\
511
   var img = ext_drag_images[i];\
512
   external_ctx.drawImage(img[1],img[2],img[3],img[4],img[5],img[6],img[7],img[8],img[9]);\
513
  };\
514
 };\
515
};\
516
function setxy(evt){\
517
 if( ! selected_image ){\
518
  var mouse = getMouse(evt,external_canvas);\
519
  var xm = mouse.x;\
520
  var ym = mouse.y;\
521
  var img;var xmarge;var ymarge;\
522
  for(var p = 0 ; p < ext_image_cnt ; p++){\
523
   if( ext_drag_images[p] ){\
524
    img = ext_drag_images[p];\
525
    if( img[0] != 0 ){\
526
     xmarge = 0.5*img[8];ymarge = 0.5*img[9];\
527
     if( xm > img[6] - xmarge && xm < img[6] + img[8]){\
528
      if( ym > img[7] - ymarge && ym < img[7] + img[9]){\
529
       if( img[0] == 1){\
530
        if( dragdrop_reply[img[10]].clicked == 1 ){\
531
         dragdrop_reply[img[10]].clicked = 0;external_ctx.strokeStyle = '#ffffff';\
532
        }\
533
        else\
534
        {\
535
         dragdrop_reply[img[10]].clicked = 1;external_ctx.strokeStyle = '#00ff00';\
536
        };\
537
        external_ctx.lineWidth = 2;\
538
        external_ctx.beginPath();\
539
        external_ctx.rect(img[6]-2,img[7]-2,img[8]+2,img[9]+2);\
540
        external_ctx.closePath();\
541
        external_ctx.stroke();\
542
        return;\
543
       }\
544
       else\
545
       {\
546
        img[6] = xm;\
547
        img[7] = ym;\
548
        selected_image = p;\
549
        dragxy(evt);\
550
        ext_drag_images[p] = img;\
551
       };\
552
      };\
553
     };\
554
    };\
555
   };\
556
  };\
16844 bpr 557
 } else {\
15111 schaersvoo 558
  selected_image = null;\
559
 };\
560
};");
561
break;
562
 
563
case DRAW_EXTERNAL_IMAGE:
564
/*
565
the external_canvas is already created: it needs to be FIRST in order to do some drawing onto it
566
only drag_xy !
567
snaptogrid | xsnaptogrid | ysnaptogrid works
568
14/5/2019
569
on heavy styled wims (unice.fr etc problems with mouse?
570
now uniform method of retreiving mouse coordinates getMouse()
571
*/
572
fprintf(js_include_file,"\n/* draw external images */\
573
function draw_external_image(URL,sx,sy,swidth,sheight,x0,y0,width,height,use_offset,resize){\
574
 var external_ctx = image_copy_canvas.getContext(\"2d\");\n\
575
 var image = new Image();\
576
 image.src = URL;\
577
 image.onload = function(){\
578
  if( sx < 1 ){ sx = 0; };\
579
  if( sy < 1 ){ sy = 0; };\
580
  if( swidth < 1 ){swidth = image.width;};\
581
  if( sheight < 1 ){sheight = image.height;};\
582
  if( width < 1 ){width = image.width;};\
583
  if( height < 1 ){height = image.height;};\
584
  if( resize == 1 ){\
585
   if( swidth > image.width ){ swidth = image.width; };\
586
   if( sheight > image.height){ sheight = image.height;};\
587
   if( width > image.width ){ width = image.width; };\
588
   if( height > image.height){ height = image.height;};\
589
  };\
590
  var img = new Array(9);\
591
  img[0] = image;img[1] = sx;img[2] = sy;img[3] = swidth;img[4] = sheight;img[5] = x0;img[6] = y0;\
592
  if(use_offset == 4 ){img[5] = parseInt(img[5] - 0.5*width);img[6] = parseInt(img[6] - 0.5*height);};\
593
  img[7] = width;img[8] = height;\
594
  external_ctx.drawImage(img[0],img[1],img[2],img[3],img[4],img[5],img[6],img[7],img[8]);\
595
 };\
596
};");
597
break;
598
 
599
case DRAW_BEZIER:
600
fprintf(js_include_file,"\n/* draw bezier curve */\
601
if( typeof(all_fill_patterns) != 'object' ){ var all_fill_patterns = []; };\
602
function draw_bezier(canvas_type,linewidth,xy_points,fill_color,fill_opacity,stroke_color,stroke_opacity,use_filled,use_dashed,dashtype0,dashtype1,use_rotate,angle){\
603
 var obj;\
604
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
605
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 606
 } else {\
15111 schaersvoo 607
  obj = create_canvas%d(canvas_type,xsize,ysize);\
608
 };\
609
 var ctx = obj.getContext(\"2d\");\
610
 ctx.save();\
611
 ctx.strokeStyle=\"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
612
 ctx.lineWidth = linewidth;\
613
 if(use_rotate == 1 ){ctx.rotate(angle*Math.PI/180);};\
614
 if(use_dashed == 1){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{ctx.mozDash = [dashtype0,dashtype1];};};\
615
 ctx.beginPath();\
616
 ctx.moveTo(x2px(xy_points[0]),y2px(xy_points[1]));\
617
 ctx.bezierCurveTo(x2px(xy_points[2]),y2px(xy_points[3]),x2px(xy_points[4]),y2px(xy_points[5]),x2px(xy_points[6]),y2px(xy_points[7]));\
618
 var color = \"rgba(\"+fill_color+\",\"+fill_opacity+\")\";\
619
 if(use_filled > 1 ){ if(! all_fill_patterns[use_filled] ){ var pat = create_Pattern(0,0,use_filled,color); all_fill_patterns[use_filled] = pat;};ctx.fillStyle = all_fill_patterns[use_filled]; } else { ctx.fillStyle = color;};\
620
 ctx.stroke();\
621
 ctx.restore();\
622
};",canvas_root_id,canvas_root_id,canvas_root_id);
623
break;
624
 
625
case DRAW_GRIDFILL:/* not used for userdraw */
626
fprintf(js_include_file,"\n/* draw gridfill */\
627
var grid_fill_pattern;\
628
function draw_gridfill(canvas_type,x0,y0,dx,dy,linewidth,color,opacity,xsize,ysize,use_userdraw){\
629
 if( dx == 0 || dy == 0 ){alert(\"increment is zero !!! \");return;};\
630
 var fc = %d+canvas_type;\
631
 var obj = create_canvas%d(fc,xsize,ysize);\
632
 var ctx = obj.getContext('2d');\
633
 var x,y;\
634
 ctx.fillStyle='rgba(255,255,255,0.01)';\
635
 ctx.rect(0,0,xsize,ysize);\
636
 ctx.fill();\
637
 ctx.lineWidth = linewidth;\
638
 ctx.strokeStyle=\"rgba(\"+color+\",0.01)\";\
639
 for( x = 0 ; x <= xsize ; x = x + dx ){\
640
  ctx.beginPath();\
641
  ctx.moveTo(x,0);\
642
  ctx.lineTo(x,ysize);\
643
  ctx.closePath();\
644
  ctx.stroke();\
645
 };\
646
 for( y = 0 ; y <= ysize; y = y + dy ){\
647
  ctx.beginPath();\
648
  ctx.moveTo(0,y);\
649
  ctx.lineTo(xsize,y);\
650
  ctx.closePath();\
651
  ctx.stroke();\
652
 };\
653
 if( use_userdraw ){\
654
  grid_fill_pattern = ctx;\
16844 bpr 655
 } else {\
15111 schaersvoo 656
  setTimeout(function(){ filltoborder( x0,y0,color,color,canvas_type,true,ctx); },500);};return;\
657
};",canvas_root_id,canvas_root_id);
658
break;
659
 
660
case DRAW_IMAGEFILL:/* not  used for userdraw */
661
fprintf(js_include_file,"\n/* draw imagefill */\
662
function draw_imagefill(canvas_type,x0,y0,URL,xsize,ysize,use_userdraw,use_scaling){\
663
 var fc = %d+canvas_type;\
664
 var obj = create_canvas%d(fc,xsize,ysize);\
665
 var ctx = obj.getContext('2d');\
666
 var img = new Image();\
667
 img.src = URL;\
668
 obj.style.visibility = 'hidden';\
669
 img.onload = function(){\
670
  if( use_scaling == 1 ){\
671
   ctx.drawImage(img,x0,y0,xsize,ysize);\
672
  }else{\
673
   var w0 = img.width;var h0 = img.height;var w;var h;\
674
   for( w = x0; w < xsize ; w = w + w0 ){\
675
    for( h = y0; h < ysize; h = h + h0){\
676
      ctx.drawImage(img,w,h,w0,h0);\
677
    };\
678
   };\
679
  };\
680
  if( use_userdraw ){\
681
   image_pattern = ctx;\
16844 bpr 682
  } else {\
15111 schaersvoo 683
   setTimeout(function(){ filltoborder( x0,y0,'red','red',canvas_type,true,ctx); },500);\
684
  };\
685
 };\
686
};",canvas_root_id,canvas_root_id);
18553 bpr 687
        break;
15111 schaersvoo 688
 
18553 bpr 689
      case DRAW_DOTFILL:/* not  used for userdraw */
690
        fprintf(js_include_file,"\n/* draw dotfill */\
15111 schaersvoo 691
var dot_fill_pattern;\
692
function draw_dotfill(canvas_type,x0,y0,dx,dy,radius,color,opacity,xsize,ysize,use_userdraw){\
693
if( dx == 0 || dy == 0 ){alert(\"increment is zero !!! \");return;};\
694
var fc = %d+canvas_type;\
695
 var obj = create_canvas%d(fc,xsize,ysize);\
696
 var ctx = obj.getContext('2d');\
697
 var x,y;\
698
 ctx.fillStyle='rgba(255,255,255,0.01)';\
699
 ctx.rect(0,0,xsize,ysize);\
700
 ctx.fill();\
701
 ctx.fillStyle=\"rgba(\"+color+\",0.01)\";\
702
 ctx.strokeStyle=\"rgba(\"+color+\",0.01)\";\
703
 for( x = 0 ; x < xsize ; x = x + dx ){\
704
  for( y = 0 ; y < ysize ; y = y + dy ){\
705
   ctx.beginPath();\
706
   ctx.arc(x,y,radius,0,2*Math.PI,false);\
707
   ctx.closePath();\
708
   ctx.fill();\
709
  };\
710
 };\
711
 if( use_userdraw ){\
712
  dot_fill_pattern = ctx;\
16844 bpr 713
 } else {\
15111 schaersvoo 714
 setTimeout(function(){ filltoborder( x0,y0,color,color,canvas_type,true,ctx); },500);\
715
 };\
716
return;\
717
};",canvas_root_id,canvas_root_id);
18553 bpr 718
        break;
719
      case DRAW_SUBSUP:/* Uses 'script-size for numbers and for sub & sup strings */
16722 schaersvoo 720
/* use_offset
721
 0=none;
722
 1=yoffset
723
 2=xoffset
724
 3=xyoffset
725
 4=centered
726
*/
18553 bpr 727
        fprintf(js_include_file,"\n/* draw sub sup imitation in canvas */\
16722 schaersvoo 728
function draw_subsup(ctx,x,y,txt,use_offset){\
16712 schaersvoo 729
 var C;var c;var len = txt.length;var baseline = 'middle';var space = 0;ctx.save();var subsup=0;\
16722 schaersvoo 730
 var m = 0.4*(ctx.measureText(txt).width);\
731
 var h = ctx.measureText('M').width;\
732
 switch(use_offset){\
733
  case 0:break;\
17586 schaersvoo 734
  case 1:y=y-h;break;\
16722 schaersvoo 735
  case 2:x=x+h;break;\
17586 schaersvoo 736
  case 3:x=x+h;y=y-h;break;\
16722 schaersvoo 737
  case 4:x=x-m;break;\
738
  default:break;\
739
 };\
17586 schaersvoo 740
 var alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\
741
 function check_alpha(C){if(alphabet.indexOf(C) != -1){return 1;}else{return 0;};};\
16845 bpr 742
 var S = parseInt(ctx.font) || 10;var s = 0.8*(parseInt(ctx.font)) || 8;\
743
 ctx.font = S+'px Helvetica';ctx.textBaseline = baseline;\
16705 schaersvoo 744
 for( c = 0 ; c < len ;c++){\
16700 schaersvoo 745
  C = txt.charAt(c);space = 0;\
16845 bpr 746
  if(C == '_'){ctx.font = s+'px Helvetica';subsup = 1;c++;space = 3;baseline='top';C = txt.charAt(c);}\
16714 schaersvoo 747
  else\
16845 bpr 748
  if(C == '^'){ctx.font = s+'px Helvetica';subsup = 1;c++;space = 3;baseline='bottom';C = txt.charAt(c);}\
16714 schaersvoo 749
  else\
17586 schaersvoo 750
  if(C == ' ' ){ctx.font = S+'px Helvetica';if(subsup == 1){space = 0.6*s - (ctx.measureText('M').width);subsup = 0;};baseline = 'middle';}\
16714 schaersvoo 751
  else\
17586 schaersvoo 752
  if( subsup == 1 &&  c == '-'){c = '\\u2212';ctx.font = s+'px Helvetica';}\
753
  else\
754
  if( subsup == 1 &&  c == '+'){c = '\\u002B';ctx.font = s+'px Helvetica';}\
755
  else\
756
  if( check_alpha(C) == 1 ){ctx.font = S+'px Helvetica';subsup = 0;baseline = 'middle';}\
757
  else\
16714 schaersvoo 758
  if(C >= '0' && C <= '9'){ctx.font = s+'px Helvetica';}\
17586 schaersvoo 759
  else{ ctx.font = S+'px Helvetica';};\
16712 schaersvoo 760
  ctx.textBaseline = baseline;ctx.fillText(C,x,y);x = space + x + ctx.measureText(C).width;\
16700 schaersvoo 761
 };\
16705 schaersvoo 762
 ctx.stroke();\
16700 schaersvoo 763
 ctx.restore();\
764
 return;\
765
};");
18553 bpr 766
        break;
767
      case DRAW_TEXTFILL:/* not  used for userdraw */
768
        fprintf(js_include_file,"\n/* draw textfill */\
15111 schaersvoo 769
var text_fill_pattern;\
770
function draw_textfill(canvas_type,x0,y0,color,fontfamily,xsize,ysize,txt,use_userdraw){\
771
var fc = %d+canvas_type;\
772
var obj = create_canvas%d(fc,xsize,ysize);\
773
var ctx = obj.getContext('2d');\
774
ctx.font = fontfamily;\
775
var dx = 2+(ctx.measureText(txt)).width;\
776
var dy = 2+(ctx.measureText('MI')).width;\
777
ctx.fillStyle='rgba(255,255,255,0.01)';\
778
ctx.rect(0,0,xsize,ysize);\
779
ctx.fill();\
780
ctx.fillStyle=\"rgba(\"+color+\",0.01)\";\
781
ctx.strokeStyle=\"rgba(\"+color+\",0.01)\";\
782
for(var x = 0 ; x <= xsize ; x = x + dx ){\
783
 for(var y = 0 ; y <= ysize ; y = y + dy ){\
784
  ctx.fillText(txt, x, y);\
785
 };\
786
};\
787
if( use_userdraw ){\
788
 text_fill_pattern = ctx;\
789
}\
790
else\
791
{\
792
 setTimeout(function(){ filltoborder( x0,y0,color,color,canvas_type,true,ctx); },500);\
793
};\
794
return;};",canvas_root_id,canvas_root_id);
18553 bpr 795
        break;
796
      case DRAW_DIAMONDFILL:/* not used for userdraw */
797
        fprintf(js_include_file,"\n/* draw hatch fill */\
15111 schaersvoo 798
var diamond_fill_pattern;\
799
function draw_diamondfill(canvas_type,x0,y0,dx,dy,linewidth,color,stroke_opacity,xsize,ysize,use_userdraw){\
800
if( dx == 0 || dy == 0 ){alert(\"increment is zero !!! \");return;};\
801
var fc = %d+canvas_type;\
802
var obj = create_canvas%d(fc,xsize,ysize);\
803
var ctx = obj.getContext('2d');\
804
var x;\
805
var y;\
806
ctx.lineWidth = linewidth;\
807
ctx.fillStyle='rgba(255,255,255,0.01)';\
808
ctx.rect(0,0,xsize,ysize);\
809
ctx.fill();\
810
ctx.strokeStyle=\"rgba(\"+color+\",0.01)\";\
811
y = ysize;\
812
ctx.beginPath();\
813
for( x = 0 ; x <= xsize ; x = x + dx ){\
814
  ctx.moveTo(x,0);\
815
  ctx.lineTo(xsize,y);\
816
  y = y - dy;\
817
};\
818
y=0;\
819
for( x = xsize ; x >= 0 ; x = x - dx){\
820
 ctx.moveTo(x,ysize);\
821
 ctx.lineTo(0,y);\
822
 y = y + dy;\
823
};\
824
y = 0;\
825
for( x = 0 ; x <= xsize ; x = x + dx ){\
826
 ctx.moveTo(x,0);\
827
 ctx.lineTo(0,y);\
828
 y = y + dy;\
829
};\
830
y = 0;\
831
for( x = 0 ; x <= xsize ; x = x + dx ){\
832
 ctx.moveTo(xsize,y);\
833
 ctx.lineTo(x,ysize);\
834
 y = y + dy;\
835
};\
836
ctx.closePath();\
837
ctx.stroke();\
838
if( use_userdraw ){\
839
 diamond_fill_pattern = ctx;\
840
}\
841
else\
842
{\
843
 setTimeout(function(){ filltoborder( x0,y0,color,color,canvas_type,true,ctx); },500);\
844
};\
845
return;\
846
}",canvas_root_id,canvas_root_id);
18553 bpr 847
        break;
848
      case DRAW_HATCHFILL:/* not used for userdraw */
849
        fprintf(js_include_file,"\n/* draw hatch fill */\
15111 schaersvoo 850
var hatch_fill_pattern;\
851
function draw_hatchfill(canvas_type,x0,y0,dx,dy,linewidth,color,stroke_opacity,xsize,ysize,use_userdraw){\
852
if( dx == 0 || dy == 0 ){alert(\"increment is zero !!! \");return;};\
853
var fc = %d+canvas_type;\
854
var obj = create_canvas%d(fc,xsize,ysize);\
855
var ctx = obj.getContext('2d');\
856
var x,y;\
857
ctx.fillStyle='rgba(255,255,255,0.01)';\
858
ctx.rect(0,0,xsize,ysize);\
859
ctx.fill();\
860
ctx.strokeStyle=\"rgba(\"+color+\",0.01)\";\
861
ctx.lineWidth = linewidth;\
862
y = ysize;\
863
ctx.beginPath();\
864
for( x = 0 ; x <= xsize ; x = x + dx ){\
865
 ctx.moveTo(x,0);\
866
 ctx.lineTo(xsize,y);\
867
 y = y - dy;\
868
};\
869
y = 0;\
870
for( x = xsize ; x >= dx ; x = x - dx){\
871
 ctx.moveTo(x,ysize);\
872
 ctx.lineTo(0,y);\
873
 y = y + dy;\
874
};\
875
ctx.closePath();\
876
ctx.stroke();\
877
if( use_userdraw ){\
878
 hatch_fill_pattern = ctx;\
879
}\
880
else\
881
{\
882
 setTimeout(function(){ filltoborder( x0,y0,color,color,canvas_type,true,ctx); },500);\
883
};\
884
return;};",canvas_root_id,canvas_root_id);
18553 bpr 885
        break;
15111 schaersvoo 886
 
18553 bpr 887
      case DRAW_LINES:/*  used for js-tracing */
888
        fprintf(js_include_file,"\n/* draw lines */\
15111 schaersvoo 889
function calc_line(x1,x2,y1,y2){\
890
 var marge = 2;\
891
 if(x1 < x2+marge && x1>x2-marge){\
892
  return [x1,0,x1,ysize];\
893
 };\
894
 if(y1 < y2+marge && y1>y2-marge){\
895
  return [0,y1,xsize,y1];\
896
 };\
897
 var Y1 = y1 - (x1)*(y2 - y1)/(x2 - x1);\
898
 var Y2 = y1 + (xsize - x1)*(y2 - y1)/(x2 - x1);\
899
 return [0,Y1,xsize,Y2];\
900
};\
901
var draw_lines = function(ctx,x_points,y_points,line_width,stroke_color,stroke_opacity,use_dashed,dashtype0,dashtype1,use_rotate,angle){\
902
 ctx.save();\
903
 var line = new Array(4);\
904
 if(use_rotate == 1 ){ctx.rotate(angle*Math.PI/180);}\
905
 ctx.lineWidth = line_width;\
906
 ctx.strokeStyle=\"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
907
 if(use_dashed == 1){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{ctx.mozDash = [dashtype0,dashtype1];};};\
908
 for(var p = 0 ; p < x_points.length ; p = p+2 ){\
909
  line = calc_line(x_points[p],x_points[p+1],y_points[p],y_points[p+1]);\
910
  ctx.beginPath();\
911
  ctx.moveTo(line[0],line[1]);\
912
  ctx.lineTo(line[2],line[3]);\
913
  ctx.closePath();\
914
  ctx.stroke();\
915
  }\
916
  ctx.restore();\
917
  return;\
918
 };");
18553 bpr 919
        break;
15111 schaersvoo 920
 
18553 bpr 921
      case DRAW_CROSSHAIRS:/*  used for tracing */
922
        fprintf(js_include_file,"\n/* draw crosshairs  */\
15111 schaersvoo 923
var draw_crosshairs = function(ctx,x_points,y_points,line_width,crosshair_size,stroke_color,stroke_opacity,use_rotate,angle){\
924
 ctx.save();\
925
 if(use_rotate == 1 ){ctx.rotate(angle*Math.PI/180);}\
926
 ctx.lineWidth = line_width;\
927
 ctx.strokeStyle=\"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
928
 var x1,x2,y1,y2;\
929
 for(var p = 0 ; p < x_points.length ; p++ ){\
930
  x1 = x_points[p] - crosshair_size;\
931
  x2 = x_points[p] + crosshair_size;\
932
  y1 = y_points[p] - crosshair_size;\
933
  y2 = y_points[p] + crosshair_size;\
934
  ctx.beginPath();\
935
  ctx.moveTo(x1,y1);\
936
  ctx.lineTo(x2,y2);\
937
  ctx.closePath();\
938
  ctx.stroke();\
939
  ctx.beginPath();\
940
  ctx.moveTo(x2,y1);\
941
  ctx.lineTo(x1,y2);\
942
  ctx.closePath();\
943
  ctx.stroke();\
944
 }\
945
 ctx.restore();\
946
  return;\
947
};");
18553 bpr 948
        break;
949
      case DRAW_VIDEO:/* not  used for userdraw */
950
        fprintf(js_include_file,"\n/* draw video */\
15111 schaersvoo 951
var draw_video = function(canvas_root_id,x,y,w,h,URL){\
952
 var canvas_div = document.getElementById(\"canvas_div\"+canvas_root_id);\
953
 var video_div = document.createElement(\"div\");\
954
 canvas_div.appendChild(video_div);\
955
 video_div.style.position = \"absolute\";\
956
 video_div.style.left = x+\"px\";\
957
 video_div.style.top = y+\"px\";\
958
 video_div.style.width = w+\"px\";\
959
 video_div.style.height = h+\"px\";\
960
 var video = document.createElement(\"video\");\
961
 video_div.appendChild(video);\
962
 video.style.width = w+\"px\";\
963
 video.style.height = h+\"px\";\
964
 video.autobuffer = true;\
965
 video.controls = true;video.autoplay = false;\
966
 var src = document.createElement(\"source\");\
967
 src.type = \"video/mp4\";\
968
 src.src = URL;\
969
 video.appendChild(src);\
970
 video.load();\
971
 return;\
972
};");
18553 bpr 973
        break;
974
      case DRAW_AUDIO:/* not used for userdraw */
975
        fprintf(js_include_file,"\n/* draw audio */\
15111 schaersvoo 976
var draw_audio = function(canvas_root_id,x,y,w,h,loop,visible,URL1,URL2){\
977
 var canvas_div = document.getElementById(\"canvas_div\"+canvas_root_id);\
978
 var audio_div = document.createElement(\"div\");\
979
 canvas_div.appendChild(audio_div);\
980
 audio_div.style.position = \"absolute\";\
981
 audio_div.style.left = x+\"px\";\
982
 audio_div.style.top = y+\"px\";\
983
 audio_div.style.width = w+\"px\";\
984
 audio_div.style.height = h+\"px\";\
985
 var audio = document.createElement(\"audio\");\
986
 audio_div.appendChild(audio);\
987
 audio.setAttribute(\"style\",\"width:\"+w+\"px;height:\"+h+\"px\");\
988
 audio.autobuffer = true;\
989
 if(visible == 1 ){ audio.controls = true;audio.autoplay = false;}else{ audio.controls = false;audio.autoplay = true;};\
990
 if(loop == 1 ){ audio.loop = true;}else{ audio.loop = false;};\
991
 var src1 = document.createElement(\"source\");\
992
 src1.type = \"audio/ogg\";\
993
 src1.src = URL1;\
994
 audio.appendChild(src1);\
995
 var src2 = document.createElement(\"source\");\
996
 src2.type = \"audio/mpeg\";\
997
 src2.src = URL2;\
998
 audio.appendChild(src2);\
999
 audio.load();\
1000
 return;\
1001
};");
18553 bpr 1002
        break;
1003
      case DRAW_HTTP:/* not  used for userdraw */
1004
        fprintf(js_include_file,"\n/* draw http */\
15111 schaersvoo 1005
var draw_http = function(canvas_root_id,x,y,w,h,URL){\
1006
 var canvas_div = document.getElementById(\"canvas_div\"+canvas_root_id);\
1007
 var http_div = document.createElement(\"div\");\
1008
 var iframe = document.createElement(\"iframe\");\
1009
 canvas_div.appendChild(http_div);\
1010
 http_div.appendChild(iframe);\
1011
 iframe.src = URL;\
1012
 iframe.setAttribute(\"width\",w);\
1013
 iframe.setAttribute(\"height\",h);\
1014
 return;\
1015
};");
18553 bpr 1016
        break;
1017
      case DRAW_XML:
15111 schaersvoo 1018
/*
1019
!! function draw_xml(object)
1020
!! produced by : DRAW_XML
1021
!! used by commands: katex,latex,mathml,html,obabel
1022
!! uses js-functions: calculate_size(xml_div),rad2deg(rad)
1023
!! user interaction: drag,onclick,slider
1024
!! reply function: read_dragdrop()
1025
 
1026
onclick=0; no interaction
1027
onclick=1: click
1028
onclick=2: drag ==> always centered (use_offset=4)
1029
onclick=3: slider + click on object
1030
onclick=4: slider (always active , keyword 'active' in slider params)
1031
onclick=5: slider + dragdrop
1032
xy:  drag_type = 0;
1033
x:   drag_type = 1;
1034
y:   drag_type = 2;
1035
 
1036
obj.type: mathml,katex,html,svg,image(bitmap) (attention: for centering image needs to be measured after loading page !!)
1037
*/
1038
 
18553 bpr 1039
        fprintf(js_include_file,"\n/* draw xml */\
15111 schaersvoo 1040
function centered(xml_div,obj){\
1041
 var ww = 0.5*(parseFloat(window.getComputedStyle(xml_div).width));\
1042
 var hh = 0.5*(parseFloat(window.getComputedStyle(xml_div).height));\
1043
 obj.offset = [ ww , hh ];\
16845 bpr 1044
 /*xml_div.style.left = obj.x[0] - obj.offset[0] +'px';\
1045
 xml_div.style.top  = obj.y[0] - obj.offset[1] +'px';*/\
1046
 xml_div.style.left = (obj.x[0] - obj.offset[0])/xsize*100 +'%%';\
1047
 xml_div.style.top  = (obj.y[0] - obj.offset[1])/ysize*100 +'%%';\
15111 schaersvoo 1048
};\
1049
function rad2deg(rad){return rad*180/(Math.PI);};\
1050
if( typeof(slidergroup) !== 'object' ){ var slidergroup = [];};\
1051
function draw_xml(obj){\
1052
 var mouse;var xy = [obj.x[0],obj.y[0]];\
1053
 var xml_div;\
1054
 dragdrop_reply[obj.object_cnt] = {object_cnt:obj.object_cnt,type:obj.onclick,x:obj.x,y:obj.y,r:0,angle:obj.angle,clicked:0};\
1055
 if( obj.once ){\
1056
  xml_div = document.createElement('DIV');\
1057
  canvas_div.appendChild(xml_div);\
1058
  if( obj.type == 'katex' ){ obj.mathml = LoadKaTeX('html',obj.mathml);};\
1059
  xml_div.id = 'xml_div'+obj.id;\
1060
  xml_div.innerHTML = obj.mathml;\
16845 bpr 1061
  xml_div.classList.add('canvasdraw_xml');\
15111 schaersvoo 1062
  xml_div.style.color = \"rgba(\"+obj.stroke_color+\",\"+obj.stroke_opacity+\")\";\
1063
  xml_div.style.font = obj.fontfamily;\
1064
  if( ! obj.rotation_center){obj.rotation_center = [obj.x[0],obj.y[0]];}else{obj.rotation_center=[x2px(obj.rotation_center[0]) ,y2px(obj.rotation_center[1]) ];};\
16642 schaersvoo 1065
  if( obj.use_center == 1 ){if( obj.type == 'image' ){xml_div.addEventListener('load',function(e){centered(xml_div,obj);},true);}else{%s};};\
16844 bpr 1066
 } else {\
15111 schaersvoo 1067
  xml_div = document.getElementById('xml_div'+obj.id );\
1068
 };\
16845 bpr 1069
 if( typeof(xml_div) === 'undefined' ){console.log('[canvasutil] xml_div undefined'); return;};\
1070
 /*xml_div.style.left = parseInt(obj.x[0] - obj.offset[0]) +'px';\
1071
 xml_div.style.top  = parseInt(obj.y[0] - obj.offset[1]) +'px';*/\
1072
 xml_div.style.left = parseInt((obj.x[0] - obj.offset[0])/xsize*100) +'%%';\
1073
 xml_div.style.top  = parseInt((obj.y[0] - obj.offset[1])/ysize*100) +'%%';\
15111 schaersvoo 1074
 var back_color = 'rgba('+obj.fill_color+','+obj.fill_opacity+')';\
1075
 var no_color = 'rgba(255,255,255,0)';var dragging = false;\
1076
 if( obj.use_affine != 0 || obj.angle != 0 ){\
1077
  xml_div.style.transform = 'matrix('+obj.transform[0]+','+obj.transform[1]+','+obj.transform[2]+','+obj.transform[3]+','+obj.transform[4]+','+obj.transform[5]+') rotate('+rad2deg(obj.angle)+'deg)';\
1078
 };\
1079
 if( obj.once ){\
1080
  obj.once = false;\
1081
  switch(obj.onclick){\
1082
   case 2:\
1083
    if(isTouch){\
1084
     canvas_div.addEventListener('touchmove' ,function(evt){ evt.preventDefault();if(!dragging){return;};mouse = getMouse(evt.changedTouches[0],xml_div);xy = multisnap_check( mouse.x,mouse.y,obj.use_snap);xml_div.style.left = xy[0] - obj.offset[0] - obj.transform[4] + 'px';xml_div.style.top = xy[1] - obj.offset[1] - obj.transform[5] + 'px';},false);\
1085
     canvas_div.addEventListener('touchend'  ,function(evt){ evt.preventDefault();dragging = false;xml_div.style.backgroundColor = no_color;dragdrop_reply[obj.object_cnt] = {object_cnt:obj.object_cnt,type:obj.onclick,x:xy[0],y:xy[1],r:0,angle:obj.angle,clicked:0};obj.x[0] = xy[0];obj.y[0] = xy[1];},false);\
1086
     xml_div.addEventListener('touchstart',function(evt){evt.preventDefault();if(dragging){dragging = false;xml_div.style.backgroundColor = no_color;}else{dragging = true;xml_div.style.backgroundColor = back_color;};},false);\
1087
     xml_div.addEventListener('touchend'  ,function(evt){ evt.preventDefault();dragging = false;xml_div.style.backgroundColor = no_color;dragdrop_reply[obj.object_cnt] = {object_cnt:obj.object_cnt,type:obj.onclick,x:xy[0],y:xy[1],r:0,angle:obj.angle,clicked:0};obj.x[0] = xy[0];obj.y[0] = xy[1];},false);\
16844 bpr 1088
    } else {\
15111 schaersvoo 1089
     canvas_div.addEventListener('mousemove',function(evt){if(!dragging){return;};mouse = getMouse(evt,xml_div);xy = multisnap_check( mouse.x,mouse.y,obj.use_snap);xml_div.style.left = xy[0] - obj.offset[0] - obj.transform[4] + 'px';xml_div.style.top = xy[1] - obj.offset[1] - obj.transform[5] + 'px';},false);\
1090
     canvas_div.addEventListener('mouseup',function(evt){dragging = false;xml_div.style.backgroundColor = no_color;dragdrop_reply[obj.object_cnt] = {object_cnt:obj.object_cnt,type:obj.onclick,x:xy[0],y:xy[1],r:0,angle:obj.angle,clicked:0};obj.x[0] = xy[0];obj.y[0] = xy[1];},false);\
1091
     xml_div.addEventListener('mousedown', function(evt){if(dragging){dragging = false;xml_div.style.backgroundColor = no_color;}else{dragging = true;xml_div.style.backgroundColor = back_color;};},false);\
1092
    };\
1093
    break;\
1094
   case 1:\
16844 bpr 1095
    if(isTouch){\
15111 schaersvoo 1096
     xml_div.addEventListener('touchstart', function(e){ e.preventDefault();if(dragdrop_reply[obj.object_cnt].clicked == 0){ dragdrop_reply[obj.object_cnt].clicked = 1;xml_div.style.backgroundColor = back_color;}else{dragdrop_reply[obj.object_cnt].clicked = 0;xml_div.style.backgroundColor = no_color;};} ,false);\
16844 bpr 1097
    } else {\
15111 schaersvoo 1098
     xml_div.addEventListener('mousedown'  , function(evt){if(dragdrop_reply[obj.object_cnt].clicked == 0){ dragdrop_reply[obj.object_cnt].clicked = 1;xml_div.style.backgroundColor = back_color;}else{dragdrop_reply[obj.object_cnt].clicked = 0;xml_div.style.backgroundColor = no_color;};} ,false);\
1099
    };\
1100
    break;\
1101
   case 3:\
16844 bpr 1102
    if(isTouch){\
15111 schaersvoo 1103
     xml_div.addEventListener('touchstart',function(evt){ if(slidergroup[obj.object_cnt] == null ){ slidergroup[obj.object_cnt] = obj;xml_div.style.backgroundColor = back_color;}else{ slidergroup[obj.object_cnt] = null;xml_div.style.backgroundColor = no_color;};} ,false);\
16844 bpr 1104
    } else {\
15111 schaersvoo 1105
     xml_div.addEventListener('mousedown' ,function(evt){ if(slidergroup[obj.object_cnt] == null ){ slidergroup[obj.object_cnt] = obj;xml_div.style.backgroundColor = back_color;}else{ slidergroup[obj.object_cnt] = null;xml_div.style.backgroundColor = no_color;};} ,false);\
1106
    };\
1107
    break;\
1108
   case 4:slidergroup[obj.object_cnt] = obj;break;\
1109
   case 5:slidergroup[obj.object_cnt] = obj;\
1110
    canvas_div.addEventListener('mousemove',function(evt){if(!dragging){return;};mouse = getMouse(evt,xml_div);xy = multisnap_check( mouse.x,mouse.y,obj.use_snap);xml_div.style.left = xy[0] - obj.offset[0] - obj.transform[4] + 'px';xml_div.style.top = xy[1] - obj.offset[1] - obj.transform[5] + 'px';},false);\
1111
    canvas_div.addEventListener('mouseup',function(evt){dragging = false;xml_div.style.backgroundColor = no_color;dragdrop_reply[obj.object_cnt] = {object_cnt:obj.object_cnt,type:obj.onclick,x:xy[0],y:xy[1],r:0,angle:obj.angle,clicked:0};obj.x[0] = xy[0];obj.y[0] = xy[1];},false);\
1112
    xml_div.addEventListener('mousedown', function(evt){if(dragging){dragging = false;xml_div.style.backgroundColor = no_color;}else{dragging = true;xml_div.style.backgroundColor = back_color;};},false);\
1113
    break;\
1114
   default: break;\
1115
  };\
1116
 };\
1117
 return;\
16642 schaersvoo 1118
};",mathjax);
15111 schaersvoo 1119
 
1120
/*
1121
console.log('angle='+obj.angle+'obj.use_center='+obj.use_center+'id='+obj.id+'xml_div.id = '+xml_div.id+' once ='+obj.once+' offset='+ obj.offset+' div x = '+xml_div.style.left+' div y ='+xml_div.style.top+' obj.x='+px2x(obj.x)+'obj.y='+px2y(obj.y));\
1122
*/
18553 bpr 1123
        break;
1124
      case DRAW_SGRAPH:
15111 schaersvoo 1125
/*
1126
 xstart = given
1127
 ystart = given
1128
 sgraph(canvas_type,precision,xmajor,ymajor,xminor,yminor,majorcolor,minorcolor,fontfamily)
1129
*/
18553 bpr 1130
        fprintf(js_include_file,"\n/* draw sgraph */\
15111 schaersvoo 1131
var draw_sgraph = function(canvas_type,precision,xmajor,ymajor,xminor,yminor,majorcolor,minorcolor,fontfamily,opacity,font_size){\
1132
 var obj;if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){obj = document.getElementById(\"wims_canvas%d\"+canvas_type);}else{ obj = create_canvas%d(canvas_type,xsize,ysize);};\
1133
 var ctx = obj.getContext(\"2d\");\
1134
 ctx.font = fontfamily;\
1135
 var minor_opacity = 0.8*opacity;\
1136
 ctx.clearRect(0,0,xsize,ysize);\
1137
 var zero_x = 0.1*xsize;\
1138
 var zero_y = 0.9*ysize;\
1139
 var snor_x;var snor_y;\
1140
 if( xstart != xmin){\
1141
  snor_x = 0.1*xsize;\
16844 bpr 1142
 }else{\
15111 schaersvoo 1143
  snor_x = 0;\
1144
  xstart = xmin;\
1145
 };\
1146
 ctx.strokeStyle = \"rgba(\"+majorcolor+\",\"+opacity+\")\";\
1147
 ctx.lineWidth = 2;\
1148
 ctx.beginPath();\
1149
 ctx.moveTo(xsize,zero_y);\
1150
 ctx.lineTo(zero_x,zero_y);\
1151
 ctx.lineTo(zero_x,0);\
1152
 ctx.stroke();\
1153
 ctx.closePath();\
1154
 ctx.beginPath();\
1155
 ctx.moveTo(zero_x,zero_y);\
1156
 ctx.lineTo(zero_x + 0.25*snor_x,zero_y - 0.1*snor_x);\
1157
 ctx.lineTo(zero_x + 0.5*snor_x,zero_y + 0.1*snor_x);\
1158
 ctx.lineTo(zero_x + 0.75*snor_x,zero_y - 0.1*snor_x);\
1159
 ctx.lineTo(zero_x + snor_x,zero_y);\
1160
 ctx.stroke();\
1161
 ctx.closePath();\
1162
 ctx.beginPath();\
1163
 var num = xstart;\
1164
 var flipflop = 1;\
1165
 var step_x = xmajor*(xsize - zero_x - snor_x)/(xmax - xstart);\
1166
 var txtsize;var txt_marge=step_x - 5;\
1167
 for(var x = zero_x+snor_x ; x < xsize;x = x + step_x){\
1168
  txtsize = ctx.measureText(num).width;\
1169
  if( txtsize > txt_marge ){if( flipflop == 1 ){flipflop = 0;}else{flipflop = 1;};};\
1170
  if( flipflop == 1){\
1171
   ctx.fillText(num,x - 0.5*txtsize,zero_y+font_size);\
16844 bpr 1172
  } else {\
15111 schaersvoo 1173
   ctx.fillText(num,x - 0.5*txtsize,zero_y+2*font_size);\
1174
  };\
1175
  num = num + xmajor;\
1176
 };\
1177
 ctx.stroke();\
1178
 ctx.closePath();\
1179
 ctx.lineWidth = 1;\
1180
 ctx.beginPath();\
1181
 for(var x = zero_x+snor_x ; x < xsize;x = x + step_x){\
1182
   ctx.moveTo(x,zero_y);\
1183
   ctx.lineTo(x,0);\
1184
 };\
1185
 ctx.stroke();\
1186
 ctx.closePath();\
1187
 if( xminor > 1){\
1188
  ctx.lineWidth = 0.5;\
1189
  ctx.beginPath();\
1190
  ctx.strokeStyle = \"rgba(\"+minorcolor+\",\"+minor_opacity+\")\";\
1191
  var minor_step_x = step_x / xminor;\
1192
  var nx;\
1193
  for(var x = zero_x+snor_x; x < xsize;x = x + step_x){\
1194
    num = 1;\
1195
    for(var p = 1 ; p < xminor ; p++){\
1196
     nx = x + num*minor_step_x;\
1197
     ctx.moveTo(nx,zero_y);\
1198
     ctx.lineTo(nx,0);\
1199
     num++;\
1200
    };\
1201
  };\
1202
  ctx.stroke();\
1203
  ctx.closePath();\
1204
  ctx.beginPath();\
1205
  ctx.lineWidth = 2;\
1206
  ctx.strokeStyle = \"rgba(\"+majorcolor+\",\"+opacity+\")\";\
1207
  for(var x = zero_x+snor_x ; x < xsize;x = x + step_x){\
1208
   ctx.moveTo(x,zero_y);ctx.lineTo(x,zero_y - 12);\
1209
  };\
1210
  for(var x = zero_x+snor_x ; x < xsize;x = x + minor_step_x){\
1211
   ctx.moveTo(x,zero_y);ctx.lineTo(x,zero_y - 6);\
1212
  };\
1213
  ctx.stroke();\
1214
  ctx.closePath();\
1215
  ctx.lineWidth = 0.5;\
1216
 };\
1217
 xmin = xstart - (xmajor*(zero_x+snor_x)/step_x);\
1218
 if( ystart != ymin){\
1219
  snor_y = 0.1*ysize;\
16844 bpr 1220
 } else {\
15111 schaersvoo 1221
  snor_y = 0;\
1222
  ystart = ymin;\
1223
 };\
1224
 ctx.lineWidth = 2;\
1225
 ctx.strokeStyle = \"rgba(\"+majorcolor+\",\"+opacity+\")\";\
1226
 ctx.beginPath();\
1227
 ctx.moveTo(zero_x,zero_y);\
1228
 ctx.lineTo(zero_x - 0.1*snor_y,zero_y - 0.25*snor_y);\
1229
 ctx.lineTo(zero_x + 0.1*snor_y,zero_y - 0.5*snor_y);\
1230
 ctx.lineTo(zero_x - 0.1*snor_y,zero_y - 0.75*snor_y);\
1231
 ctx.lineTo(zero_x,zero_y - snor_y);\
1232
 ctx.stroke();\
1233
 ctx.closePath();\
1234
 ctx.beginPath();\
1235
 ctx.lineWidth = 1;\
1236
 num = ystart;\
1237
 var step_y = ymajor*(zero_y - snor_y)/(ymax - ystart);\
1238
 for(var y = zero_y - snor_y ; y > 0; y = y - step_y){\
1239
  ctx.moveTo(zero_x,y);\
1240
  ctx.lineTo(xsize,y);\
1241
  ctx.fillText(num,zero_x - ctx.measureText(num+\" \").width,parseInt(y+0.2*font_size));\
1242
  num = num + ymajor;\
1243
 };\
1244
 ctx.stroke();\
1245
 ctx.closePath();\
1246
 if( yminor > 1){\
1247
  ctx.lineWidth = 0.5;\
1248
  ctx.beginPath();\
1249
  ctx.strokeStyle = \"rgba(\"+minorcolor+\",\"+minor_opacity+\")\";\
1250
  var minor_step_y = step_y / yminor;\
1251
  var ny;\
1252
  for(var y = 0 ; y < zero_y - snor_y ;y = y + step_y){\
1253
   num = 1;\
1254
   for(var p = 1 ;p < yminor;p++){\
1255
     ny = y + num*minor_step_y;\
1256
     ctx.moveTo(zero_x,ny);\
1257
     ctx.lineTo(xsize,ny);\
1258
     num++;\
1259
    };\
1260
  };\
1261
  ctx.stroke();\
1262
  ctx.closePath();\
1263
  ctx.lineWidth = 2;\
1264
  ctx.beginPath();\
1265
  ctx.strokeStyle = \"rgba(\"+majorcolor+\",\"+opacity+\")\";\
1266
  for(var y = zero_y - snor_y ; y > 0 ;y = y - step_y){\
1267
   ctx.moveTo(zero_x,y);\
1268
   ctx.lineTo(zero_x+12,y);\
1269
  };\
1270
  for(var y = zero_y - snor_y ; y > 0 ;y = y - minor_step_y){\
1271
   ctx.moveTo(zero_x,y);\
1272
   ctx.lineTo(zero_x+6,y);\
1273
  };\
1274
  ctx.stroke();\
1275
  ctx.closePath();\
1276
 };\
1277
 ymin = ystart - (ymajor*(ysize - zero_y + snor_y)/step_y);\
1278
 if( typeof(legend%d)  !== 'undefined' ){\
1279
  ctx.globalAlpha = 1.0;\
1280
  var y_offset = 2*font_size;\
1281
  var txt;var txt_size;\
1282
  var x_offset = xsize - 2*font_size;\
1283
  var l_length = legend%d.length;var barcolor = new Array();\
1284
  if( typeof(legendcolors%d) !== 'undefined' ){\
1285
   for(var p = 0 ; p < l_length ; p++){\
1286
    barcolor[p] = legendcolors%d[p];\
1287
   };\
16844 bpr 1288
  } else {\
15111 schaersvoo 1289
   if( barcolor.length == 0 ){\
1290
    for(var p = 0 ; p < l_length ; p++){\
1291
     barcolor[p] = stroke_color;\
1292
    };\
1293
   };\
1294
  };\
1295
  for(var p = 0; p < l_length; p++){\
1296
   ctx.fillStyle = barcolor[p];\
1297
   txt = legend%d[p];\
1298
   txt_size = ctx.measureText(txt).width;\
1299
   ctx.fillText(legend%d[p],x_offset - txt_size, y_offset);\
1300
   y_offset = parseInt(y_offset + 1.5*font_size);\
1301
  };\
1302
 };\
1303
 if( typeof(xaxislabel) !== 'undefined' ){\
1304
   ctx.fillStyle = \'#000000\';\
1305
   var txt_size = ctx.measureText(xaxislabel).width + 4 ;\
1306
   ctx.fillText(xaxislabel,xsize - txt_size, zero_y - 7);\
1307
 };\
1308
 if( typeof(yaxislabel) !== 'undefined'){\
1309
   ctx.save();\
1310
   ctx.fillStyle = \'#000000\';\
1311
   var txt_size = ctx.measureText(yaxislabel).width;\
1312
   ctx.translate(zero_x+8 + font_size,txt_size+font_size);\
1313
   ctx.rotate(-0.5*Math.PI);\
1314
   ctx.fillText(yaxislabel,0,0);\
1315
   ctx.restore();\
1316
 };\
1317
};",canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 1318
        break;
1319
      case DRAW_NUMBERLINE:
15111 schaersvoo 1320
fprintf(js_include_file,"\n/* draw numberline */\
1321
var draw_numberline = function(canvas_type,use_axis_numbering,x0,x1,xmajor,xminor,y0,y1,linewidth,strokecolor,strokeopacity,fontfamily,fontcolor,precision){\
1322
 var obj = create_canvas%d(canvas_type,xsize,ysize);\
1323
 var ctx = obj.getContext(\"2d\");\
1324
 ctx.lineWidth = linewidth || 1;\
1325
 ctx.strokeStyle = \"rgba(\"+strokecolor+\",\"+strokeopacity+\")\";\
15313 schaersvoo 1326
 ctx.font = fontfamily || 'Arial 12px';\
15111 schaersvoo 1327
 var fontsize = parseInt(ctx.font);\
1328
 ctx.fillStyle =  \"rgba(\"+fontcolor+\",\"+strokeopacity+\")\";\
1329
 x1 = x2px(x1);\
1330
 x0 = x2px(x0);\
1331
 y0 = y2px(y0);\
1332
 y1 = y2px(y1);\
1333
 var sub_devision = -1;\
1334
 if( xminor%%2 == 0 ){ sub_devision = xminor/2; };\
1335
 var ybase1 = parseInt( y0 + fontsize + 2 );\
1336
 var ybase2 = parseInt( ybase1 + fontsize + 2 );\
1337
 var yh = Math.abs(parseInt( y0 - 0.3*(y0 -y1)));\
1338
 var ys = Math.abs(parseInt( y0 - 0.6*(y0 -y1)));\
1339
 xmajor = x2px(xmajor) - x2px(0);\
1340
 var i;var len;var p;\
1341
 xminor = xmajor / xminor;\
1342
 ctx.beginPath();\
1343
 for(p = x0 ; p < x1 ; p = p + xmajor){\
1344
  ctx.moveTo(p,y0);ctx.lineTo(p,y1);i = 0;\
1345
  for(var s = p ; s < p + xmajor ; s = s + xminor ){\
1346
   ctx.moveTo(s,y0);\
1347
   if( sub_devision == i){ ctx.lineTo(s,ys); } else { ctx.lineTo(s,yh); };\
1348
   i++;\
1349
  };\
1350
 };\
1351
 ctx.moveTo(p,y0);ctx.lineTo(p,y1);\
1352
 ctx.closePath();\
1353
 ctx.stroke();\
1354
 if( use_axis_numbering >-1 ){\
1355
  var str = x_strings[use_axis_numbering];\
1356
  len = str.length;if((len/2+0.5)%%2 == 0){ alert(\"xaxis number unpaired:  text missing ! \");return;};\
1357
  var corr;var x_nums;var x_text;var flipflop = 0;var off = ybase1;\
1358
  ctx.beginPath();\
1359
  if( x_strings_up[use_axis_numbering] == null){\
1360
   for(var p = 0 ; p < len ; p = p+2){\
1361
    var x_nums = x2px(eval(str[p]));\
1362
    var x_text = str[p+1];\
1363
    corr = ctx.measureText(x_text).width;\
1364
    if( corr > xmajor){ if(flipflop == 0 ){flipflop = 1; off = ybase2;}else{flipflop = 0; off = ybase1;};};\
1365
    ctx.fillText(x_text,parseInt(x_nums-0.5*corr),off);\
1366
   };\
16844 bpr 1367
  } else {\
15111 schaersvoo 1368
   for(var p = 0 ; p < len ; p = p+2){\
1369
    x_nums = x2px(eval(str[p]));\
1370
    x_text = str[p+1];\
1371
    corr = ctx.measureText(x_text).width + ybase1 - fontsize;\
1372
    ctx.save();\
1373
    ctx.translate(x_nums+0.5*fontsize, corr);\
1374
    ctx.rotate(-1.5708);\
1375
    ctx.fillText(x_text,0,0);\
1376
    ctx.restore();\
1377
   };\
1378
  }\
16844 bpr 1379
 } else {\
15111 schaersvoo 1380
  var corr;var num;var flipflop = 0;var off = ybase1;\
1381
  var prec = parseInt(Math.log(precision)/Math.log(10));\
1382
  for(var p = x0 ; p < x1+xmajor ; p = p+xmajor){\
1383
   num = (px2x(p)).toFixed(prec);\
1384
   corr = ctx.measureText(num).width;\
1385
   if( corr > xmajor){ if(flipflop == 0 ){flipflop = 1; off = ybase2;}else{flipflop = 0; off = ybase1;};};\
1386
   ctx.fillText(num,parseInt(p - 0.5*corr),off);\
1387
  };\
1388
 };\
1389
};",canvas_root_id);
18553 bpr 1390
        break;
1391
      case DRAW_GRID:/* not used for userdraw */
1392
        fprintf(js_include_file,"\n/* draw grid */\
15111 schaersvoo 1393
var draw_grid%d = function(canvas_type,precision,stroke_opacity,xmajor,ymajor,xminor,yminor,tics_length,line_width,stroke_color,axis_color,font_size,font_family,use_axis,use_axis_numbering,use_dashed,dashtype0,dashtype1,font_color,fill_opacity){\
1394
var obj;if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){obj = document.getElementById(\"wims_canvas%d\"+canvas_type);}else{obj = create_canvas%d(canvas_type,xsize,ysize);};\
1395
var ctx = obj.getContext(\"2d\");ctx.clearRect(0,0,xsize,ysize);\
1396
if(use_dashed == 1){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{ctx.mozDash = [dashtype0,dashtype1];};};\
1397
ctx.save();\
1398
var stroke_color = \"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
1399
ctx.fillStyle = \"rgba(\"+font_color+\",\"+1.0+\")\";\
1400
var axis_color = \"rgba(\"+axis_color+\",\"+stroke_opacity+\")\";\
1401
ctx.font = font_family;\
1402
var barcolor = new Array();\
1403
var xstep = xsize*xmajor/(xmax - xmin);\
1404
var ystep = ysize*ymajor/(ymax - ymin);\
1405
var x2step = xstep / xminor;\
1406
var y2step = ystep / yminor;\
1407
var zero_x = x2px(0);var zero_y = y2px(0);var f_x;var f_y;\
1408
if(xmin < 0 ){ f_x = -1;}else{ f_x = 1;};\
1409
if(ymin < 0 ){ f_y = -1;}else{ f_y = 1;};\
1410
ctx.beginPath();\
1411
ctx.lineWidth = line_width;\
1412
ctx.strokeStyle = stroke_color;\
1413
for(var p = zero_x ; p < xsize; p = p + xstep){\
1414
 ctx.moveTo(p,0);\
1415
 ctx.lineTo(p,ysize);\
1416
};\
1417
for(var p = zero_x ; p > 0; p = p - xstep){\
1418
 ctx.moveTo(p,0);\
1419
 ctx.lineTo(p,ysize);\
1420
};\
1421
for(var p = zero_y ; p < ysize; p = p + ystep){\
1422
 ctx.moveTo(0,p);\
1423
 ctx.lineTo(xsize,p);\
1424
};\
1425
for(var p = zero_y ; p > 0; p = p - ystep){\
1426
 ctx.moveTo(0,p);\
1427
 ctx.lineTo(xsize,p);\
1428
};\
1429
if( typeof(xaxislabel) !== 'undefined' ){\
1430
 ctx.save();\
15313 schaersvoo 1431
 ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 1432
 var corr =  parseInt(1.1*ctx.measureText(xaxislabel).width);\
1433
 ctx.fillText(xaxislabel,xsize - corr,zero_y - tics_length - 0.4*font_size);\
1434
 ctx.restore();\
1435
};\
1436
if( typeof(yaxislabel) !== 'undefined' ){\
1437
 ctx.save();\
15313 schaersvoo 1438
 ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 1439
 var corr = parseInt(ctx.measureText(yaxislabel).width + font_size);\
1440
 ctx.translate(zero_x+tics_length + font_size,corr);\
1441
 ctx.rotate(-0.5*Math.PI);\
1442
 ctx.fillText(yaxislabel,0,0);\
1443
 ctx.restore();\
1444
};\
1445
ctx.stroke();\
1446
ctx.closePath();",canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id);
1447
 
18553 bpr 1448
        if( use_axis == 1 ){
1449
          fprintf(js_include_file,"\
15111 schaersvoo 1450
 ctx.save();\
1451
 ctx.beginPath();\
1452
 ctx.strokeStyle = stroke_color;\
1453
 ctx.lineWidth = 0.6*line_width;\
1454
 for(var p = zero_x ; p < xsize; p = p + x2step){\
1455
  ctx.moveTo(p,0);\
1456
  ctx.lineTo(p,ysize);\
1457
 };\
1458
 for(var p = zero_x ; p > 0; p = p - x2step){\
1459
  ctx.moveTo(p,0);\
1460
  ctx.lineTo(p,ysize);\
1461
 };\
1462
 for(var p = zero_y ; p < ysize; p = p + y2step){\
1463
  ctx.moveTo(0,p);\
1464
  ctx.lineTo(xsize,p);\
1465
 };\
1466
 for(var p = zero_y ; p > 0; p = p - y2step){\
1467
  ctx.moveTo(0,p);\
1468
  ctx.lineTo(xsize,p);\
1469
 };\
1470
 ctx.stroke();\
1471
 ctx.closePath();\
1472
 ctx.beginPath();\
1473
 ctx.lineWidth = 2*line_width;\
1474
 ctx.strokeStyle = axis_color;\
1475
 ctx.moveTo(0,zero_y);\
1476
 ctx.lineTo(xsize,zero_y);\
1477
 ctx.moveTo(zero_x,0);\
1478
 ctx.lineTo(zero_x,ysize);\
1479
 ctx.stroke();\
1480
 ctx.closePath();\
1481
 ctx.lineWidth = line_width+0.5;\
1482
 ctx.beginPath();\
1483
 for(var p = zero_x ; p < xsize; p = p + xstep){\
1484
  ctx.moveTo(p,zero_y-tics_length);\
1485
  ctx.lineTo(p,zero_y+tics_length);\
1486
 };\
1487
 for(var p = zero_x ; p > 0; p = p - xstep){\
1488
  ctx.moveTo(p,zero_y-tics_length);\
1489
  ctx.lineTo(p,zero_y+tics_length);\
1490
 };\
1491
 for(var p = zero_y ; p < ysize; p = p + ystep){\
1492
  ctx.moveTo(zero_x-tics_length,p);\
1493
  ctx.lineTo(zero_x+tics_length,p);\
1494
 };\
1495
 for(var p = zero_y ; p > 0; p = p - ystep){\
1496
  ctx.moveTo(zero_x-tics_length,p);\
1497
  ctx.lineTo(zero_x+tics_length,p);\
1498
 };\
1499
 for(var p = zero_x ; p < xsize; p = p + x2step){\
1500
  ctx.moveTo(p,zero_y-0.5*tics_length);\
1501
  ctx.lineTo(p,zero_y+0.5*tics_length);\
1502
 };\
1503
 for(var p = zero_x ; p > 0; p = p - x2step){\
1504
  ctx.moveTo(p,zero_y-0.5*tics_length);\
1505
  ctx.lineTo(p,zero_y+0.5*tics_length);\
1506
 };\
1507
 for(var p = zero_y ; p < ysize; p = p + y2step){\
1508
  ctx.moveTo(zero_x-0.5*tics_length,p);\
1509
  ctx.lineTo(zero_x+0.5*tics_length,p);\
1510
 };\
1511
 for(var p = zero_y ; p > 0; p = p - y2step){\
1512
  ctx.moveTo(zero_x-0.5*tics_length,p);\
1513
  ctx.lineTo(zero_x+0.5*tics_length,p);\
1514
 };\
1515
 ctx.stroke();\
1516
 ctx.closePath();\
1517
 ctx.restore();");
18553 bpr 1518
        }
1519
        if( use_axis_numbering != -1 ){
1520
          fprintf(js_include_file,"\
15111 schaersvoo 1521
 ctx.save();\
1522
 ctx.fillColor = axis_color;\
1523
 ctx.strokeStyle = axis_color;\
1524
 ctx.lineWidth = 2*line_width;\
1525
 ctx.font = font_family;\
1526
 var shift = zero_y+2*font_size;var flip=0;var skip=0;var corr;var cnt;var disp_cnt;var prec;\
1527
 if( x_strings[use_axis_numbering] != null ){\
1528
  var str = x_strings[use_axis_numbering];\
1529
  var len = str.length;if((len/2+0.5)%%2 == 0){ alert(\"xaxis number unpaired:  text missing ! \");return;};\
1530
  ctx.beginPath();\
1531
  if( x_strings_up[use_axis_numbering] == null){\
1532
   for(var p = 0 ; p < len ; p = p+2){\
1533
    var x_nums = x2px(eval(str[p]));\
1534
    var x_text = str[p+1];\
1535
    corr = ctx.measureText(x_text).width;\
1536
    skip = 1.2*corr/xstep;\
1537
    if( zero_y+2*font_size > ysize ){shift = ysize - 2*font_size;};\
1538
    if( skip > 1 ){if(flip == 0 ){flip = 1; shift = shift + font_size;}else{flip = 0; shift = shift - font_size;};};\
1539
    ctx.fillText(x_text,parseInt(x_nums-0.5*corr),shift);\
1540
    ctx.moveTo(x_nums,zero_y - tics_length);\
1541
    ctx.lineTo(x_nums,zero_y + tics_length);\
1542
   };\
16844 bpr 1543
  } else {\
15111 schaersvoo 1544
   for(var p = 0 ; p < len ; p = p+2){\
1545
    var x_nums = x2px(eval(str[p]));\
1546
    var x_text = str[p+1];\
1547
    corr = 2 + tics_length + zero_y + ctx.measureText(x_text).width;\
1548
    if( corr > ysize ){corr = ysize;};\
1549
    ctx.save();\
1550
    ctx.translate(x_nums+0.25*font_size, corr);\
1551
    ctx.rotate(-1.5708);\
1552
    ctx.fillText(x_text,0,0);\
1553
    ctx.restore();\
1554
    ctx.moveTo(x_nums,zero_y - tics_length);\
1555
    ctx.lineTo(x_nums,zero_y + tics_length);\
1556
   };\
1557
  };\
1558
  ctx.closePath();\
16844 bpr 1559
 } else {\
15111 schaersvoo 1560
  skip = 1;cnt = px2x(zero_x);\
1561
  prec = Math.log(precision)/(Math.log(10));\
1562
  var y_basis;if(f_y == 1){ y_basis = ysize }else{ y_basis = zero_y + 1.4*font_size;};\
1563
  for( var p = zero_x ; p < xsize ; p = p+xstep){\
1564
   if(skip == 0 ){\
1565
    disp_cnt = cnt.toFixed(prec);\
1566
    corr = ctx.measureText(disp_cnt).width;\
1567
    skip = parseInt(1.2*corr/xstep);\
1568
    ctx.fillText(disp_cnt,p-0.5*corr,y_basis);\
16844 bpr 1569
   } else {\
15111 schaersvoo 1570
    skip--;\
1571
   };\
1572
   cnt = cnt + xmajor;\
1573
  };\
1574
  cnt = px2x(zero_x);skip = 1;\
1575
  for( var p = zero_x ; p > 0 ; p = p-xstep){\
1576
   if(skip == 0 ){\
1577
    disp_cnt = cnt.toFixed(prec);\
1578
    corr = ctx.measureText(disp_cnt).width;\
1579
    skip = parseInt(1.2*corr/xstep);\
1580
    ctx.fillText(disp_cnt,p-0.5*corr,y_basis);\
16844 bpr 1581
   } else {\
15111 schaersvoo 1582
    skip--;\
1583
   };\
1584
   cnt = cnt - xmajor;\
1585
  };\
1586
 };\
1587
 if( y_strings != null ){\
1588
  var len = y_strings.length;if((len/2+0.5)%%2 == 0){ alert(\"yaxis number unpaired:  text missing ! \");return;};\
1589
  ctx.beginPath();\
1590
  for(var p = 0 ; p < len ; p = p+2){\
1591
   var y_nums = y2px(eval(y_strings[p]));\
1592
   var y_text = y_strings[p+1];\
1593
   corr = 2 + tics_length + ctx.measureText(y_text).width;\
1594
   if( corr > zero_x){corr = parseInt(zero_x+2); }\
1595
   ctx.fillText(y_text,zero_x - corr,y_nums + 0.5*font_size);\
1596
   ctx.moveTo(zero_x - tics_length,y_nums);\
1597
   ctx.lineTo(zero_x + tics_length,y_nums);\
1598
  };\
1599
  ctx.closePath();\
16844 bpr 1600
 } else {\
15111 schaersvoo 1601
  if(f_x == 1){ corr = 1.5*tics_length; }\
1602
  cnt = px2y(zero_y);skip = 1;\
1603
  for( var p = zero_y ; p < ysize ; p = p+ystep){\
1604
   if(skip == 0 ){\
1605
    skip = parseInt(1.4*font_size/ystep);\
1606
    disp_cnt = cnt.toFixed(prec);\
1607
    if(f_x == -1 ){ corr = parseInt(zero_x - (2 + tics_length + ctx.measureText(disp_cnt).width));};\
1608
    ctx.fillText(disp_cnt,parseInt(corr),parseInt(p+(0.4*font_size)));\
16844 bpr 1609
   } else {\
15111 schaersvoo 1610
    skip--;\
1611
   };\
1612
   cnt = cnt - ymajor;\
1613
  };\
1614
  corr = 0;cnt = px2y(zero_y);skip = 1;\
1615
  if(f_x == 1){ corr = 1.5*tics_length; }\
1616
  for( var p = zero_y ; p > 0 ; p = p-ystep){\
1617
   if(skip == 0 ){\
1618
    skip = parseInt(1.4*font_size/ystep);\
1619
    disp_cnt = cnt.toFixed(prec);\
1620
    if(f_x == -1 ){corr = parseInt(zero_x - (2 + tics_length + ctx.measureText(disp_cnt).width));};\
1621
    ctx.fillText(disp_cnt,parseInt(corr),parseInt(p+(0.4*font_size)));\
16844 bpr 1622
   } else {\
15111 schaersvoo 1623
    skip--;\
1624
   };\
1625
   cnt = cnt + ymajor;\
1626
  };\
1627
 };\
1628
 ctx.stroke();\
1629
 ctx.restore();");
18553 bpr 1630
        }
1631
        if( legend_cnt != -1 ){
1632
          fprintf(js_include_file,"ctx.save();\
15111 schaersvoo 1633
 ctx.globalAlpha = 1.0;\
15313 schaersvoo 1634
 ctx.font = \"bold \"+font_size+\"px Arial\";\
15111 schaersvoo 1635
 var y_offset = 2*font_size;\
1636
 var txt;var txt_size;\
1637
 var x_offset = xsize - 2*font_size;\
1638
 var l_length = legend0.length;\
1639
 if( typeof(legendcolors0) !== 'undefined' ){\
1640
  for(var p = 0 ; p < l_length ; p++){\
1641
    barcolor[p] = legendcolors0[p];\
1642
  };\
16844 bpr 1643
 } else {\
15111 schaersvoo 1644
  if( barcolor.length == 0 ){\
1645
   for(var p = 0 ; p < l_length ; p++){\
1646
    barcolor[p] = stroke_color;\
1647
   };\
1648
  };\
1649
 };\
1650
 for(var p = 0; p < l_length; p++){\
1651
  ctx.fillStyle = barcolor[p];\
1652
  txt = legend0[p];\
1653
  txt_size = ctx.measureText(txt).width;\
1654
  ctx.fillText(legend0[p],x_offset - txt_size, y_offset);\
1655
  y_offset = parseInt(y_offset + 1.5*font_size);\
1656
 };\
1657
 ctx.restore();");
18553 bpr 1658
        }
1659
        if( barchart_cnt != 0 ){
1660
          fprintf(js_include_file,"ctx.save();\
15111 schaersvoo 1661
 var num_barcharts = 0;\
1662
 var bar_name = eval('barchart_0');\
1663
 while( typeof(bar_name) !== 'undefined' ){\
1664
    try{ bar_name = eval('barchart_'+num_barcharts);num_barcharts++;}catch(e){break;};\
1665
 };\
1666
 var bar_width = parseInt(0.8*x2step/(num_barcharts));\
1667
 for(var i=0 ; i< num_barcharts ; i++){\
1668
  bar_name = eval('barchart_'+i);\
1669
  var bar_x = new Array();\
1670
  var bar_y = new Array();\
1671
  var lb = bar_name.length;\
1672
  var idx = 0;\
1673
  var dx = parseInt(0.5*i*bar_width);\
1674
  for( var p = 0 ; p < lb ; p = p + 3 ){\
1675
   bar_x[idx] = x2px(bar_name[p]);\
1676
   bar_y[idx] = y2px(bar_name[p+1]);\
1677
   barcolor[idx] = bar_name[p+2];\
1678
   idx++;\
1679
  };\
1680
  ctx.globalAlpha = fill_opacity;\
1681
  for( var p = 0; p < idx ; p++ ){\
1682
   ctx.beginPath();\
1683
   ctx.strokeStyle = barcolor[p];\
1684
   ctx.fillStyle = barcolor[p];\
1685
   ctx.rect(bar_x[p]-0.4*x2step+dx,bar_y[p],bar_width,zero_y - bar_y[p]);\
1686
   ctx.fill();\
1687
   ctx.stroke();\
1688
   ctx.closePath();\
1689
  };\
1690
 };\
1691
 ctx.restore();");
18553 bpr 1692
        }
1693
        if( linegraph_cnt != 0 ){
1694
          fprintf(js_include_file,"ctx.save();\
15111 schaersvoo 1695
 ctx.globalAlpha = 1.0;\
1696
 var i = 0;\
1697
 var line_name = eval('linegraph_'+i);\
1698
 while ( typeof(line_name) !== 'undefined' ){\
1699
  ctx.strokeStyle = 'rgba('+line_name[0]+','+stroke_opacity+')';\
1700
  ctx.lineWidth = parseInt(line_name[1]);\
1701
  if(line_name[2] == \"1\"){\
1702
   var d1 = parseInt(line_name[3]);\
1703
   var d2 = parseInt(line_name[4]);\
1704
   if(ctx.setLineDash){ ctx.setLineDash([d1,d2]); } else { ctx.mozDash = [d1,d2];};\
16844 bpr 1705
  } else {\
15111 schaersvoo 1706
  if(ctx.setLineDash){ctx.setLineDash = null;};if(ctx.mozDash){ctx.mozDash = null;}\
1707
  };\
1708
  var data_x = new Array();var data_y = new Array();\
1709
  var lb = line_name.length;var idx = 0;\
1710
  for( var p = 5 ; p < lb ; p = p + 2 ){\
1711
   data_x[idx] = x2px(line_name[p]);\
1712
   data_y[idx] = y2px(line_name[p+1]);\
1713
   idx++;\
1714
  };\
1715
  for( var p = 0; p < idx ; p++){\
1716
   ctx.beginPath();\
1717
   ctx.moveTo(data_x[p],data_y[p]);\
1718
   ctx.lineTo(data_x[p+1],data_y[p+1]);\
1719
   ctx.stroke();\
1720
   ctx.closePath();\
1721
  };\
1722
  i++;\
1723
  try{ line_name = eval('linegraph_'+i); }catch(e){ break; }\
1724
 };\
1725
 ctx.restore();return;");
18553 bpr 1726
        }
1727
        fprintf(js_include_file,"};");
1728
        break;
1729
      case DRAW_PIECHART:
1730
        fprintf(js_include_file,"\n/* draw piecharts */\
15111 schaersvoo 1731
if( typeof(all_fill_patterns) != 'object' ){ var all_fill_patterns = []; };\
1732
function draw_piechart(canvas_type,x_center,y_center,radius, data_color_list,fill_opacity,legend_cnt,font_family,use_filled,use_offset){\
1733
 var obj;\
1734
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
1735
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 1736
 } else {\
15111 schaersvoo 1737
  obj = create_canvas%d(canvas_type,xsize,ysize);\
1738
 };\
1739
 var center_text = use_offset || 0;\
1740
 var ld = data_color_list.length;\
1741
 var sum = 0;\
1742
 var idx = 0;\
1743
 var font_size = parseInt(font_family.replace(/[^0-9\\.]+/g, \"\"));\
1744
 var colors = new Array();\
1745
 var data = new Array();\
1746
 for(var p = 0;p < ld; p = p + 2){\
1747
  data[idx] = parseFloat(data_color_list[p]);\
1748
  sum = sum + data[idx];\
1749
  colors[idx] = data_color_list[p+1];\
1750
  idx++;\
1751
 };\
1752
 if( use_filled > 1 ){\
1753
  var i = 2;\
1754
  for(var p = 0 ;  p < idx ; p++){\
1755
   if(i > 5 ){ i = 2; };\
1756
   var pat = create_Pattern(0,0,i,colors[p]); all_fill_patterns[p] = pat;i++;\
1757
  };\
1758
 };\
1759
 var ctx = obj.getContext(\"2d\");\
1760
 ctx.save();\
1761
 var angle;\
1762
 var angle_end = 0;\
1763
 var offset = Math.PI / 2;\
1764
 ctx.globalAlpha = fill_opacity;\
1765
 var angles = [];\
1766
 for(var p=0; p < idx; p++){\
1767
  ctx.beginPath();\
1768
  ctx.moveTo(x_center,y_center);\
1769
  angle = Math.PI * (2 * data[p] / sum);\
1770
  ctx.arc(x_center,y_center, radius, angle_end - offset, angle_end + angle - offset, false);\
1771
  ctx.lineTo(x_center, y_center);\
1772
  if( use_filled > 1 ){ ctx.fillStyle = all_fill_patterns[p]; }else{ ctx.fillStyle = colors[p];};\
1773
  ctx.fill();\
1774
  ctx.closePath();\
1775
  angles.push(angle_end + angle - offset);\
1776
  angle_end  = angle_end + angle;\
1777
 };\
1778
 if(typeof(legend0) !== 'undefined'){\
1779
  var legenda = eval(\"legend\"+legend_cnt);\
1780
  ctx.globalAlpha = 1.0;\
1781
  ctx.font = font_family;\
1782
  var y_offset = font_size;\
1783
  var x_offset = 0;\
1784
  var txt;var txt_size;\
1785
  for(var p = 0; p < idx; p++){\
1786
   ctx.fillStyle = colors[p];\
1787
   txt = legenda[p];\
1788
   txt_size = ctx.measureText(txt).width + 2;\
1789
   if(center_text == 4){\
1790
    ctx.save();\
1791
    ctx.translate(x_center, y_center);\
1792
    ctx.rotate(angles[p]);\
1793
    ctx.fillText(txt,radius-txt_size,0);\
1794
    ctx.restore();\
16844 bpr 1795
   } else {\
15111 schaersvoo 1796
    if( x_center + radius + txt_size > xsize ){ x_offset =  x_center + radius + txt_size - xsize;} else { x_offset = 0; };\
1797
    ctx.fillText(txt,x_center + radius - x_offset, y_center - radius + y_offset);\
1798
    y_offset = parseInt(y_offset + 1.5*font_size);\
1799
   };\
1800
  };\
1801
 };\
1802
 ctx.restore();\
1803
};",canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 1804
        break;
15111 schaersvoo 1805
 
18553 bpr 1806
      case DRAW_JSBOXPLOT:
1807
        fprintf(js_include_file,"\n/* draw jsboxplots */\
15111 schaersvoo 1808
if( typeof(all_fill_patterns) != 'object' ){ var all_fill_patterns = []; };\
1809
function statistics(data){\
1810
 var len = data.length;\
1811
 var min = 10000000;\
1812
 var max = -10000000;\
1813
 var sum = 0;var d;\
1814
 for(var i=0;i<len;i++){\
1815
  d = data[i];\
1816
  if(d < min){min = d;}else{if(d > max){max = d;};};\
1817
  sum+= parseFloat(data[i]);\
1818
 };\
1819
 var mean = parseFloat(sum/len);\
1820
 var variance = 0;\
1821
 for(var i=0;i<len;i++){\
1822
  d = data[i];\
1823
  variance += (d - mean)*(d - mean);\
1824
 };\
1825
 variance = parseFloat(variance / len);\
1826
 var std = Math.sqrt(variance);\
1827
 data.sort(function(a,b){return a - b;});\
1828
 var median;var Q1;var Q3;\
1829
 var half = Math.floor(0.5*len);\
1830
 var q1 = Math.floor(0.25*len);\
1831
 var q3 = Math.floor(0.75*len);\
1832
 var half = Math.floor(0.5*len);\
1833
 if(len %%2 == 1){\
1834
  median = data[half];\
1835
  Q1 = data[q1];\
1836
  Q3 = data[q3];\
16844 bpr 1837
 } else {\
15111 schaersvoo 1838
  median = (data[half - 1] + data[half] )/2;\
1839
  Q1 = (data[q1 - 1] + data[q1] )/2;\
1840
  Q3 = (data[q3 - 1] + data[q3] )/2;\
1841
 };\
1842
 return [min,Q1,median,Q3,max];\
1843
};");
18553 bpr 1844
        break;
1845
      case DRAW_BOXPLOT:
1846
        fprintf(js_include_file,"\n/* draw boxplots */\
15111 schaersvoo 1847
if( typeof(all_fill_patterns) != 'object' ){ var all_fill_patterns = []; };\
1848
draw_boxplot = function(canvas_type,xy,hw,cxy,data,line_width,stroke_color,stroke_opacity,fill_color,fill_opacity,use_filled,use_dashed,dashtype0,dashtype1){\
1849
 var obj;\
1850
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){obj = document.getElementById(\"wims_canvas%d\"+canvas_type);}else{obj = create_canvas%d(canvas_type,xsize,ysize);};\
1851
 var ctx = obj.getContext(\"2d\");\
1852
 ctx.clearRect(0,0,xsize,ysize);\
1853
 ctx.save();\
1854
 ctx.lineWidth = line_width;\
1855
 ctx.strokeStyle =  \"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
1856
 var colors = new Array(2);\
1857
 colors[0] = \"rgba(\"+fill_color+\",\"+fill_opacity+\")\";\
1858
 colors[1] = \"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
1859
 if( use_filled > 1 ){\
1860
   var pat = create_Pattern(0,0,3,colors[0]);\
1861
   all_fill_patterns[0] = pat;\
1862
   pat = create_Pattern(0,0,4,colors[1]);\
1863
   all_fill_patterns[1] = pat;\
1864
 };\
1865
 if(use_dashed == 1){if(ctx.setLineDash){ctx.setLineDash([dashtype0,dashtype1]);}else{if(ctx.mozDash){ ctx.mozDash = [dashtype0,dashtype1];};};};\
1866
 var hh = 0.25*hw;\
1867
 switch(boxplot_source){\
1868
  case 1: if( typeof(jsboxplot_data) === 'undefined'){return;};data = statistics(jsboxplot_data);break;\
1869
  case 2: if( typeof(student_boxplot_data) === 'undefined'){return;};data = statistics(student_boxplot_data);break;\
1870
  case 3: if( typeof(student_boxplot) === 'undefined'){return;};data = student_boxplot;break;\
1871
  default: break;\
1872
 };\
1873
 var min,Q1,median,Q3,max;\
1874
 if(xy == 1 ){\
1875
  min=x2px(data[0]);Q1=x2px(data[1]);median=x2px(data[2]);Q3=x2px(data[3]);max=x2px(data[4]);\
1876
  hh = Math.abs(y2px(hh) - y2px(ystart));\
1877
  hw = Math.abs(y2px(hw) - y2px(ystart));\
1878
  cxy = y2px(cxy);\
1879
  ctx.beginPath();\
1880
  ctx.moveTo(min,cxy);\
1881
  ctx.lineTo(Q1,cxy);\
1882
  ctx.moveTo(Q3,cxy);\
1883
  ctx.lineTo(max,cxy);\
1884
  ctx.moveTo(min,cxy+hh);\
1885
  ctx.lineTo(min,cxy-hh);\
1886
  ctx.moveTo(max,cxy+hh);\
1887
  ctx.lineTo(max,cxy-hh);\
1888
  ctx.closePath();\
1889
  ctx.stroke();\
1890
  ctx.beginPath();\
1891
  ctx.rect(Q1,cxy-2*hh,median-Q1,hw);\
1892
  ctx.closePath();\
1893
  if( use_filled != 0 ){\
1894
   if( use_filled == 1 ) {ctx.fillStyle = colors[0]; }else{ ctx.fillStyle = all_fill_patterns[0] };\
1895
   ctx.fill();\
1896
  };\
1897
  ctx.stroke();\
1898
  ctx.beginPath();\
1899
  ctx.rect(median,cxy-2*hh,Q3-median,hw);\
1900
  ctx.closePath();\
1901
  if( use_filled != 0 ){\
1902
   if( use_filled == 1 ) {ctx.fillStyle = colors[1]; }else{ ctx.fillStyle = all_fill_patterns[1] };\
1903
   ctx.fill();\
1904
  };\
1905
  ctx.stroke();\
1906
 }else{\
1907
  min=y2px(data[0]);Q1=y2px(data[1]);median=y2px(data[2]);Q3=y2px(data[3]);max=y2px(data[4]);\
1908
  hh = Math.abs(x2px(hh) - x2px(xstart));\
1909
  hw = Math.abs(x2px(hw) - x2px(xstart));\
1910
  cxy = x2px(cxy);\
1911
  ctx.beginPath();\
1912
  ctx.moveTo(cxy,min);\
1913
  ctx.lineTo(cxy,Q1);\
1914
  ctx.moveTo(cxy,Q3);\
1915
  ctx.lineTo(cxy,max);\
1916
  ctx.moveTo(cxy + hh,min);\
1917
  ctx.lineTo(cxy - hh,min);\
1918
  ctx.moveTo(cxy + hh,max);\
1919
  ctx.lineTo(cxy - hh,max);\
1920
  ctx.closePath;\
1921
  ctx.stroke();\
1922
  ctx.beginPath();\
1923
  ctx.rect(cxy - 2*hh,Q1,hw,median - Q1);\
1924
  ctx.closePath();\
1925
  if( use_filled != 0 ){\
1926
   if( use_filled == 1 ) {ctx.fillStyle = colors[0]; }else{ ctx.fillStyle = all_fill_patterns[0] };\
1927
   ctx.fill();\
1928
  };\
1929
  ctx.stroke();\
1930
  ctx.beginPath();\
1931
  ctx.rect(cxy - 2*hh,median,hw,Q3 - median);\
1932
  ctx.closePath();\
1933
  if( use_filled != 0 ){\
1934
   if( use_filled == 1 ) {ctx.fillStyle = colors[1]; }else{ ctx.fillStyle = all_fill_patterns[1] };\
1935
   ctx.fill();\
1936
  };\
1937
  ctx.stroke();\
1938
 };\
1939
 ctx.restore();};",canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 1940
        break;
1941
      case DRAW_CENTERSTRING:
1942
        fprintf(js_include_file,"\n/* draw centerstring */\
15111 schaersvoo 1943
var draw_centerstring = function(canvas_type,y,font_family,stroke_color,stroke_opacity,text){\
1944
 var obj;\
1945
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
1946
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 1947
 } else {\
15111 schaersvoo 1948
  obj = create_canvas%d(canvas_type,xsize,ysize);\
1949
 };\
1950
 var ctx = obj.getContext(\"2d\");\
1951
 ctx.save();\
1952
 ctx.clearRect(0,0,xsize,ysize);\
1953
 ctx.font = font_family;\
1954
 ctx.fillStyle = \"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
1955
 var stringwidth = ctx.measureText(text).width;\
1956
 var x = parseInt((xsize - stringwidth)/2);if( x < 0 ){x = 0;};\
1957
 ctx.fillText(text,x,y2px(y));\
1958
 ctx.restore();\
1959
return;\
1960
};",canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 1961
        break;
1962
      case DRAW_TEXTS:
1963
        fprintf(js_include_file,"\n/* draw text */\
15111 schaersvoo 1964
var draw_text = function(canvas_type,x,y,font_size,font_family,stroke_color,stroke_opacity,angle2,text,use_rotate,angle,use_offset){\
1965
 var obj;\
1966
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
1967
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 1968
 } else {\
15111 schaersvoo 1969
  obj = create_canvas%d(canvas_type,xsize,ysize);\
1970
 };\
1971
 var ctx = obj.getContext(\"2d\");\
1972
 if( font_family != 'null' ){\
1973
  ctx.font = font_family;\
16844 bpr 1974
 } else {\
15313 schaersvoo 1975
  ctx.font = font_size+'px Arial';\
15111 schaersvoo 1976
 };\
1977
 if( use_offset == 3 ){if(angle2 < 0 ){ y = y + 0.8*font_size; x = x + (Math.cos(angle2))*font_size; }else{y = y - 0.8*font_size; x = x + (Math.sin(angle2))*font_size;};};\
1978
 if(angle2 == 0 && angle != 0){\
1979
  ctx.save();\
1980
  if(use_rotate == 1 ){\
1981
  ctx.rotate(angle*Math.PI/180);};\
1982
  ctx.restore();\
1983
 };\
1984
 ctx.fillStyle = \"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
1985
 if(angle2 != 0){\
1986
  ctx.save();\
1987
  ctx.translate(x,y);\
1988
  ctx.rotate((360-angle2)*(Math.PI / 180));\
1989
  ctx.fillText(text,0,0);\
1990
  ctx.restore();\
16844 bpr 1991
 } else {\
15111 schaersvoo 1992
  ctx.fillText(text,x,y);\
1993
 };\
1994
 return;\
1995
};",canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 1996
        break;
1997
      case DRAW_INPUTS:
15111 schaersvoo 1998
/* function center_input may be used for other centering...if not? integrate it in draw_static_inputs...*/
18553 bpr 1999
        fprintf(js_include_file,"\n/* draw static input fields */\
15111 schaersvoo 2000
if(typeof(input_cnt) !== 'undefined' ){var input_cnt = 0;};\
2001
var draw_static_inputs = function(root_id,num,x,y,size,readonly,css_class,value,use_offset){\
2002
var input = document.createElement(\"input\");\
2003
input.setAttribute(\"id\",\"canvas_input\"+num);\
2004
input.setAttribute(\"style\",\"text-align:center;position:absolute;left:\"+x+\"px;top:\"+y+\"px;\");\
2005
if(css_class != \"none\"){input.setAttribute(\"class\",css_class);};\
2006
input.setAttribute(\"size\",size);\
2007
input.setAttribute(\"value\",value);\
2008
input.addEventListener(\"touchstart\", function(e){this.focus();},false);\
2009
if( readonly == 0 || wims_status == \"done\" ){ input.setAttribute(\"readonly\",\"readonly\");if( wims_status == \"done\" ){input.setAttribute(\"value\",\"\");};};\
2010
canvas_div.appendChild(input);\
2011
if(use_offset != 0 ){ center_input('canvas_input'+num,x,y,css_class);};\
2012
input_cnt++;\
2013
};\
2014
function center_input(id,x,y,css_class){\
2015
 var inp = document.getElementById(id);\
2016
 var pos = inp.getBoundingClientRect();\
2017
 var center_x = parseInt(x - 0.5*(pos.width));\
2018
 var center_y = parseInt(y - 0.5*(pos.height));\
2019
 try{if(css_class != \"none\"){inp.setAttribute(\"class\",css_class);};inp.setAttribute(\"style\",\"text-align:center;position:absolute;left:\"+center_x+\"px;top:\"+center_y+\"px;\");}\
2020
 catch(e){console.log('error :'+e);return;};\
2021
};");
18553 bpr 2022
        break;
2023
      case DRAW_TEXTAREAS:
2024
        fprintf(js_include_file,"\n/* draw text area inputfields */\
15111 schaersvoo 2025
var draw_textareas = function(root_id,num,x,y,cols,rows,readonly,style,value){\
2026
var canvas_div = document.getElementById(\"canvas_div\"+root_id);\
2027
var textarea = document.createElement(\"textarea\");\
2028
textarea.setAttribute(\"id\",\"canvas_input\"+num);\
2029
textarea.setAttribute(\"style\",\"position:absolute;left:\"+x+\"px;top:\"+y+\"px;\"+style);\
2030
textarea.setAttribute(\"cols\",cols);\
2031
textarea.setAttribute(\"rows\",rows);\
2032
textarea.value = value;\
2033
if( readonly == 0 || wims_status == \"done\" ){ textarea.setAttribute(\"readonly\",\"readonly\");if( wims_status == \"done\" ){textarea.value=\"\";};};\
2034
canvas_div.appendChild(textarea);};");
18553 bpr 2035
        break;
2036
      case DRAW_PIXELS:
2037
        fprintf(js_include_file,"\n/* draw pixel */\
15111 schaersvoo 2038
var draw_setpixel = function(x,y,color,opacity,pixelsize){\
2039
 var idx = 2000+Math.ceil(1000*(Math.random()));\
2040
 var canvas = create_canvas%d(idx,xsize,ysize);\
2041
 var d = 0.5*pixelsize;\
2042
 var ctx = canvas.getContext(\"2d\");\
2043
 if(pixelsize%%2 == 1){ ctx.translate(0.5,0.5);};\
2044
 ctx.fillStyle = \"rgba(\"+color+\",\"+opacity+\")\";\
2045
 ctx.clearRect(0,0,xsize,ysize);\
2046
 for(var p=0; p<x.length;p++){\
2047
  ctx.fillRect( x2px(x[p]) - d, y2px(y[p]) - d , pixelsize, pixelsize );\
2048
 };\
2049
 ctx.fill();ctx.stroke();\
2050
};",canvas_root_id);
18553 bpr 2051
        break;
2052
      case DRAW_CLOCK:
2053
        fprintf(js_include_file,"\n/* command clock */\
15111 schaersvoo 2054
var clock_canvas = create_canvas%d(%d,xsize,ysize);\
2055
var clock_ctx = clock_canvas.getContext(\"2d\");\
2056
var clock = function(xc,yc,radius,H,M,S,type,interaction,h_color,m_color,s_color,bg_color,fg_color){\
2057
 clock_ctx.clearRect(xc - radius,yc - radius,2*radius,2*radius);\
2058
 clock_ctx.save();\
2059
 clock_ctx.globalAlpha = clock_bg_opacity;\
2060
 this.type = type || 0;\
2061
 this.interaction = interaction || 0;\
2062
 this.H = H;\
2063
 this.M = M;\
2064
 this.S = S;\
2065
 this.xc = xc || xsize/2;\
2066
 this.yc = yc || ysize/2;\
2067
 this.radius = radius || xsize/4;\
2068
 var font_size = parseInt(0.2*this.radius);\
2069
 this.H_color = h_color || \"black\";\
2070
 this.M_color = m_color || \"black\";\
2071
 this.S_color = s_color || \"black\";\
2072
 this.fg_color = fg_color || \"black\";\
2073
 this.bg_color = bg_color || \"white\";\
2074
 clock_ctx.translate(this.xc,this.yc);\
2075
 clock_ctx.beginPath();\
2076
 clock_ctx.arc(0,0,this.radius,0,2*Math.PI,false);\
2077
 clock_ctx.fillStyle = this.bg_color;\
2078
 clock_ctx.fill();\
2079
 clock_ctx.closePath();\
2080
 clock_ctx.beginPath();\
2081
 clock_ctx.font = font_size+\"px Arial\";\
2082
 clock_ctx.fillStyle = this.fg_color;\
2083
 clock_ctx.textAlign = \"center\";\
2084
 clock_ctx.textBaseline = 'middle';\
2085
 var angle;var x1,y1,x2,y2;\
2086
 var angle_cos;var angle_sin;\
2087
 clock_ctx.globalAlpha = clock_fg_opacity;\
2088
 switch(type){\
2089
 case 0:clock_ctx.beginPath();\
2090
 for(var p = 1; p <= 12 ; p++){\
2091
  angle_cos = this.radius*(Math.cos(p * (Math.PI * 2) / 12));\
2092
  angle_sin = this.radius*(Math.sin(p * (Math.PI * 2) / 12));\
2093
  x1 = 0.8*angle_cos;y1 = 0.8*angle_sin;x2 = angle_cos;y2 = angle_sin;\
2094
  clock_ctx.moveTo(x1,y1);\
2095
  clock_ctx.lineTo(x2,y2);\
2096
 };\
2097
 for(var p = 1; p <= 60 ; p++){\
2098
  angle_cos = this.radius*(Math.cos(p * (Math.PI * 2) / 60));\
2099
  angle_sin = this.radius*(Math.sin(p * (Math.PI * 2) / 60));\
2100
  x1 = 0.9*angle_cos;y1 = 0.9*angle_sin;x2 = angle_cos;y2 = angle_sin;\
2101
  clock_ctx.moveTo(x1,y1);\
2102
  clock_ctx.lineTo(x2,y2);\
2103
 };\
2104
 clock_ctx.closePath();\
2105
 clock_ctx.stroke();\
2106
 break;\
2107
 case 1:\
2108
 for(var p= 1; p <= 12 ; p++){ angle = (p - 3) * (Math.PI * 2) / 12;x1 = 0.9*this.radius*Math.cos(angle);y1 = 0.9*this.radius*Math.sin(angle);clock_ctx.fillText(p, x1, y1);};break;\
2109
 case 2:\
2110
 for(var p= 1; p <= 12 ; p++){ angle = (p - 3) * (Math.PI * 2) / 12;x1 = 0.8*this.radius*Math.cos(angle);y1 = 0.8*this.radius*Math.sin(angle);clock_ctx.fillText(p, x1, y1);};\
2111
 clock_ctx.beginPath();\
2112
 for(var p = 1; p <= 12 ; p++){\
2113
  angle_cos = this.radius*(Math.cos(p * (Math.PI * 2) / 12));\
2114
  angle_sin = this.radius*(Math.sin(p * (Math.PI * 2) / 12));\
2115
  x1 = 0.9*angle_cos;y1 = 0.9*angle_sin;x2 = angle_cos;y2 = angle_sin;\
2116
  clock_ctx.moveTo(x1,y1);\
2117
  clock_ctx.lineTo(x2,y2);\
2118
 };\
2119
 for(var p = 1; p <= 60 ; p++){\
2120
  angle_cos = this.radius*(Math.cos(p * (Math.PI * 2) / 60));\
2121
  angle_sin = this.radius*(Math.sin(p * (Math.PI * 2) / 60));\
2122
  x1 = 0.95*angle_cos;y1 = 0.95*angle_sin;x2 = angle_cos;y2 = angle_sin;\
2123
  clock_ctx.moveTo(x1,y1);\
2124
  clock_ctx.lineTo(x2,y2);\
2125
 };\
2126
 clock_ctx.closePath();\
2127
 clock_ctx.stroke();\
2128
 break;\
2129
 };\
2130
 angle = (this.H - 3 + this.M/60 ) * 2 * Math.PI / 12;\
2131
 clock_ctx.rotate(angle);\
2132
 clock_ctx.beginPath();\
2133
 clock_ctx.moveTo(-3, -2);\
2134
 clock_ctx.lineTo(-3, 2);\
2135
 clock_ctx.lineTo(this.radius * 0.6, 1);\
2136
 clock_ctx.lineTo(this.radius  * 0.6, -1);\
2137
 clock_ctx.fillStyle = this.H_color;\
2138
 clock_ctx.fill();\
2139
 clock_ctx.rotate(-angle);\
2140
 angle = (this.M - 15 + this.S/60) * 2 * Math.PI / 60;\
2141
 clock_ctx.rotate(angle);\
2142
 clock_ctx.beginPath();\
2143
 clock_ctx.moveTo(-3, -2);\
2144
 clock_ctx.lineTo(-3, 2);\
2145
 clock_ctx.lineTo(this.radius  * 0.8, 1);\
2146
 clock_ctx.lineTo(this.radius  * 0.8, -1);\
2147
 clock_ctx.fillStyle = this.M_color;\
2148
 clock_ctx.fill();\
2149
 clock_ctx.rotate(-angle);\
2150
 angle = (this.S - 15) * 2 * Math.PI / 60;\
2151
 clock_ctx.rotate(angle);\
2152
 clock_ctx.beginPath();\
2153
 clock_ctx.moveTo(0,0);\
2154
 clock_ctx.lineTo(this.radius  * 0.9, 1);\
2155
 clock_ctx.lineTo(this.radius  * 0.9, -1);\
2156
 clock_ctx.strokeStyle = this.S_color;\
2157
 clock_ctx.stroke();\
2158
 clock_ctx.restore();\
2159
};",canvas_root_id,CLOCK_CANVAS);
18553 bpr 2160
        break;
2161
      case DRAW_LATTICE:
2162
        fprintf(js_include_file,"\n/* draw lattice */\
15111 schaersvoo 2163
var draw_lattice = function(canvas_type,line_width,x0,y0,dx1,dy1,dx2,dy2,n1,n2,fill_color,fill_opacity,stroke_color,stroke_opacity,use_rotate,angle,use_filled){\
2164
 var obj;\
2165
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
2166
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 2167
 } else {\
15111 schaersvoo 2168
  obj = create_canvas%d(canvas_type,xsize,ysize);\
2169
 };\
2170
 var ctx = obj.getContext(\"2d\");\
2171
 ctx.save();\
2172
 if(use_rotate == 1 ){ctx.rotate(angle*Math.PI/180);}\
2173
 var color = \"rgba(\"+fill_color+\",\"+fill_opacity+\")\";\
2174
 if(use_filled > 1 ){ if(! all_fill_patterns[use_filled] ){ var pat = create_Pattern(0,0,use_filled,color); all_fill_patterns[use_filled] = pat;};ctx.fillStyle = all_fill_patterns[use_filled]; } else { ctx.fillStyle = color;};\
2175
 ctx.strokeStyle=\"rgba(\"+stroke_color+\",\"+stroke_opacity+\")\";\
2176
 var radius = line_width;\
2177
 var x = 0;\
2178
 var y = 0;\
2179
 var x_step_px = xsize/(xmax-xmin);\
2180
 var y_step_px = ysize/(ymax-ymin);\
2181
 var xv1 = dx1*x_step_px;\
2182
 var yv1 = dy1*y_step_px;\
2183
 var xv2 = dx2*x_step_px;\
2184
 var yv2 = dy2*y_step_px;\
2185
 for(var p = 0; p < n1 ;p++){\
2186
  x = p*xv1 + x0;\
2187
  y = p*yv1 + y0;\
2188
  for(var c = 0; c < n2 ; c++){\
2189
   ctx.beginPath();\
2190
   ctx.arc(x+c*xv2,y+c*yv2,radius,0,2*Math.PI,false);\
2191
   ctx.fill();\
2192
   ctx.stroke();\
2193
   ctx.closePath();\
2194
  };\
2195
 };\
2196
 ctx.restore();\
2197
 return;\
2198
};",canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 2199
        break;
2200
      case DRAW_XYLOGSCALE:
2201
        fprintf(js_include_file,"\n/* draw xylogscale */\
15111 schaersvoo 2202
var draw_grid%d = function(canvas_type,line_width,major_color,minor_color,major_opacity,minor_opacity,font_size,font_family,font_color,use_axis_numbering,precision){\
2203
 var obj;\
2204
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
2205
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 2206
 } else {\
15111 schaersvoo 2207
  obj = create_canvas%d(canvas_type,xsize,ysize);\
2208
 };\
2209
 var ctx = obj.getContext(\"2d\");\
2210
 ctx.clearRect(0,0,xsize,ysize);\
2211
 ctx.save();\
2212
 var xmarge;var ymarge;var x_e;var y_e;var num;var corr;var xtxt;var ytxt;\
2213
 var x_min = Math.log(xmin)/Math.log(xlogbase);\
2214
 var x_max = Math.log(xmax)/Math.log(xlogbase);\
2215
 var y_min = Math.log(ymin)/Math.log(ylogbase);\
2216
 var y_max = Math.log(ymax)/Math.log(ylogbase);\
2217
 if(use_axis_numbering != -1){\
2218
  ctx.font = font_family;\
2219
  xmarge = ctx.measureText(ylogbase+'^'+y_max.toFixed(0)+' ').width;\
2220
  ymarge = parseInt(1.5*font_size);\
2221
  ctx.save();\
2222
  ctx.fillStyle=\"rgba(255,215,0,0.2)\";\
2223
  ctx.rect(0,0,xmarge,ysize);\
2224
  ctx.rect(0,ysize-ymarge,xsize,ysize);\
2225
  ctx.fill();\
2226
  ctx.restore();\
2227
 }else{xmarge = 0;ymarge = 0;};\
2228
 if( typeof(xaxislabel) !== 'undefined' ){\
2229
  ctx.save();\
15313 schaersvoo 2230
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2231
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2232
  corr =  ctx.measureText(xaxislabel).width;\
2233
  ctx.fillText(xaxislabel,xsize - 1.5*corr,ysize - 2*font_size);\
2234
  ctx.restore();\
2235
 };\
2236
 if( typeof(yaxislabel) !== 'undefined' ){\
2237
  ctx.save();\
15313 schaersvoo 2238
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2239
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2240
  corr = ctx.measureText(yaxislabel).width;\
2241
  ctx.translate(xmarge+font_size,corr+font_size);\
2242
  ctx.rotate(-0.5*Math.PI);\
2243
  ctx.fillText(yaxislabel,0,0);\
2244
  ctx.restore();\
2245
 };\
2246
 ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2247
 ctx.lineWidth = line_width;\
2248
 for(var p = x_min; p <= x_max ; p++){\
2249
  num = Math.pow(xlogbase,p);\
2250
  for(var i = 1 ; i < xlogbase ; i++){\
2251
   x_e = x2px(i*num);\
2252
   if( i == 1 ){\
2253
    ctx.lineWidth = line_width;\
2254
    ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2255
    if( use_axis_numbering != -1 && p > x_min){\
2256
      xtxt = xlogbase+'^'+p.toFixed(0);\
2257
      corr = 0.5*(ctx.measureText(xtxt).width);\
2258
      ctx.fillText(xtxt,x_e - corr,ysize - 4);\
2259
    };\
2260
   }else{\
2261
    ctx.lineWidth = 0.2*line_width;\
2262
    ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2263
   };\
2264
   if( x_e >= xmarge ){\
2265
    ctx.beginPath();\
2266
    ctx.moveTo(x_e,0);\
2267
    ctx.lineTo(x_e,ysize - ymarge);\
2268
    ctx.stroke();\
2269
    ctx.closePath();\
2270
   };\
2271
  };\
2272
 };\
2273
 for(var p = y_min; p <= y_max ; p++){\
2274
  num = Math.pow(ylogbase,p);\
2275
  for(var i = 1 ; i < ylogbase ; i++){\
2276
   y_e = y2px(i*num);\
2277
   if( i == 1 ){\
2278
    ctx.lineWidth = line_width;\
2279
    ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2280
    if( use_axis_numbering != -1 && p > y_min){\
2281
     ctx.fillText(ylogbase+'^'+p.toFixed(0),0,y_e);\
2282
    };\
2283
   }else{\
2284
    ctx.lineWidth = 0.2*line_width;\
2285
    ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2286
   };\
2287
   ctx.beginPath();\
2288
   ctx.moveTo(xmarge,y_e);\
2289
   ctx.lineTo(xsize,y_e);\
2290
   ctx.stroke();\
2291
   ctx.closePath();\
2292
  };\
2293
 };\
2294
 ctx.restore();\
2295
};",canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 2296
        break;
2297
      case DRAW_XLOGSCALE:
2298
        fprintf(js_include_file,"\n/* draw xlogscale */\
15111 schaersvoo 2299
var draw_grid%d = function(canvas_type,line_width,major_color,minor_color,major_opacity,minor_opacity,font_size,font_family,font_color,use_axis_numbering,ymajor,yminor,precision){\
2300
 var obj;\
2301
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
2302
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 2303
 } else {\
15111 schaersvoo 2304
  obj = create_canvas%d(canvas_type,xsize,ysize);\
2305
 };\
2306
 var ctx = obj.getContext(\"2d\");\
2307
 ctx.clearRect(0,0,xsize,ysize);\
2308
 ctx.save();\
2309
 ctx.lineWidth = line_width;\
2310
 var prec = Math.log(precision)/Math.log(10);\
2311
 var x_min = Math.log(xmin)/Math.log(xlogbase);\
2312
 var x_max = Math.log(xmax)/Math.log(xlogbase);\
2313
 var y_min = 0;var y_max = ysize;var x_e;var corr;\
2314
 var xtxt;var ytxt;var num;var xmarge;var ymarge;\
2315
 if(use_axis_numbering != -1){\
2316
  ctx.font = font_family;\
2317
  xmarge = ctx.measureText(ymax.toFixed(prec)+' ').width;\
2318
  ymarge = parseInt(1.5*font_size);\
2319
  ctx.save();\
2320
  ctx.fillStyle=\"rgba(255,215,0,0.2)\";\
2321
  ctx.rect(0,0,xmarge,ysize);\
2322
  ctx.rect(0,ysize-ymarge,xsize,ysize);\
2323
  ctx.fill();\
2324
  ctx.restore();\
2325
 }else{xmarge = 0;ymarge = 0;};\
2326
 if( typeof(xaxislabel) !== 'undefined' ){\
2327
  ctx.save();\
15313 schaersvoo 2328
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2329
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2330
  corr =  ctx.measureText(xaxislabel).width;\
2331
  ctx.fillText(xaxislabel,xsize - 1.5*corr,ysize - 2*font_size);\
2332
  ctx.restore();\
2333
 };\
2334
 if( typeof(yaxislabel) !== 'undefined' ){\
2335
  ctx.save();\
15313 schaersvoo 2336
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2337
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2338
  corr = ctx.measureText(yaxislabel).width;\
2339
  ctx.translate(xmarge+font_size,corr+font_size);\
2340
  ctx.rotate(-0.5*Math.PI);\
2341
  ctx.fillText(yaxislabel,0,0);\
2342
  ctx.restore();\
2343
 };\
2344
 ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2345
 ctx.lineWidth = line_width;\
2346
 for(var p = x_min; p <= x_max ; p++){\
2347
  num = Math.pow(xlogbase,p);\
2348
  for(var i = 1 ; i < xlogbase ; i++){\
2349
   x_e = x2px(i*num);\
2350
   if( i == 1 ){\
2351
     ctx.lineWidth = line_width;\
2352
     ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2353
    if( use_axis_numbering != -1 && p > x_min ){\
2354
      xtxt = xlogbase+'^'+p.toFixed(0);\
2355
      corr = 0.5*(ctx.measureText(xtxt).width);\
2356
      ctx.fillText(xtxt,x_e - corr,ysize - 4);\
2357
    };\
2358
   }else{\
2359
    ctx.lineWidth = 0.2*line_width;\
2360
    ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2361
   };\
2362
   if( x_e >= xmarge ){\
2363
    ctx.beginPath();\
2364
    ctx.moveTo(x_e,0);\
2365
    ctx.lineTo(x_e,ysize - ymarge);\
2366
    ctx.stroke();\
2367
    ctx.closePath();\
2368
   };\
2369
  };\
2370
 };\
2371
 var stepy = Math.abs(y2px(ymajor) - y2px(0));\
2372
 var minor_step = stepy / yminor;\
2373
 for(var y = 0 ; y < ysize - stepy ; y = y + stepy){\
2374
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2375
  ctx.lineWidth = line_width;\
2376
  ctx.beginPath();\
2377
  ctx.moveTo(xmarge,y);\
2378
  ctx.lineTo(xsize,y);\
2379
  ctx.stroke();\
2380
  ctx.closePath();\
2381
  if( use_axis_numbering != -1){\
2382
   ytxt = (px2y(y)).toFixed(prec);\
2383
   ctx.fillText( ytxt,0 ,y + 0.5*font_size );\
2384
  };\
2385
  for(var dy = 1 ; dy < yminor ; dy++){\
2386
   ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2387
   ctx.lineWidth = 0.2*line_width;\
2388
   ctx.beginPath();\
2389
   ctx.moveTo(xmarge,y+dy*minor_step);\
2390
   ctx.lineTo(xsize,y+dy*minor_step);\
2391
   ctx.stroke();\
2392
   ctx.closePath();\
2393
  };\
2394
 };\
2395
 ctx.restore();\
2396
};",canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 2397
        break;
2398
      case DRAW_YLOGSCALE:
2399
        fprintf(js_include_file,"\n/* draw ylogscale */\
15111 schaersvoo 2400
var draw_grid%d = function(canvas_type,line_width,major_color,minor_color,major_opacity,minor_opacity,font_size,font_family,font_color,use_axis_numbering,xmajor,xminor,precision){\
2401
 var obj;\
2402
 if( document.getElementById(\"wims_canvas%d\"+canvas_type) ){\
2403
  obj = document.getElementById(\"wims_canvas%d\"+canvas_type);\
16844 bpr 2404
 } else {\
15111 schaersvoo 2405
  obj = create_canvas%d(canvas_type,xsize,ysize);\
2406
 };\
2407
 var ctx = obj.getContext(\"2d\");\
2408
 ctx.clearRect(0,0,xsize,ysize);\
2409
 ctx.save();\
2410
 ctx.lineWidth = line_width;\
2411
 var y_min = Math.log(ymin)/Math.log(ylogbase);\
2412
 var y_max = Math.log(ymax)/Math.log(ylogbase);\
2413
 var x_min = 0;var x_max = xsize;var y_s;var y_e;var num;var xmarge;var ymarge;\
2414
 if(use_axis_numbering != -1){\
2415
  ctx.font = font_family;\
2416
  xmarge = ctx.measureText(ylogbase+\"^\"+y_max.toFixed(0)+' ').width;\
2417
  ymarge = 2*font_size;\
2418
  ctx.save();\
2419
  ctx.fillStyle=\"rgba(255,215,0,0.2)\";\
2420
  ctx.rect(0,0,xmarge,ysize);\
2421
  ctx.rect(0,ysize-ymarge,xsize,ysize);\
2422
  ctx.fill();\
2423
  ctx.restore();\
2424
 }else{xmarge = 0;ymarge = 0;};\
2425
 if( typeof(xaxislabel) !== 'undefined' ){\
2426
  ctx.save();\
15313 schaersvoo 2427
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2428
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2429
  corr =  ctx.measureText(xaxislabel).width;\
2430
  ctx.fillText(xaxislabel,xsize - 1.5*corr,ysize - 2*font_size);\
2431
  ctx.restore();\
2432
 };\
2433
 if( typeof(yaxislabel) !== 'undefined' ){\
2434
  ctx.save();\
15313 schaersvoo 2435
  ctx.font = \"italic \"+font_size+\"px Arial\";\
15111 schaersvoo 2436
  ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2437
  corr = ctx.measureText(yaxislabel).width;\
2438
  ctx.translate(xmarge+font_size,corr+font_size);\
2439
  ctx.rotate(-0.5*Math.PI);\
2440
  ctx.fillText(yaxislabel,0,0);\
2441
  ctx.restore();\
2442
 };\
2443
 ctx.fillStyle = \"rgba(\"+font_color+\",\"+major_opacity+\")\";\
2444
 ctx.lineWidth = line_width;\
2445
 for(var p = y_min; p <= y_max ; p++){\
2446
  num = Math.pow(ylogbase,p);\
2447
  for(var i = 1 ; i < ylogbase ; i++){\
2448
   y_e = y2px(i*num);\
2449
   if( i == 1 ){\
2450
    ctx.lineWidth = line_width;\
2451
    ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2452
    if( use_axis_numbering != -1 && p > y_min){\
2453
     ctx.fillText(ylogbase+'^'+p.toFixed(0),0,y_e);\
2454
    };\
2455
   }else{\
2456
    ctx.lineWidth = 0.2*line_width;\
2457
    ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2458
   };\
2459
   ctx.beginPath();\
2460
   ctx.moveTo(xmarge,y_e);\
2461
   ctx.lineTo(xsize,y_e);\
2462
   ctx.stroke();\
2463
   ctx.closePath();\
2464
  };\
2465
 };\
2466
 var stepx = Math.abs(x2px(xmajor) - x2px(0));\
2467
 var minor_step = stepx / xminor;\
2468
 var prec = Math.log(precision)/Math.log(10);\
2469
 var xtxt;var corr;var flip = 0;\
2470
 for(var x = stepx ; x < xsize ; x = x + stepx){\
2471
  ctx.strokeStyle=\"rgba(\"+major_color+\",\"+major_opacity+\")\";\
2472
  ctx.lineWidth = line_width;\
2473
  ctx.beginPath();\
2474
  ctx.moveTo(x,ysize-ymarge);\
2475
  ctx.lineTo(x,0);\
2476
  ctx.stroke();\
2477
  ctx.closePath();\
2478
  if( use_axis_numbering != -1){\
2479
   xtxt = (px2x(x)).toFixed(prec);\
2480
   corr = 0.5*(ctx.measureText(xtxt).width);\
2481
   if(flip == 0 ){flip = 1;ctx.fillText( xtxt,x - corr ,ysize - 0.2*font_size );}else{\
2482
   flip = 0;ctx.fillText( xtxt,x - corr ,ysize - 1.2*font_size );};\
2483
  };\
2484
  for(var dx = 1 ; dx < xminor ; dx++){\
2485
   ctx.strokeStyle=\"rgba(\"+minor_color+\",\"+minor_opacity+\")\";\
2486
   ctx.lineWidth = 0.2*line_width;\
2487
   ctx.beginPath();\
2488
   ctx.moveTo(x+dx*minor_step,ysize - ymarge);\
2489
   ctx.lineTo(x+dx*minor_step,0);\
2490
   ctx.stroke();\
2491
   ctx.closePath();\
2492
  };\
2493
 };\
2494
 ctx.restore();\
2495
};",canvas_root_id,canvas_root_id,canvas_root_id,canvas_root_id);
18553 bpr 2496
        break;
2497
      case ADD_USER_INPUTS:
2498
        fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2499
function add_input_xy(){\
2500
if( wims_status == \"done\" ){return;};\
2501
var tooltip_div = document.getElementById(\"tooltip_placeholder_div%d\");\
2502
var input_xy_div = document.createElement('div');\
2503
input_xy_div.id = \"input_xy_div\";\
2504
tooltip_div.appendChild(input_xy_div);",canvas_root_id);
2505
 
18553 bpr 2506
      if(strstr(draw_type,"polyline") != NULL || strstr(draw_type,"point") != NULL || strstr(draw_type,"cross") != NULL ){ /* ( input_x : input_y )*/
2507
        type=1;
2508
        fprintf(js_include_file,"input_xy_div.innerHTML=\"<br><span class='%s'>(<input type='text' size='2' value='' id='userinput_x'/> : <input type='text' size='2' value='' id='userinput_y'/>)<input id='update_button' type='button' value='OK' onclick='' style='color:red;background-color:lightblue;'/><input id='delete_button' type='button' value='NOK' onclick='' style='color:blue;background-color:red;'/></span> \";",css_class);
2509
      }else{
2510
        if(strstr(draw_type,"circle") != NULL ){/* M: (input_x :input_y) R:input_r*/
2511
          type=2;
2512
          fprintf(js_include_file,"input_xy_div.innerHTML=\"<br><span class='%s'>M:&nbsp;(<input type='text' size='2' value='' id='userinput_x'/> : <input type='text' size='2' value='' id='userinput_y'/>) &nbsp;&nbsp;R:&nbsp;<input type='text' size='2' value='' id='userinput_r'/><input id='update_button' type='button' value='OK' onclick='' style='color:red;background-color:lightblue;'/><input id='delete_button' type='button' value='NOK' onclick='' style='color:blue;background-color:red;'/></span> \";",css_class);
2513
        }else{
2514
            type=3;
2515
            if(strstr(draw_type,"segment") != NULL  ||  strstr(draw_type,"line") != NULL  ||  strstr(draw_type,"arrow") != NULL  ){  /* ( input_x1 : input_y1) --- ( input_x2 : input_y2) */
2516
              fprintf(js_include_file,"input_xy_div.innerHTML=\"<br><span class='%s'>(<input type='text' size='2' value='' id='userinput_x1'/> : <input type='text' size='2' value='' id='userinput_y1'/>) &minus;&minus;&minus; (<input type='text' size='2' value='' id='userinput_x2'/> : <input type='text' size='2' value='' id='userinput_y2'/>)<input id='update_button' type='button' value='OK' onclick='' style='color:red;background-color:lightblue;'/><input id='delete_button' type='button' value='NOK' onclick='' style='color:blue;background-color:red;'/></span> \";",css_class);
2517
            }else{
2518
              type=4;
2519
              if(strstr(draw_type,"triang") != NULL ){ /* ( input_x1 : input_y1) --- ( input_x2 : input_y2) --- ( input_x3 : input_y3) */
2520
                fprintf(js_include_file,"input_xy_div.innerHTML=\"<br><span class='%s'>&#8420;&nbsp;&nbsp;<input id='update_button' type='button' value='OK' onclick='' style='color:red;background-color:lightblue;'/><input id='delete_button' type='button' value='NOK' onclick='' style='color:blue;background-color:red;'/><br/>(<input type='text' size='2' value='' id='userinput_x1'/> : <input type='text' size='2' value='' id='userinput_y1'/>)<br/>(<input type='text' size='2' value='' id='userinput_x2'/> : <input type='text' size='2' value='' id='userinput_y2'/>)<br/>(<input type='text' size='2' value='' id='userinput_x3'/> : <input type='text' size='2' value='' id='userinput_y3'/>)</span> \";",css_class);
2521
              }else{
2522
                type=5;
2523
                if(strstr(draw_type,"text") != NULL ){ /*Text: input_r ( input_x1 : input_y1)  */
2524
                  fprintf(js_include_file,"input_xy_div.innerHTML=\"<br><span class='%s'>(<input type='text' size='2' value='' id='userinput_x'/>:<input type='text' size='2' value='' id='userinput_y'/>)&nbsp;<input id='update_button' type='button' value='OK' onclick='' style='color:red;background-color:lightblue;'/><input id='delete_button' type='button' value='NOK' onclick='' style='color:blue;background-color:red;'/></span> \";",css_class);
2525
                }else{canvas_error("command 'userinput' or 'userinput_xy' is not supported for your 'userdraw' type...");
2526
              }
2527
            }
2528
          }
2529
        }
2530
      }
2531
      fprintf(js_include_file,"\
15111 schaersvoo 2532
var update_button = document.getElementById(\"update_button\");\
2533
var delete_button = document.getElementById(\"delete_button\");\
2534
update_button.addEventListener(\"mousedown\",function(e){update_me();redraw_userdraw();return;},false);\
2535
delete_button.addEventListener(\"mousedown\",function(e){remove_last();return;},false);\
2536
};add_input_xy();");
2537
 
18553 bpr 2538
      switch(type){
2539
        case 1:
2540
          fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2541
function update_me(){\
15529 schaersvoo 2542
 var x = safe_eval(document.getElementById('userinput_x').value);\
2543
 var y = safe_eval(document.getElementById('userinput_y').value);\
15111 schaersvoo 2544
 userdraw_x.push(x2px(x));userdraw_y.push(y2px(y));\
2545
 return;\
2546
};");break;
18553 bpr 2547
        case 2:
2548
          fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2549
function update_me(){\
15529 schaersvoo 2550
 var x = safe_eval(document.getElementById('userinput_x').value);\
2551
 var y = safe_eval(document.getElementById('userinput_y').value);\
2552
 var r = safe_eval(document.getElementById('userinput_r').value);\
15111 schaersvoo 2553
 r = scale_x_radius(r);\
2554
 userdraw_radius.push(r);userdraw_x.push(x2px(x));userdraw_y.push(y2px(y));\
2555
 return;\
2556
};");break;
18553 bpr 2557
        case 3:
2558
          fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2559
function update_me(){\
2560
 var x,y;\
2561
 for(var p=1; p<3;p++){\
15529 schaersvoo 2562
  x = safe_eval(document.getElementById('userinput_x'+p).value);\
2563
  y = safe_eval(document.getElementById('userinput_y'+p).value);\
15111 schaersvoo 2564
  userdraw_x.push(x2px(x));userdraw_y.push(y2px(y));\
2565
 };\
2566
 return;\
2567
};");
18553 bpr 2568
          break;
2569
        case 4:
2570
          fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2571
function update_me(){\
2572
 var x,y;\
2573
 for(var p=1; p<4;p++){\
15529 schaersvoo 2574
  x = safe_eval(document.getElementById('userinput_x'+p).value);\
2575
  y = safe_eval(document.getElementById('userinput_y'+p).value);\
15111 schaersvoo 2576
  userdraw_x.push(x2px(x));userdraw_y.push(y2px(y));\
2577
 };\
2578
 return;\
2579
};");
18553 bpr 2580
          break;
2581
        case 5:
15111 schaersvoo 2582
 /* a inputfield with 'id=userdraw_text_abc' is provided by command 'userdraw text,color'*/
18553 bpr 2583
          fprintf(js_include_file,"\n/* add_input_xy */\
15111 schaersvoo 2584
function update_me(){\
15529 schaersvoo 2585
 var x = safe_eval(document.getElementById('userinput_x').value);\
2586
 var y = safe_eval(document.getElementById('userinput_y').value);\
15111 schaersvoo 2587
 var txt = conv_to_unicode(document.getElementById('userdraw_text_abc').value);\
2588
 userdraw_x.push(x2px(x));userdraw_y.push(y2px(y));userdraw_text.push(txt.replace(/\\\\/g,' '));\
2589
 return;\
2590
};");
18553 bpr 2591
          break;
2592
        default: canvas_error("hmmm, I've no idea...");break;
2593
      }
2594
      break;
2595
      default:break;
2596
      }
2597
    }
15111 schaersvoo 2598
  }
2599
  return;
2600
}