Subversion Repositories wimsdev

Rev

Rev 16847 | Rev 16943 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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